utkuozbulak / pytorch-cnn-visualizations

Pytorch implementation of convolutional neural network visualization techniques
MIT License
7.82k stars 1.49k forks source link

Guided Backprop with ResNet18 #32

Closed MFreidank closed 5 years ago

MFreidank commented 5 years ago

Hello,

first off: thanks a lot for open-sourcing all these methods in a single place of reference, incredibly useful!

I am trying to use src/guided_backprop.py to visualize predictions of a torchvision.models.resnet18 model. I figured it should be as easy as replacing pretrained_model = models.alexnet(pretrained=True) with pretrained_model = models.resnet18(pretrained=True) in line 179 of src/misc_functions.py.

However, the images from guided backpropagation do not look nearly as good as for alexnet, in particular, it is not possible to distinguish the snake in them: snake_guided_bp_color

Could you provide guidance on how to use your guided backpropagation implementation with resnet models?

Thanks in advance, MFreidank

utkuozbulak commented 5 years ago

Hello,

No, it won't work like that for ResNet because of residual layers' nested nature. You have to update the line where the code iterates through the layers to cover nested sequential layers.

Have a look at how ResNet architecture is organized in Pytorch and for example, AlexNet and you will understand it better.

You don't need to make HUGE changes, just a couple of lines will be enough.

Hope it helps, Utku

MFreidank commented 5 years ago

Hi again,

thanks for your comments and explanation. I was able to make the guided_backprop.py code work with resnet18 and up with minor modifications. I decided this is probably not worthy of a pull request, since my changes are local only to guided_backprop.py. Instead I published my changes in a gist for future reference as similar requests seem to have appeared in the past (issue #3).

Thanks a lot again, MFreidank