jacobgil / pytorch-grad-cam

Advanced AI Explainability for computer vision. Support for CNNs, Vision Transformers, Classification, Object detection, Segmentation, Image similarity and more.
https://jacobgil.github.io/pytorch-gradcam-book
MIT License
10.06k stars 1.52k forks source link

Require grad error in Tutorial EigenCAM for YOLO5 #251

Closed zhenyu-brice-zhao closed 2 years ago

zhenyu-brice-zhao commented 2 years ago

Hello, I have tryed your tutorial EigenCAM for yolo5 but I got an error below RuntimeError: element 0 of tensors does not require grad and does not have a grad_fn pointing to line grayscale_cam = cam(tensor)[0, :, :] and inner line 82 in file base_cam.py: loss.backward(retain_graph=True) I'm not sure why this error occured because I just run your code without any modification. It seems that some tensors are not required grad, so it can not go backward. Any help would be greatly appreciated.

zhenyu-brice-zhao commented 2 years ago

run separately on pytorch version 1.11.1 and 1.8.1, I got the same error

jacobgil commented 2 years ago

Hi, Can you please upgrade the pytorch-grad-cam version ? Which version are you using ?

zhenyu-brice-zhao commented 2 years ago

Thank you, I use pip install grad-cam to get your code without checking the version.

jacobgil commented 2 years ago

Can you please do pip install grad-cam --upgrade And check if you still have this problem ?

zhenyu-brice-zhao commented 2 years ago

I tryed to upgrade but I got the same error.

zhenyu-brice-zhao commented 2 years ago

I think now the version is 1.3.7. Is it the latest version?

jacobgil commented 2 years ago

Hi, It's 1.3.9, I just pushed a new version, Please try again and let me know, I think it should be solved now. pip install grad-cam --upgrade

zhenyu-brice-zhao commented 2 years ago

It's ok now. Thank you!