[PYTHON] Display the image after Data Augmentation with Pytorch

background

** I want to display the image after Data Augmentation! ** **

I thought and implemented it.

Data Augmentation is a technology for padding a single image, and the following operations are added.

--Random Crop (Random crop image) --Random Horizontal Flip (image is flipped horizontally with a certain probability) --Random Erasing (Randomly add noise to a part of the image) --Random Affine (Randomly scales / rotates the image)

There are many other things.

Implementation

This time, I loaded the training image dataset of CIFAR-10 and tried to incorporate Random Horizontal Flip and Random Erasing into transforms.

test.py


import torch
import numpy as np
import torchvision
import torchvision.transforms as transforms
from torch.utils.data import Dataset,DataLoader
import torchvision.datasets as dsets
import matplotlib.pyplot as plt

#Loading images
batch_size = 100
train_data = dsets.CIFAR10(root='./tmp/cifar-10', train=True, download=False, transform=transforms.Compose([transforms.RandomHorizontalFlip(p=0.5), transforms.ToTensor(), transforms.RandomErasing(p=0.5, scale=(0.02, 0.4), ratio=(0.33, 3.0))]))
train_loader = DataLoader(train_data,batch_size=batch_size,shuffle=True)
test_data = dsets.CIFAR10(root='./tmp/cifar-10', train=False, download=False, transform=transforms.Compose([transforms.ToTensor(),]))
test_loader = DataLoader(test_data,batch_size=batch_size,shuffle=False)

def image_show(data_loader,n):

  #Read Augmented image data
  tmp = iter(data_loader)
  images,labels = tmp.next()

  #Convert image from tensor to numpy
  images = images.numpy()

  #Take out n images one by one and display them
  for i in range(n):
    image = np.transpose(images[i],[1,2,0])
    plt.imshow(image)
    plt.show()

image_show(train_loader,10)

The image_show function is a function that displays the image after Augmentation.

Get one mini-batch from DataLoader with iter ().

Then, use .next () to store the image data in images and the labels in labels.

images = images.numpy () converts image data from tensor to numpy.

At this point, images has a structure of ** [batch size, number of channels, width, height] **, but to display images with pyplot of matplotlib ** [width, height, number of channels] Must be **.

Therefore, it is transformed using np.transpose.

Execution result example

1.png 02.png

It was confirmed that it was flipped horizontally and that noise was added by Random Erasing.

Recommended Posts

Display the image after Data Augmentation with Pytorch
View image after Data Augmentation in PyTorch
How to Data Augmentation with PyTorch
Image display taken with the built-in ISIGHT
Data Augmentation with openCV
[PyTorch] Data Augmentation for segmentation
Install the data files with setup.py
Try blurring the image with opencv2
[Django] Display registration data associated with users on the registration / edit form (Form)
Display Python 3 in the browser with MAMP
Try to image the elevation data of the Geographical Survey Institute with Python
Send and receive image data as JSON over the network with Python
Right-click the image → realize "Compress with TinyPNG"
I tried playing with the image with Pillow
Display markers above the border with matplotlib
[PyTorch] Handle image pairs with Dataset & DataLorder
Execute raw SQL using python data source with redash and display the result
How to display in the entire window when setting the background image with tkinter
Location information data display in Python --Try plotting with the map display library (folium)-
Mouse over Matplotlib to display the corresponding image
I tried "smoothing" the image with Python + OpenCV
Crop the image to rounded corners with pythonista
I tried "differentiating" the image with Python + OpenCV
Generate and post dummy image data with Django
I tried to save the data with discord
Dynamically display epidemic data using the Grafana Dashboard
Determine the numbers in the image taken with the webcam
I tried "binarizing" the image with Python + OpenCV
Display the edge
Play with PyTorch
Cross-validation with PyTorch
Display n digits after the decimal point in python
Extract the table of image files with OneDrive & Python
How to scrape image data from flickr with python
Display images taken with the Raspberry Pi camera module
Hit the Twitter API after Oauth authentication with Django
[Python] Get the numbers in the graph image with OCR
Display the integrated temperature for each field with Z-GIS
Identify the name from the flower image with keras (tensorflow)
Convert the image in .zip to PDF with Python
[PyTorch Tutorial ⑦] Visualizing Models, Data, And Training With Tensorboard
Get comments and subscribers with the YouTube Data API
Preparing the execution environment of PyTorch with Docker November 2019
Challenge image classification with TensorFlow2 + Keras 3 ~ Visualize MNIST data ~
POST the image with json and receive it with flask
Display / update the graph according to the input with PySimpleGui
Display the graph while changing the parameters with PySimpleGUI + Matplotlib
Delete DynamoDB data after 5 minutes with AWS Step Functions
Extract the band information of raster data with python
I tried to display the point cloud data DB of Shizuoka prefecture with Vue + Leaflet