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 modal然后使用你的 Modal 账户验证 CLI:
modal 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 卷会在函数运行之间持久保存数据。训练后的权重会保存到 /data/runs/train/weights/。
恭喜!你已成功在 Modal 上设置了 Ultralytics YOLO26。如需进一步学习:
- 探索 Ultralytics YOLO26 文档 以了解高级功能
- 了解有关使用自己的数据集 训练自定义模型 的信息
- 尝试 Docker 快速入门 进行容器化部署
- 访问 Modal 文档 以了解高级平台功能
Link to this section常见问题解答#
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 卷加载权重文件,在 Modal 上运行你自己的自定义训练 YOLO 模型:
model = YOLO("/data/my_custom_model.pt")有关训练自定义模型的更多信息,请参阅 训练指南。