【问题标题】: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)TypeError:无法将 cuda:0 设备类型张量转换为 numpy。使用 Tensor.cpu() 先将张量复制到主机内存(Segmentation using yolact edge)
【发布时间】:2021-07-07 21:55:58
【问题描述】:

我在 yolact 边缘运行分割。我正在尝试使用我自己的算法找到掩码的最小和最大 x 和 y 像素坐标。 我正在尝试将元组的值转换为 numpy。但是我得到了以下错误

TypeError: can't convert cuda:0 device type tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory first.

代码


    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)

注意: t 是一个 pytorch 张量,由默认程序输出,其中包含 t[3] 中的掩码数据。我该如何解决这个问题?

输出:

torch.Size([2, 720, 1280])
<class 'torch.Tensor'>
(tensor([105, 105, 105,  ..., 503, 503, 503]), tensor([427, 428, 429,  ..., 468, 469, 470]))
Traceback (most recent call last):
  File "eval.py", line 1303, in <module>
    evaluate(net, dataset)
  File "eval.py", line 928, in evaluate
    evalimage(net, inp, out, detections=detections, image_id="0")
  File "eval.py", line 621, in evalimage
    img_numpy = prep_display(preds, frame, None, None, undo_transform=False)
  File "eval.py", line 198, in prep_display
    horizontal_translation_numpy = np.asarray(horizontal_translation[1])
  File "/home/nvidia/.local/lib/python3.6/site-packages/numpy/core/_asarray.py", line 83, in asarray
    return array(a, dtype, copy=False, order=order)
  File "/home/nvidia/.local/lib/python3.6/site-packages/torch/tensor.py", 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.

【问题讨论】:

    标签: python pytorch image-segmentation tensor


    【解决方案1】:

    这应该可行

    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)
    

    【讨论】:

    • 我想要遮罩极值像素的坐标(x 和 y)。我该怎么做?
    • 你的horizontal_translation_numpy是什么?
    • 因此,基本上,horizo​​ntal_translation_numpy 和 Horizo​​ntal_translation 包含由 torch.where() 返回的两个张量。第二个张量包含 th 像素的所有 x 坐标,这些像素是检测到的苹果掩码的一部分。第一个张量包含掩码中像素所在的所有行。
    【解决方案2】:

    horizontal_translation.cpu().numpy()
    

    不工作?

    【讨论】:

    • 将您的解决方案表述为答案。如果是建议,请将其写为评论。
    • @Xiddoc 他无法发表评论。至少需要 50 次。
    猜你喜欢
    • 2020-09-09
    • 2021-06-19
    • 1970-01-01
    • 2019-05-22
    • 2020-07-28
    • 2021-03-09
    • 2022-01-03
    • 1970-01-01
    相关资源
    最近更新 更多