Data augmentation is the process of applying various transformations to your training data. It helps to increase the diversity of datasets and prevent overfitting. Overfitting mainly occurs when you have limited data to train your model.
Here you will learn how to diversify your dataset using TensorFlow’s data augmentation module. This will generate new data points that differ slightly from the original data and prevent overfitting.
Sample dataset to use
Use the Kaggle cat and dog dataset. This dataset contains approximately 3,000 cat and dog images. These images are split into training, test, and validation sets.
A label of 1.0 represents a dog and a label of 0.0 represents a cat.
Full source code implementing data augmentation techniques and not is available in our GitHub repository.
Install and import TensorFlow
Basic knowledge of Python is required to proceed. Also, basic knowledge of machine learning is required. If you need a refresher, consider following some tutorials on machine learning.
Open Google Colab. Change the runtime type to GPU. Then run the following magic command in the first code cell to install TensorFlow into your environment:
!pip install tensorflow
Import TensorFlow and its related modules and classes.
import tensorflow as tf
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout
of tensorflow.keras.preprocessing.image Data augmentation can now be performed on the dataset.
Create an instance of the ImageDataGenerator class
creates an instance of Image Data Generator Class for train data. Use this object to preprocess the training data. Generate batches of augmented image data in real time during model training.
For the task of classifying images as cats or dogs, flipping, random width, random height, random brightness, and zoom data augmentation techniques can be used. These techniques generate new data containing variations of the original data representing real-world scenarios.
train_datagen = ImageDataGenerator(rescale=1./255,
horizontal_flip=True,
width_shift_range=0.2,
height_shift_range=0.2,
brightness_range=[0.2,1.0],
zoom_range=0.2)
creates another instance of Image Data Generator Class of test data. will become necessary. Rescaling parameters. Normalize the pixel values in the test images to match the format used during training.
test_datagen = ImageDataGenerator(rescale=1./255)
create a final instance of Image Data Generator Validation data class. Rescale the validation data in the same way as the test data.
validation_datagen = ImageDataGenerator(rescale=1./255)
No need to apply other augmentation techniques to test and validation data. This is because the model uses test and validation data only for evaluation purposes. It should reflect the original data distribution.
data load
create directory iterator Objects from the training directory. Generate a batch of augmented images. Next, specify the directory where you want to store your training data.Resize image to fixed size 64×64 pixel. Specifies the number of images to use in each batch.Finally, specify the type of label to display binary (i.e. cat or dog).
train_data = train_datagen.flow_from_directory(directory=r'/content/drive/MyDrive/cats_and_dogs_filtered/train',
target_size=(64, 64),
batch_size=32,
class_mode='binary')
create another directory iterator Objects from the testing directory. Set the parameters to the same values as your training data.
test_data = test_datagen.flow_from_directory(directory='/content/drive/MyDrive/cats_and_dogs_filtered/test',
target_size=(64, 64),
batch_size=32,
class_mode='binary')
create the final directory iterator Object from verification directory. The parameters remain the same for training and test data.
validation_data = validation_datagen.flow_from_directory(directory='/content/drive/MyDrive/cats_and_dogs_filtered/validation',
target_size=(64, 64),
batch_size=32,
class_mode='binary')
Directory iterators do not extend validation and test datasets.
Model definition
Defines the neural network architecture. Use a convolutional neural network (CNN). CNNs are designed to recognize patterns and features in images.
model = Sequential()
model.add(Conv2D(32, (3, 3), activation='relu', input_shape=(64, 64, 3)))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(1, activation='sigmoid'))
Compile the model using the binary cross entropy loss function. Binary classification problems commonly use It. For the optimizer, Adam OptimizerThis is an adaptive learning rate optimization algorithm. Finally, we evaluate the accuracy of the model.
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
Prints a summary of the model’s architecture to the console.
model.summary()
The following screenshot shows a visualization of the model architecture.
This will give you an overview of what your model design will look like.
model training
to train the model. fit() Method.number of steps per epoch and number of training samples batch sizeAlso set the validation data and the number of validation steps.
history = model.fit(train_data,
steps_per_epoch=train_data.n // train_data.batch_size,
epochs=50,
validation_data=validation_data,
validation_steps=validation_data.n // validation_data.batch_size)
of Image Data Generator The class applies data augmentation to the training data in real time. This slows down the model training process.
model evaluation
to evaluate model performance on test data. evaluation() Method. It also prints the test loss and accuracy to the console.
test_loss, test_acc = model.evaluate(test_data,
steps=test_data.n // test_data.batch_size)
print(f'Test loss: {test_loss}')
print(f'Test accuracy: {test_acc}')
The following screenshot shows the performance of the model.
This model works well even with unseen data.
When running code that does not implement any data augmentation technique, the model has a training accuracy of 1. This means overfitting. It will also perform poorly on data that has never been seen before. This is to learn the characteristics of the dataset.
When is data augmentation useless?
- If your dataset is already diverse and large: Data augmentation increases the size and diversity of the dataset. Data augmentation is useless if the dataset is already large and diverse.
- If the dataset is too small: Data augmentation cannot create new features that do not exist in the original dataset. Therefore, it cannot compensate for small datasets that lack most features that the model needs to learn.
- Wrong type of data extension: For example, image rotation may not help if object orientation is important.
What you can do with TensorFlow
TensorFlow is a versatile and powerful library. It can train complex deep learning models and run on devices ranging from smartphones to clusters of servers. This helps power edge computing devices that utilize machine learning.
