当前位置: 首页 > news >正文

pytorch2.6.0版本测试YOLOv5中detect.py错误解决办法

当出现如下错误时:File “D:\PythonProject_base\yolov5-v5.0\detect.py”, line 178, in
detect()
File “D:\PythonProject_base\yolov5-v5.0\detect.py”, line 34, in detect
model = attempt_load(weights, map_location=device) # load FP32 model
File “D:\PythonProject_base\yolov5-v5.0\models\experimental.py”, line 118, in attempt_load
ckpt = torch.load(w, map_location=map_location) # load
File “D:\PythonProject_base.venv\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 numpy.core.multiarray._reconstruct was not an allowed global by default. Please use torch.serialization.add_safe_globals([_reconstruct]) or the torch.serialization.safe_globals([_reconstruct]) 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.

错误发生在 attempt_load 函数里调用 torch.load 时,这是因为 PyTorch 2.6 及之后版本中 weights_only 参数默认值改变,导致加载包含不被允许的全局对象 。numpy.core.multiarray._reconstruct 的文件失败。

  • 下面为你提供具体的解决办法:
    如果你确定文件来源可靠,可以把 torch.load 中的 weights_only 参数设置为 False。在 models/experimental.py 文件里找到 attempt_load 函数,将 torch.load 修改如下:
# 在 models/experimental.py 文件中找到 attempt_load 函数
def attempt_load(weights, map_location=None):
    # ... 其他代码 ...
    ckpt = torch.load(w, map_location=map_location, weights_only=False)  # 修改这一行,添加 weights_only=False
    # ... 其他代码 ...
    return model

也要将detect.py文件中的torch.load中的weights_only参数改为False

相关文章:

  • http报文的content-type参数和spring mvc传参问题
  • 高频 SQL 50 题(基础版)_550. 游戏玩法分析 IV
  • 系统架构设计师—计算机基础篇—系统性能评价
  • 深度学习pytorch之4种归一化方法(Normalization)原理公式解析和参数使用
  • 小结:BGP协议
  • AtCoder Beginner Contest 001(A - 積雪深差、B - 視程の通報、C - 風力観測、D - 感雨時刻の整理)题目翻译
  • 贪心算法+题目
  • Sqli-labs
  • 从零开始用react + tailwindcss + express + mongodb实现一个聊天程序(八) 聊天框用户列表
  • ByteBuddy
  • 联合省选 2025 游记
  • 【随手笔记】FFT+音乐频谱(二)
  • 工程化与框架系列(8)--持续集成实践
  • OpenFlow 全解析:前世今生与未来走向
  • 【极客时间】浏览器工作原理与实践-2 宏观视角下的浏览器 (6讲) - 2.5 渲染流程(上):HTML、CSS和JavaScript,是如何变成页面的?
  • leetcode第40题组合总和Ⅱ
  • Day32 第八章 贪心算法 part05
  • leetcode第17题求电话号码组合
  • 14. LangChain项目实战1——基于公司制度RAG回答机器人
  • MCAL-存储器驱动
  • 企业网站管理系统项目文档/青岛seo博客
  • 电脑网站建设方案/百度竞价排名案例分析
  • 做高端品牌网站/seo是怎么优化
  • 做动态的网站的参考资料有哪些/手机如何建网站
  • 美观网站建设价格/软文广告营销
  • 做新网站 备案证明交接/seo数据分析哪些方面