yolov5遇到AttributeError: ‘list‘ object has no attribute ‘cpu‘和TypeError: can t convert cuda:0 device

在运行yolov5的detect.py过程中,想要获取检测框的坐标,遇到以下问题:

  1. TypeError: can t convert cuda:0 device type tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory first.
  2. AttributeError: ‘list’ object has no attribute ‘cpu’

输出完整的坐标列表为:

print("\n检测框坐标:" + str(xyxy))

在这里插入图片描述
但是可以发现其中含有device='cuda:0’这样的CUDA tensor,并不是我们需要的量,尝试:

print("\n检测框坐标:" + str(xyxy[0]))

在这里插入图片描述

对于这样一个含有的张量列表,且只能索引整数或切片,要获得无device='cuda:0’项的坐标值输出,可以采用xyxy.cpu().numpy(),但是发现又报了错:

在这里插入图片描述

仔细检查后发现,可采用以下方式获得我们想要的坐标:

print("\n检测框坐标:" + str(xyxy[0].cpu().numpy()))

在这里插入图片描述

其余列坐标同理可得。

  • 9
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值