解决yolov8推理报错:ModuleNotFoundError: No module named ‘ultralytics.nn.modules.conv‘

发布时间:2023年12月27日

1.项目场景:

在GitHub上下载了一个yolov8的代码,训练好检测模型,运行作者给的推理代码报错:

Traceback (most recent call last):
  File "E:\Pycharm\pytorch-project\YOLOv8\inference.py", line 9, in <module>
    model = YOLO("./runs/detect/train/weights/last.pt")
  File "E:\Pycharm\pytorch-project\YOLOv8\ultralytics\yolo\engine\model.py", line 55, in __init__
    {'.pt': self._load, '.yaml': self._new}[Path(model).suffix](model)
  File "E:\Pycharm\pytorch-project\YOLOv8\ultralytics\yolo\engine\model.py", line 83, in _load
    self.model, self.ckpt = attempt_load_one_weight(weights)
  File "E:\Pycharm\pytorch-project\YOLOv8\ultralytics\nn\tasks.py", line 341, in attempt_load_one_weight
    ckpt = torch.load(attempt_download(weight), map_location='cpu')  # load
  File "E:\Anaconda3\envs\pytorch\lib\site-packages\torch\serialization.py", line 809, in load
    return _load(opened_zipfile, map_location, pickle_module, **pickle_load_args)
  File "E:\Anaconda3\envs\pytorch\lib\site-packages\torch\serialization.py", line 1172, in _load
    result = unpickler.load()
  File "E:\Anaconda3\envs\pytorch\lib\site-packages\torch\serialization.py", line 1165, in find_class
    return super().find_class(mod_name, name)
ModuleNotFoundError: No module named 'ultralytics.nn.modules.conv'; 'ultralytics.nn.modules' is not a package

2.原因分析:

作者仓库地址为:https://github.com/DataXujing/YOLOv8
首先看错误原因:'ultralytics.nn.modules'is not a package,说明ultralytics.nn模块有问题。
查看作者ultralytics包的内容,并与最新版本对比:
8.0.0:
在这里插入图片描述
8.0.229:
在这里插入图片描述

可以看出,两者的modules不同


3.解决方案:

解决办法很简单,把下载下来的nn模块替换掉即可,问题解决。

文章来源:https://blog.csdn.net/qq_44368508/article/details/135248616
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。