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 무료 크레딧 혜택을 받아 프로젝트를 시작할 수 있습니다.

GCP 외에도 브라우저 기반 환경을 위한 Google Colab 노트북 Open In Colab이나 Amazon AWS의 확장성 등 YOLOv5를 위한 다른 빠른 시작 옵션들도 살펴보십시오. 또한, 컨테이너 사용자라면 공식 Docker Hub Docker Pulls 이미지를 사용하여 격리된 환경을 구축할 수 있으며, 당사의 Docker 빠른 시작 가이드를 따를 수 있습니다.

1단계: Deep Learning VM 생성 및 구성

딥러닝에 최적화된 가상 머신을 생성하는 것부터 시작하겠습니다:

  1. GCP 마켓플레이스로 이동하여 Deep Learning VM을 선택합니다.
  2. n1-standard-8 인스턴스를 선택하십시오. 8개의 vCPU와 30GB 메모리의 균형을 갖추고 있어 많은 ML 작업에 적합합니다.
  3. GPU를 선택합니다. 선택은 워크로드에 따라 다르지만, 기본적인 T4 GPU라도 모델 학습 속도를 크게 향상시킬 수 있습니다.
  4. 원활한 설정을 위해 '첫 시작 시 NVIDIA GPU 드라이버 자동 설치(Install NVIDIA GPU driver automatically on first startup?)' 확인란을 선택하십시오.
  5. I/O 병목 현상을 방지하기 위해 300GB SSD 영구 디스크(Persistent Disk)를 할당합니다.
  6. '배포(Deploy)'를 클릭하고 GCP가 사용자 지정 Deep Learning VM을 프로비저닝할 때까지 기다립니다.

이 VM은 YOLOv5에 필요한 많은 종속성을 편리하게 포함하고 있는 Anaconda Python 배포판을 포함하여 필수 도구와 프레임워크가 사전 로드되어 제공됩니다.

Deep Learning VM 설정을 보여주는 GCP 마켓플레이스 이미지

2단계: YOLOv5를 위한 VM 준비

환경 설정을 완료한 후, YOLOv5를 설치하고 준비하겠습니다:

# Clone the YOLOv5 repository
git clone https://github.com/ultralytics/yolov5
cd yolov5

# Install dependencies
pip install -r requirements.txt

This 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를 사용하여 특정 요구 사항에 맞춘 사용자 지정 객체 탐지 모델을 학습시키거나 사전 학습된 가중치를 활용하여 다양한 작업에서 신속한 결과를 얻을 수 있습니다. 내보내기 후 다양한 모델 배포 옵션을 탐색해 보십시오.

GCP VM에서의 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를 학습하려면 다음 일반 단계를 따르십시오:

  1. YOLOv5 형식(이미지 및 해당 레이블 파일)에 따라 데이터셋을 준비합니다. 지침은 데이터셋 개요를 참조하십시오.

  2. gcloud compute scp 또는 웹 콘솔의 SSH 기능을 사용하여 데이터셋을 GCP VM에 업로드합니다.

  3. 학습 및 검증 데이터 경로, 클래스 수, 클래스 이름을 지정하는 데이터셋 구성 YAML 파일(custom_dataset.yaml)을 생성합니다.

  4. 사용자 지정 데이터셋 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 세계로의 중요한 발걸음을 내디뎠습니다.

모델을 학습하고 관리하기 위한 간소화된 노코드(no-code) 경험을 원하신다면 Ultralytics 플랫폼 사용을 고려해 보십시오.

진행 상황을 기록하고, 활발한 Ultralytics 커뮤니티와 통찰력을 공유하며, 협업과 지원을 위해 GitHub 토론과 같은 리소스를 활용하는 것을 잊지 마십시오. 이제 YOLOv5와 GCP로 혁신을 시작하십시오!

ML 기술을 계속 향상시키고 싶으신가요? 문서를 살펴보고 더 많은 튜토리얼과 통찰력을 얻으려면 Ultralytics 블로그를 확인하십시오. AI 모험을 계속 이어가시기 바랍니다!

댓글