Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/Advanced Computer Vision with TensorFlow/Week 4 - Visualization and Interpretability/Copy of C3W4_Assignment.ipynb
Views: 13371
Week 4 Assignment: Saliency Maps
Welcome to the final programming exercise of this course! For this week, your task is to adapt the Cats vs Dogs Class Activation Map ungraded lab (the second ungraded lab of this week) and make it generate saliency maps instead.
As discussed in the lectures, a saliency map shows the pixels which greatly impacts the classification of an image.
This is done by getting the gradient of the loss with respect to changes in the pixel values, then plotting the results.
From there, you can see if your model is looking at the correct features when classifying an image.
For example, if you're building a dog breed classifier, you should be wary if your saliency map shows strong pixels outside the dog itself (e.g. sky, grass, dog house, etc...).
In this assignment you will be given prompts but less starter code to fill in in.
It's good practice for you to try and write as much of this code as you can from memory and from searching the web.
Whenever you feel stuck, please refer back to the labs of this week to see how to write the code. In particular, look at:
Ungraded Lab 2: Cats vs Dogs CAM
Ungraded Lab 3: Saliency
Download test files and weights
Let's begin by first downloading files we will be using for this lab.
Import the required packages
Please import:
Tensorflow
Tensorflow Datasets
Numpy
Matplotlib's PyPlot
Keras plot_model utility
Keras Models API classes you will be using
Keras layers you will be using
OpenCV (cv2)
Download and prepare the dataset.
Load Cats vs Dogs
Required: Use Tensorflow Datasets to fetch the
cats_vs_dogs
dataset.Use the first 80% of the train split of the said dataset to create your training set.
Set the
as_supervised
flag to create(image, label)
pairs.
Optional: You can create validation and test sets from the remaining 20% of the train split of
cats_vs_dogs
(i.e. you already used 80% for the train set). This is if you intend to train the model beyond what is required for submission.
Create preprocessing function
Define a function that takes in an image and label. This will:
cast the image to float32
normalize the pixel values to [0, 1]
resize the image to 300 x 300
Preprocess the training set
Use the map()
and pass in the method that you just defined to preprocess the training set.
Create batches of the training set.
This is already provided for you. Normally, you will want to shuffle the training set. But for predictability in the grading, we will simply create the batches.
Build the Cats vs Dogs classifier
You'll define a model that is nearly the same as the one in the Cats vs. Dogs CAM lab.
Please preserve the architecture of the model in the Cats vs Dogs CAM lab (this week's second lab) except for the final
Dense
layer.You should modify the Cats vs Dogs model at the last dense layer to output 2 neurons instead of 1.
This is because you will adapt the
do_salience()
function from the lab and that works with one-hot encoded labels.You can do this by changing the
units
argument of the output Dense layer from 1 to 2, with one for each of the classes (i.e. cats and dogs).You should choose an activation that outputs a probability for each of the 2 classes (i.e. categories), where the sum of the probabilities adds up to 1.
Expected Output:
Create a function to generate the saliency map
Complete the do_salience()
function below to save the normalized_tensor image.
The major steps are listed as comments below.
Each section may involve multiple lines of code.
Try your best to write the code from memory or by performing web searches.
Whenever you get stuck, you can review the "saliency" lab (the third lab of this week) to help remind you of what code to write
Generate saliency maps with untrained model
As a sanity check, you will load initialized (i.e. untrained) weights and use the function you just implemented.
This will check if you built the model correctly and are able to create a saliency map.
If an error pops up when loading the weights or the function does not run, please check your implementation for bugs.
You can check the ungraded labs of this week.
Please apply your do_salience()
function on the following image files:
cat1.jpg
cat2.jpg
catanddog.jpg
dog1.jpg
dog2.jpg
Cats will have the label 0
while dogs will have the label 1
.
For the catanddog, please use
0
.For the prefix of the salience images that will be generated, please use the prefix
epoch0_salient
.
With untrained weights, you will see something like this in the output.
You will see strong pixels outside the cat that the model uses that when classifying the image.
After training that these will slowly start to localize to features inside the pet.
Configure the model for training
Use model.compile()
to define the loss, metrics and optimizer.
Choose a loss function for the model to use when training.
For
model.compile()
the ground truth labels from the training set are passed to the model as integers (i.e. 0 or 1) as opposed to one-hot encoded vectors.The model predictions are class probabilities.
You can browse the tf.keras.losses and determine which one is best used for this case.
Remember that you can pass the function as a string (e.g.
loss = 'loss_function_a'
).
For metrics, you can measure
accuracy
.For the optimizer, please use RMSProp.
Please use the default learning rate of
0.001
.
Train your model
Please pass in the training batches and train your model for just 3 epochs.
Note: Please do not exceed 3 epochs because the grader will expect 3 epochs when grading your output.
After submitting your zipped folder for grading, feel free to continue training to improve your model.
We have loaded pre-trained weights for 15 epochs so you can get a better output when you visualize the saliency maps.
Generate saliency maps at 18 epochs
You will now use your do_salience()
function again on the same test images. Please use the same parameters as before but this time, use the prefix salient
.
You should see that the strong pixels are now very less than the ones you generated earlier. Moreover, most of them are now found on features within the pet.
Zip the images for grading
Please run the cell below to zip the normalized tensor images you generated at 18 epochs. If you get an error, please check that you have files named:
salientcat1.jpg
salientcat2.jpg
salientcatanddog.jpg
salientdog1.jpg
salientdog2.jpg
Afterwards, please download the images.zip from the Files bar on the left.
Optional: Saliency Maps at 95 epochs
We have pre-trained weights generated at 95 epochs and you can see the difference between the maps you generated at 18 epochs.
Congratulations on completing this week's assignment! Please go back to the Coursera classroom and upload the zipped folder to be graded.