Docker部署yolov5

目录

  • 环境
  • 下载源码
  • 构建Docker镜像
  • 运行docker镜像
  • 运行目标检测
    • 出现partially initialized module 'cv2' has no attribute '_registerMatType'错误
    • 出现ImportError: libSM.so.6: cannot open shared object file: No such file or directory错误
    • 出现AttributeError: 'Upsample' object has no attribute 'recompute_scale_factor'错误
  • 参考链接

环境

Ubuntu:22.10
Docker:24.0.0

下载源码

这里我下载了6.0版本的,想要别的版本的自己修改,但dockerfile可能目录不同,可以通过find指令查询

git clone -b v6.0 https://github.com/ultralytics/yolov5.git

构建Docker镜像

这里要等待很长时间,构建大概20g的镜像

sudo docker build -f [your dockerfile path] -t [your image name]:[version] .

在这里插入图片描述

运行docker镜像

# 创建后台运行的docker 容器
sudo docker run -itd -w /workspace -v [your youlov5 path]:/workspace --net=host --ipc=host --gpus=all -e DISPLAY=$DISPLAY --env="QT_X11_NO_MITSHM=1" --name [your container name] [your image name]:[version] /bin/bash

# 进入docker容器
sudo docker exec -it [your container name] /bin/bash

运行目标检测

python detect.py --source [your picture path] --view-img

出现partially initialized module ‘cv2’ has no attribute '_registerMatType’错误

具体错误详情:

Traceback (most recent call last):
  File "detect.py", line 14, in <module>
    import cv2
  File "/opt/conda/lib/python3.8/site-packages/cv2/__init__.py", line 181, in <module>
    bootstrap()
  File "/opt/conda/lib/python3.8/site-packages/cv2/__init__.py", line 175, in bootstrap
    if __load_extra_py_code_for_module("cv2", submodule, DEBUG):
  File "/opt/conda/lib/python3.8/site-packages/cv2/__init__.py", line 28, in __load_extra_py_code_for_module
    py_module = importlib.import_module(module_name)
  File "/opt/conda/lib/python3.8/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "/opt/conda/lib/python3.8/site-packages/cv2/mat_wrapper/__init__.py", line 33, in <module>
    cv._registerMatType(Mat)
AttributeError: partially initialized module 'cv2' has no attribute '_registerMatType' (most likely due to a circular import)

这是因为版本不匹配的问题,Dockerfile中只限制了opencv-python的版本大于4.1.2并没有限制最高版本,最高版本是,这里我们重新安装

sudo pip install 'opencv-python>=4.1.2,<4.3' --force-reinstall

重新运行

python detect.py --source [your picture path] --view-img

出现ImportError: libSM.so.6: cannot open shared object file: No such file or directory错误

具体错误详情:

Traceback (most recent call last):
  File "detect.py", line 14, in <module>
    import cv2
  File "/opt/conda/lib/python3.8/site-packages/cv2/__init__.py", line 5, in <module>
    from .cv2 import *
ImportError: libSM.so.6: cannot open shared object file: No such file or directory

这是没有库文件的原因,执行以下命令:

sudo apt-get install libsm6 libxrender1

重新运行

python detect.py --source [your picture path] --view-img

出现AttributeError: ‘Upsample’ object has no attribute 'recompute_scale_factor’错误

具体错误详情:

Traceback (most recent call last):
  File "detect.py", line 307, in <module>
    main(opt)
  File "detect.py", line 302, in main
    run(**vars(opt))
  File "/opt/conda/lib/python3.8/site-packages/torch/utils/_contextlib.py", line 115, in decorate_context
    return func(*args, **kwargs)
  File "detect.py", line 133, in run
    model(torch.zeros(1, 3, *imgsz).to(device).type_as(next(model.parameters())))  # run once
  File "/opt/conda/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1501, in _call_impl
    return forward_call(*args, **kwargs)
  File "/workspace/yolov5/models/yolo.py", line 126, in forward
    return self._forward_once(x, profile, visualize)  # single-scale inference, train
  File "/workspace/yolov5/models/yolo.py", line 149, in _forward_once
    x = m(x)  # run
  File "/opt/conda/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1501, in _call_impl
    return forward_call(*args, **kwargs)
  File "/opt/conda/lib/python3.8/site-packages/torch/nn/modules/upsampling.py", line 157, in forward
    recompute_scale_factor=self.recompute_scale_factor)
  File "/opt/conda/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1614, in __getattr__
    raise AttributeError("'{}' object has no attribute '{}'".format(
AttributeError: 'Upsample' object has no attribute 'recompute_scale_factor'

需要根据出错目录去修改upsampling.py文件,具体修改方法参考recompute_scale_factor错误参考
修改完毕后重新运行:

python detect.py --source [your picture path] --view-img

出现如下提示就是成功,如果出现x server的问题请看参考链接:
Docker部署yolov5_第1张图片
Docker部署yolov5_第2张图片

参考链接

x server错误参考
recompute_scale_factor错误参考

你可能感兴趣的:(Linux移植,docker,YOLO,容器,yolov5)