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/015_CNN_008_Transfer_Learning.ipynb
Views: 47
CNN 8 - Transfer Learning
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
What is data augmentation and why is it useful
Let's start
We'll import the libraries first:
We'll have to load training and validation data from different directories throughout the notebook
The best practice is to declare a function for that
The function will also apply data augmentation to the training dataset:
Let's now load our dogs and cats dataset:
Transfer Learning in TensorFlow
With transfer learning, we're basically loading a huge pretrained model without the top clasification layer
That way, we can freeze the learned weights and only add the output layer to match our case
For example, most pretrained models were trained on ImageNet dataset which has 1000 classes
We only have two classes (cat and dog), so we'll need to specify that
We'll also add a couple of additional layers to prevent overfitting:
We got amazing accuracy right from the start!
We couldn't surpass 77% accuracy on the validation set with the custom architecture, and we're at 93% with the VGG16 model
The beauty of transfer learning isn't only that it yields a highly accurate models - you can also train models with less data, as the model doesn't have to learn as much
Transfer Learning on a 20 times smaller subset
We want to see if reducing the dataset size negatively effects the predictive power
To do so, we'll create a new directory structure for training and validation images:
Here's the directory structure printed:
Now, we'll copy only a sample of images to the new folders
We'll declare a
copy_sample()
function whcih takesn
images from thesrc_folder
and copies them to thetgt_folder
We'll keep
n
to 500 by default, which is a pretty small number:
Let's now copy the training and validation images
For the validation set, we'll copy only 100 images per class
Let's count the number of files in each folder to verify the images were copied successfully:
Now use
init_data()
to load in the images again:
There's total of 1000 training images
It will be interesting to see if we can get a decent model out of a dataset this small
Model architecture is the same, but we'll train for more epochs just because the dataset is smaller
Also, we can afford to train for longer since the training time per epoch is reduced:
It looks like we got roughly the same validation accuracy as with the model trained on 25K images, which is amazing!
Homework:
Use both models to predict the entire test set directory
How do the accuracies compare?