Link to this sectionUltralytics 的 Modal 快速入门指南#
This guide provides a comprehensive introduction to running Ultralytics YOLO26 on Modal, covering serverless GPU inference and model training.
Link to this section什么是 Modal?#
Modal is a serverless cloud computing platform for AI and machine learning workloads. It handles provisioning, scaling, and execution automatically — you write Python code locally and Modal runs it in the cloud with GPU access. This makes it ideal for running deep learning models like YOLO26 without managing infrastructure.
Link to this section你将学到什么#
- 设置 Modal 并进行身份验证
- 在 Modal 上运行 YOLO26 推理
- 使用 GPU 加速推理
- 在 Modal 上训练 YOLO26 模型
Link to this section先决条件#
- 一个 Modal 账户(可在 modal.com 免费注册)
- 在你的本地计算机上安装 Python 3.9 或更高版本
Link to this section安装#
安装 Modal Python 包并进行身份验证:
pip install modalmodal token newmodal token new 命令将打开一个浏览器窗口来验证你的 Modal 账户。验证完成后,你就可以从终端运行 Modal 命令了。
Link to this section运行 YOLO26 推理#
创建一个名为 modal_yolo.py 的新 Python 文件,其中包含以下代码:
"""
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")运行推理:
modal run modal_yolo.py预期输出:
✓ 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.你可以在 Modal 仪表板中监控函数的执行情况:
Link to this section使用 GPU 加速推理#
通过指定 gpu 参数将 GPU 添加到你的函数中:
@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)| GPU | 内存 | 最佳用途 |
|---|---|---|
| T4 | 16 GB | 推理,小型模型训练 |
| A10G | 24 GB | 中型训练任务 |
| A100 | 40 GB | 大规模训练 |
| H100 | 80 GB | 最高性能 |
Link to this section在 Modal 上训练 YOLO26#
对于训练,请使用 GPU 和用于持久化存储的 Modal Volumes。创建一个名为 train_yolo.py 的新 Python 文件:
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()运行训练:
modal run train_yolo.pyModal Volumes 在函数运行之间持久保存数据。训练后的权重会保存到 /data/runs/detect/train/weights/。
恭喜!你已成功在 Modal 上设置好 Ultralytics YOLO26。若需进一步了解:
- 探索 Ultralytics YOLO26 文档 以了解高级功能
- 了解如何使用自己的数据集 训练自定义模型
- 访问 Modal 文档 以了解高级平台功能
Link to this section常见问题 (FAQ)#
Link to this section如何为我的 YOLO26 工作负载选择合适的 GPU?#
对于推理,NVIDIA T4 (16 GB) 通常就足够且具有成本效益。对于训练或更大的模型(如 YOLO26x),请考虑使用 A10G 或 A100 GPU。
Link to this section在 Modal 上运行 YOLO26 的成本是多少?#
Modal 使用按秒计费的定价模式。大致费率:CPU ~0.05 美元/小时,T4 ~0.59 美元/小时,A10G ~1.10 美元/小时,A100 ~2.10 美元/小时。请查看 Modal 定价 获取最新费率。
Link to this section我可以使用自己训练的自定义 YOLO 模型吗?#
可以!从 Modal Volume 加载自定义模型:
model = YOLO("/data/my_custom_model.pt")有关训练自定义模型的更多信息,请参阅 训练指南。