utkuozbulak / pytorch-cnn-visualizations

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

Support for non-VGG models. #106

Closed zerovirus123 closed 2 years ago

zerovirus123 commented 2 years ago

Hi, thanks for the great work. Does this codebase support non-VGG models? For example, I tried to generate GradCams for ResNet architectures, but failed because the ResNet models do not have the feature attributes.

Traceback (most recent call last):
  File "cnn_visualize.py", line 99, in <module>
    visualizer.visualize()
  File "cnn_visualize.py", line 88, in visualize
    cam = grad_cam.generate_cam(image)
  File "F:\MMCET\cnn_visualization\grad_cam.py", line 62, in generate_cam
    conv_output, model_output = self.extractor.forward_pass(input_image)
  File "F:\MMCET\cnn_visualization\grad_cam.py", line 41, in forward_pass
    conv_output, x = self.forward_pass_on_convolutions(x)
  File "F:\MMCET\cnn_visualization\grad_cam.py", line 29, in forward_pass_on_convolutions
    for module_pos, module in self.model.features._modules.items():
  File "C:\Users\Deployment\.conda\envs\classifier_eval\lib\site-packages\torch\nn\modules\module.py", line 1131, in __getattr__
    type(self).__name__, name))
AttributeError: 'ResNet' object has no attribute 'features'
utkuozbulak commented 2 years ago

It is because of the nested layers, you have to select the conv layers within the residual layers. You can modify two or three lines of code and make it compatible with resnets or other non-vgg-like models. Check previously closed issues, if I remember correctly, there was some code that had resnet compatibility.

The reason it is not supported out of the box is because If I push some code that support resnets, there will be someone asking support for inceptions, then for densenet, then for vit, then for mxnet, then for deit. You see how this spirals out of control. Eventually, this repository is going to have more code about models than the techniques themselves.