Open chr5tphr opened 11 months ago
Hi @chr5tphr is there any news on this bug? I noticed that when setting zero_params='bias'
for EpsilonPlusFlat composite total attribution sums to ~1 but sometimes negative attribution may occur and that negative attribution sometimes is small but sometimes it accounts to ~-1 and more so its significant and positive part of the attribution in that case accounts to almost ~2 to negate the effect.
Here is code I used to replicate this issue (multiple runs may be required):
import torch
import torch.nn as nn
from torchvision.models import resnet34
from zennit.torchvision import ResNetCanonizer
from zennit.composites import EpsilonPlusFlat
import matplotlib.pyplot as plt
model = resnet34(weights=None)
# create a composite, specifying the canonizers
composite = EpsilonPlusFlat(canonizers=[ResNetCanonizer()], zero_params='bias')
target = torch.eye(1000)[[437]]
input_data = torch.rand(1, 3, 224, 224)
input_data.requires_grad = True
with composite.context(model) as modified_model:
output = modified_model(input_data)
attribution, = torch.autograd.grad(output, input_data, target)
relevance = attribution.cpu().sum(1).squeeze(0)
if torch.any(relevance < 0):
print(relevance[relevance < 0].sum())
print(relevance[relevance > 0].sum())
print(relevance.sum())
plt.imshow(relevance.numpy())
plt.show()
If you maybe don't have the time to deal with this issue could you maybe point me in the right direction so I could try to fix it since it is critical for me to fix this issue in order to continue with my research.
Hey @MikiFER
I am not sure what you are experiencing is concerning this issue, as the relevance still sums to 1.
The bug may be a little bit elusive, but in general it is okay if there is negative relevance (for EpsilonPlusFlat anyways).
You should be fine on 0.5.1
, the bug referred to in this issue is only on master
, caused by #185.
Did you use master
?
Here's a little snippet to check the model relevance in detail. I have added an extra rule to switch off the residual branch to circumvent the LRP-instabilities discussed in #148. The canonizer is simply ineffective for vgg11, which does not need one (although, since ResNetCanonizer includes MergeBatchNorm, vgg11bn would also work):
And this is the output I get on 0.5.1
:
If you are on master
: Do you have a more concrete example of the bug?
This would help me to pin the issue down.
E.g., the snippet above should produce relevance sums not equal to one. You can try to comment out the line with the layer_map=
in the composite to enable relevance from the residual connection.
Maybe skipping the residual branch already fixes your issue?
Hi @chr5tphr thank you for your response. I have since discovered that I had a misunderstanding about the EpsilonPlusFlat composite and have decided that pure Alpha1Beta0 rule is actually what I need because I want to obtain relevance map with only positive influence in the input.
With the introduction of #185 , ResNet18 attributions result in negative attribution sums in the input layer, leading to bad attributions. Although #185 increased the stability of the attribution sums for ResNet, the previous instability seems to have inflated the positive parts of the attributions, circumventing this problem pre #185.
This seems to be related to leaking attributions (#193 ) combined with skip connections that can cause negative attributions.
A quickfix for
EpsilonGammaBox
is to use a slightly higher gamma value.