How to Improve Your Machine Learning Model With TensorFlow’s Data Augmentation

Data augmentation is the process of applying various transformations to the training data. It helps increase the diversity of the dataset and prevent overfitting. Overfitting mostly occurs when you have limited data to train your model.

Here, you will learn how to use TensorFlow’s data augmentation module to diversify your dataset. This will prevent overfitting by generating new data points that are slightly different from the original data.

4

The Sample Dataset You Will Use

You will use the cats and dogs dataset fromKaggle. This dataset contains approximately 3,000 images of cats and dogs. These images are split into training, testing, and validation sets.

The label 1.0 represents a dog while the label 0.0 represents a cat.

A code on a table displaying some code

The full source code implementing data augmentation techniques and the one that does not are available in aGitHub repository.

Installing and Importing TensorFlow

To follow through, you should have abasic understanding of Python. You should also have basic knowledge of machine learning. If you require a refresher, you may want to consider following sometutorials on machine learning.

OpenGoogle Colab. Change the runtime type to GPU. Then, execute the following magic command on the first code cell to install TensorFlow into your environment.

A cat and dog dataset with labels

Import TensorFlow and its relevant modules and classes.

Thetensorflow.keras.preprocessing.imagewill enable you to perform data augmentation on your dataset.

A machine learning model summary printed on the console

Creating Instances of the ImageDataGenerator Class

Create an instance of theImageDataGeneratorclass for the train data. You will use this object for preprocessing the training data. It will generate batches of augmented image data in real time during model training.

In the task of classifying whether an image is a cat or a dog, you may use the flipping, random width, random height, random brightness, and zooming data augmentation techniques. These techniques will generate new data which contains variations of the original data representing real-world scenarios.

Evaluation results of a machine learning model

Create another instance of theImageDataGeneratorclass for the test data. You will need therescaleparameter. It will normalize the pixel values of the test images to match the format used during training.

Create a final instance of theImageDataGeneratorclass for the validation data. Rescale the validation data the same way as the test data.

You do not need to apply the other augmentation techniques to the test and validation data. This is because the model uses the test and validation data for evaluation purposes only. They should reflect the original data distribution.

Loading Your Data

Create aDirectoryIteratorobject from the training directory. It will generate batches of augmented images. Then specify the directory that stores the training data. Resize the images to a fixed size of64x64pixels. Specify the number of images that each batch will use. Lastly, specify the type of label to bebinary(i.e., cat or dog).

Create anotherDirectoryIteratorobject from the testing directory. Set the parameters to the same values as those of the training data.

Create a finalDirectoryIteratorobject from the validation directory. The parameters remain the same as those of the training and testing data.

The directory iterators do not augment the validation and test datasets.

Defining Your Model

Define the architecture of your neural network. Use aConvolutional Neural Network(CNN). CNNs are designed to recognize patterns and features in images.

Compile the model by using the binarycross-entropyloss function. Binary classification problems commonly use It. For the optimizer, use theAdam optimizer. It is an adaptive learning rate optimization algorithm. Finally, evaluate the model in terms of accuracy.

Print a summary of the model’s architecture to the console.

The following screenshot shows the visualization of the model architecture.

This gives you an overview of how your model design looks.

Training Your Model

Train the model using thefit()method. Set the number of steps per epoch to be the number of training samples divided by thebatch_size. Also, set the validation data and the number of validation steps.

TheImageDataGeneratorclass applies data augmentation to the training data in real time. This makes the training process of the model slower.

Evaluating Your Model

Evaluate the performance of your model on the test data using theevaluate()method. Also, print the test loss and accuracy to the console.

The following screenshot shows the model’s performance.

The model performs reasonably well on never seen data.

When you run code that does not implement the data augmentation techniques, the model training accuracy is 1. Which means it overfits. It also performs poorly on data it has never seen before. This is because it learns the peculiarities of the dataset.

When Is Data Augmentation Not Helpful?

What Is TensorFlow Capable Of

TensorFlow is a diverse and powerful library. It is capable of training complex deep learning models and can run on a range of devices from smartphones to clusters of servers. It has helped power edge computing devices that utilize machine learning.

Can the Raspberry Pi manage machine learning? If you install Google TensorFlow, yes! These four examples show what you can do.

Your phone’s camera app doesn’t show this, so it’s easy to miss.

The best features aren’t the ones being advertised.

This small feature makes a massive difference.

One casual AI chat exposed how vulnerable I was.

My iPhone does it all, but I still need my dumb phone.

Technology Explained

PC & Mobile