GradCam - Zero output

username: Francesco_Grimaldi
cooked: <p>Hello everyone,</p>
<p>I just started to dig into Explainable ML/DNN field and was trying to play with “captum” a bit when I found a problem that I cannot explain.</p>
<p>N.B.<br>
Since I can’t upload more than image and share more than 2 links (D:), ill share 2 images by links.</p>
<p>I’ve a custom model to perform classification on MNIST (it reaches 97% accuracy and since I use it as a playground im not interested to improve it) with this architecture:</p>
<pre><code class="lang-auto">MnistNet(
  (FeatureExtractor): Sequential(
    (0): Conv2d(1, 8, kernel_size=(2, 2), stride=(1, 1), padding=(1, 1))
    (1): ReLU()
    (2): Conv2d(8, 16, kernel_size=(2, 2), stride=(2, 2), padding=(1, 1))
    (3): ReLU()
    (4): Conv2d(16, 20, kernel_size=(2, 2), stride=(1, 1), padding=(1, 1))
    (5): ReLU()
    (6): Conv2d(20, 24, kernel_size=(2, 2), stride=(2, 2), padding=(1, 1))
    (7): ReLU()
  )
  (Classifier): Sequential(
    (0): Flatten()
    (1): Linear(in_features=1944, out_features=256, bias=True)
    (2): Dropout(p=0.3, inplace=False)
    (3): ReLU()
    (4): Linear(in_features=256, out_features=64, bias=True)
    (5): Dropout(p=0.3, inplace=False)
    (6): ReLU()
    (7): Linear(in_features=64, out_features=10, bias=True)
  )
)
</code></pre>
<p>Now I’m trying to perform GuidedGradCam against an image of a four which is rightfully classified with 0.999 probability.</p>
<p>After I perform a “run” of Guided GradCam using every possible target (0-9) i get these results:<br>
<a href="https://freeimage.host/i/dfkz0X" rel="noopener nofollow ugc">Results of GuidedGradCam</a><br>
Now, even without considering the poor general results, the real problem is the image for the target 4 (the true label and the real model output), in fact it’s “blank”!!</p>
<p>I interpreted this result as something caused by the final ReLU pass in the GradCam so to test it, i implemented my own version of GradCam (not GuidedGradCam!) and this is the result with the same input:<br>
<a href="https://freeimage.host/i/dfknsI" rel="noopener nofollow ugc">Result of custom GradCam</a><br>
Not how the results are similar (target 4 and 9 still blank) with expected differences (its GradCam without matmul from DeepLift, so a “less resolution” is expected as it has written in the paper).</p>
<p>Finally i test what happens if i take out the ReLU from my own version of GradCam. In other words i modify the last line of code such that the return now is:</p>
<pre><code class="lang-auto">gradcam = torch.sum(A_k*alpha_k, dim=(0))
</code></pre>
<p>and not</p>
<pre><code class="lang-auto">gradcam = nn.ReLU()(torch.sum(A_k*alpha_k, dim=(0)))
</code></pre>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/8/5/859d2a6fce1630cbd2684a5778ab94369b244b83.png" data-download-href="https://discuss.pytorch.org/uploads/default/859d2a6fce1630cbd2684a5778ab94369b244b83" title="not_relu_results"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/8/5/859d2a6fce1630cbd2684a5778ab94369b244b83_2_517x251.png" alt="not_relu_results" data-base62-sha1="j40eeSaEVTm9ex3sGLyEbPEvR3d" width="517" height="251" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/8/5/859d2a6fce1630cbd2684a5778ab94369b244b83_2_517x251.png, https://discuss.pytorch.org/uploads/default/optimized/3X/8/5/859d2a6fce1630cbd2684a5778ab94369b244b83_2_775x376.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/8/5/859d2a6fce1630cbd2684a5778ab94369b244b83_2_1034x502.png 2x" data-dominant-color="A3717B"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">not_relu_results</span><span class="informations">1161×565 55.3 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>Now target 4 and 9 have very “important” outputs with really low values (up to -50 and -14). It seems that the output of GradCam should be considered in its absolute value without performing ReLU.</p>
<p><strong>Question</strong><br>
Given the above context and results, has anyone a good explanations of why this happens and why using ReLU make sense in the original GradCam?</p>

username: vivekm
cooked: <p>Hi <a class="mention" href="/u/francesco_grimaldi">@Francesco_Grimaldi</a>, this is a surprising result. To understand the cause of this further, I’d recommend looking at the activations and gradients separately of the last convolutional layer, since the product of these leads to GradCAM. You can do this with Captum using LayerActivation and dividing LayerGradientXActivation by LayerActivation for the gradient.</p>
<p>It could be that gradients of the target class of 4 with respect to the final conv output is mostly negative, potentially because the output is saturated, so the gradients are not representative of the “importance” of the corresponding activation. Looking at the gradients and activations separately will be helpful to further understand this.</p>
<p>Also, Captum does offer GradCAM itself as LayerGradCAM (since it technically outputs attributions with shape matching the layer output) with an argument relu_attributions to decide whether to apply a ReLU to the final attribution.</p>

username: Francesco_Grimaldi
cooked: <p>Thank your very much for the answer <em>vivekm</em>. I’ll try to use both my version and the Captum one to see if they are consistent and try to inspect this particular case a bit more (99% of the times using relu works fine).</p>