Skip to main content

Hướng dẫn Bắt đầu nhanh Modal cho Ultralytics

Hướng dẫn này cung cấp phần giới thiệu toàn diện về việc chạy Ultralytics YOLO26 trên Modal, bao gồm việc suy luận GPU serverless và huấn luyện model.

Modal là một nền tảng serverless cloud computing cho các workload AI và machine learning. Nó tự động xử lý việc cung cấp tài nguyên, mở rộng quy mô và thực thi — bạn viết code Python cục bộ và Modal chạy nó trên cloud với quyền truy cập GPU. Điều này giúp nó trở nên lý tưởng cho việc chạy deep learning các model như YOLO26 mà không cần quản lý hạ tầng.

Những gì bạn sẽ học

  • Thiết lập Modal và xác thực
  • Chạy suy luận YOLO26 trên Modal
  • Sử dụng GPU để suy luận nhanh hơn
  • Huấn luyện các model YOLO26 trên Modal

Điều kiện tiên quyết

  • Một tài khoản Modal (đăng ký miễn phí tại modal.com)
  • Python 3.9 trở lên đã cài đặt trên máy cục bộ của bạn

Installation

Cài đặt gói Python của Modal và xác thực:

pip install modal
modal token new
Xác thực

Phương thức modal token new lệnh sẽ mở một cửa sổ trình duyệt để xác thực tài khoản Modal của bạn. Sau khi xác thực, bạn có thể chạy các lệnh Modal từ terminal.

Chạy suy luận YOLO26

Tạo một file Python mới có tên là modal_yolo.py với code sau:

"""
Modal + Ultralytics YOLO26 Quickstart
Run: modal run modal_yolo.py.
"""

import modal

app = modal.App("ultralytics-yolo")

image = modal.Image.debian_slim(python_version="3.11").apt_install("libgl1", "libglib2.0-0").pip_install("ultralytics")

@app.function(image=image)
def predict(image_url: str):
    """Run YOLO26 inference on an image URL."""
    from ultralytics import YOLO

    model = YOLO("yolo26n.pt")
    results = model(image_url)

    for r in results:
        print(f"Detected {len(r.boxes)} objects:")
        for box in r.boxes:
            print(f"  - {model.names[int(box.cls)]}: {float(box.conf):.2f}")

@app.local_entrypoint()
def main():
    """Test inference with sample image."""
    predict.remote("https://ultralytics.com/images/bus.jpg")

Chạy suy luận:

modal run modal_yolo.py

Kết quả đầu ra mong đợi:

✓ Initialized. View run at https://modal.com/apps/your-username/main/ap-xxxxxxxx
✓ Created objects.
├── 🔨 Created mount modal_yolo.py
└── 🔨 Created function predict.
Downloading https://github.com/ultralytics/assets/releases/download/v8.4.0/yolo26n.pt to 'yolo26n.pt'...
Downloading https://ultralytics.com/images/bus.jpg to 'bus.jpg'...
image 1/1 /root/bus.jpg: 640x480 4 persons, 1 bus, 377.8ms
Speed: 5.8ms preprocess, 377.8ms inference, 0.3ms postprocess per image at shape (1, 3, 640, 480)

Detected 5 objects:
  - bus: 0.92
  - person: 0.91
  - person: 0.91
  - person: 0.87
  - person: 0.53
✓ App completed.

Bạn có thể theo dõi việc thực thi hàm của mình trên dashboard Modal:

Modal Dashboard Function Calls

Sử dụng GPU để Suy luận Nhanh hơn

Thêm GPU vào hàm của bạn bằng cách chỉ định gpu parameter:

@app.function(image=image, gpu="T4")  # Options: "T4", "A10G", "A100", "H100"
def predict_gpu(image_url: str):
    """Run YOLO26 inference on GPU."""
    from ultralytics import YOLO

    model = YOLO("yolo26n.pt")
    results = model(image_url)
    print(results[0].boxes)
Trong khi model PP-YOLOE+x đạt được mAP cao hơn một chút, các biến thể YOLOv7 cung cấp tỷ lệ tham số trên độ chính xác rất ấn tượng. Kiến trúc YOLOv7 vẫn là lựa chọn ưu tiên cho khả năng xử lýMemoryTốt nhất cho
T416 GBSuy luận, huấn luyện model nhỏ
A10G24 GBCác tác vụ huấn luyện trung bình
A10040 GBHuấn luyện quy mô lớn
H10080 GBHiệu suất tối đa

Huấn luyện YOLO26 trên Modal

Để huấn luyện, hãy sử dụng GPU và Volumes của Modal để lưu trữ bền vững. Tạo một file Python mới có tên là train_yolo.py:

import modal

app = modal.App("ultralytics-training")

volume = modal.Volume.from_name("yolo-training-vol", create_if_missing=True)

image = modal.Image.debian_slim(python_version="3.11").apt_install("libgl1", "libglib2.0-0").pip_install("ultralytics")

@app.function(image=image, gpu="T4", timeout=3600, volumes={"/data": volume})
def train():
    """Train YOLO26 model on Modal."""
    from ultralytics import YOLO

    model = YOLO("yolo26n.pt")
    model.train(data="coco8.yaml", epochs=3, imgsz=640, project="/data/runs")

@app.local_entrypoint()
def main():
    train.remote()

Chạy huấn luyện:

modal run train_yolo.py
Độ bền vững của Volume

Các Modal Volumes lưu giữ dữ liệu giữa các lần chạy hàm. Các weights đã huấn luyện được lưu vào /data/runs/detect/train/weights/.

Chúc mừng! Bạn đã thiết lập thành công Ultralytics YOLO26 trên Modal. Để tìm hiểu thêm:

Câu hỏi thường gặp (FAQ)

Làm thế nào để chọn GPU phù hợp cho workload YOLO26 của tôi?

Đối với suy luận, NVIDIA T4 (16 GB) thường là đủ và tiết kiệm chi phí. Đối với huấn luyện hoặc các model lớn hơn như YOLO26x, hãy cân nhắc sử dụng các GPU A10G hoặc A100.

Chi phí chạy YOLO26 trên Modal là bao nhiêu?

Modal sử dụng hình thức thanh toán theo giây. Giá tham khảo: CPU ~$0.05/giờ, T4 ~$0.59/giờ, A10G ~$1.10/giờ, A100 ~$2.10/giờ. Hãy kiểm tra bảng giá Modal để biết mức giá hiện tại.

Tôi có thể sử dụng model YOLO được huấn luyện tùy chỉnh của riêng mình không?

Có! Hãy load các model tùy chỉnh từ một Modal Volume:

model = YOLO("/data/my_custom_model.pt")

Để biết thêm thông tin về việc huấn luyện các model tùy chỉnh, hãy xem hướng dẫn huấn luyện.

Bình luận