In-depth learning models generally require a lot of data for learning. In general, the more data there is, the higher the performance of the model. But obtaining huge amounts of data involves its own problems. Instead of collecting data manually for days on end, we can use image magnification methods. The zoom allows you to decorate existing images without having to work manually.

In this lesson we will discuss the concept of image enhancement, its usefulness and the different image enhancement techniques. We will also implement these image magnification methods with torchvision.transformms.

Image enhancement – the process of generating new images for the CNN formation model. These new images are generated from existing training images, so we don’t have to create them manually.

How does a transformer increase data?

Transform the operations applied to your original images with each batch generation. This way, your training data set remains unchanged, only the batch images are copied and transformed with each iteration.

When we perform a data expansion, we save our original dataset and add the other versions (mirror, rotate, crop… etc.).

There are several methods to enlarge an image, and we will discuss some of the most common and widely used ones. I’m going to use 8 dog pictures to show how the image can be enlarged. You can also try other images that meet your needs.

import torch
import torch vision
import torch vision.transform as transformation
import matplotlib.pyplot as plt
import glob
from torch.utils.data import DataLoader,dataset
from PIL import image
import numpy as np

img_list=glob.glob(‘DOG/*.jpeg’)

class DogsDataset(Dataset):
def __init__(self,image_list,transformms=None):
self.image_list=image_list
self.transformms=transformed
def __len__(self):
return len(self).image_list)
def __getitem__(self,i) :
img=plt.imread(self.image_list[i])
img=image.fromarray(img).convert(‘RGB’)
img=np.array(img).astype(np.uint8).

if the transformation itself is not No:
img=self.transformms(img)
return torch.tensor(img,dtype=torch.float)

def show_img(img):
plt.figure(figsize=(40,38))
npimg=img.numpy()
plt.imshow(npimg,(1,2,0)).
plt.show()

Image rotation

By rotating the image, our model becomes more resistant to changes in the orientation of the object. The information on the picture remains the same, for example a dog is a dog, even if we see him from a different angle. Let’s see how we can spin it. To rotate the image we use the RandomRotation function in torchvision.transformms.

transform=transform.compose([
transforms.inPILImage(),
transforms.resize((164.164)),
transforms.random.rotation(50,expand=true),
transforms.resize((164.164)),
transforms.ToTensor(),
]).

dog_dataloader=Data Loader(DogsDataset(img_list,transform),batch_size=8,shuffle=True)

data=iter(dog_dataloader)
show_img(torchvision.utils.make_grid(data.next()).

If rotated by mistake, some data points are returned as originals and some as rotated (for example, 4 rotated points and 4 originals). In other words, you get 8 data points (some with rotation and some without rotation) for an iteration of the elements in the dataset.

Randomly cut

Otherwise cropped images are the most important aspect of image diversity. If your network is used by real users, the object may be located elsewhere in the image. In addition, the object may cover the entire image and may not be completely present in the image (i.e. it may be cut off at the edges of the object). The code shows the part of the image in random order.

transform=transform.compose([
transforms.toPILImage(),
transforms.toPILImage(),
transforms.toTensor(),
]).

dog_dataloader=Data Loader(DogsDataset(img_list,transform),batch_size=8,shuffle=True)

data=iter(dog_dataloader)
show_img(torchvision.utils.make_grid(data.next()).

Image scrolling

Your network trains itself to repair images that are randomly discarded from the original dataset, sometimes with a probability of = 0.5. Let’s see how we can organize a coup.

transform=transforms.compose([
transformms.ToPILImage(),
transformms.Resize((164.164)),
transformms.RandomVerticalFlip(0.4),
transformms.RandomHorizontalFlip(0.4),
transformms.ToTensor(),
]).

dog_dataloader=Data Loader(DogsDataset(img_list,transform),batch_size=8,shuffle=True)

data=iter(dog_dataloader)
show_img(torchvision.utils.make_grid(data.next()).

In this way we can reverse the image and create generalized models that examine both the original and the inverted images.

Brightness, contrast, saturation, colour

The quality of the images will not be the same for all sources. Some images can be of very high quality, while others are simply of poor quality. In these scenarios we can blur the picture. This contributes to making our deep learning model more reliable. Transformations are a class that makes it possible to randomly change the brightness, contrast and saturation of the image.

transform=transforms.compose([
transformms.ToPILImage(),
transformms.Resize((164.164)),
transformms.ColorJitter(Brightness=0.1, Contrast=0.2, Saturation=0, Hue=0),
transformms.ToTensor(),
]).

dog_dataloader=Data Loader(DogsDataset(img_list,transform),batch_size=8,shuffle=True)

data=iter(dog_dataloader)
show_img(torchvision.utils.make_grid(data.next()).

Gaussian noise on images

Adding random noise to images is also a technique for enlarging the image. Let’s see how we can do that. We use a Gaussian filter to blur the image.

Class AddGaussianNoise(object) :
def __init__(self, mean=0., std=1.) :
self.std = std
self.mean = mean

def __call__(Self, Tensor):
returns Tensor + Torch.randn(Tensor.size()) * self.std + self.mean

def __repr__(self):
returns self.__class__.__name__ + ‘(mean={0}, std={1})’.format(self.mean, self.std)

Transformations allow fine tuning of the transformation promoter. You can use functional transformation to create transformation classes with user-defined behavior.

transform=transform.compose([
transforms.inPILImage(),
transforms.resize((164.164)),
transforms.inTensor(),
AddGaussianNoise(0.1, 0.08)
]).

dog_dataloader=Data Loader(DogsDataset(img_list,transform),batch_size=8,shuffle=True)

data=iter(dog_dataloader)
show_img(torchvision.utils.make_grid(data.next()).

This allows our model to learn how to separate the signal from the image noise. I take the standard deviation of the sound as 0.08 (you can also change this value). Please note that increasing this value will result in more noise in the image and vice versa.

Accidental deletion

Randomly select an area of a rectangle in an image and remove the pixels with random values. This procedure generates training images with different occlusal planes, which reduces the risk of adjustment and makes the model resistant to occlusion.

transform=transform.compose([transform.inPILImage(),transform.inPILImage(),transform.inTensor(),transform.random.deletion(),]).

dog_dataloader=Data Loader(DogsDataset(img_list,transform),batch_size=8,shuffle=True)

data=iter(dog_dataloader)
show_img(torchvision.utils.make_grid(data.next()).

These are some of the image magnification methods that make our in-depth learning model reliable and generalizable. It also broadens the scope of the training programme.

All transformations change the image somehow. They leave the original intact and only return the modified copy. For the same input image, some methods always apply the same changes (e.g. tensor conversion, adaptation of the format to a fixed shape, etc.) Other methods use transformations with random parameters that produce different results each time (e.g. random image area, random change in brightness or saturation, etc.). This means that you will receive a different version of the dataset each time,

The purpose of data enrichment is to try to reach an upper limit for the dissemination of invisible data.

You May Also Like

🥇 Calculate the Cost of a Product in Excel  Step by Step Guide ▷ 2020

If you want to sell a product or offer a service to…

🥇 MICROSOFT POWERPOINT  What is it? + Alternatives ▷ 2020

One of the advantages of using Office software packages is that you…

Fix: Google Photos not backing up on iPhone

For many iPhone users, Google Photos remains the first choice, even though…

VR Educational Apps – Linux Hint

Virtual reality is in vogue, thanks to its supportive character and incredible…