CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
Path: blob/main/014_CNN_007_Data_Augmentation.ipynb
Views: 47
CNN 7 - Data Augmentation
Dataset:
The dataset isn't deep-learning-compatible by default, here's how to preprocess it:
What you should know by now:
How to preprocess image data
How to load image data from a directory
What's a convolution, pooling, and a fully-connected layer
Categorical vs. binary classification
To start, let's get the library imports out of the way
We only need a few
The
plot_image()
function plots a single image from an array
Let's load a sample image:
Data Augmentation with TensorFlow in Action
We can declare a model that does different things to an image
For example, the model below will resize the image to 224x224 and rescale the underlying matrix to a 0-1 range:
You typically handle resizing inside the
flow_from_directory()
function, but you can also use it hereThe more interesting transformations are random flips and rotations
A
RandomFlip
will flip the image horizontally, vertically, or both, depending on the mode parameterA
RandomRotation
will rotate the image by some some factor (0.2 means 0.2 * 2PI)
Let's extend it even more!
A
RandomZoom
will zoom images - 0.2 means 20%A
RandomTranslation
shifts the image (height = vertically, width = horizontally) - 0.2 means 20%
To verify these transformations are indeed random, we can make a 3x3 plot showing results of 9 random transformations:
Some of them make sense, while others don't - but you can always classify the image as a cat
Next, let's see how to handle data augmentation with TensorFlow's
ImageDataGenerator
Data Augmentation with TensorFlow's ImageDataGenerator
You now know what individual transformations do to an image, but it isn't common to write data augmentation as a separate sequential model
It's more common to apply the transformations when loading the data - with TensorFlow's
ImageDataGenerator
Keep in mind - You should only augment training data!
We'll now visualize a single batch of images to see if transformations worked
Load in the test set:
Extract the first batch:
A function to visualize 64 images at once:
We definitely have some weird ones, but overall, data augmentation is doing a decent job by adding variety to our dataset
Finally, let's load in both training and validation images
We need to reset the training images because we went over the first batch
And now we can train the model!
Model Training - Can Data Augmentation Improve Accuracy?
We'll use the same model architecture we used when first training an image classifier with CNNs
It resulted in 75% accuracy on the validation set
Training the same model on augmented data should improve the validation accuracy
Note: I'm facing some GPU issues so the model was actually trained on the CPU, hence the long training time
RTX3060Ti usually goes over an epoch in 22 seconds
We've increased the validation accuracy by almost 3%
It's definitely a step in the right direction, and a way to improve it even further is by using transfer learning
You'll learn all about it in the next notebook