TypeError: can't convert cuda:0 device type tensor to numpy.,am getting this error how to solve ? Use Tensor.cpu() to copy the tensor to host memory first

frame_str = base64.b64decode(input_data[“frame1”])
frame = torch.Tensor(resize(jpeg.decode(frame_str),(640,640))).permute(2,0,1)
frame = frame
predictions = model.predict(frame)
boxes = numpy.asarray(predictions[0][‘boxes’])
print(“Converted Predictions to numpy format - %s” %(type(boxes)))
print(“Predictions %s” %(predictions))

    if isinstance(boxes, list):
        boxes = np.array(boxes)
        boxes = boxes.astype("int")

TypeError: can’t convert cuda:0 device type tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory first.
i’m getting this error ,what am i doing wrong?

Hi @uzumaki_N, can you share the exact line where this error is shown? It’ll be the last line with the stack trace.

The error is because you have a Tensor which is currently stored on your GPU but you’re trying to use it with numpy which is a CPU only library. Therefore, before sending this Tensor to numpy you need to first send it to your CPU, then call whatever numpy operation you want.

You can send the Tensor to the CPU via the .cpu() method,

x = torch.randn(4,4,device='cuda') #currently on GPU
x = x.cpu() #moved back to CPU
1 Like