Quickstart

This guide will get you running object detection training in minutes.

Training a Model

Using the CLI

The easiest way to train is using the CLI with a config file:

# Train Faster R-CNN on COCO
objdet fit --config configs/coco_frcnn.yaml

Using Python

import lightning as L
from objdet.models import FasterRCNN
from objdet.data import COCODataModule

# Initialize model
model = FasterRCNN(
    num_classes=80,
    backbone="resnet50_fpn_v2",
    pretrained_backbone=True,
)

# Initialize data
datamodule = COCODataModule(
    data_dir="/path/to/coco",
    batch_size=8,
)

# Train
trainer = L.Trainer(
    max_epochs=50,
    accelerator="cuda",
    devices=1,
)
trainer.fit(model, datamodule)

Running Inference

from objdet.inference import Predictor

# Load trained model
predictor = Predictor.from_checkpoint("outputs/best.ckpt")

# Run inference
result = predictor.predict("image.jpg")

print(f"Found {len(result['boxes'])} objects")
for box, label, score in zip(
    result["boxes"], result["labels"], result["scores"]
):
    print(f"  {label}: {score:.2f} at {box}")

Deploying as REST API

from objdet.serving import run_server

run_server(
    checkpoint_path="outputs/best.ckpt",
    host="0.0.0.0",
    port=8000,
)

Then send requests:

curl -X POST http://localhost:8000/predict \
  -H "Content-Type: application/json" \
  -d '{"url": "https://example.com/image.jpg"}'

Next Steps