Path: blob/main/C2/W3/assignment/C2W3_Assignment.ipynb
2955 views
Week 3: Transfer Learning
Welcome to this assignment! This week, you are going to use a technique called Transfer Learning
in which you utilize an already trained network to help you solve a similar problem to the one it was originally trained to solve.
TIPS FOR SUCCESSFUL GRADING OF YOUR ASSIGNMENT:
All cells are frozen except for the ones where you need to submit your solutions or when explicitly mentioned you can interact with it.
You can add new cells to experiment but these will be omitted by the grader, so don't rely on newly created cells to host your solution code, use the provided places for this.
ou can add the comment # grade-up-to-here in any graded cell to signal the grader that it must only evaluate up to that point. This is helpful if you want to check if you are on the right track even if you are not done with the whole assignment. Be sure to remember to delete the comment afterwards!
Avoid using global variables unless you absolutely have to. The grader tests your code in an isolated environment without running all cells from the top. As a result, global variables may be unavailable when scoring your submission. Global variables that are meant to be used will be defined in UPPERCASE.
To submit your notebook, save it and then click on the blue submit button at the beginning of the page.
Let's get started!
Dataset
For this assignment, you will use the Horse or Human dataset
, which contains images of horses and humans.
All the images are contained within the ./data/
directory. The complete tree looks like this:
Now take a look at a sample image of each one of the classes. You will simply be picking the first image from each class in the train
folder.
By plotting the images with matplotlib
it is easy to see that these images have a resolution of 300x300 (look at the image axes) and are colored, but you can double check this by using the code below:
As expected, the sample image has a resolution of 300x300 and the last dimension is used for each one of the RGB channels to represent color.
Exercise 1: train_val_datasets
Now that you have a better understanding of the images you are dealing with, it is time for you to code the datsets that will feed these images to your network. For this, complete the train_val_datasets
function below, in which you will be using the image_dataset_from_directory
function from tf.keras.utils
. For grading purposes, use a batch size of 32 for the generators, you can later test what happens if you change this parameter.
Important Note: The images have a resolution of 300x300 but the image_dataset_from_directory
method you will use allows you to set a target resolution. In this case, set a image_size
of (150, 150). This will heavily lower the number of trainable parameters in your final network, yielding much quicker training times without compromising the accuracy!
Expected Output:
Ultimately, you will want to use your trained model to predict new images, so it is always good to reserve some images for the test set. This will be images never seen by the model, which you can use to check your final model performance. As the original dataset doesn't contain a test set, you will create one by splitting the validation dataset.
Exercise 2: create_pre_trained_model
For this assignment, you will be using the pretrained model inception V3
available on Tensorflow. In the model
folder, you can already find the inception V3
weights, so you can use them to initialize the InceptionV3
model.
Complete the create_pre_trained_model
function below. You should specify the correct input_shape
for the model (remember that you set a new resolution for the images instead of the native 300x300). Remember to make all of the layers non-trainable, since you will be using the weights you just downloaded.
Expected Output:
Now print the summary for the pre_trained_model
. If you scroll down to the end of your output you will see that the layers in the model are set to non-trainable, since the number of Total params
is the same as Non-trainable params
.
Creating callbacks for later
You do not want your model to train more than it is necessary, so you will be creating a callback to stop the training once an accuracy of 99.9% is reached. Since you have already worked with callbacks beforehand in this specialization, this callback is provided for you, just run the cell below.
Exercise 3: output_of_last_layer
Now that the pre-trained model is ready, you need to "glue" it to your own model to solve the task at hand. For this you will need the last output of the pre-trained model, since this will be the input for your own. Complete the output_of_last_layer
function below.
Note: For grading purposes use the mixed7
layer as the last layer of the pre-trained model. However, after submitting feel free to come back here and play around with this.
Check that everything works as expected:
Expected Output (if mixed7
layer was used):
Now you will create the final model by adding some additional layers on top of the pre-trained model.
Complete the create_final_model
function below. You will need to use Tensorflow's Functional API for this since the pretrained model has been created using it.
Let's double check this first:
Exercise 4: create_final_model
To create the final model, you will use tf.keras.Model class by defining the appropriate inputs and outputs. If you need any help doing this, you can check the official docs.
There is more than one way to implement the final layer for this kind of binary classification problem. For this exercise, use a layer with a single unit and a sigmoid activation function along with an appropriate loss function. This way the number of parameters to train is consistent with the expected outputs presented later.
To help you build the full model, remember that you can get the input from any existing model by using its input
attribute and by using the Funcional API you can use the last layer directly as output when creating the final model.
Expected Output:
Wow, that is a lot of parameters!
After submitting your assignment later, try re-running this notebook but using the original resolution of 300x300, you will be surprised to see how many more parameters there are for that case.
Before training the model, there is one small preprocessing you need to apply to the input images. According to the inception_v3
documentation, the model expects you to apply tf.keras.applications.inception_v3.preprocess_input
to the images, which simply scales the input pixels between 1 and -1. Run the cell below to define a preprocess
function, which you can then apply to the data.
Now that you have defined your model and the preprocessing function, go ahead and train it. Note the map
method used to apply the preprocessing to the train, validation and test datasets.
The training should have stopped after less than 5 epochs and it should have reached an accuracy over 99,9% (firing the callback). This happened so quickly because of the pre-trained model you used, which already contained information to classify humans from horses. Really cool!
Now take a quick look at the training and validation accuracies for each epoch of training. Of course, since the training was done so fast you will not have many points to visualize.
Testing your model
Now that you have trained your full model, you can go ahead and test the performance on the test data you created earlier. You can simply use the .evaluate
method for this purpose:
Congratulations on finishing this week's assignment!
You have successfully implemented a convolutional neural network that leverages a pre-trained network to help you solve the problem of classifying humans from horses.
Keep it up!