Computer Vision
Clothing Segmentation
Case study: A fashion retailer
The model recognizes each piece of clothing in a photo, pixel by pixel. In the demo it runs inside your browser, so your photo never leaves your device.
Segment a photoThe problem
Case study: a fashion retailer wants to follow what people wear in photos posted online: which items show up, in what proportions, and how that changes from one season to the next. Nobody can label thousands of photos by hand. The first building block is a model that labels every pixel of a photo with what it shows: top, trousers, dress, shoes, bag, but also hair, skin and background. This is called semantic segmentation.
The brief was a feasibility test, with no training involved. Take a model already fine-tuned for clothes and published on Hugging Face (SegFormer-B3), call it through Hugging Face's hosted API, check its quality on 50 photos annotated by hand, and estimate what processing 500,000 images a month would take.
What the demo does
Pick one of the example photos, upload your own, take one with your phone or use the webcam. Each item is colored by type, with a legend in English or French and a slider to make the mask more or less transparent. Skin, face and hair stay hidden unless you ask for them.
The model runs in your browser. It downloads once (28.8 MB), then each photo takes about a second on the Mac I tested it on, and nothing is uploaded. The examples open instantly because their masks were computed in advance with the same model; one button runs the model live on them, on your device.
The examples are public-domain photos (CC0). The 50 test photos of the original project are not published here, for image rights reasons.
The model in the demo is not the one from the project. The results below explain the switch.
Results
- 0.765Overlap with hand-drawn outlines (mIoU), 50 test photos
- For each item in a photo, the area the model colors is compared with the area a person outlined: shared pixels divided by all the pixels covered by either. 1 would be a perfect match. The model used in the original project scored 0.567 on the same photos, mostly because it mixed up left and right shoes, legs and arms.
- 28.8 MBModel size, downloaded once
- The browser downloads the model the first time you run it and keeps it in its cache. The photo is processed on your device and never sent to a server.
- 0.95 sPer photo, in the browser
- Median time per photo on an Apple Silicon Mac in Chromium, using 4 processor threads. With the graphics card (WebGPU) and a 55.5 MB version of the model, it drops to 0.17 s. Not yet measured on a real phone.
The original project, through the hosted API:
- All 50 photos processed, no failures, 3.18 s per photo on average. That time includes the network and the pauses I added between calls to stay within the API's limits.
- mIoU 0.567 (median 0.551, worst photo 0.364, best 0.787).
mIoU is the standard score for segmentation. For each type of item present in the hand-made annotation, it divides the pixels where the model and the annotator agree by all the pixels either of them marked. The scores are averaged over the items in the photo (background ignored), then over the 50 photos.
To prepare the demo, I compared that model with a smaller one from the same family: SegFormer-B2, fine-tuned for the same 18 classes on a human-parsing dataset (ATR) and already converted for the browser. Same 50 photos, same metric:
- Project model (B3), converted for the browser: 49.6 MB, mIoU 0.568, 1.26 s per photo.
- Smaller model (B2): 28.8 MB, mIoU 0.765, 0.95 s per photo.
At full precision the converted B3 scores 0.567, exactly the notebook's figure, so the conversion and the metric are reproduced faithfully.
The gap comes from one weakness, which I had already spotted by eye in the project presentation: the B3 mixes up left and right. Pooled over the 50 photos, it reaches an IoU of 0.31 on the left shoe where the B2 reaches 0.57, and 0.25 against 0.84 on the right leg. Once left and right are merged into one class, the two models tie at 0.80 (difference -0.0005, 95% interval -0.010 to +0.009). So the project's model recognized clothes about as well; the metric punished it for getting the side wrong. For the demo the smaller model still wins: the same quality where it matters, 42% lighter, faster, and already published in ONNX, a format browsers can run.
Limits:
- 50 photos are enough to compare two models on the same images (the B2 scores higher on all 50), not to approve a model for production.
- Small items are the hardest: belts reach 0.49, shoes 0.57 to 0.60.
- The model knows 18 fixed classes. A coat, a shirt and a T-shirt are all "upper clothes", which limits what a trend analysis can say without a finer model.
- Times were measured on one Mac in Chromium. The demo still has to be timed on real phones.
How it's built
Architecture, stack and technical choices.
Diagram as text
flowchart LR
subgraph Project["Original project: hosted API"]
A["Test photo"] --> B["Resize to 1,024 px max, JPEG"] --> C["Hugging Face API<br/>SegFormer-B3"]
C -->|"one mask per class, base64"| D["Merged class map"] --> E["mIoU against hand-drawn masks"]
end
subgraph Demo["Demo: in the browser"]
F["Your photo"] --> G["Web Worker"] --> H["Transformers.js<br/>ONNX Runtime Web"] --> I["SegFormer-B2, int8<br/>28.8 MB"] --> J["Colored mask on the photo"]
endStack
- Python
- Hugging Face Inference API
- SegFormer
- requests
- Pillow
- NumPy
- scikit-learn
- Matplotlib
- ONNX
- Transformers.js
- ONNX Runtime Web
- WebGPU
- uv
The project notebook:
- The API returns one mask per class, encoded in base64. The notebook decodes them, resizes them to the photo's size (nearest neighbor, so classes do not blend) and merges them into a single map of class IDs.
- Before sending, each photo is shrunk to 1,024 px on its longest side and re-encoded as JPEG: less data to send and fewer timeouts.
- The hosted API is shared, so calls can fail while it is busy or while the model loads. The notebook retries on 429, 503 and 504 errors and on network errors, waiting 1, 2, then 4 s and so on between attempts, and never sends more than 30 calls per minute.
- The access token is read from a
.envfile that stays out of Git.
The demo:
- Transformers.js runs the model in a Web Worker, so the page stays responsive during the computation.
- The model is served by the Hugging Face CDN at a pinned revision, and the browser caches it.
- Using 4 processor threads requires two security headers on the page (cross-origin isolation). They make the computation 2.6 times faster than with a single thread.
- The int8 version stores the weights on 8 bits. It gives the same class as the full model on 99.5% of pixels, at about a quarter of the size.
- The masks computed in the browser match the Python ones on 99.7 to 99.9% of pixels.
- The evaluation script reads the 50 test photos in memory and writes only aggregate numbers to disk.
Next steps
- Compare a few models before settling on one. The project tested a single model; the comparison done for this demo found a smaller one that is at least as good on the same photos.
- Pick the metric with the business question in mind. For trend analysis, telling a left shoe from a right shoe does not matter, and merging those classes gives a fairer picture of quality (0.80 instead of 0.57).
- Estimate cost from throughput measured on the target machine. My estimate multiplied an average time per image, measured end to end through the shared API, by an hourly price. The demo shows another option: when the model is small enough, the visitor's device does the work and there is no inference server to pay for.
- Build the test set my validation plan called for: more photos, more varied (poses, lighting, styles, types of clothing), with per-class scores and a pass mark agreed before testing.