在 Google Cloud Platform (GCP) Deep Learning VM 上掌握 YOLOv5 部署
Embarking on the journey of artificial intelligence (AI) and machine learning (ML) can be exhilarating, especially when you leverage the power and flexibility of a cloud computing platform. Google Cloud Platform (GCP) offers robust tools tailored for ML enthusiasts and professionals alike. One such tool is the Deep Learning VM, preconfigured for data science and ML tasks. In this tutorial, we will navigate the process of setting up Ultralytics YOLOv5 on a GCP Deep Learning VM. Whether you're taking your first steps in ML or you're a seasoned practitioner, this guide provides a clear pathway to implementing object detection models powered by YOLOv5.
🆓 此外,如果你是 GCP 新用户,你有幸获得 300 美元的免费额度来启动你的项目。
In addition to GCP, explore other accessible quickstart options for YOLOv5, like our Google Colab Notebook for a browser-based experience, or the scalability of Amazon AWS. Furthermore, container aficionados can utilize our official Docker image available on Docker Hub
for an encapsulated environment, following our Docker Quickstart Guide.
第 1 步:创建并配置你的 Deep Learning VM
让我们开始创建一个针对深度学习优化的虚拟机:
- 导航到 GCP 市场并选择 Deep Learning VM。
- 选择 n1-standard-8 实例;它平衡了 8 个 vCPU 和 30 GB 内存,适用于许多 ML 任务。
- 选择一个 GPU。选择取决于你的工作负载;即使是基础的 T4 GPU 也能显著加速模型训练。
- 勾选“在首次启动时自动安装 NVIDIA GPU 驱动程序?”选项以实现无缝设置。
- 分配一个 300 GB 的 SSD 持久磁盘,以防止 I/O 瓶颈。
- 点击“部署”并允许 GCP 配置你的自定义 Deep Learning VM。
该 VM 预装了基本工具和框架,包括 Anaconda Python 发行版,它方便地捆绑了许多 YOLOv5 所需的依赖项。

第 2 步:为 YOLOv5 准备 VM
设置好环境后,让我们安装并准备好 YOLOv5:
# Clone the YOLOv5 repository
git clone https://github.com/ultralytics/yolov5
cd yolov5
# Install dependencies
pip install -r requirements.txtThis setup process ensures you have a Python environment version 3.8.0 or newer and PyTorch 1.8 or later. Our scripts automatically download models and datasets from the latest YOLOv5 release, simplifying the process of starting model training.
第 3 步:训练并部署你的 YOLOv5 模型
设置完成后,你就可以在你的 GCP VM 上使用 YOLOv5 进行训练、验证、预测和导出:
# Train a YOLOv5 model on your dataset (e.g., yolov5s)
python train.py --data coco128.yaml --weights yolov5s.pt --img 640
# Validate the trained model to check Precision, Recall, and mAP
python val.py --weights yolov5s.pt --data coco128.yaml
# Run inference using the trained model on images or videos
python detect.py --weights yolov5s.pt --source path/to/your/images_or_videos
# Export the trained model to various formats like ONNX, CoreML, TFLite for deployment
python export.py --weights yolov5s.pt --include onnx coreml tflite只需几个命令,YOLOv5 就能让你训练针对你特定需求量身定制的目标检测模型,或利用预训练权重快速获得各种任务的结果。导出后,请探索不同的模型部署选项。

分配交换空间(可选)
如果你处理的数据集特别大,可能会超过 VM 的 RAM,请考虑添加交换空间以防止内存错误:
# Allocate a 64GB swap file
sudo fallocate -l 64G /swapfile
# Set the correct permissions for the swap file
sudo chmod 600 /swapfile
# Set up the Linux swap area
sudo mkswap /swapfile
# Enable the swap file
sudo swapon /swapfile
# Verify the swap space allocation (should show increased swap memory)
free -h训练自定义数据集
要在 GCP 内使用自定义数据集训练 YOLOv5,请遵循以下常规步骤:
-
按照 YOLOv5 格式准备你的数据集(图像和相应的标签文件)。有关指导,请参阅我们的数据集概述。
-
使用
gcloud compute scp或 Web 控制台的 SSH 功能将你的数据集上传到你的 GCP VM。 -
创建一个数据集配置 YAML 文件 (
custom_dataset.yaml),指定训练和验证数据的路径、类别的数量以及类别名称。 -
使用你的自定义数据集 YAML 开始训练过程,并可能从预训练权重开始:
# Example: Train YOLOv5s on a custom dataset for 100 epochs python train.py --img 640 --batch 16 --epochs 100 --data custom_dataset.yaml --weights yolov5s.pt
有关准备数据和使用自定义数据集进行训练的全面说明,请咨询 Ultralytics YOLOv5 训练文档。
利用云存储
为了实现高效的数据管理,特别是对于大型数据集或大量实验,请将你的 YOLOv5 工作流与 Google Cloud Storage 集成:
# Ensure Google Cloud SDK is installed and initialized
# If not installed: curl https://sdk.cloud.google.com/ | bash
# Then initialize: gcloud init
# Example: Copy your dataset from a GCS bucket to your VM
gsutil cp -r gs://your-data-bucket/my_dataset ./datasets/
# Example: Copy trained model weights from your VM to a GCS bucket
gsutil cp -r ./runs/train/exp/weights gs://your-models-bucket/yolov5_custom_weights/这种方法允许你在云端安全且经济高效地存储大型数据集和训练后的模型,从而最大限度地减少对你的 VM 实例的存储要求。
总结思考
恭喜!你现在已经具备了利用 Ultralytics YOLOv5 的功能并结合 Google Cloud Platform 的计算能力的能力。此设置可为你的目标检测项目提供可扩展性、效率和多功能性。无论是用于个人探索、学术研究还是构建工业解决方案,你都已经在云端 AI 和 ML 的世界中迈出了重要的一步。
考虑使用 Ultralytics Platform 来获得无代码的精简体验,以训练和管理你的模型。
记得记录你的进展,与充满活力的 Ultralytics 社区分享见解,并利用 GitHub 讨论 等资源进行协作和获取支持。现在,继续利用 YOLOv5 和 GCP 进行创新吧!
想要继续提升你的 ML 技能吗?深入阅读我们的文档,并浏览 Ultralytics 博客获取更多教程和见解。让你的 AI 之旅继续吧!