Image Augmentation with skimage — Python

栏目: IT技术 · 发布时间: 5年前

Image Augmentation with skimage — Python

Image Augmentation with skimage — Python

PC: Flickr

Hey buddies, recently I was working on an image classification problem. But unfortunately, there were no enough samples in one of class. I searched on the internet and learned about a technique called Image Augmentation. Here I have shared my understanding of this technique and shared some codes using skimage, you can find the jupyter notebook at the bottom.

What is Image Augmentation?

Image Augmentation is a technique used to artificially increase the size of your image dataset. It can be achieved by applying random transformations to your image.

We know Deep learning models are able to generalize well when it is able to see more data, Data Augmentation can create variations of existing images which helps to generalize well. Image Augmentation can be applied mainly on two domains of Image

  1. Position Augmentation
  2. Color Augmentation

What is Position Augmentation?

Positon Augmentation is simple where we apply different transformations on pixel positions.

Scaling, Rotation, Cropping, Flipping, Padding, Zoom, Translation, Shearing, and other Affine transformations are examples for the Position Augmentation. Let us try applying some of these transformations.

import numpy as np
from skimage.io import imread, imsave
import matplotlib.pyplot as plt
from skimage import transform
from skimage.transform import rotate, AffineTransform
from skimage.util import random_noise
from skimage.filters import gaussian
from scipy import ndimage# load Image
img = imread('./butterfly.jpg') / 255# plot original Image
plt.imshow(img)
plt.show()
Original Image PC: Flickr
# image rotation using skimage.transformation.rotate
rotate30 = rotate(img, angle=30)
rotate45 = rotate(img, angle=45)
rotate60 = rotate(img, angle=60)
rotate90 = rotate(img, angle=90)fig = plt.figure(tight_layout='auto', figsize=(10, 7))fig.add_subplot(221)
plt.title('Rotate 30')
plt.imshow(rotate30)fig.add_subplot(222)
plt.title('Rotate 45')
plt.imshow(rotate45)fig.add_subplot(223)
plt.title('Rotate 60')
plt.imshow(rotate60)fig.add_subplot(224)
plt.title('Rotate 90')
plt.imshow(rotate90)plt.show()
# image shearing using sklearn.transform.AffineTransform
# try out with differnt values of shear 
tf = AffineTransform(shear=-0.5)
sheared = transform.warp(img, tf, order=1, preserve_range=True, mode='wrap')sheared_fig = plot_side_by_side(img, sheared, 'Original', 'Sheared')
# Image rescaling with sklearn.transform.rescale
rescaled = transform.rescale(img, 1.1)rescaled_fig = plot_side_by_side(img, rescaled, 'Original', 'Rescaled')
plt.show()print('Original Shape: ',img.shape)
print('Rescaled Shape: ',rescaled.shape)Output: 
Original Shape: (684, 1024, 3)
Rescaled Shape: (752, 1126, 3)
# flip up-down using np.flipud
up_down = np.flipud(img)fig_updown = plot_side_by_side(img, up_down, 'Original', 'Up-Down')
plt.show()
# flip up-down using np.flipud
left_right = np.fliplr(img)fig_lr = plot_side_by_side(img, left_right, 'Original', 'Up-Right')
plt.show()

What is Color Augmentation?

Color Augmentation is the technique where we play with the intensity value of pixels.

We reproduce different images by tweaking Brightness, Contrast, Saturation, and also we can add random noise to the Image.

# Apply Random Noise to image using skimage.utils.random_noise
noised = random_noise(img, var=0.1**2)fig_noised = plot_side_by_side(img, noised, 'Original', 'Noised')
plt.show()
# Increasing the brighness of the Image
# Note: Here we add 100/255 since we scaled Intensity values of Image when loading (by dividing it 255)
highB = img + (100/255)fig_highB = plot_side_by_side(img, highB, 'Original', 'highB')
plt.show()
# Increasing the contrast of the Image
# Note: Here we add 100/255 since we scaled Intensity values of Image when loading (by dividing it 255)
highC = img * 1.5fig_highB = plot_side_by_side(img, highC, 'Original', 'highC')
plt.show()

Did you notice one thing, we have already created 11 different images from 1 Image. Note we can still play with parameters and create a lot more.

When training neural networks we can add Random transformations to the ImageLoader. There are other advanced techniques like using GAN for Data Augmentation, let us see that in another article. I hope now you understand what is Image Augmentation.

You can find the notebook at https://github.com/Mathanraj-Sharma/sample-for-medium-article/blob/master/image-augmentation-skimage/image-augmentation.ipynb


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

创新者的窘境

创新者的窘境

克莱顿•克里斯坦森( Clayton M. Christensen ) / 胡建桥 / 中信出版社 / 2010-6 / 38.00元

管理类经典图书 o 被《福布斯》评为20世纪最具影响的20本商业图书之一 o “全球商业书籍奖”获奖图书 “颠覆大师”克莱顿•克里斯坦森经典力作。 《金融时报》/布兹•亚兰及汉密顿全球商务书刊颁发“1997年最佳商务书”奖 “1997年最佳商务‘实用’书”奖 一本书, 让志在必得者战战兢兢, 让犹豫不前者胸有成竹, 掀起激荡决策者的脑力风暴, ......一起来看看 《创新者的窘境》 这本书的介绍吧!

在线进制转换器
在线进制转换器

各进制数互转换器

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具