In the training process of deep learning models, data augmentation is an indispensable link. There are many existing deep learning parameters. The amount of parameters that can be trained in a general model is basically tens of millions to millions, and it is difficult to have so many samples in the training set.
Secondly, data augmentation can expand the sample space. Suppose the current classification model needs to classify cars, the left is car A, and the right is car B. If you do not use any data augmentation method, the deep learning model will judge from the perspective of the car head, rather than the specific difference of the car.
Image Augmentation is the common used technique to improve the performance of computer vision system.
Refer to the W2 of Convolutional Neutral Network Course on Cousera.
specially in the WIDS dataset, which is an unbalanced dataset.
Upsampling the images with oil-palm is the way to handle the unbalanced problem.
Image augmentation artificially creates training images through different ways of processing or combination of multiple processing,
such as mirroring, random rotation, shifts, shear and flips, etc.
Keras has keras.preprocessing.image.ImageDataGenerator function to do image augmentation. Here showed how to use OpenCV to rotate, flip, and add Gaussian noise to original images.
Reference:
https://towardsdatascience.com/image-augmentation-examples-in-python-d552c26f2873
https://medium.com/@thimblot/data-augmentation-boost-your-image-dataset-with-few-lines-of-python-155c2dc1baec
https://www.kaggle.com/hanzh0420/image-augmentation-with-opencv
import cv2
import random
class Data_augmentation:
def __init__(self, path, image_name):
'''
Import image
:param path: Path to the image
:param image_name: image name
'''
self.path = path
self.name = image_name
print(path+image_name)
self.image = cv2.imread(path+image_name)
def rotate(self, image, angle=90, scale=1.0):
'''
Rotate the image
:param image: image to be processed
:param angle: Rotation angle in degrees. Positive values mean counter-clockwise rotation (the coordinate origin is assumed to be the top-left corner).
:param scale: Isotropic scale factor.
'''
w = image.shape[1]
h = image.shape[0]
#rotate matrix
M = cv2.getRotationMatrix2D((w/2,h/2), angle, scale)
#rotate
image = cv2.warpAffine(image,M,(w,h))
return image
def flip(self, image, vflip=False, hflip=False):
'''
Flip the image
:param image: image to be processed
:param vflip: whether to flip the image vertically
:param hflip: whether to flip the image horizontally
'''
if hflip or vflip:
if hflip and vflip:
c = -1
else:
c = 0 if vflip else 1
image = cv2.flip(image, flipCode=c)
return image
def image_augment(self, save_path):
'''
Create the new image with imge augmentation
:param path: the path to store the new image
'''
img = self.image.copy()
img_flip = self.flip(img, vflip=True, hflip=False)
img_rot = self.rotate(img)
img_gaussian = self.add_GaussianNoise(img)
name_int = self.name[:len(self.name)-4]
cv2.imwrite(save_path+'%s' %str(name_int)+'_vflip.jpg', img_flip)
cv2.imwrite(save_path+'%s' %str(name_int)+'_rot.jpg', img_rot)
cv2.imwrite(save_path+'%s' %str(name_int)+'_GaussianNoise.jpg', img_gaussian)