Developer docs / AI Fabric tutorials
Fine-tune + serve YOLO
- GPU
- Volume
- Web endpoint
- Image DSL
Take it for a spin
The whole program in one file, ready to run.
import bladekit
import io
app = bladekit.app("yolo-finetune")
data_vol = bladekit.Volume.from_name("yolo-data", create_if_missing=True)
weights_vol = bladekit.Volume.from_name("yolo-weights", create_if_missing=True)
image = (
# fabric/torch ships torch (cu128 wheels) but NOT torchvision, which
# ultralytics needs. Install torchvision from the same cu128 index FIRST so
# its compiled ops (e.g. torchvision::nms) match the pinned torch. Letting
# pip pull torchvision from PyPI as an ultralytics dep gives a build compiled
# against a different torch and crashes at import ("operator ... does not exist").
bladekit.Image.from_catalog("fabric/torch")
# ultralytics pulls in opencv, which needs these system libs at import time.
.apt_install("libgl1", "libglib2.0-0")
.pip_install("torchvision", index_url="https://download.pytorch.org/whl/cu128")
.pip_install("ultralytics", "Pillow")
)
@app.function(
gpu="RTX-A4500",
image=image,
timeout=7200,
volumes={"/data": data_vol, "/weights": weights_vol},
retries=1,
)
def train(data_yaml: str = "coco128.yaml", epochs: int = 5) -> str:
from ultralytics import YOLO
# "coco128.yaml" is a built-in 128-image dataset that ultralytics
# auto-downloads (self-contained, no upload needed). Point data_yaml at a
# path on the /data Volume to train on your own dataset.
model = YOLO("yolo11n.pt")
model.train(data=data_yaml, epochs=epochs, project="/weights", name="custom")
return "/weights/custom/weights/best.pt"
@app.web_endpoint(methods=["POST"], min_replicas=0, max_replicas=4)
@app.function(gpu="RTX-A4500", image=image, timeout=30, volumes={"/weights": weights_vol})
def detect(item: dict) -> dict:
import base64
from ultralytics import YOLO
from PIL import Image as PILImage
img_bytes = base64.b64decode(item["image_b64"])
img = PILImage.open(io.BytesIO(img_bytes))
model = YOLO("/weights/custom/weights/best.pt")
results = model(img)
detections = [
{"class": model.names[int(b.cls)], "confidence": float(b.conf), "box": b.xyxy[0].tolist()}
for r in results for b in r.boxes
]
return {"detections": detections}
@app.local_entrypoint()
def main():
# Self-contained: trains a few epochs on the built-in coco128 dataset
# (auto-downloaded by ultralytics), then prints where the best weights live.
best = train.remote()
print(f"Best weights saved to: {best}")Run it
- Train on the built-in dataset:
blade run yolo_finetune.py. It fine-tunes a few epochs oncoco128(auto-downloaded) and saves best weights to theyolo-weightsVolume. - To train on your own data, upload images + a
dataset.yamlto theyolo-dataVolume and pass its path:train.remote("/data/dataset.yaml", epochs=50). - Deploy the endpoint:
blade deploy yolo_finetune.py. - Detect objects:
curl -X POST <URL> -H 'Authorization: Bearer <FABRIC_TOKEN>' -d '{"image_b64": "..."}' | jq .detections. Endpoints are private by default, so pass your AI Fabric token (or deploy withpublic=Truefor an unauthenticated endpoint).
Concepts used: GPU, Endpoint, Scale-to-zero, Image build