EDUCBA

EDUCBA

MENUMENU
  • Blog
  • Free Courses
  • All Courses
  • All in One Bundle
  • Login
Home Data Science Data Science Tutorials Keras Tutorial Keras Data Augmentation

Keras Data Augmentation

Keras Data Augmentation

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.

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

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:

Keras Data Augmentation - importing

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:

Keras Data Augmentation - downloading

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:

Keras Data Augmentation - Retrieve Image

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:

Processing the image

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:

Keras Data Augmentation - Defining

Keras Data Augmentation - data augmentation

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:

Keras Data Augmentation - import keyword

After importing the module now in this step, we are loading the training dataset.

Code:

(X_train ….) = mnist.load_data()

Output:

Keras Data Augmentation 9

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:

we are creating the grid

After creating the grid of images now in this step we are displaying the image.

Code:

plt.show()

Output:

displaying the image

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:

Keras Data Augmentation preprocessing layer

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:

layer of preprocessing

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:

sequential model

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:

Keras Data Augmentation - Shifting the image

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:

Keras Data Augmentation - percentage value

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:

Keras Data Augmentation 17

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:

we are using layers

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 –

  1. Keras Tuner
  2. What is Keras GPU?
  3. Keras Embedding
  4. Keras predict
EVIEWS Training
32+ Hours of HD Videos
11 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5
MYSQL Certification Course
115+ Hours of HD Videos
18 Courses
3 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
CLOUD COMPUTING Certification Course
141+ Hours of HD Videos
23 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5
SPLUNK Training Program
70+ Hours of HD Videos
11 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5
Primary Sidebar
Popular Course in this category
KERAS Training
 40+ Hour of HD Videos
10 Courses
Verifiable Certificate of Completion
  Lifetime Access
4.5
Price

View Course
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more