yolov9目标检测报错AttributeError: ‘list‘ object has no attribute ‘device‘

深度学习


文章目录


前言

yolov9运行自己训练的模型时,出现以下错误:

bash 复制代码
root@b219ae83c78f:/yolov9# python detect.py --source './data/images/horses.jpg' --img 640 --device 0 --weights runs/train/yolov9-c8/weights/best.pt --name yolov9_c_c_640_detect2
detect: weights=['runs/train/yolov9-c8/weights/best.pt'], source=./data/images/horses.jpg, data=data/coco128.yaml, imgsz=[640, 640], conf_thres=0.25, iou_thres=0.45, max_det=1000, device=0, view_img=False, save_txt=False, save_conf=False, save_crop=False, nosave=False, classes=None, agnostic_nms=False, augment=False, visualize=False, update=False, project=runs/detect, name=yolov9_c_c_640_detect2, exist_ok=False, line_thickness=3, hide_labels=False, hide_conf=False, half=False, dnn=False, vid_stride=1
YOLO 🚀 v0.1-104-g5b1ea9a Python-3.8.12 torch-1.11.0a0+b6df043 CUDA:0 (NVIDIA TITAN V, 12057MiB)

Fusing layers... 
yolov9-c summary: 604 layers, 50880768 parameters, 0 gradients, 237.6 GFLOPs
Traceback (most recent call last):
  File "detect.py", line 231, in <module>
    main(opt)
  File "detect.py", line 226, in main
    run(**vars(opt))
  File "/opt/conda/lib/python3.8/site-packages/torch/autograd/grad_mode.py", line 28, in decorate_context
    return func(*args, **kwargs)
  File "detect.py", line 102, in run
    pred = non_max_suppression(pred, conf_thres, iou_thres, classes, agnostic_nms, max_det=max_det)
  File "/yolov9/utils/general.py", line 905, in non_max_suppression
    device = prediction.device
AttributeError: 'list' object has no attribute 'device'

File "/yolov9/utils/general.py", line 905, in non_max_suppression

这行代码错误,应该是照抄了yolov5的代码

bash 复制代码
 if isinstance(prediction, (list, tuple)):  # YOLO model in validation model, output = (inference_out, loss_out)
        prediction = prediction[0]  # select only inference output

    device = prediction.device
    mps = 'mps' in device.type  # Apple MPS
    if mps:  # MPS not fully supported yet, convert tensors to CPU before NMS
        prediction = prediction.cpu()
    bs = prediction.shape[0]  # batch size
    nc = prediction.shape[1] - nm - 4  # number of classes
    mi = 4 + nc  # mask start index
    xc = prediction[:, 4:mi].amax(1) > conf_thres  # candidates

改成以下代码,问题解决,

bash 复制代码
    if isinstance(prediction, (list, tuple)):  # YOLO model in validation model, output = (inference_out, loss_out)
        processed_predictions = []
        for pred_tensor in prediction:
            processed_tensor = pred_tensor[0]
            processed_predictions.append(processed_tensor)
        #prediction = prediction[0]  # select only inference output
        prediction = processed_predictions[0]

    device = prediction.device
    mps = 'mps' in device.type  # Apple MPS
    if mps:  # MPS not fully supported yet, convert tensors to CPU before NMS
        prediction = prediction.cpu()
    bs = prediction.shape[0]  # batch size
    nc = prediction.shape[1] - nm - 4  # number of classes
    mi = 4 + nc  # mask start index
    xc = prediction[:, 4:mi].amax(1) > conf_thres  # candidates

完美解决。

相关推荐
TaoMetrix3 小时前
TaoMetrix:AI 硬件创业公司如何做好原型制造
人工智能·制造
智圣新创013 小时前
面向多级组织协同场景 高校第二课堂一站式管理中枢落地全场景实操答疑
大数据·人工智能
cd_949217213 小时前
AI纹理和Substance Painter手绘纹理哪个更适合游戏资产制作?
人工智能·游戏·substance painter
sali-tec3 小时前
C# 基于OpenCv的视觉工作流-章106-差值追踪
图像处理·人工智能·opencv·算法·计算机视觉
Shockang3 小时前
用 AI 打造高品质 Web 应用
人工智能
l1258653 小时前
# LangGraph Memory机制深度解析:短期记忆与长期记忆的工程实践
前端·人工智能·python·langchain·bootstrap
手写码匠3 小时前
Dify 多 Agent 工具权限与安全沙箱实战:让智能体“有能力,但不越权“
人工智能·深度学习·算法·aigc
ZGIAI3 小时前
ZGI Skill Loop:给工具调用设边界
人工智能·架构
黎阳之光3 小时前
打破堆场感知黑盒:黎阳之光视频孪生,构建港口码头网格化透明管控新体系
大数据·人工智能·算法·安全·数字孪生
ZGIAI3 小时前
ZGI 工作区权限:用户为何看不到资源
人工智能·架构