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

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:
解决yolov8推理报错:ModuleNotFoundError: No module named ‘ultralytics.nn.modules.conv‘_第1张图片
8.0.229:
解决yolov8推理报错:ModuleNotFoundError: No module named ‘ultralytics.nn.modules.conv‘_第2张图片

可以看出,两者的modules不同


3.解决方案:

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

你可能感兴趣的:(目标检测,pytorch,YOLO,yolov8)