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

cv::ColorMap only supports source images of type CV_8UC1 or CV_8UC3 #339

Open lunaryan opened 2 years ago

lunaryan commented 2 years ago

I encountered the following error:

Traceback (most recent call last):
File "inverse_trigger_match.py", line 627, in evaluate_trojai
show_cam(model, [target_layer], poisonX, target_label)
File "inverse_trigger_match.py", line 451, in show_cam
cam_image = show_cam_on_image(batch_data, grayscale_cam)
File "/home/yan390/miniconda3/envs/py3.6/lib/python3.6/site-packages/pytorch_grad_cam/utils/image.py", line 47, in show_cam_on_image
heatmap = cv2.applyColorMap((255 * mask).astype(np.uint8), colormap)
cv2.error: OpenCV(4.6.0) /io/opencv/modules/imgproc/src/colormap.cpp:736: error: (-5:Bad argument) cv::ColorMap only supports source images of type CV_8UC1 or CV_8UC3 in function 'operator()'

I searched online (https://stackoverflow.com/questions/59873709/saving-16-bit-image-in-opencv-with-cmap-cvcolormap-only-supports-source-image) and it should be good since the library already changed the mask type as np.uint8. However, the error still exists. I also tried other opencv versions, e.g., 4.5.5.64 and found the error as well. That is so weird.

jacobgil commented 2 years ago

What is the format of batch_data here ? show_cam_on_image expects a single numpy image as an input.

Waterkin commented 2 years ago

I also came across this problem.

Does it mean if we create cam(input_tensor=images, targets=targets) with cam.batch_size = 256, we can't use visualization = show_cam_on_image(images, grayscale_cam, use_rgb=True)?

Bumpeet commented 1 year ago

I encountered the following error:

Traceback (most recent call last): File "inverse_trigger_match.py", line 627, in evaluate_trojai show_cam(model, [target_layer], poisonX, target_label) File "inverse_trigger_match.py", line 451, in show_cam cam_image = show_cam_on_image(batch_data, grayscale_cam) File "/home/yan390/miniconda3/envs/py3.6/lib/python3.6/site-packages/pytorch_grad_cam/utils/image.py", line 47, in show_cam_on_image heatmap = cv2.applyColorMap((255 * mask).astype(np.uint8), colormap) cv2.error: OpenCV(4.6.0) /io/opencv/modules/imgproc/src/colormap.cpp:736: error: (-5:Bad argument) cv::ColorMap only supports source images of type CV_8UC1 or CV_8UC3 in function 'operator()'

I searched online (https://stackoverflow.com/questions/59873709/saving-16-bit-image-in-opencv-with-cmap-cvcolormap-only-supports-source-image) and it should be good since the library already changed the mask type as np.uint8. However, the error still exists. I also tried other opencv versions, e.g., 4.5.5.64 and found the error as well. That is so weird.

We need to permute the dimensions of the mask as the shape of mask is (1,28,28). Make it into (28,28,1)