How to flip and crop an image using MXNet

This recipe helps you flip and crop an image using MXNet

Recipe Objective: How to flip and crop an image using MXNet?

This recipe explains how to flip and crop an image using MXNet.

Build a Chatbot in Python from Scratch!

Step 1: Importing library

Let us first import the necessary libraries. We'll import mxnet, NumPy, gluoncv, and matplotlib.pyplot.

import numpy as np
import mxnet as mx
import gluoncv as gcv
import matplotlib.pyplot as plt

Step 2: Loading The Sample Image

We'll be downloading a sample image of a dog from the URL "https://raw.githubusercontent.com/zhreshold/mxnet-ssd/master/data/demo/dog.jpg"

image_url = 'https://raw.githubusercontent.com/zhreshold/mxnet-ssd/master/data/demo/dog.jpg'
image_path='dog.jpg'
gcv.utils.download(url=image_url,path=image_path)
img = mx.image.imread('dog.jpg')

Step 3: Processing

We'll be defining a utility function that we will use for the augmentation visualization. We have loaded the image with numpy.int8 datatype because for some functions, for example, swapexe don't compute on int type, so we have converted int18 to float32.

def plot(arr):
imshow((arr.clip(0, 255)/255).asnumpy())
from matplotlib.pyplot import imshow
img = image.astype("float32")
plot(img)

Step 3: Flip And Crop Image

We have cropped the image using mx.image.RandomCropAug and horizontally flipped the image using mx.image.HorizontalFlipAug where p is the probability of flip occurring.

img1 = mx.image.RandomCropAug(size=(450, 450))
img2 = img1(img)
plot(img2)
img1 = mx.image.HorizontalFlipAug(p=1)
img2 = aug(image)
plot(img2)

What Users are saying..

profile image

Anand Kumpatla

Sr Data Scientist @ Doubleslash Software Solutions Pvt Ltd
linkedin profile url

ProjectPro is a unique platform and helps many people in the industry to solve real-life problems with a step-by-step walkthrough of projects. A platform with some fantastic resources to gain... Read More

Relevant Projects

Build CNN for Image Colorization using Deep Transfer Learning
Image Processing Project -Train a model for colorization to make grayscale images colorful using convolutional autoencoders.

Build Classification Algorithms for Digital Transformation[Banking]
Implement a machine learning approach using various classification techniques in Python to examine the digitalisation process of bank customers.

Build a Text Generator Model using Amazon SageMaker
In this Deep Learning Project, you will train a Text Generator Model on Amazon Reviews Dataset using LSTM Algorithm in PyTorch and deploy it on Amazon SageMaker.

Ensemble Machine Learning Project - All State Insurance Claims Severity Prediction
In this ensemble machine learning project, we will predict what kind of claims an insurance company will get. This is implemented in python using ensemble machine learning algorithms.

Build an End-to-End AWS SageMaker Classification Model
MLOps on AWS SageMaker -Learn to Build an End-to-End Classification Model on SageMaker to predict a patient’s cause of death.

Build a Logistic Regression Model in Python from Scratch
Regression project to implement logistic regression in python from scratch on streaming app data.

GCP MLOps Project to Deploy ARIMA Model using uWSGI Flask
Build an end-to-end MLOps Pipeline to deploy a Time Series ARIMA Model on GCP using uWSGI and Flask

MLOps Project to Deploy Resume Parser Model on Paperspace
In this MLOps project, you will learn how to deploy a Resume Parser Streamlit Application on Paperspace Private Cloud.

AWS MLOps Project to Deploy a Classification Model [Banking]
In this AWS MLOps project, you will learn how to deploy a classification model using Flask on AWS.

NLP Project on LDA Topic Modelling Python using RACE Dataset
Use the RACE dataset to extract a dominant topic from each document and perform LDA topic modeling in python.