Skip to content

[Bug] Potential NoneType model issue in base_cam.py #569

Open
@Leacius

Description

@Leacius

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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions