环境:

  • 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()