Case 03 / Computer vision
Segmenting visible pigmentation, pixel by pixel.
A PyTorch U-Net for binary masks of visible dark spots and melasma-like pigmentation in facial images, supported by a complete training, evaluation, and inference pipeline.
- U-Net
- CNN
- Skip Connections
- Bilinear Upsampling
- PyTorch
- Binary Segmentation
- FastAPI
- 01 / LABELAnnotate imagesMore than 5,000 images marked manually
- 02 / LEARNTrain U-NetRGB images paired with binary masks
- 03 / PREDICTGenerate masksProbability maps, thresholds, and overlays
- 04 / CHECKEvaluate errorsDice, IoU, precision, recall, and per-image review
- Dataset preparation
- 5,000+images manually annotated
- Reported test Dice
- 0.7286at probability threshold 0.5
- Reported test IoU
- 0.5939on the project test set
Find regions, not just a label.
A classification score can say that pigmentation is present, but it cannot show its location. This project predicts a binary mask so the visible regions can be compared with manual annotations at pixel level.
Dataset preparation included manually annotating more than 5,000 images. That work matters because the model learns the boundary and coverage of regions from the masks supplied to it.
From images to inspectable masks.
The network uses a four-stage U-Net encoder and skip-connected decoder with bilinear upsampling, a 64-channel base width, one output channel, and 640 × 640 RGB input. Training combines binary cross-entropy and Dice loss.
- Paired image-mask loading keeps training and evaluation examples aligned.
- Evaluation calculates aggregate Dice, IoU, precision, recall, and F1.
- Per-image IoU ranking helps locate examples where the model and annotation disagree.
- Inference can produce a binary mask and a red overlay for one image or a directory.
- The repository includes inference weights and an optional API that processes uploaded images in memory.
Look at both agreement and failure.
The repository reports evaluation on a 200+ image test set at a probability threshold of 0.5.
| Metric | Reported value | What it captures |
|---|---|---|
| Dice | 0.7286 | Overlap between predicted and reference masks |
| IoU | 0.5939 | Intersection over union of mask regions |
| Precision | 0.7619 | How much predicted area matches the reference |
| Recall | 0.7525 | How much reference area is found |
| F1 | 0.7571 | Balance of precision and recall |
Example image source: dark spots — v1_clean_640_dark_spots, CC BY 4.0. Prediction grids and metrics are from this project.
Segmentation is not diagnosis.
These metrics describe performance on the project's test set and do not establish reliability across all lighting, image quality, skin tones, or capture conditions. Individual examples show that agreement can vary substantially, with over-segmentation and missed regions in difficult images.