1Konny / gradcam_plus_plus-pytorch

A Simple pytorch implementation of GradCAM and GradCAM++
359 stars 95 forks source link

TypeError: can't convert CUDA tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory first #5

Closed HokkaidoKurbis closed 5 years ago

HokkaidoKurbis commented 5 years ago

Hi I tried your code to visualize a trained densenet169 model.

But the code throws error

 heatmap, result = visualize_cam(mask, torch_img)
  File "/home/user/vizualize_Nets/utils.py", line 15, in visualize_cam
    heatmap = cv2.applyColorMap(np.uint8(255 * mask.squeeze()), cv2.COLORMAP_JET)
  File "/home/user/.local/lib/python3.6/site-packages/torch/tensor.py", line 460, in __array__
    return self.numpy().astype(dtype, copy=False)
TypeError: can't convert CUDA tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory first.

Simple converting to CUDA tensor or cpu().numpy() tensor does not solve the problem. Any idea how to solve it ?

Thanks in advance.

PeterKim1 commented 3 years ago

how to solve this problem?

I try to converting to CUDA tensor to .cpu(), but it can't solve problem.

XHD5656123 commented 11 months ago

I need answer too,please!