pytorch-grad-cam icon indicating copy to clipboard operation
pytorch-grad-cam copied to clipboard

[Bug] Potential NoneType model issue in base_cam.py

Open Leacius opened this issue 6 months ago • 0 comments

Image

Hi,

I encountered an issue where the model becomes NoneType when using GradCAM. Here is the traceback:

with GradCAM(model=self.model, target_layers=self.target_layers) as cam:
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Leacius\miniconda3\envs\ATDT\Lib\site-packages\pytorch_grad_cam\grad_cam.py", line 11, in __init__
    self).__init__(
          ^^^^^^^^^
  File "C:\Users\Leacius\miniconda3\envs\ATDT\Lib\site-packages\pytorch_grad_cam\base_cam.py", line 28, in __init__
    self.device = next(self.model.parameters()).device
                       ^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'parameters'

The root cause seems to be the following line in base_cam.py (around line 24):

self.model = model.eval()

Replacing it with two separate lines solves the problem:

self.model = model
self.model.eval()

This change ensures that the model instance is properly preserved and avoids the NoneType issue.

Thank you!

Leacius avatar Apr 28 '25 11:04 Leacius