frgfm / torch-cam

Class activation maps for your PyTorch models (CAM, Grad-CAM, Grad-CAM++, Smooth Grad-CAM++, Score-CAM, SS-CAM, IS-CAM, XGrad-CAM, Layer-CAM)
https://frgfm.github.io/torch-cam/
Apache License 2.0
2.02k stars 207 forks source link

RuntimeError: cannot register a hook on a tensor that doesn't require gradient #217

Closed ZHEGG closed 1 year ago

ZHEGG commented 1 year ago

I have encountered an error when I wish to observe a 3D convolution cam of the model in the uniformer in the video domain

File "/home/mdisk3/bianzhewu/medical_repertory/miccai2023/LLD-MMRI2023/main/models/uniformer.py", line 139, in forward
    x = x + self.drop_path(self.attn(self.norm1(x)))
  File "/home/bianzhewu/.virtualenvs/miccai2023/lib/python3.8/site-packages/torch/nn/modules/module.py", line 893, in _call_impl
    hook_result = hook(self, input, result)
  File "/home/bianzhewu/.virtualenvs/miccai2023/lib/python3.8/site-packages/torchcam/methods/gradient.py", line 50, in _hook_g
    self.hook_handles.append(output.register_hook(partial(self._store_grad, idx=idx)))
  File "/home/bianzhewu/.virtualenvs/miccai2023/lib/python3.8/site-packages/torch/tensor.py", line 279, in register_hook
    raise RuntimeError("cannot register a hook on a tensor that "
RuntimeError: cannot register a hook on a tensor that doesn't require gradient

I want to check the cam when testing and my simple examples as follows:

model = uniformer()
model.load_state_dict()
model.eval()
cam_extractor = SmoothGradCAMpp(model,target_layer=model.blocks3[-1].attn,input_shape=(8,14,112,112))
with torch.no_grad():
      output = model(input)
      activate_map = cam_extractor(target,output)
frgfm commented 1 year ago

Hi @ZHEGG :wave:

I imagine you found within the discussions/issues the solution? If not, you're doing inference and CAM computation with torch.no_grad() as context. And SmoothGradCampp as its name implies, is leveraging gradients. So you can switch to an activation-only CAM method or consider removing the no_grad context :)

Cheers!