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

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

Open lunaryan opened this issue 2 years ago • 3 comments

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.

lunaryan avatar Oct 03 '22 02:10 lunaryan

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

jacobgil avatar Oct 12 '22 07:10 jacobgil

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)?

Waterkin avatar Nov 18 '22 02:11 Waterkin

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)

Bumpeet avatar Aug 05 '23 06:08 Bumpeet