Link to this section掌握在 Google Cloud Platform (GCP) 深度学习虚拟机上部署 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.
Link to this section第 1 步:创建并配置你的深度学习虚拟机#
让我们从创建针对 深度学习 优化的虚拟机开始:
- 导航至 GCP 市场 并选择 深度学习虚拟机 (Deep Learning VM)。
- 选择一个 n1-standard-8 实例;它平衡了 8 个 vCPU 和 30 GB 内存,适合许多机器学习任务。
- 选择一个 GPU。选择取决于你的工作负载;即使是基础的 T4 GPU 也能显著加速模型训练。
- 勾选“在首次启动时自动安装 NVIDIA GPU 驱动程序?”复选框,以实现无缝设置。
- 分配 300 GB 的 SSD 永久磁盘,以防止 I/O 瓶颈。
- 点击“部署”并让 GCP 配置你的自定义深度学习虚拟机。
该虚拟机预装了必要的工具和框架,包括 Anaconda Python 发行版,它方便地捆绑了许多 YOLOv5 所需的依赖项。

Link to this section第 2 步:为 YOLOv5 准备虚拟机#
环境设置完成后,让我们安装 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.
Link to this section第 3 步:训练并部署你的 YOLOv5 模型#
设置完成后,你就可以在 GCP 虚拟机上使用 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 就能让你训练针对特定需求定制的 目标检测 模型,或者利用预训练权重在各种任务中快速获得结果。导出后,探索不同的 模型部署选项。

Link to this section分配交换空间(可选)#
如果你处理的数据集特别大,可能会超出虚拟机的内存 (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 -hLink to this section训练自定义数据集#
要在 GCP 上使用你的自定义数据集训练 YOLOv5,请遵循以下常规步骤:
-
根据 YOLOv5 格式准备你的数据集(图像和相应的标签文件)。请参阅我们的 数据集概览 获取指导。
-
使用
gcloud compute scp或网页控制台的 SSH 功能将你的数据集上传到 GCP 虚拟机。 -
创建一个数据集配置文件 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 训练文档。
Link to this section利用云存储#
为了进行高效的数据管理,特别是处理大数据集或进行大量实验时,请将你的 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/这种方法允许你在云端安全且经济高效地存储大数据集和训练好的模型,从而最大限度地减少虚拟机实例的存储需求。
Link to this section结语#
恭喜!你现在已经准备好利用 Ultralytics YOLOv5 的功能,结合 Google Cloud Platform 的计算能力。此设置可为你的目标检测项目提供可扩展性、高效性和通用性。无论是个人探索、学术研究,还是构建工业 解决方案,你都已经向云端 AI 和机器学习的世界迈出了重要的一步。
考虑使用 Ultralytics Platform 来获得流线型的无代码体验,以训练和管理你的模型。
记得记录你的进展,与活跃的 Ultralytics 社区分享见解,并利用 GitHub discussions 等资源进行协作和获取支持。现在,开始利用 YOLOv5 和 GCP 进行创新吧!
想继续提升你的机器学习技能吗?深入阅读我们的 文档 并探索 Ultralytics 博客 以获取更多教程和见解。让你的 AI 之旅继续前行!