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

Posted ʚVVcatɞ

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了TypeError: can‘t convert cuda:0 device type tensor to numpy. Use Tensor.cpu() to copy the tensor to(相关的知识,希望对你有一定的参考价值。

环境:

  • Python:3.7.10
  • Anaconda:2.0.4
  • YOLOv5:3.1
  • torch:1.7.0+cu101
  • torchvision:0.8.1+cu101

Traceback (most recent call last):
File “train.py”, line 460, in
train(hyp, opt, device, tb_writer)
File “train.py”, line 320, in train
plots=epoch == 0 or final_epoch) # plot first and last
File “C:\\Users\\vvcat\\Desktop\\yolov5\\yolov5-3.1\\test.py”, line 197, in test
plot_images(img, output_to_target(output, width, height), paths, str(f), names) # predictions
File “C:\\Users\\vvcat\\Desktop\\yolov5\\yolov5-3.1\\utils\\general.py”, line 951, in output_to_target
return np.array(targets)
File “H:\\AnacondaNavigator\\Anaconda\\envs\\yolov5\\lib\\site-packages\\torch\\tensor.py”, line 630, 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.

根据错误提示找到 该文件

H:\\AnacondaNavigator\\Anaconda\\envs\\yolov5\\lib\\site-packages\\torch\\tensor.py

在 630 行找到

return self.numpy()

更改为

return self.cpu().numpy()

以上是关于TypeError: can‘t convert cuda:0 device type tensor to numpy. Use Tensor.cpu() to copy the tensor to(的主要内容,如果未能解决你的问题,请参考以下文章

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

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

python3.4.0 input Can't convert 'int' object to str implicitly 如何正确输出?

成功解决TypeError: only size-1 arrays can be converted to Python scalars

TypeError: only integer scalar arrays can be converted to a scalar index

TypeError: only integer scalar arrays can be converted to a scalar index