I am having this error:
error: OpenCV(4.7.0) D:\a\opencv-python\opencv-python\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 'cv::colormap::ColorMap::operator ()'
that traces back to this code:
heatmap=cv2.applyColorMap(np.uint8(cam),cv2.COLORMAP_JET)
Here is my full code ( I am trying grad-cam in my subset):
for i_batch, (data, target, meta) in enumerate(tqdm(dataloader_test)):
data, target = map(lambda x: x.to(device), (data, target))
for imname, metaitemm in metadata_test.items():
output = model(data)
output.argmax(dim=1)
output[:, 5].sum().backward()
metaitem = metadata_test[imname]
impath = metaitem['impath']
impath = cv2.imread(impath)
imgdata = torch.Tensor(impath)
imgdata = torch.unsqueeze(imgdata, 0)
imgdata = torch.einsum('n h w c -> n c h w', imgdata)
model.layer4[-1].register_forward_hook(forward_hook)
model.layer4[-1].register_backward_hook(backward_hook)
grads=grad[0].cpu().data.numpy().squeeze()
fmap=activation[0].cpu().data.numpy().squeeze()
tmp=grads.reshape([grads.shape[0],-1])
weights=np.mean(tmp,axis=1)
cam = np.zeros(grads.shape[1:])
for i,w in enumerate(weights):
cam += w*fmap[i,:]
#relu
#cam=(cam>0)*cam
cam = np.maximum(cam, 0)
#print("cam.shape",cam.shape)
#normalize heatmap
#cam=cam/cam.max()*224
cam /= np.max(cam)
# make the heatmap to be a numpy array
npic = np.array(torchvision.transforms.ToPILImage()(impath).convert('RGB'))
cam = cv2.resize(cam,(npic.shape[1], npic.shape[0]))
#cam = np.uint8(224 * cam)
#cam = (cam/256).astype(np.uint8)
heatmap=cv2.applyColorMap(np.uint8(cam),cv2.COLORMAP_JET)
I have tried
cam = np.uint8(224 * cam)
and cam = (cam/256).astype(np.uint8)
but nothing seems to solve this problem.
Any suggestions for me? Thank you in advance