Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem generating CAMs using the saved model #486

Open
Q512147211 opened this issue Mar 6, 2024 · 2 comments
Open

Problem generating CAMs using the saved model #486

Q512147211 opened this issue Mar 6, 2024 · 2 comments

Comments

@Q512147211
Copy link

Q512147211 commented Mar 6, 2024

Hi,

I am facing a problem generating CAMs using the saved pre-trained VGG-13 model. The model is loaded correctly and verified using the message "All keys matched successfully." I also printed the input_tensor to verify it was reading the image correctly.

     tensor([[[[ 0.6078,  0.6000,  0.6235,  ...,  0.6314,  0.6157,  0.6314],
      [ 0.6157,  0.6000,  0.5922,  ...,  0.6314,  0.6314,  0.6314],
      [ 0.5216,  0.5137,  0.4824,  ...,  0.6235,  0.6314,  0.6314],
      ...,
      [ 0.3098,  0.2941,  0.2706,  ...,  0.0824,  0.2157,  0.2627],
      [ 0.1294,  0.1608,  0.2471,  ..., -0.1137, -0.0588,  0.1765],
      [ 0.0353, -0.0902, -0.0510,  ...,  0.0902,  0.0431,  0.0667]],

     [[ 0.5137,  0.5137,  0.4902,  ...,  0.5843,  0.5608,  0.5765],
      [ 0.5216,  0.5059,  0.4588,  ...,  0.5843,  0.5765,  0.5765],
      [ 0.4275,  0.4039,  0.3176,  ...,  0.5686,  0.5765,  0.5765],
      ...,
      [-0.4431, -0.4902, -0.5216,  ..., -0.6784, -0.5373, -0.4745],
      [-0.6549, -0.5922, -0.5373,  ..., -0.8902, -0.8275, -0.5843],
      [-0.6941, -0.6863, -0.6941,  ..., -0.7333, -0.7804, -0.7490]],

     [[ 0.6078,  0.5765,  0.6078,  ...,  0.6392,  0.6235,  0.6392],
      [ 0.6157,  0.5765,  0.5922,  ...,  0.6314,  0.6392,  0.6392],
      [ 0.5529,  0.5373,  0.5216,  ...,  0.6157,  0.6392,  0.6392],
      ...,
      [ 0.2392,  0.2000,  0.2000,  ...,  0.0980,  0.2157,  0.2314],
      [ 0.0980,  0.1059,  0.1765,  ..., -0.0588, -0.0431,  0.1529],
      [ 0.0745, -0.0275, -0.0275,  ...,  0.0745,  0.0118,  0.0196]]]])
image_loc = 'test.png'
img = np.array(Image.open(image_loc))
img = cv2.resize(img, (256, 256))
img = np.float32(img) / 255
input_tensor = preprocess_image(img)

targets = None
target_layers = [model.features[-1]]
with GradCAM(model=model, target_layers=target_layers) as cam:
    grayscale_cams = cam(input_tensor=input_tensor, targets=targets)
    cam_image = show_cam_on_image(img, grayscale_cams[0, :], use_rgb=True)
cam = np.uint8(255*grayscale_cams[0, :])
cam = cv2.merge([cam, cam, cam])
images = np.hstack((np.uint8(255*img), cam , cam_image))
Image.fromarray(images)

      An exception occurred in CAM with block: <class 'numpy.exceptions.AxisError'>. Message: axis 2 is out of bounds 
     for array of dimension 0
      ---------------------------------------------------------------------------
      NameError                                 Traceback (most recent call last)
      Cell In[5], line 6
            4     grayscale_cams = cam(input_tensor=input_tensor, targets=targets)
            5     cam_image = show_cam_on_image(img, grayscale_cams[0, :], use_rgb=True)
      ----> 6 cam = np.uint8(255*grayscale_cams[0, :])
            7 cam = cv2.merge([cam, cam, cam])
            8 images = np.hstack((np.uint8(255*img), cam , cam_image))
      
      NameError: name 'grayscale_cams' is not defined
@sivaji123256
Copy link

hi @Q512147211 ,Are you able to solve the issue?

@Q512147211
Copy link
Author

model = torchvision.models.vgg13(pretrained=False)

for param in model.parameters():
    param.requires_grad = False

model.avgpool = Identity()
model.classifier = nn.Sequential(
    nn.Linear(41472, 100), nn.ReLU(), nn.Linear(100, num_classes)
)
model.to(device)

I was using the pre-trained model as given above and was getting the error due to the

model.avgpool = Identity()

By removing this average pooling layer, I was able to generate CAMs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants