Ultralytics 的 Modal 快速入门指南
本指南全面介绍了如何运行 Ultralytics YOLO26在Modal,涵盖无服务器 GPU 推理和模型训练。
什么是 Modal?
Modal 是一个用于 AI 和 云计算 工作负载的无服务器 机器学习 平台。它会自动处理配置、扩展和执行——你只需在本地编写 Python 代码,Modal 就会在云端通过 GPU 访问来运行它。这使得它非常适合运行 深度学习 像 YOLO26 这样的模型,而无需管理基础设施。
你将学到什么
- 设置 Modal 并进行身份验证
- 在 Modal 上运行 YOLO26 推理
- 使用 GPU 加快推理速度
- 在 Modal 上训练 YOLO26 模型
先决条件
- 一个 Modal 账户(在 modal.com)
- 免费注册)
安装
本地计算机上安装了 Python 3.9 或更高版本
pip install modalmodal token new安装 Modal Python 包并进行身份验证:
在 modal token new身份验证
命令将打开浏览器窗口以验证你的 Modal 账户。完成身份验证后,你即可从终端运行 Modal 命令。
运行 YOLO26 推理modal_yolo.py创建一个名为
"""
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")的新 Python 文件,其中包含以下代码:
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 仪表板中监控你的函数执行情况:
使用 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 | 通过指定 | 选项,将 GPU 添加到你的函数中。 |
|---|---|---|
| 内存 | 适用场景 | T4 |
| 16 GB | 推理,小型模型训练 | A10G |
| 24 GB | 中型训练任务 | A100 |
| 40 GB | 大规模训练 | H100 |
80 GB
最高性能在 Modal 上训练 YOLO26对于训练,请使用 GPU 和 Modal 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()Volumes
modal run train_yolo.py进行持久化存储。创建一个名为
的新 Python 文件。/data/runs/detect/train/weights/.
运行训练:
- 示例:使用 Ultralytics YOLO26 文档Volume 持久化
- Modal Volumes 在函数运行之间持久化数据。训练好的权重将保存到 恭喜!你已成功在 Modal 上设置了 Ultralytics YOLO26。进一步学习: 获取高级功能
- 访问 了解关于 使用你自己的数据集
FAQ
训练自定义模型
Modal 文档
获取高级平台功能
我该如何为我的 YOLO26 工作负载选择合适的 GPU?对于推理,NVIDIA T4 (16 GB) 通常足够且具有成本效益。对于训练或更大的模型(如 YOLO26x),请考虑 A10G 或 A100 GPU。在 Modal 上运行 YOLO26 的成本是多少?
Modal 使用按秒付费的定价模式。近似费率:CPU ~$0.05/小时,T4 ~$0.59/小时,A10G ~$1.10/小时,A100 ~$2.10/小时。查看
Modal 定价
model = YOLO("/data/my_custom_model.pt")以获取最新费率。training guide.