Explain the Git Repo:Gradcam pytorch implementation- Part 1

umanagendra malla
2 min readJun 1, 2021

Hello everyone,

there is an ocean of repositories in GitHub, the open-sourced ones, but the main problem with these projects are there is less to no documentation of the code and what actually happens in the code.

In this article i am going to explain about the open source implementation of code of the repo https://github.com/kazuto1011/grad-cam-pytorch

this is how the repo looks like.

we will try to reproduce the results mentioned in this repo documentation.

step1:

lets create a virtual environment in a anaconda environment.

this will make the package installation and reproducing the results much more fun without messing with existing installation on conda

create a virtual environment with the name gradcam

conda create -n gradcam python=3.6 anaconda

conda activate gradcam ## activate the virtual environment

#install needed pkg inside the gradcam environment

pip install click opencv-python matplotlib tqdm numpy

pip install “torch>=0.4.1” torchvision

##move inside a directory

mkdir gradcam_image ##make a folder

cd gradcam_image/ ###move into the folder

##clone the repo

pip install “torch>=0.4.1” torchvision

##move into the repo

cd grad-cam-pytorch/

run the following command

python main.py demo1 --help  ###gives the details on the flagsDemo1:example 1:single image
python main.py demo1 -a resnet152 \
-t layer4 \
-i samples/cat_dog.png
example2: more than a single imagepython main.py demo1 -a resnet152 \
-t layer4 \
-i samples/cat_dog.png \
-i samples/vegetables.jpg
Demo 2:
function: Grad-CAM at different layers of model(resnet152 here)
class:"bull mastiff"
python main.py demo2 -i samples/cat_dog.pngDemo 3:
until now we have used resnet152 model, for this demo3,
we will use xception v1 from other repo and visualize at last convolution layer.
python main.py demo3 -i samples/cat_dog.png
all the results of these commands are saved at results folder.
please have a look at the end of each demo

The part 1 is ended here where our sole requirement is to reproduce the results and see them with our eyes.

How the demo 1,demo 2 and demo 3 is done we will get into the detail in the next set of parts.

references:

https://github.com/kazuto1011/grad-cam-pytorch

https://pythonawesome.com/pytorch-implementation-of-grad-cam/

--

--