PyTorch_YOLOv4 icon indicating copy to clipboard operation
PyTorch_YOLOv4 copied to clipboard

Pytorch 1.8: Tensor to cumpy

Open dhakalkamal opened this issue 3 years ago • 8 comments

Hi, I am using pytorch 1.8 with cuda 11.2. While training on coco or custom dataset I get this error message. I used pretained weight yolov4.conv.137.pth to train the custom dataset. Does anybody has the same problem and fixed it?

Traceback (most recent call last):
  File "train.py", line 438, in <module>
    train(hyp, opt, device, tb_writer)
  File "train.py", line 301, in train
    results, maps, times = test.test(opt.data,
  File "/home/kamal/Documents/PyTorch_YOLOv4/test.py", line 206, in test
    plot_images(img, output_to_target(output, width, height), paths, str(f), names)  # predictions
  File "/home/kamal/Documents/PyTorch_YOLOv4/utils/general.py", line 907, in output_to_target
    return np.array(targets)
  File "/home/kamal/.virtualenvs/pytorch-yolov4/lib/python3.8/site-packages/torch/tensor.py", line 621, 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.

dhakalkamal avatar Mar 19 '21 11:03 dhakalkamal

Yes,I have the same problem in pytorch1.7.1 with cuda 10.2.I also want the solutions.

Arthur-zfc avatar Mar 20 '21 02:03 Arthur-zfc

@WongKinYiu can you please have a look at this?

dhakalkamal avatar Mar 20 '21 12:03 dhakalkamal

Yes,I have the same problem in pytorch1.7.1 with cuda 10.2.I also want the solutions.

@Arthur-zfc were you able to fix it?

dhakalkamal avatar Mar 25 '21 18:03 dhakalkamal

@shuuny-matrix No, I haven't solved it yet

Arthur-zfc avatar Mar 26 '21 01:03 Arthur-zfc

我是用的是单机8卡,在general.py的output_to_target方法中 将这两句注释if isinstance(output, torch.Tensor): output = output.cpu().numpy() 并加上 output = [temp.cpu().numpy() for temp in output]

asizk avatar Mar 30 '21 01:03 asizk

我是用的是单机8卡,在general.py的output_to_target方法中 将这两句注释if isinstance(output, torch.Tensor): output = output.cpu().numpy() 并加上 output = [temp.cpu().numpy() for temp in output]

Thank you, worked like a charm. But need to output.cpu() >> output in test.py in output_to_target()

dhakalkamal avatar Mar 30 '21 22:03 dhakalkamal

我是用的是单机8卡,在general.py的output_to_target方法中 将这两句注释if isinstance(output, torch.Tensor): output = output.cpu().numpy() 并加上 output = [temp.cpu().numpy() for temp in output]

Thank you!

Arthur-zfc avatar Mar 31 '21 12:03 Arthur-zfc

谢谢,现在需要修改的地方放在了utils/plots.py!

xiaofu3322 avatar Mar 18 '23 10:03 xiaofu3322