【yolo】yolo推理报错,以及解决方案

背景

我安装好了yolo8的环境后出现的报错,安装命令如下:
1.创建python环境
conda create -n yolo python==3.8
查看现有环境
conda env list
激活环境
conda activate yolo

2.安装库
pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118
pip install ultralytics==8.1 -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install onnx -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install onnxsim -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install onnxruntime -i https://pypi.tuna.tsinghua.edu.cn/simple
(最新的yolo8改用默认GPU版的onnxruntime,要安装一下下面的库,否则ONNX转换会有警告)
pip install onnxruntime-gpu -i https://pypi.tuna.tsinghua.edu.cn/simple

报错内容如下:

命令:
推理检测
yolo predict model=yolov8n.pt source=4.png

输入命令推理时报错:

(yolo8) F:\temp\yolotest>yolo predict model=yolov8n.pt source=2.bmp
Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "F:\envs\yolo8\Scripts\yolo.exe\__main__.py", line 7, in <module>
  File "F:\envs\yolo8\Lib\site-packages\ultralytics\cfg\__init__.py", line 540, in entrypoint
    model = YOLO(model, task=task)
            ^^^^^^^^^^^^^^^^^^^^^^
  File "F:\envs\yolo8\Lib\site-packages\ultralytics\engine\model.py", line 95, in __init__
    self._load(model, task)
  File "F:\envs\yolo8\Lib\site-packages\ultralytics\engine\model.py", line 161, in _load
    self.model, self.ckpt = attempt_load_one_weight(weights)
                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "F:\envs\yolo8\Lib\site-packages\ultralytics\nn\tasks.py", line 701, in attempt_load_one_weight
    ckpt, weight = torch_safe_load(weight)  # load ckpt
                   ^^^^^^^^^^^^^^^^^^^^^^^
  File "F:\envs\yolo8\Lib\site-packages\ultralytics\nn\tasks.py", line 634, in torch_safe_load
    return torch.load(file, map_location="cpu"), file  # load
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "F:\envs\yolo8\Lib\site-packages\torch\serialization.py", line 1470, in load
    raise pickle.UnpicklingError(_get_wo_message(str(e))) from None
_pickle.UnpicklingError: Weights only load failed. This file can still be loaded, to do so you have two options, do those steps only if you trust the source of the checkpoint.
        (1) In PyTorch 2.6, we changed the default value of the `weights_only` argument in `torch.load` from `False` to `True`. Re-running `torch.load` with `weights_only` set to `False` will likely succeed, but it can result in arbitrary code execution. Do it only if you got the file from a trusted source.
        (2) Alternatively, to load with `weights_only=True` please check the recommended steps in the following error message.
        WeightsUnpickler error: Unsupported global: GLOBAL ultralytics.nn.tasks.DetectionModel was not an allowed global by default. Please use `torch.serialization.add_safe_globals([DetectionModel])` or the `torch.serialization.safe_globals([DetectionModel])` context manager to allowlist this global if you trust this class/function.

Check the documentation of torch.load to learn more about types accepted by default with weights_only https://pytorch.org/docs/stable/generated/torch.load.html.

解决方案

这个错误是由于 PyTorch 2.6 及以上版本中 torch.load 的默认行为发生了变化,weights_only 参数默认设置为 True,导致加载 YOLO 模型权重时出现问题。以下是解决该问题的方法:


修改 torch.load 参数

在 YOLO 的代码中,找到 torch.load 的调用位置(通常是 ultralytics/nn/tasks.py 文件),将 weights_only 参数设置为 False。具体步骤如下:

  1. 打开文件:

    # 找到文件路径
    F:\envs\yolo8\Lib\site-packages\ultralytics\nn\tasks.py
    
  2. 找到 torch.load 的调用位置(大约在第 634 行):

    return torch.load(file, map_location="cpu"), file  # load
    
  3. 修改为:

    return torch.load(file, map_location="cpu", weights_only=False), file  # load
    

在这里插入图片描述
改完后,立竿见影,推理成功:
在这里插入图片描述
这说明环境搭建成功!

### X-AnyLabeling 使用 YOLOv7 报错解决方案 当在 X-AnyLabeling 中集成和使用 YOLOv7 进行目标检测时,可能会遇到一些常见的错误。以下是针对这些报错的可能原因分析及相应的解决方法。 #### 1. 环境配置问题 如果环境变量设置不正确或依赖库版本冲突,则可能导致程序无法正常运行。建议先确认 Python 版本是否兼容,并安装最新版 PyTorchtorchvision 库: ```bash pip install torch==1.13.1+cu117 torchvision==0.14.1+cu117 torchaudio==0.13.1 --extra-index-url https://download.pytorch.org/whl/cu117 ``` 对于特定于 YOLOv7 的需求,还需确保已按照官方文档说明完成必要的准备工作[^2]。 #### 2. 模型加载失败 有时会因为路径不对或是预训练权重文件损坏而导致模型加载异常。可以尝试重新下载官方发布的预训练模型参数文件,并将其放置于指定位置;另外也要注意检查 `config` 文件中的路径设定是否准确无误。 #### 3. 数据格式不符 输入图片尺寸不符合预期、标签文件格式有误等情况也会引发错误提示。应仔细核对所使用的数据集结构与 YOLOv7 所需的标准格式是否一致,必要时可参照相关教程调整数据准备流程[^3]。 #### 4. GPU 资源不足 若显存不够大,在批量处理多张高分辨率图像时容易触发 CUDA out of memory 错误。此时可以通过减少 batch size 或者优化网络架构来缓解这一状况。也可以考虑切换至 CPU 设备继续调试工作。 通过以上几个方面的排查,通常能够有效定位并修复大部分由 YOLOv7 引起的问题。当然,具体场景下还需要结合实际情况灵活应对。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

code bean

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值