TypeError: can't convert cuda:0 device type tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory first (Segmentation using yolact edge)
I am running segmentation on yolact edge. I am trying to find coordinates of the minimu and maximum x and y pixel coordinated of the mask using my own algorithm.
I am trying to convert the values of a tuple to numpy. However I am getting the follwoing errror
TypeError: can't convert cuda:0 device type tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory first.

Code

    xmin = []
    xmax = []
    y = []
    print(np.shape(t[3]))
    print(type(t[3][:][:][:]))
    #row = (t[3][1][360][:]==1).nonzero(as_tuple=True) 
    for i in range (0, 2):
        t_cpu = t[3].clone().detach().cpu()
        horizontal_translation = torch.where(t[3][i][:][:]==1)
        print(horizontal_translation)
        horizontal_translation_numpy = np.asarray(horizontal_translation[1])
        x_min = np.amin(horizontal_translation_numpy)
        x_max = np.amax(horizontal_translation_numpy)
        np.append(xmin,x_min)
        np.append(xmax, x_max)
    print(xmin)
    print(xmax)

Note:
t is a pytorch tensor that is output by the default program that contains mask data in t[3]. How do I fix this?
Output:
torch.Size([2, 720, 1280])
&lt;class 'torch.Tensor'&gt;
(tensor([105, 105, 105,  ..., 503, 503, 503]), tensor([427, 428, 429,  ..., 468, 469, 470]))
Traceback (most recent call last):
  File &quot;eval.py&quot;, line 1303, in &lt;module&gt;
    evaluate(net, dataset)
  File &quot;eval.py&quot;, line 928, in evaluate
    evalimage(net, inp, out, detections=detections, image_id=&quot;0&quot;)
  File &quot;eval.py&quot;, line 621, in evalimage
    img_numpy = prep_display(preds, frame, None, None, undo_transform=False)
  File &quot;eval.py&quot;, line 198, in prep_display
    horizontal_translation_numpy = np.asarray(horizontal_translation[1])
  File &quot;/home/nvidia/.local/lib/python3.6/site-packages/numpy/core/_asarray.py&quot;, line 83, in asarray
    return array(a, dtype, copy=False, order=order)
  File &quot;/home/nvidia/.local/lib/python3.6/site-packages/torch/tensor.py&quot;, line 480, in __array__
    return self.numpy()
TypeError: can't convert cuda:0 device type tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory first.


This should work
xmin = []
xmax = []
y = []
print(np.shape(t[3]))
print(type(t[3][:][:][:]))
#row = (t[3][1][360][:]==1).nonzero(as_tuple=True) 
for i in range (0, 2):
    t_cpu = t[3].clone().detach().cpu()
    horizontal_translation = torch.where(t[3][i][:][:]==1)
    print(horizontal_translation)
    horizontal_translation_numpy = horizontal_translation.cpu().numpy()
    x_min = np.amin(horizontal_translation_numpy)
    x_max = np.amax(horizontal_translation_numpy)
    np.append(xmin,x_min)
    np.append(xmax, x_max)
print(xmin)
print(xmax)

