When dealing with Deep Learning on images, the first question that arises in your mind is where you will get this particular type of image in a huge amount? The answer is you can with a small image dataset and perform some augmentations on this image dataset.
What is Image Augmentation?
Image Augmentation is an image processing technique where we expand the training dataset size by creating new images through some changes in the existing photos. Image sample generated from data augmentation increases the current data by two times or three times, helping you build more generalized models.
In Keras, we have ImageDataGenerator API, which generates the images in batches with real-time data augmentation.
The import command for Image Data Generator is:
from tensorflow.keras.preprocessing.image import ImageDataGenerator
This Image Data Generator has various capabilities to produce new images such as :
- Applying rotation,
- Changing dimensions,
- Feature standardization
- Sample standardization
- Save the augmented image
- Moreover, we can define custom augmentation functions for images.
For all the above processing on images, we have the parameters in Data Generator. Let’s discuss some of them.
We can rotate the image to a random degree through the rotation_angle parameter in the image data generator.
from tensorflow.keras.datasets import mnist (X_train, y_train), (X_test, y_test) = mnist.load_data() #loading data X_train = X_train.reshape((X_train.shape, 28, 28, 1)) X_test = X_test.reshape((X_test.shape, 28, 28, 1)) X_train = X_train.astype('float32') X_test = X_test.astype('float32') data_generator = ImageDataGenerator(rotation_range=90) #defining datagenerator data_generator.fit(X_train) for X_batch, y_batch in data_generator.flow(X_train, y_train, batch_size=9): for i in range(0, 9): plt.subplot(330 + 1 + i) plt.imshow(X_batch[i].reshape(28, 28), cmap=plt.get_cmap('gray')) plt.show() break
Shearing and Zooming
Shearing an image means shifting a part of an image to one direction and the other part in another direction.
datagen = ImageDataGenerator(shear_range = 0.2,zoom_range = 0.3) # prepare iterator it = datagen.flow(data, batch_size=1) # generate samples and plot for i in range(9): # define subplot plt.subplot(330 + 1 + i) # generate batch of images batch = it.next() # convert to unsigned integers for viewing image = batch.astype('uint8') # plot raw pixel data plt.imshow(image) # show the figure plot.show()
Like the above parameters, there are various parameters with which you can play. To learn more, visit: https://www.tensorflow.org/api_docs/python/tf/keras/preprocessing/image/ImageDataGenerator/
There is a parameter where we can pass the user-defined image augmentation function. To learn more about custom image augmentation, visit:
To learn more about image segmentation, click the link below: https://machinelearningmastery.com/image-augmentation-deep-learning-keras.