Aks*_*rya 6 python image-segmentation pytorch tensor
我正在 yolac Edge 上运行分段。我正在尝试使用我自己的算法找到掩模的最小和最大 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.
Run Code Online (Sandbox Code Playgroud)
代码
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)
Run Code Online (Sandbox Code Playgroud)
注意: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.
Run Code Online (Sandbox Code Playgroud)
这应该有效
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)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6565 次 |
| 最近记录: |