Giter Club home page Giter Club logo

pytorch-cnn-visualizations's Introduction

Convolutional Neural Network Visualizations

This repository contains a number of convolutional neural network visualization techniques implemented in PyTorch.

Note: I removed cv2 dependencies and moved the repository towards PIL. A few things might be broken (although I tested all methods), I would appreciate if you could create an issue if something does not work.

Note: The code in this repository was tested with torch version 0.4.1 and some of the functions may not work as intended in later versions. Although it shouldn't be too much of an effort to make it work, I have no plans at the moment to make the code in this repository compatible with the latest version because I'm still using 0.4.1.

Implemented Techniques

General Information

Depending on the technique, the code uses pretrained AlexNet or VGG from the model zoo. Some of the code also assumes that the layers in the model are separated into two sections; features, which contains the convolutional layers and classifier, that contains the fully connected layer (after flatting out convolutions). If you want to port this code to use it on your model that does not have such separation, you just need to do some editing on parts where it calls model.features and model.classifier.

Every technique has its own python file (e.g. gradcam.py) which I hope will make things easier to understand. misc_functions.py contains functions like image processing and image recreation which is shared by the implemented techniques.

All images are pre-processed with mean and std of the ImageNet dataset before being fed to the model. None of the code uses GPU as these operations are quite fast for a single image (except for deep dream because of the example image that is used for it is huge). You can make use of gpu with very little effort. The example pictures below include numbers in the brackets after the description, like Mastiff (243), this number represents the class id in the ImageNet dataset.

I tried to comment on the code as much as possible, if you have any issues understanding it or porting it, don't hesitate to send an email or create an issue.

Below, are some sample results for each operation.

Gradient Visualization

Target class: King Snake (56) Target class: Mastiff (243) Target class: Spider (72)
Original Image
Colored Vanilla Backpropagation
Vanilla Backpropagation Saliency
Colored Guided Backpropagation

(GB)
Guided Backpropagation Saliency

(GB)
Guided Backpropagation Negative Saliency

(GB)
Guided Backpropagation Positive Saliency

(GB)
Gradient-weighted Class Activation Map

(Grad-CAM)
Gradient-weighted Class Activation Heatmap

(Grad-CAM)
Gradient-weighted Class Activation Heatmap on Image

(Grad-CAM)
Score-weighted Class Activation Map

(Score-CAM)
Score-weighted Class Activation Heatmap

(Score-CAM)
Score-weighted Class Activation Heatmap on Image

(Score-CAM)
Colored Guided Gradient-weighted Class Activation Map

(Guided-Grad-CAM)
Guided Gradient-weighted Class Activation Map Saliency

(Guided-Grad-CAM)
Integrated Gradients
(without image multiplication)
Layerwise Relevance
(LRP) - Layer 7
Layerwise Relevance
(LRP) - Layer 1

Hierarchical Gradient Visualization

LayerCAM [16] is a simple modification of Grad-CAM [3], which can generate reliable class activation maps from different layers. For the examples provided below, a pre-trained VGG16 was used.

Class Activation Map Class Activation HeatMap Class Activation HeatMap on Image
LayerCAM
(Layer 9)
LayerCAM
(Layer 16)
LayerCAM
(Layer 23)
LayerCAM
(Layer 30)

Grad Times Image

Another technique that is proposed is simply multiplying the gradients with the image itself. Results obtained with the usage of multiple gradient techniques are below.

Vanilla Grad
X
Image
Guided Grad
X
Image
Integrated Grad
X
Image

Smooth Grad

Smooth grad is adding some Gaussian noise to the original image and calculating gradients multiple times and averaging the results [8]. There are two examples at the bottom which use vanilla and guided backpropagation to calculate the gradients. Number of images (n) to average over is selected as 50. σ is shown at the bottom of the images.

Vanilla Backprop
Guided Backprop

Convolutional Neural Network Filter Visualization

