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.
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?