Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Grad cam visualizer #883

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added GRAD CAM Visualizer/Images/dog_and_cat.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
217 changes: 217 additions & 0 deletions GRAD CAM Visualizer/Model/grad_cam_visualizer.ipynb

Large diffs are not rendered by default.

43 changes: 43 additions & 0 deletions GRAD CAM Visualizer/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
## GRAD-CAM Visualizer

### Libraries used
- numpy
- keras
- matplotlib
- tensorflow

### How to run
- Create a virtual environment using venv
- Activate the virtual environme
- Install dependencies
- Run the python notebook

### Overview

#### What is the Xception model
The Xception model, short for "Extreme Inception," is a deep convolutional neural network architecture. Xception builds upon the Inception architecture but replaces the standard Inception modules with depthwise separable convolutions.

#### What is GRAD-CAM
GRAD-CAM, which stands for Gradient-weighted Class Activation Mapping, is a technique used in the field of computer vision to visualize the regions of an image that are important for a convolutional neural network's decision-making process. So GRAD-CAM can be used to explain why the Xception model is classifying that as an "Persian cat" ( Or anything else ) visually through a heatmap.

#### Use Case
Since ML techniques like CNN are essentially "Black Boxes", it is hard for us to understand why it made that choice. Using GRAD-CAM we are able to explain why the CNN model made that particular choice that it did. It helps us to visually understand the "why" of the classification through a heatmap. So basically it helps us to understand visually on what features the CNN model is basing its decision on.

#### Results
In the [code](https://github.com/AMS003010/DL-Simplified/blob/grad-cam-visualizer/GRAD%20CAM%20Visualizer/Model/grad_cam_visualizer.ipynb), the `Xception` model(which is a prebuilt model in keras) is loaded and the below image is taken as input and the `imagenet` weights are loaded.

![dog_cat.png](https://github.com/AMS003010/DL-Simplified/blob/grad-cam-visualizer/GRAD%20CAM%20Visualizer/Images/dog_and_cat.png)

The model predicts it to be a 'golden_retriever' below

![image](https://github.com/user-attachments/assets/e044965a-655c-4aaf-b0cf-040bb86ede1b)

The final convulation layer is extracted to calculate the gradient and it is generated as a heatmap below

![image](https://github.com/user-attachments/assets/61af1076-f629-4536-a89b-1122b22ea549)

Finally the input image and generated heatmap is superimposed to generate the final output below which showcases "why" the `Xception` model classifies it as a 'golden_retriever'

![image](https://github.com/user-attachments/assets/ba06662f-b188-46b8-8863-be85066f1e95)

So you can see the features on which the model classifies it is highlighted in the above image.
58 changes: 58 additions & 0 deletions GRAD CAM Visualizer/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
absl-py==2.1.0
APScheduler==3.10.4
astunparse==1.6.3
certifi==2024.7.4
charset-normalizer==3.3.2
contourpy==1.2.1
cycler==0.12.1
flatbuffers==24.3.25
fonttools==4.53.1
gast==0.6.0
google-pasta==0.2.0
grpcio==1.64.1
h5py==3.11.0
idna==3.7
imutils==0.5.4
install==1.3.5
joblib==1.4.2
keras==3.4.1
kiwisolver==1.4.5
libclang==18.1.1
Markdown==3.6
markdown-it-py==3.0.0
MarkupSafe==2.1.5
matplotlib==3.9.1
mdurl==0.1.2
ml-dtypes==0.3.2
namex==0.0.8
numpy==1.26.4
opencv-python==4.10.0.84
opt-einsum==3.3.0
optree==0.12.1
packaging==24.1
pandas==1.3.5
pillow==10.4.0
protobuf==4.25.3
psutil==6.0.0
Pygments==2.18.0
pyparsing==3.1.2
python-dateutil==2.9.0.post0
pytz==2024.1
requests==2.32.3
rich==13.7.1
scikit-learn==1.5.1
scipy==1.14.0
six==1.16.0
tensorboard==2.16.2
tensorboard-data-server==0.7.2
tensorflow==2.16.2
tensorflow-intel==2.16.2
tensorflow-io-gcs-filesystem==0.31.0
termcolor==2.4.0
threadpoolctl==3.5.0
typing_extensions==4.12.2
tzdata==2024.1
tzlocal==5.2
urllib3==2.2.2
Werkzeug==3.0.3
wrapt==1.16.0
Loading