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

完美解决。

相关推荐
AI编程教父1 分钟前
企业AI转型做自动化,哪些靠规则、哪些交给AI?红烁AI企业AI培训用两类任务对比
人工智能
m0_719640871 分钟前
适合教学科研的人工智能实验室建设方案有哪些?(含测评)
人工智能·经验分享
智学AI1 分钟前
AI博主写作素材管理方法分享:资料归集、笔记关联与文档归档
人工智能·笔记
dozenyaoyida2 分钟前
AI与大模型新闻日报 | 2026-10-30
人工智能·ai·chatgpt·大模型·新闻
Rocky Ding*7 分钟前
深入浅出Xiaomi-Lens-0技术解析:像素监督如何改变视觉推理
论文阅读·人工智能·深度学习·机器学习·aigc·多模态·ai-native
万岳软件开发小城11 分钟前
AI数字人系统源码如何实现批量视频生成?任务队列与自动化生产流程解析
人工智能·自动化·语音识别·ai数字人小程序·ai数字人系统源码·ai数字人软件开发·ai数字人平台开发
默|笙20 分钟前
【AI应用】认识机器学习与深度学习
人工智能·深度学习·机器学习
TangGeeA21 分钟前
数据库、SQL 与 DB AI
数据库·人工智能·sql
火山引擎开发者社区22 分钟前
案例实践 | 让检索为 Agent 而生:ContextSearch 重构企业售前知识库
人工智能
青蓝智慧马老师24 分钟前
大模型应用落地,到底靠什么?RAG、智能体和上下文工程一次讲清
人工智能