开始使用 Docker 运行 YOLOv5 🚀

Welcome to the Ultralytics YOLOv5 Docker Quickstart Guide! This tutorial provides step-by-step instructions for setting up and running YOLOv5 within a Docker container. Using Docker enables you to run YOLOv5 in an isolated, consistent environment, simplifying deployment and dependency management across different systems. This approach leverages containerization to package the application and its dependencies together.

若需其他设置方法,请参考我们的 Colab Notebook Open In Colab Open In KaggleGCP Deep Learning VMAmazon AWS 指南。有关 Ultralytics 模型使用 Docker 的常规概述,请参阅 Ultralytics Docker 快速入门指南

先决条件

在开始之前,请确保已安装以下内容:

  1. Docker:从 Docker 官方网站 下载并安装 Docker。Docker 是创建和管理容器的基础。
  2. NVIDIA DriversGPU 支持必需):确保已安装 455.23 或更高版本的 NVIDIA 驱动程序。你可以从 NVIDIA 官网 下载最新驱动。
  3. NVIDIA Container Toolkit(GPU 支持必需):此工具包允许 Docker 容器访问宿主机的 NVIDIA GPU。请遵循官方 NVIDIA Container Toolkit 安装指南 获取详细说明。

设置 NVIDIA Container Toolkit(GPU 用户)

首先,通过运行以下命令验证 NVIDIA 驱动程序是否安装正确:

nvidia-smi

该命令应显示有关你的 GPU 和已安装驱动程序版本的信息。

接下来,安装 NVIDIA Container Toolkit。以下命令适用于 Ubuntu 等 Debian 系系统以及 Fedora/CentOS 等 RHEL 系系统,但请参考上述官方指南以获取特定于你发行版的说明:

curl -fsSL https://nvidia.github.io/libnvidia-container/gpgkey | sudo gpg --dearmor -o /usr/share/keyrings/nvidia-container-toolkit-keyring.gpg \
  && curl -s -L https://nvidia.github.io/libnvidia-container/stable/deb/nvidia-container-toolkit.list \
  | sed 's#deb https://#deb [signed-by=/usr/share/keyrings/nvidia-container-toolkit-keyring.gpg] https://#g' \
    | sudo tee /etc/apt/sources.list.d/nvidia-container-toolkit.list

更新软件包列表并安装 nvidia-container-toolkit 软件包:

sudo apt-get update

安装最新版本的 nvidia-container-toolkit:

sudo apt-get install -y nvidia-container-toolkit \
  nvidia-container-toolkit-base libnvidia-container-tools \
  libnvidia-container1
可选:安装特定版本的 nvidia-container-toolkit

你也可以通过设置 NVIDIA_CONTAINER_TOOLKIT_VERSION 环境变量来安装特定版本的 nvidia-container-toolkit:

export NVIDIA_CONTAINER_TOOLKIT_VERSION=1.17.8-1
sudo apt-get install -y \
  nvidia-container-toolkit=${NVIDIA_CONTAINER_TOOLKIT_VERSION} \
  nvidia-container-toolkit-base=${NVIDIA_CONTAINER_TOOLKIT_VERSION} \
  libnvidia-container-tools=${NVIDIA_CONTAINER_TOOLKIT_VERSION} \
  libnvidia-container1=${NVIDIA_CONTAINER_TOOLKIT_VERSION}
sudo nvidia-ctk runtime configure --runtime=docker
sudo systemctl restart docker

验证 Docker 的 NVIDIA 运行时

运行 docker info | grep -i runtime 以确保 nvidia 出现在运行时列表中:

docker info | grep -i runtime

你应该会看到 nvidia 列为可用运行时之一。

第 1 步:拉取 YOLOv5 Docker 镜像

Ultralytics 在 Docker Hub 上提供官方 YOLOv5 镜像。latest 标签跟踪最新的仓库提交,确保你始终获得最新版本。使用以下命令拉取镜像:

# Define the image name with tag
t=ultralytics/yolov5:latest

# Pull the latest YOLOv5 image from Docker Hub
sudo docker pull $t

你可以在 Ultralytics YOLOv5 Docker Hub 仓库 中浏览所有可用镜像。

第 2 步:运行 Docker 容器

镜像拉取完成后,你就可以将其作为容器运行。

仅使用 CPU

要仅使用 CPU 运行交互式容器实例,请使用 -it 标志。--ipc=host 标志允许共享宿主机 IPC 命名空间,这对共享内存访问非常重要。

# Run an interactive container instance using CPU
sudo docker run -it --runtime=nvidia --ipc=host $t

使用 GPU

要在容器内启用 GPU 访问,请使用 --gpus 标志。这要求 NVIDIA Container Toolkit 已正确安装。

# Run with access to all available GPUs
sudo docker run -it --runtime=nvidia --ipc=host --gpus all $t

# Run with access to specific GPUs (e.g., GPUs 2 and 3)
sudo docker run -it --runtime=nvidia --ipc=host --gpus '"device=2,3"' $t

有关命令选项的更多详细信息,请参阅 Docker 运行参考

挂载本地目录

要在容器内处理本地文件(数据集、模型权重等),请使用 -v 标志将宿主机目录挂载到容器中:

# Mount /path/on/host (your local machine) to /path/in/container (inside the container)
sudo docker run -it --runtime=nvidia --ipc=host --gpus all -v /path/on/host:/path/in/container $t

/path/on/host 替换为你机器上的实际路径,将 /path/in/container 替换为 Docker 容器内所需的路径(例如 /usr/src/datasets)。

第 3 步:在 Docker 容器内使用 YOLOv5 🚀

你现在已进入正在运行的 YOLOv5 Docker 容器中!在此处,你可以执行标准的 YOLOv5 命令,完成各种 Machine LearningDeep Learning 任务,例如 Object Detection

# Train a YOLOv5 model on your custom dataset (ensure data is mounted or downloaded)
python train.py --data your_dataset.yaml --weights yolov5s.pt --img 640 # Start training

# Validate the trained model's performance (Precision, Recall, mAP)
python val.py --weights path/to/your/best.pt --data your_dataset.yaml # Validate accuracy

# Run inference on images or videos using a trained model
python detect.py --weights yolov5s.pt --source path/to/your/images_or_videos # Perform detection

# Export the trained model to various formats like ONNX, CoreML, or TFLite for deployment
python export.py --weights yolov5s.pt --include onnx coreml tflite # Export model

探索文档以了解不同模式的详细用法:

了解有关评估指标的更多信息,如 PrecisionRecallmAP。理解不同的导出格式,例如 ONNXCoreMLTFLite,并探索各种 Model Deployment Options。记得有效管理你的 model weights

Running YOLOv5 inside a Docker container on GCP

你已成功在 Docker 容器中设置并运行了 YOLOv5。

评论