目录
环境选择
前提:Ubuntu + cuda已经安装好了
基于之前测试的环境,选择python3.8的版本
选择一个基础的docker 镜像
有几个选择:
- yolov5——版本不对,不够兼容,还是自己安装好
- pytorch + gpu版本——同上
- python + pip安装——需要安装很多东西
1、拉取容器
docker pull python:3.8.16-buster
2、启动容器
# 根据自己的场景来,我还共享了目录和其他的一些配置
docker run -it --gpus all --name detection_v5 --restart=always -d python:3.8.16-buster
3、配置容器环境
3.1 安装yolov5的基础环境
环境版本:Python-3.8.16 torch-1.9.0+cu102
requirements.txt
# YOLOv5 requirements
# Usage: pip install -r requirements.txt
# Base ----------------------------------------
gitpython # using v5 7.0
matplotlib>=3.2.2
numpy>=1.18.5
opencv-python>=4.1.1
Pillow>=7.1.2
PyYAML>=5.3.1
requests>=2.23.0
scipy>=1.4.1
torch==1.9.0
torchvision>=0.8.1
tqdm>=4.64.0
protobuf<=3.20.1 # https://github.com/ultralytics/yolov5/issues/8012
# Logging -------------------------------------
tensorboard>=2.4.1
# wandb
# clearml
# Plotting ------------------------------------
pandas>=1.1.4
seaborn>=0.11.0
# Export --------------------------------------
# coremltools>=5.2 # CoreML export
onnx>=1.9.0 # ONNX export
# onnx-simplifier>=0.4.1 # ONNX simplifier
# nvidia-pyindex # TensorRT export
# nvidia-tensorrt # TensorRT export
# scikit-learn==0.19.2 # CoreML quantization
# tensorflow>=2.4.1 # TFLite export (or tensorflow-cpu, tensorflow-aarch64)
# tensorflowjs>=3.9.0 # TF.js export
# openvino-dev # OpenVINO export
# Extras --------------------------------------
ipython # interactive notebook
psutil # system utilization
thop>=0.1.1 # FLOPs computation
# albumentations>=1.0.3
# pycocotools>=2.0 # COCO mAP
# roboflow
安装pip依赖包
pip install -r requirements.txt -i https://pypi.douban.com/simple
修改推理代码,尝试第一次推理
root@116725319c42:~/app# python v5_detect.py
Traceback (most recent call last):
File "v5_detect.py", line 46, in <module>
from models.common import DetectMultiBackend
File "/root/app/models/common.py", line 18, in <module>
import cv2
File "/usr/local/lib/python3.8/site-packages/cv2/__init__.py", line 181, in <module>
bootstrap()
File "/usr/local/lib/python3.8/site-packages/cv2/__init__.py", line 153, in bootstrap
native_module = importlib.import_module("cv2")
File "/usr/local/lib/python3.8/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
ImportError: libGL.so.1: cannot open shared object file: No such file or directory
# 自己的没有写过代码的可以尝试用官方的代码
https://github.com/ultralytics/yolov5
3.2 需要安装其他环境
更换阿里源
# 参考
https://www.cnblogs.com/gaobing/p/15718482.html
//1.先备份
cp /etc/apt/sources.list /etc/apt/sources.list.bak
//2.清空
echo " " > /etc/apt/sources.list
//3.写阿里源
echo "deb https://mirrors.aliyun.com/debian stable main contrib non-free">>/etc/apt/sources.list
echo "deb https://mirrors.aliyun.com/debian stable-updates main contrib non-free">>/etc/apt/sources.list
//4.清空缓存
apt-get clean
apt-get update
安装一些使用工具
apt install vim htop
解决没有openCV依赖包的问题
# 参考
https://blog.csdn.net/weixin_42990464/article/details/125203404
apt-get install ffmpeg libsm6 libxext6 -y
# 到这一步推理环境已经ok
4、提交容器
# 116725319c42 已启动的容器id 用指定的name也可以
# jingwei_detection_v5:v1 名字和版本(自定义)
docker commit 116725319c42 detection_v5:v1
# 导出容器和文件代码就可以自由部署到其他机器