I tried to produce the GradCAM on different classes through a loop. However, I found that all the underlying images are the same, only the gradient is different. This is my code.
class Hook():
def __init__(self, m):
self.hook = m.register_forward_hook(self.hook_func)
def hook_func(self, m, i, o): self.stored = o.detach().clone()
def __enter__(self, *args): return self
def __exit__(self, *args): self.hook.remove()
class HookBwd():
def __init__(self, m):
self.hook = m.register_backward_hook(self.hook_func)
def hook_func(self, m, gi, go): self.stored = go[0].detach().clone()
def __enter__(self, *args): return self
def __exit__(self, *args): self.hook.remove()
imgs=[
'/content/AUC-DDD-v2/v2/train/c0/1015.jpg',
'/content/AUC-DDD-v2/v2/train/c1/1015.jpg',
'/content/AUC-DDD-v2/v2/train/c2/1015.jpg',
'/content/AUC-DDD-v2/v2/train/c3/05397.jpg',
'/content/AUC-DDD-v2/v2/train/c4/1015.jpg',
'/content/AUC-DDD-v2/v2/train/c5/1015.jpg',
'/content/AUC-DDD-v2/v2/train/c6/1015.jpg',
'/content/AUC-DDD-v2/v2/train/c7/012442.jpg',
'/content/AUC-DDD-v2/v2/train/c8/1015.jpg',
'/content/AUC-DDD-v2/v2/train/c9/1015.jpg'
]
cls=0
for i in imgs:
img=PILImage.create(i)
x, = first(data.test_dl([img]))
with HookBwd(learn.model[0]) as hookg:
with Hook(learn.model[0]) as hook:
output = learn.model.eval()(x.cuda())
act = hook.stored
output[0,cls].backward()
grad = hookg.stored
w = grad[0].mean(dim=[1,2], keepdim=True)
cam_map = (w * act[0]).sum(0)
_,ax = plt.subplots()
x_dec.show(ctx=ax)
ax.imshow(cam_map.detach().cpu(), alpha=0.6, extent=(0,224,224,0),
interpolation='bilinear', cmap='magma');
cls+=1
I have no idea why.