In this assignment, we will be understanding how CNN’s work and how different layers extract the important features from the image. For this assignment, we will be using only pytorch libraries for CNN implementation. For GradCAM and Occlusion you are free to use the Captum library.

We invite you to watch the following video and this reference that provides the context of the assignment and describes the tools you can use to implement the explainers.

For the tasks below you will be using the dataset from Huggingface. You can use the datasets library to load the dataset.

Task-1 - Convolutional Neural Networks for Image Classification

In this task you will be training a simple CNN model to perform a binary classification on cats and dogs. You will be provided with a dataset

For submission , upload the following :

  1. model summary by calling model.summary()
  2. The train vs val loss and accuracy plots
  3. The confusion matrix
  4. The classification report on test dataset

Task-2 - GradCAM and Occlusion

A cool task for the above model is to learn how different features are extracted by model in each layer . To do this we will using GradCAM and Occlusion methods.

GradCAM

GradCAM uses the gradients of the target class w.r.t. the feature maps of a CNN layer to compute importance weights. In this exercise we want you to generate visulaizations and write short notes on your understanding on how each layer contributes to the final classification result. You should also display overlay of heat maps of feature vectors on the image to support your understanding of the model’s behavior.

Occlusion

Occlusion ( occlusion sensitivity) is a simple but powerful interpretability technique for CNNs. Essentially, we make the model freak out by masking the input image but in specific small patches. You take an input image and systematically mask (occlude) different parts of it (e.g., by placing a gray or black square patch over a region). For each occluded version of the image, you run it through the CNN and record how much the model’s prediction score for the target class changes.

The idea:

  • If covering a region makes the score drop a lot, that region is important for the decision.

  • If covering it barely changes the score, it’s less relevant.

For submission you will require to submit the accuracy score with at least 3 different occlusion masks and heatmap plot for each mask

Task - 3 Fast Gradient Sign Method for Adversarial Attacks

Lets play a game of adversaries. Convolutional Neural Networks (CNNs) have achieved remarkable success in image classification tasks, from medical imaging to self-driving cars. However, CNNs are not invincible. Tiny, carefully crafted perturbations that are often imperceptible to the human eye, can cause a model to make confident but wrong predictions. This phenomenon is known as an adversarial attack.

One of the simplest and most famous attacks is the Fast Gradient Sign Method (FGSM), introduced by Goodfellow et al. (2015).

In this task we will use the same technique to check if the CNN classifier you developed in task-1 is vulnerable to adversarial attacks. You will need to use FGSM to create adversarial examples for the test dataset and check if the model is still able to make correct predictions.

For submission you will require to submit the accuracy score on the adversarial examples with at least 3 different epsilon values and display the adversarial images for each epsilon value.