Error with convert CUDA tensor to numpy

Hi guys,

I am beginner with Pytorch and Object Detection,
I am running the code on Google Colab with example code


Does anyone have idea about this problem?

Thank you so much

You cannot directly convert a tensor stored on the GPU to a numpy array, which would be stored on the CPU. As the error message describes, transfer the tensor to the CPU first and then convert it to a numpy array:

cpu_arr = gpu_tensor.cpu().numpy()

Thank you for your response,

I have tried

nms_prediction = apply_nms(prediction, iou_thresh=0.01)
prediction = nms_prediction.gpu_tensor.cpu().numpy()
plot_img_bbox(torch_to_pil(img), nms_prediction)

However, it is not working because of nms_prediction is ‘dict’ and has on attribute.

AttributeError                            Traceback (most recent call last)
<ipython-input-64-09fd68999e27> in <module>
----> 1 nms_prediction.values.gpu_tensor.cpu().numpy()

AttributeError: 'builtin_function_or_method' object has no attribute 'gpu_tensor'

Do you have any solution for this?

Sorry for the confusion. gpu_tensor is just a placeholder and example tor any tensor stored on the GPU.
I assume nms_prediction is already a CUDATensor, so something like:

nms_array = nms_prediction.cpu().numpy()
# or 
nms_array = nms_prediction.detach().cpu().numpy()

plot_img_bbox(torch_to_pil(img), nms_array)

might work.