Introduction to Keras Data Augmentation
Keras data augmentation is used to increase the diversity of training which was set while applying the random transformations such as rotation of an image. We are applying data augmentation by using two ways. It is the technique that was used to expand the training size by creating and modifying the versions of the dataset. Deep learning neural network multiple data will be resulting in skillful models.
Key Takeaways
- In the field of deep learning, the performance of the model is improving with the amount of data that we are using for training the data.
- Data augmentation will increase the training set size while generating a new variant of the training set. It is a well-known technique.
What is Keras Data Augmentation?
The data augmentation technique is used to create variations of images that improve the ability of models to generalize what we have learned into new images. The neural network deep learning library allows you to fit models using image data augmentation and the class name as the image data generator.
It encompasses a wide range of techniques used to generate new training samples using random jitters. The primary goal of using data augmentation in Keras is to increase the generalizability of the specified model. At the time of testing, no data augmentation is used, and the trained network is simply executed.
Keras Data Augmentation Configure
To use the data augmentation in keras we need to follow the below steps. We are importing the specified module.
1. In the first step we are importing the module required to set up the data augmentation as follows.
Code:
import tensorflow_datasets as tfds
from tensorflow.keras import layers
import matplotlib.pyplot as plt
import numpy as np
import tensorflow as tf
Output:
2. After importing the module now in this step, we are downloading the dataset as follows.
Code:
(train_ds, val_ds, test_ds), metadata = tfds.load(
'tf_flowers',
split=['train[:80%]', 'train[80%:90%]', 'train[90%:]'],
with_info=True,
as_supervised=True,
)
ncls = metadata.features ['label'].num_classes
print(ncls)
Output:
3. After loading the dataset now in this step we are retrieving the image from the dataset and demonstrating the augmentation.
Code:
lname = metadata.features['label'].int2str
image, label = next(iter(train_ds))
plt.imshow(image)
plt.title(lname(label))
plt.show()
Output:
4. After retrieving the image from the dataset now in this step we are using keras processing layers.
Code:
isize = 160
res_scale = tf.keras.Sequential([
layers.Resizing(isize, isize),
layers.Rescaling(1./255)
])
res = res_scale(image)
plt.imshow(res)
Output:
After using the layers of keras preprocessing, now in this step we are defining the data augmentation.
Code:
d_aug = tf.keras.Sequential([
layers.RandomFlip("horizontal_and_vertical"),
layers.RandomRotation(0.2),
])
image = tf.cast(tf.expand_dims(image, 0), tf.float32)
plt.figure(figsize=(5, 5))
for i in range(4):
a_img = d_aug(image)
ax = plt.subplot(2, 2, i + 1)
plt.imshow(a_img[0])
plt.axis("off")
plt.show()
Output:
How to Use Image Augmentation in Keras?
The image generator class of keras provides a quick and easy way to augment our images. It provides different augmentation techniques. The keras provides the image data generator class which defines the configuration for image data preparation.
This includes the following capabilities:
- Save augmented images to the disk
- Feature wise standardization
- Sample wise standardization
- ZCA whitening
- Dimension reordering
- Random rotation, shifts, flips, and shears
To use the image augmentation in keras we need to import the below modules. We import the same using the import keyword.
Code:
import tensorflow_datasets as tfds
from tensorflow.keras import layers
import matplotlib.pyplot as plt
import numpy as np
import tensorflow as tf
from tensorflow.keras.preprocessing.image import ImageDataGenerator
datagen = ImageDataGenerator()
Output:
After importing the module now in this step, we are loading the training dataset.
Code:
(X_train ….) = mnist.load_data()
Output:
After loading the dataset now in this step, we are creating the grid of 3*3 images.
Code:
fig, ax = plt.subplots()
for i in range (3):
for j in range (3):
ax[i][j].imshow()
Output:
After creating the grid of images now in this step we are displaying the image.
Code:
plt.show()
Output:
Keras Dataset Augmentation Layers
In keras dataset augmentation there are two ways of using keras preprocessing layers. The first way to use the keras dataset augmentation layer is to make the preprocessing layer part of our model.
Code:
model = tf.keras.Sequential([
res_scale,
d_aug,
layers.Conv2D(),
layers.MaxPooling2D(),
])
Output:
The second method we need to use is the layer of preprocessing in our dataset.
Code:
aug_ds = train_ds.map(
lambda x, y: (res_scale(x, training=True), y))
Output:
In the below example, we are applying all the layers by using the sequential model.
Code:
mod = tf.keras.Sequential([
…..
layers.MaxPooling2D(),
…..
layers.MaxPooling2D(),
…..
layers.MaxPooling2D(),
layers.Flatten(),
layers.Dense(64, activation = 'relu'),
layers.Dense(ncls)
])
Output:
Horizontal and Vertical Shift – Keras Data Augmentation
There are two types of keras data augmentation i.e vertical shift and horizontal shift. The horizontal shift augmentation means the pixels of the image will shift horizontally, without changing the image dimension.
Below example shows how we can shift the image horizontally as follows:
Code:
from numpy import expand_dims
from keras.preprocessing.image import ImageDataGenerator
from matplotlib import pyplot
from tensorflow.keras.preprocessing.image import load_img
from tensorflow.keras.preprocessing.image import img_to_array
img = load_img('cat.jpg')
dimg = img_to_array(img)
imgn = expand_dims(dimg, 0)
idg = ImageDataGenerator(width_shift_range = [-200,200])
iterator = idg.flow (imgn, batch_size=1)
pyplot.subplot(330 + 1 + i)
batch = iterator.next()
img = batch[0].astype('uint8')
pyplot.imshow (img)
pyplot.show()
Output:
The below example shows how we can shift the image vertically. We are using a percentage value for shifting an image.
Code:
from numpy import expand_dims
from keras.preprocessing.image import ImageDataGenerator
from matplotlib import pyplot
from tensorflow.keras.preprocessing.image import load_img
from tensorflow.keras.preprocessing.image import img_to_array
img = load_img('dog.jpg')
dimg = img_to_array(img)
imgn = expand_dims(dimg, 0)
idg = ImageDataGenerator(height_shift_range=0.4)
itr = idg.flow(imgn, batch_size=1)
pyplot.subplot(330 + 1 + i)
batch = itr.next()
img = batch[0].astype('uint8')
pyplot.imshow(img)
pyplot.show()
Output:
Examples of Keras Data Augmentation
Given below are the examples mentioned:
Example #1
In the below example, we are defining data augmentation.
Code:
import tensorflow_datasets as tfds
from tensorflow.keras import layers
import matplotlib.pyplot as plt
import numpy as np
import tensorflow as tf
(train_ds, val_ds, test_ds), metadata = tfds.load(
'tf_flowers',
split=['train[:80%]', 'train[80%:90%]', 'train[90%:]'],
with_info=True,
as_supervised=True,
)
ncls = metadata.features['label'].num_classes
print(ncls)
lname = metadata.features['label'].int2str
image, label = next(iter(train_ds))
plt.imshow(image)
plt.title(lname(label))
plt.show()
Output:
Example #2
In the below example, we are using layers of data augmentation.
Code:
import tensorflow_datasets as tfds
from tensorflow.keras import layers
import matplotlib.pyplot as plt
import numpy as np
import tensorflow as tf
isize = 160
res_scale = tf.keras.Sequential([
layers.Resizing(isize, isize),
layers.Rescaling(1./255)
])
res = res_scale(image)
plt.imshow(res)
plt.show()
Output:
FAQ
Given below are the FAQs mentioned:
Q1. What is the use of keras data augmentation?
Answer: It is used to increase the diversity of our training set by applying random transformations to it.
Q2. Which module do we need to import at the time of using keras data augmentation?
Answer: We need to import the keras, tensorflow, numpy, and pyplot module at the time of using it.
Q3. What are vertical and horizontal shifting in keras augmentation?
Answer: There are two types of shifting available in keras data augmentation. Horizontal shifting means image pixels are shifting vertically. Whereas vertical shifting means images of pixels is shifting vertically.
Conclusion
It will encompass the wide range of techniques which was used in generating new training samples by applying the random jitters. Keras data augmentation is used to increase the diversity of training which was set while applying the random transformations such as rotation of an image.
Recommended Articles
This is a guide to Keras Data Augmentation. Here we discuss the introduction, to how to use image augmentation in keras. horizontal and vertical shift. You may also have a look at the following articles to learn more –