CNN filters can be visualized when we optimize the input image with respect to output of the specific convolution operation. For this example I used a pre-trained VGG16. Visualizations of layers start with basic color and direction filters at lower levels. As we approach towards the final layer the complexity of the filters also increase. If you employ external techniques like blurring, gradient clipping etc. you will probably produce better images.

Layer 2
(Conv 1-2)
Layer 10
(Conv 2-1)
Layer 17
(Conv 3-1)
Layer 24
(Conv 4-1)

Another way to visualize CNN layers is to to visualize activations for a specific input on a specific layer and filter. This was done in [1] Figure 3. Below example is obtained from layers/filters of VGG16 for the first image using guided backpropagation. The code for this opeations is in layer_activation_with_guided_backprop.py. The method is quite similar to guided backpropagation but instead of guiding the signal from the last layer and a specific target, it guides the signal from a specific layer and filter.

Input Image Layer Vis. (Filter=0) Filter Vis. (Layer=29)

Inverted Image Representations

I think this technique is the most complex technique in this repository in terms of understanding what the code does. It is mainly because of complex regularization. If you truly want to understand how this is implemented I suggest you read the second and third page of the paper [5], specifically, the regularization part. Here, the aim is to generate original image after nth layer. The further we go into the model, the harder it becomes. The results in the paper are incredibly good (see Figure 6) but here, the result quickly becomes messy as we iterate through the layers. This is because the authors of the paper tuned the parameters for each layer individually. You can tune the parameters just like the to ones that are given in the paper to optimize results for each layer. The inverted examples from several layers of AlexNet with the previous Snake picture are below.

Layer 0: Conv2d Layer 2: MaxPool2d Layer 4: ReLU
Layer 7: ReLU Layer 9: ReLU Layer 12: MaxPool2d

Deep Dream

Deep dream is technically the same operation as layer visualization the only difference is that you don't start with a random image but use a real picture. The samples below were created with VGG19, the produced result is entirely up to the filter so it is kind of hit or miss. The more complex models produce mode high level features. If you replace VGG19 with an Inception variant you will get more noticable shapes when you target higher conv layers. Like layer visualization, if you employ additional techniques like gradient clipping, blurring etc. you might get better visualizations.

Original Image
VGG19
Layer: 34
(Final Conv. Layer) Filter: 94
VGG19
Layer: 34
(Final Conv. Layer) Filter: 103

Class Specific Image Generation

This operation produces different outputs based on the model and the applied regularization method. Below, are some samples produced with VGG19 incorporated with Gaussian blur every other iteration (see [14] for details). The quality of generated images also depend on the model, AlexNet generally has green(ish) artifacts but VGGs produce (kind of) better images. Note that these images are generated with regular CNNs with optimizing the input and not with GANs.

Target class: Worm Snake (52) - (VGG19) Target class: Spider (72) - (VGG19)

The samples below show the produced image with no regularization, l1 and l2 regularizations on target class: flamingo (130) to show the differences between regularization methods. These images are generated with a pretrained AlexNet.

No Regularization L1 Regularization L2 Regularization

Produced samples can further be optimized to resemble the desired target class, some of the operations you can incorporate to improve quality are; blurring, clipping gradients that are below a certain treshold, random color swaps on some parts, random cropping the image, forcing generated image to follow a path to force continuity.

Some of these techniques are implemented in generate_regularized_class_specific_samples.py (courtesy of alexstoken).

Requirements:

torch == 0.4.1
torchvision >= 0.1.9
numpy >= 1.13.0
matplotlib >= 1.5
PIL >= 1.1.7

Citation

If you find the code in this repository useful for your research consider citing it.

