【Bug解决】TypeError: can‘t convert cuda:0 device type tensor to numpy. Use Tensor.cpu() to copy...

问题分析

笔者在训练yolov5模型时出现如下问题:

Traceback (most recent call last):
  File "train.py", line 460, in <module>
    train(hyp, opt, device, tb_writer)
  File "train.py", line 320, in train
    plots=epoch == 0 or final_epoch)  # plot first and last
  File "D:\yolov5-3.1\test.py", line 197, in test
    plot_images(img, output_to_target(output, width, height), paths, str(f), names)  # predictions
  File "D:\yolov5-3.1\utils\general.py", line 951, in output_to_target
    return np.array(targets)
  File "D:\Anaconda3\envs\yolov5-3\lib\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.

解决问题

根据错误提示找到D:\Anaconda3\envs\yolov5-3\lib\site-packages\torch\tensor.py文件的第621
将:

return self.numpy()

修改为:

return self.cpu().detach().numpy()

再次运行训练指令即可正常运行,问题解决

  • 15
    点赞
  • 35
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值