You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

训练YOLOv9-e/c时train.py文件出现AttributeError报错

YOLOv9-e/c训练报错排查与解决

报错详情

Traceback (most recent call last):
File "/content/yolov9/train.py", line 634, in
main(opt)
File "/content/yolov9/train.py", line 528, in main
train(opt.hyp, opt, device, callbacks)
File "/content/yolov9/train.py", line 304, in train
loss, loss_items = compute_loss(pred, targets.to(device)) # loss scaled by batch_size
File "/content/yolov9/utils/loss_tal.py", line 168, in call
pred_distri, pred_scores = torch.cat([xi.view(feats[0].shape[0], self.no, -1) for xi in feats], 2).split(
File "/content/yolov9/utils/loss_tal.py", line 168, in
pred_distri, pred_scores = torch.cat([xi.view(feats[0].shape[0], self.no, -1) for xi in feats], 2).split(
AttributeError: 'list' object has no attribute 'view'

已尝试无效操作

执行以下GPU内存清理代码后,问题未解决:

import torch
import os

torch.cuda.empty_cache()
os.environ['PYTORCH_CUDA_ALLOC_CONF'] = 'expandable_segments:True'

问题分析与修复方案

报错核心是loss_tal.py中遍历feats时,元素xi为列表而非Tensor,导致无法调用view()方法。这是模型输出结构与损失函数预期不匹配导致的,针对YOLOv9-e/c特性,可尝试以下修复:

  • 确认模型权重匹配:确保加载的是YOLOv9-e/c专属权重,而非YOLOv9-s/m等其他变体权重——不同变体的输出分支结构存在差异。
  • 修改损失函数适配输出:打开/content/yolov9/utils/loss_tal.py,定位到第168行附近,调整代码适配列表类型的输出:
    # 原代码
    pred_distri, pred_scores = torch.cat([xi.view(feats[0].shape[0], self.no, -1) for xi in feats], 2).split(...)
    # 修改后(假设xi是单Tensor的列表)
    pred_distri, pred_scores = torch.cat([xi[0].view(feats[0][0].shape[0], self.no, -1) for xi in feats], 2).split(...)
    
  • 同步代码版本:核对Roboflow提供的代码与官方YOLOv9仓库最新版本是否一致,若存在版本差,替换本地loss_tal.py、train.py等核心文件为官方最新版。
  • 检查数据集配置:确认数据集.yaml文件中的类别数nc与模型输出类别数完全匹配,类别数不匹配也会引发输出结构异常。

内容的提问来源于stack exchange,提问作者Lennard

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 03:07:17