Docker部署yolov5模型

准备工作

部署过程中的环境如下:

  • Centos7.6.1810(core 4.7.11)
  • docker-18.09.7
  • yolov5模型

Dockerfile

FROM registry.baidubce.com/ais-public/ais2.3:cuda10.1_cudnn7-ubuntu16.04-py37

# 设置环境变量
ENV PATH=/home/bml/anaconda3/envs/py3.7.4/bin:${PATH}
# Install linux packages
RUN apt update && apt install -y zip htop screen libgl1-mesa-glx

# Create working directory
RUN mkdir -p /home/yolov5_app
WORKDIR /home/yolov5_app

# Copy contents
COPY . /home/yolov5_app
# COPY yolov5s.pt /home/yolov5_app/

# Install python dependencies
RUN python -m pip install --upgrade pip && \
pip install torch==1.7.1+cu101 torchvision==0.8.2+cu101 torchaudio==0.7.2 -f https://download.pytorch.org/whl/torch_stable.html && \
pip install --index-url https://mirrors.ustc.edu.cn/pypi/web/simple --requirement requirements.txt


# Set environment variables
CMD ["python","app.py"]

镜像构建

docker build -f Dockerfile -t yolov5:v0 .

构建过程中的错误

COPY failed: stat /var/lib/docker/tmp/docker-builder166217557/yolov5s.pt: no such file or directory

解决办法:确保所有的语句没有错误,删除与Dockerfile同一目录下的.dockerignore文件,再次构建,即可成功。

启动容器并进入

  • 方法1
docker container run --name yolo_test -it yolov5:v0 /bin/bash
  • 方法2
# 启动容器
docker run --name=yolov5_v0 -itd yolov5:v0

# 进⼊容器
docker exec -it yolov5_v0 bash

容器内预测

python detect.py ‐‐source inference/images/bus.jpg ‐‐view‐img

相关报错

在容器内运行python detect.py,出现以下错误:

Traceback (most recent call last):
  File "app.py", line 10, in <module>
    from model import PyModel
  File "/home/yolov5_app/model.py", line 9, in <module>
    import cv2
  File "/opt/conda/lib/python3.6/site-packages/cv2/__init__.py", line 5, in <module>
    from .cv2 import *
ImportError: libGL.so.1: cannot open shared object file: No such file or directory

运行pip list查看安装列表,发现已安装opencv-python
解决办法:
Ubuntu运行以下语句:

apt install -y libgl1-mesa-glx

centos运行以下语句:

yum install mesa-libGL.x86_64

退出容器

在容器中输入以下命令,即可退出容器:

exit

镜像导出

docker save yolov5:v0 -o /home/yolov5_v0.tar

你可能感兴趣的:(Python,Docker,docker,python,容器)