How to Improve Your Machine Learning Model With TensorFlow’s … – MUO – MakeUseOf

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.

You will use the cats and dogs dataset from Kaggle. 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.

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

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

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

Import TensorFlow and its relevant modules and classes.

The tensorflow.keras.preprocessing.image will enable you to perform data augmentation on your dataset.

Create an instance of the ImageDataGenerator class 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 can 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.

Create another instance of the ImageDataGenerator class for the test data. You will need the rescale parameter. It will normalize the pixel values of the test images to match the format used during training.

Create a final instance of the ImageDataGenerator class 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.

Create a DirectoryIterator object 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 of 64x64 pixels. Specify the number of images that each batch will use. Lastly, specify the type of label to be binary (i.e., cat or dog).

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

Create a final DirectoryIterator object 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.

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

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 by using the binary cross-entropy loss function. Binary classification problems commonly use It. For the optimizer, use the Adam 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.

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

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

Evaluate the performance of your model on the test data using the evaluate() 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.

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.

See the rest here:
How to Improve Your Machine Learning Model With TensorFlow's ... - MUO - MakeUseOf

Related Posts

Comments are closed.