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.34k stars 1.55k forks source link

CAM dimension resize error #284

Closed fastyangmh closed 2 years ago

fastyangmh commented 2 years ago

Hello, I input the image of dimension (1, 3, 300, 300) into the model, expecting to get CAM, and the following error occurred.

I noticed that the dimension of the cam variable passed into the scale_cam_image function is (1, 1, 1536, 10, 10) and the dimension of img variable is (1, 1536, 10, 10), I think this is the cause of the cv2.resize error.

~/Desktop/ImageClassification/src/grad_cam.py in <module>
     84     for idx in tqdm(range(len(project_parameters.classes))):
     85         targets = [ClassifierOutputTarget(idx)]
---> 86         grayscale_cam = cam(input_tensor=sample,
     87                             targets=targets,
     88                             aug_smooth=True)

/usr/local/anaconda3/lib/python3.9/site-packages/pytorch_grad_cam/base_cam.py in __call__(self, input_tensor, targets, aug_smooth, eigen_smooth)
    179         # Smooth the CAM result with test time augmentation
    180         if aug_smooth is True:
--> 181             return self.forward_augmentation_smoothing(
    182                 input_tensor, targets, eigen_smooth)
    183 

/usr/local/anaconda3/lib/python3.9/site-packages/pytorch_grad_cam/base_cam.py in forward_augmentation_smoothing(self, input_tensor, targets, eigen_smooth)
    154         for transform in transforms:
    155             augmented_tensor = transform.augment_image(input_tensor)
--> 156             cam = self.forward(augmented_tensor,
    157                                targets,
    158                                eigen_smooth)

/usr/local/anaconda3/lib/python3.9/site-packages/pytorch_grad_cam/base_cam.py in forward(self, input_tensor, targets, eigen_smooth)
     91         # use all conv layers for example, all Batchnorm layers,
     92         # or something else.
---> 93         cam_per_layer = self.compute_cam_per_layer(input_tensor,
     94                                                    targets,
     95                                                    eigen_smooth)

/usr/local/anaconda3/lib/python3.9/site-packages/pytorch_grad_cam/base_cam.py in compute_cam_per_layer(self, input_tensor, targets, eigen_smooth)
    130                                      eigen_smooth)
    131             cam = np.maximum(cam, 0)
--> 132             scaled = scale_cam_image(cam, target_size)
    133             cam_per_target_layer.append(scaled[:, None, :])
    134 

/usr/local/anaconda3/lib/python3.9/site-packages/pytorch_grad_cam/utils/image.py in scale_cam_image(cam, target_size)
     55         img = img / (1e-7 + np.max(img))
     56         if target_size is not None:
---> 57             img = cv2.resize(img, target_size)
     58         result.append(img)
     59     result = np.float32(result)

error: OpenCV(4.6.0) /Users/runner/work/opencv-python/opencv-python/opencv/modules/imgproc/src/resize.cpp:3689: error: (-215:Assertion failed) !dsize.empty() in function 'resize'
jacobgil commented 2 years ago

Hi, can you shared more details, What is the target layer, and the target ? Maybe a code snippet that you used ?

What are the spatial dimensions in the output of the target_layer that you are using ?