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

Jingwei Li

Graduate Research assistance at Stony Brook University
linkedin profile url

ProjectPro is an awesome platform that helps me learn much hands-on industrial experience with a step-by-step walkthrough of projects. There are two primary paths to learn: Data Science and Big Data.... Read More

Relevant Projects

Build Regression Models in Python for House Price Prediction
In this Machine Learning Regression project, you will build and evaluate various regression models in Python for house price prediction.

Learn to Build a Siamese Neural Network for Image Similarity
In this Deep Learning Project, you will learn how to build a siamese neural network with Keras and Tensorflow for Image Similarity.

PyTorch Project to Build a GAN Model on MNIST Dataset
In this deep learning project, you will learn how to build a GAN Model on MNIST Dataset for generating new images of handwritten digits.

Build Time Series Models for Gaussian Processes in Python
Time Series Project - A hands-on approach to Gaussian Processes for Time Series Modelling in Python

Time Series Project to Build a Multiple Linear Regression Model
Learn to build a Multiple linear regression model in Python on Time Series Data

Learn to Build a Polynomial Regression Model from Scratch
In this Machine Learning Regression project, you will learn to build a polynomial regression model to predict points scored by the sports team.

Learn to Build Generative Models Using PyTorch Autoencoders
In this deep learning project, you will learn how to build a Generative Model using Autoencoders in PyTorch

CycleGAN Implementation for Image-To-Image Translation
In this GAN Deep Learning Project, you will learn how to build an image to image translation model in PyTorch with Cycle GAN.

Demand prediction of driver availability using multistep time series analysis
In this supervised learning machine learning project, you will predict the availability of a driver in a specific area by using multi step time series analysis.

Recommender System Machine Learning Project for Beginners-1
Recommender System Machine Learning Project for Beginners - Learn how to design, implement and train a rule-based recommender system in Python