@misc{uozbulak_pytorch_vis_2022,
  author = {Utku Ozbulak},
  title = {PyTorch CNN Visualizations},
  year = {2019},
  publisher = {GitHub},
  journal = {GitHub repository},
  howpublished = {\url{https://github.com/utkuozbulak/pytorch-cnn-visualizations}},
  commit = {b7e60adaf64c9be97b480509285718603d1e9ba4}
}

References:

[1] J. T. Springenberg, A. Dosovitskiy, T. Brox, and M. Riedmiller. Striving for Simplicity: The All Convolutional Net, https://arxiv.org/abs/1412.6806

[2] B. Zhou, A. Khosla, A. Lapedriza, A. Oliva, A. Torralba. Learning Deep Features for Discriminative Localization, https://arxiv.org/abs/1512.04150

[3] R. R. Selvaraju, A. Das, R. Vedantam, M. Cogswell, D. Parikh, and D. Batra. Grad-CAM: Visual Explanations from Deep Networks via Gradient-based Localization, https://arxiv.org/abs/1610.02391

[4] K. Simonyan, A. Vedaldi, A. Zisserman. Deep Inside Convolutional Networks: Visualising Image Classification Models and Saliency Maps, https://arxiv.org/abs/1312.6034

[5] A. Mahendran, A. Vedaldi. Understanding Deep Image Representations by Inverting Them, https://arxiv.org/abs/1412.0035

[6] H. Noh, S. Hong, B. Han, Learning Deconvolution Network for Semantic Segmentation https://www.cv-foundation.org/openaccess/content_iccv_2015/papers/Noh_Learning_Deconvolution_Network_ICCV_2015_paper.pdf

[7] A. Nguyen, J. Yosinski, J. Clune. Deep Neural Networks are Easily Fooled: High Confidence Predictions for Unrecognizable Images https://arxiv.org/abs/1412.1897

[8] D. Smilkov, N. Thorat, N. Kim, F. Viégas, M. Wattenberg. SmoothGrad: removing noise by adding noise https://arxiv.org/abs/1706.03825

[9] D. Erhan, Y. Bengio, A. Courville, P. Vincent. Visualizing Higher-Layer Features of a Deep Network https://www.researchgate.net/publication/265022827_Visualizing_Higher-Layer_Features_of_a_Deep_Network

[10] A. Mordvintsev, C. Olah, M. Tyka. Inceptionism: Going Deeper into Neural Networks https://research.googleblog.com/2015/06/inceptionism-going-deeper-into-neural.html

[11] I. J. Goodfellow, J. Shlens, C. Szegedy. Explaining and Harnessing Adversarial Examples https://arxiv.org/abs/1412.6572

[12] A. Shrikumar, P. Greenside, A. Shcherbina, A. Kundaje. Not Just a Black Box: Learning Important Features Through Propagating Activation Differences https://arxiv.org/abs/1605.01713

[13] M. Sundararajan, A. Taly, Q. Yan. Axiomatic Attribution for Deep Networks https://arxiv.org/abs/1703.01365

[14] J. Yosinski, J. Clune, A. Nguyen, T. Fuchs, Hod Lipson, Understanding Neural Networks Through Deep Visualization https://arxiv.org/abs/1506.06579

[15] H. Wang, Z. Wang, M. Du, F. Yang, Z. Zhang, S. Ding, P. Mardziel, X. Hu. Score-CAM: Score-Weighted Visual Explanations for Convolutional Neural Networks https://arxiv.org/abs/1910.01279

[16] P. Jiang, C. Zhang, Q. Hou, M. Cheng, Y. Wei. LayerCAM: Exploring Hierarchical Class Activation Maps for Localization http://mmcheng.net/mftp/Papers/21TIP_LayerCAM.pdf

[17] G. Montavon1, A. Binder, S. Lapuschkin, W. Samek, and K. Muller. Layer-Wise Relevance Propagation: An Overview https://www.researchgate.net/publication/335708351_Layer-Wise_Relevance_Propagation_An_Overview

pytorch-cnn-visualizations's People

Contributors

alexstoken avatar haofanwang avatar pengtaojiang avatar rgeirhos avatar utkuozbulak avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

pytorch-cnn-visualizations's Issues

Guided Backprop with ResNet18

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

How to use the backprop on densenet and other multi-channel models?

Hi,
I want to move this code to densenet and other models. Here is the problem: When I run the code 'guided_backprop' based on DenseNet. Should I also replace the Relu inside the DenseBlock and Dense Layers? I know that we need to replace the Relu layers in VGG, but in Dense Block, the connection is a little bit different and complex. So do we need to replace them as well?

Getting Runtime Error

I am getting following error while testing the code with my own image. Could anyone please help me with that?

RuntimeError: Given input size: (256x2x4). Calculated output size: (256x0x1). Output size is too small at /pytorch/aten/src/THNN/generic/SpatialDilatedMaxPooling.c:67

Sign error in CNN Layer Visualization

There is only a very minor sign error in the code. In the hook version you correctly minimize the negative mean, hence, maximizing the mean.

In the unhooked version however, the minus sign is missing, computing the input than activates the feature the least (line 102):

loss = torch.mean(self.conv_output)

Additionally, the code has to be modified to run the latest pytorch 0.4, where zero-dimensional tensor can no longer be indexed (line 101 and 63).

Running Resnet 101 torchvision model fails for gradcam (activation maps)

Alexnet works fine, but Resnet 101 fails.

I set pretrained_model = models.resnet101(pretrained=True) in get_params() in misc_funcions.py.
Then in forward_pass_on_convolutions() in gradcam, resnet models don't have model.features, so:

for module_pos, module in self.model.features._modules.items():

fails.

Any suggestion? self.model has _modules property, but that was not successful either, and doesn't seem to show all tensors/layers of Resnet 101

Gradient values and gradient relevance

Hi, I've been trying the code for a while and have a couple of questions about the gradients.
To give some context here, I would like to identify which feature maps are the most relevant for a specific class, e.g. not use all the feature maps for visualization, but only the most important.

So the paper says that after doing the back-propagation for a specific class, we average the gradients, and that captures the "importance" of a feature map. I've been exploring the distribution for each layer using alexnet and here I show the distribution of those averaged gradients for a specific layer in AlexNet:
image
So we have a distribution with both positive and negative gradients that are close to zero.
As the code is implemented, we use all of those gradients and that results in a visualization that looks like this for the class gondola from imagenet:
image
At first, I thought, ok I won't use all the gradients, I only want the gradients closest to zero and so I set a window to include those. And here comes my first question:

  1. Are the gradients closest to zero (or zero), the ones that would require a smaller update for the kernels during training? meaning that those feature maps are the most relevant and meaningful for the final classification?

After trying that I didn't get an improved visualization, so I kept exploring the gradients. Then I decided to only use the positive or only the negative gradients, here are the results:
Only positive gradients:
image
Only negative gradients:
image
It seems for me that the negative gradients are the most meaningful, even more than using all the gradients, and the same happens for other images as well. Here I have my next question:

  1. Why would the negative gradients be more relevant for the visualization?
  2. What's going on with only the positive values and why are those guiding to a completely different visualization than in the other cases?

Thanks in advance for any answer and I'm looking forward for the discussion.

code for references 4

Thank you for you providing awesome code for the github community! Which code is for reference 4?
vanilla_backprop.py? or others?

how to see saliency maps from middle layers

Hi,

Do you know how to get neuron's saliency map from the middle layer?
I want to pick some neurons from the middle layer and compare visualization result
but I cannot figure out how to pick values to see an image from lots of gradients.

Thank you,

too many indices for array

Traceback (most recent call last):
File "X:/pytorch-cnn-visualizations-master/src/cnn_layer_visualization.py", line 130, in
layer_vis.visualise_layer_without_hooks()
File "X:/pytorch-cnn-visualizations-master/src/cnn_layer_visualization.py", line 105, in visualise_layer_without_hooks
print('Iteration:', str(i), 'Loss:', "{0:.2f}".format(loss.data.numpy()[0]))
IndexError: too many indices for array
same Error in deep_dreem.py in exactly the same line but lint no 62,
same in inverted_representation.py line 106 and generate_claass_specificsamples.py line 43

visualize model that not in the model zoo

HI! as you said:

If you want to port this code to use it on your model that does not have such separation, you just need to do some editing on parts where it calls model.features and model.classifier.

I try to modify the model.features or .classifier part , but i get confused how to do it. Below is part of my script , hope that you can give some details about how to visualize own trained model.

model =torch.load(model_save_path)
for index,(layer,_) in enumerate(model.items()):
      # model.items() return the weight of this layer, eg, model.items()[0]=model.0.1.weight
      x = layer(x)
        ......

but i get the TypeError that the object is not callable. i wonder how to read the layer from the trained model that didn't have features attribute. thanks~

Other Contribution Method

I think the method “Integrated Gradients” and “LRP(Layer-relevance-propagation )”are
also popular. So, maybe these method can be implemented by pytorch framework.

Question about using features

In the code from update_relus in guided_backprop.py, why do you only add hooks for the RELUs in model.features - what about model.classifier?

        # Loop through layers, hook up ReLUs
        for pos, module in self.model.features._modules.items():
            if isinstance(module, ReLU):
                module.register_backward_hook(relu_backward_hook_function)
                module.register_forward_hook(relu_forward_hook_function)

Sharp edges in layer visualization

This is probably not an issue with your code as it does not happen for your examples:

When doing activation maximization, the right and bottom rim of the generated image has a sharp edge, were the image grey:
test_edge

Do you have any idea why this jump occurs? Is this a problem with the model I am using or with the activation maximization itself?

image size problem

Hi,I find a bug in gradcam.py
The Image package use w,h mode. However, the precessed image is a tensor with 1,c,h,w. So,the order of w and h need to be switched when using Image.resize.

Line 88,change
cam = np.uint8(Image.fromarray(cam).resize((input_image.shape[2],
input_image.shape[3]), Image.ANTIALIAS))
to
cam = np.uint8(Image.fromarray(cam).resize((input_image.shape[3],
input_image.shape[2]), Image.ANTIALIAS))

Wrong dimension for the first layer visualization

Hi,
I was using the guided backpropagation to visualize my own pretrained models and found out the resultant gradient maps did not have the same dimension as the input image, but the ouput shape of the first layer.
For instance, I have the input of [batch, 1000, 17] and the ouput of the first layer is [batch,1000,32]
Then, the gradient maps has the shape of [1000,32] with the elements from 17~32 as non zeros.

I have also tried to print the shape of the gradient in the hook function, which is also [batch, 1000, 32]
Can you help me to figure out what went wrong?

Thank you very much.
Best,
Yu.

cv2.error: OpenCV(3.4.1)

cv2im = cv2.resize(cv2im, (224, 224))
cv2.error: OpenCV(3.4.1) /io/opencv/modules/imgproc/src/resize.cpp:4044: error: (-215) ssize.width > 0 && ssize.height > 0 in function resize

do you know why I always getting these errors?

For instance, this error is generated by function: misc_functions.py

def preprocess_image(cv2im, resize_im=True):
"""
Processes image for CNNs
Args:
PIL_img (PIL_img): Image to process
resize_im (bool): Resize to 224 or not
returns:
im_as_var (Pytorch variable): Variable that contains processed float tensor
"""
# mean and std list for channels (Imagenet)
mean = [0.485, 0.456, 0.406]
std = [0.229, 0.224, 0.225]
# Resize image
if resize_im:
cv2im = cv2.resize(cv2im, (224, 224))
im_as_arr = np.float32(cv2im)
im_as_arr = np.ascontiguousarray(im_as_arr[..., ::-1])
im_as_arr = im_as_arr.transpose(2, 0, 1) # Convert array to D,W,H
# Normalize the channels
for channel, _ in enumerate(im_as_arr):
im_as_arr[channel] /= 255
im_as_arr[channel] -= mean[channel]
im_as_arr[channel] /= std[channel]
# Convert to float tensor
im_as_ten = torch.from_numpy(im_as_arr).float()
# Add one more channel to the beginning. Tensor shape = 1,3,224,224
im_as_ten.unsqueeze_(0)
# Convert to Pytorch variable
im_as_var = Variable(im_as_ten, requires_grad=True)
return im_as_var

How should I start? :)

Hey
Sorry for the stupid question, but can you please tell how i should start using this code?
What should i run if i wanna start ...
Can I give an input image and generates the maps?

Thanks

Use of hooks

Thank you for your awesome code.
I have a quick question about the use of hooks in some of your code.
In src/guided_backprop.py, it returns some tuple.
How is this being applied to the self.gradients? I tried looking up uses of hook layers, but couldn't find an example that resembles that of yours. More specifically, when update_relus returns those tuples, where do they go? I am guessing it replaces the grad_in in relu_hook_function inplace, then passed to hook_function in hook_layers, but wanted to make sure. Any guidance would be appreciated.

how to explain some operations in gradcam.py

cam = np.ones(target.shape[1:], dtype=np.float32) why not creat a array whose values are zeros
for i, w in enumerate(weights):
cam += w * target[i, :, :]
cam = cv2.resize(cam, (224, 224))
cam = np.maximum(cam, 0) why do this operation

I get the output of filters in layer2 all grey

sorry for bother you, I run the code 'cnn_layer_visualization' and observe the output of all the filters in layer2, but i get the picture all grey, and the loss decay very slow:
2018-05-22 10-21-02
2018-05-22 10-21-36
is my iteration too small?

guided_backprop register backward/forward hook seems to consume a lot of GPU memory

Hi, really a great job!
I was trying to get a guided-gradcam visualization of a video. But I found as more images have been read, very soon my server shows CUDA out of memory error. Then I discovered that it is because register_backward/forward_hook consumes a lot of GPU memory. Is there any way I could fix this issue? Thanks in advance!

Deconvolution networks

Dear Sir, if possible, would you please implement the deconvolutional networks? I think that one is very popular as well.

hook_layer?

Hey in CNN visualisation toolkit : https://github.com/utkuozbulak/pytorch-cnn-visualizations/blob/master/src/cnn_layer_visualization.py I am unable to understand :

def hook_layer(self):
        def hook_function(module, grad_in, grad_out):
            # Gets the conv output of the selected filter (from selected layer)
            self.conv_output = grad_out[0, self.selected_filter]

        # Hook the selected layer
        self.model[self.selected_layer].register_forward_hook(hook_function)

What are the hooks functions and ‘grad_out’ doing? Grad_out has been not defined anywhere yet the code works fine. Also later this is called as :


def visualise_layer_with_hooks(self):
        # Hook the selected layer
        self.hook_layer()

without any arguments passed for grad_in or out. What is this line doing and how?

register_backward_hook

Good day! I use fine-tuning loaded model ResnetXt-101 from .pth file. GradCam works good with my model, but when I try to use GuidedBackprop I get the error:

-> 80 gradients_as_arr = self.gradients.data.numpy()[0]
81 return gradients_as_arr
82

AttributeError: 'NoneType' object has no attribute 'data'

The error occurs due to hook_function is not called.

def hook_layers(self):
    def hook_function(module, grad_in, grad_out):
        print("Vaxx")
        self.gradients = grad_in[0]

Could you help me understand why it does not call?

Model does not have 'feature' attribute

Hi thanks for the code, I'm trying to visualise a trained ResNet18 model with the GradCam function. But ResNet models do not have the feature attribute as in VGGs. Should we use model.named_children() instead?

from gradcam import GradCam
grad_cam = GradCam(model, target_layer=7)

Error:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-16-0a85f7f4c865> in <module>
      8 grad_cam = GradCam(model, target_layer=0)
      9 # Generate cam mask
---> 10 cam = grad_cam.generate_cam(x, target_class=None)
     11 # Save mask
     12 save_class_activation_images(original_image, cam, file_name_to_export)

/mnt/sdh/adam/visualisation/gradcam.py in generate_cam(self, input_image, target_class)
     56         # conv_output is the output of convolutions at specified layer
     57         # model_output is the final output of the model (1, 1000)
---> 58         conv_output, model_output = self.extractor.forward_pass(input_image)
     59         if target_class is None:
     60             target_class = np.argmax(model_output.data.numpy())

/mnt/sdh/adam/visualisation/gradcam.py in forward_pass(self, x)
     35         """
     36         # Forward pass on the convolutions
---> 37         conv_output, x = self.forward_pass_on_convolutions(x)
     38         x = x.view(x.size(0), -1)  # Flatten
     39         # Forward pass on the classifier

/mnt/sdh/adam/visualisation/gradcam.py in forward_pass_on_convolutions(self, x)
     23         """
     24         conv_output = None
---> 25         for module_pos, module in self.model.features._modules.items():
     26             x = module(x)  # Forward
     27             if int(module_pos) == self.target_layer:

/mnt/sdh/adam/adam_env/lib/python3.6/site-packages/torch/nn/modules/module.py in __getattr__(self, name)
    589                 return modules[name]
    590         raise AttributeError("'{}' object has no attribute '{}'".format(
--> 591             type(self).__name__, name))
    592 
    593     def __setattr__(self, name, value):

AttributeError: 'ResNet' object has no attribute 'features'

Segmentation fault (core dumped) with gradcam

Hi
I am using the file gradcam.py to visualize my own network.
However when I begin to run it , it shows 'Segmentation fault (core dumped)'.
I can't locate the problem.
Can you help me to solve it?
Thanks a lot!

TypeError: 'Net' object does not support indexing

Hey, when I pass my own model into the CNNLayerVisualization. I get TypeError: 'Net' object does not support indexing. What is the fix for this?
My model:

class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.features = nn.Sequential(
            nn.Conv2d(3, 6, 3),
            nn.ReLU(inplace=True),
            nn.MaxPool2d(2, 2),
            nn.Conv2d(6, 16, 3),
            nn.ReLU(inplace=True),
            nn.Conv2d(16, 20, 3),
            nn.ReLU(inplace=True),
            torch.nn.Conv2d(20, 27, 3),
            nn.ReLU(inplace=True)
        )
        self.classifier = nn.Sequential(
            nn.Linear(27 * 9 * 9, 120),
            nn.ReLU(inplace=True),
            nn.Linear(120, 84),
            nn.ReLU(inplace=True),
            nn.Linear(84, 10)
        )

    def forward(self, x):
        x = self.features(x)
        x = x.view(-1, 27 * 9 * 9)
        x = self.classifier(x)
        return x
    


net = Net().cuda()
criterion = nn.CrossEntropyLoss().cuda()
init_lr=0.01
optimizer = torch.optim.Adam(net.parameters(), lr=init_lr)

``

Wrong implementation of Guided Backprop ?

Isn't there a bug in the way negative gradients are filtered out for ReLU's in Guided Backprop?

What you do is:
torch.clamp(grad_in[0], min=0.0),

while the paper states:

rather than masking out values corresponding to negative entries of the top gradient (’deconvnet’) or bottom data (backpropagation), we mask out the values for which at least one
of these values is negative

It seems to me that you forgot to include the top gradient.

Visualization with my custom model

Dear @utkuozbulak ,

I have my own custom model that looks like this:
input image --> torch.Size([1, 3, 34, 694])
cnn.conv0.weight torch.Size([64, 1, 3, 3])
cnn.conv0.bias torch.Size([64])
cnn.conv1.weight torch.Size([64, 64, 3, 3])
cnn.conv1.bias torch.Size([64])
etc etc

I tried to run the vanilla back prop code, however I got this error:
RuntimeError: Given groups=1, weight[64, 1, 3, 3], so expected input[1, 3, 34, 694] to have 1 channels, but got 3 channels instead

Do you have an idea where it might go wrong?

Thank you

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.