Skip to main content...
CV Depth: the Measurement Pipeline
25 min

Day 67: U-Net training run & evaluating with IoU

Training and measuring your U-Net

Train the U-Net on a person-segmentation dataset (or distill from SAM masks as labels — a neat trick: use SAM to auto-generate training masks for your smaller, faster model). Evaluate with IoU — the same metric from NMS day, now measuring how well your predicted mask overlaps the true mask. Mean IoU across the validation set is your segmentation score.

IoU for masks — the segmentation evaluation metric
def mask_iou(pred, target, threshold=0.5):
    pred = (pred.sigmoid() > threshold)
    target = target.bool()
    intersection = (pred & target).sum().float()
    union = (pred | target).sum().float()
    return (intersection / union.clamp(min=1e-6)).item()

# validation loop: average IoU over the set
mean_iou = sum(mask_iou(model(x), y) for x, y in val_loader) / len(val_loader)

Why build a model you already have in SAM?

SAM is large and slow; a small distilled U-Net can run faster on the CPU droplet for a well-defined task (person cutout). More importantly, *building* it taught you the architecture you need for Stage 4. In production you might still use SAM — but the learning is the point, and 'I built a U-Net and distilled SAM into it' is a genuinely strong portfolio line.

Key terms

Mean IoU (mIoU)
Average Intersection-over-Union across a dataset; the standard segmentation quality metric.
Distillation (label distillation)
Using a large model (SAM) to generate training labels for a smaller, faster model.

IoU appears twice in this stage — for NMS (Day 48) and now for segmentation. What does it measure in the segmentation case?

We use cookies

We use cookies to enhance your browsing experience, serve personalized content, and analyze our traffic. By clicking "Accept All", you consent to our use of cookies. Learn more

    Day 67: U-Net training run & evaluating with IoU | RBTechIconX