Demo
Text version of the video
The video has background music and no narration. This is what it shows on screen.
- Which bin does it go in? Example photos of metal, plastic, cardboard and glass with the model's confidence.
- Waste Sorting Vision: photograph a household item and get its recycling category, one of cardboard, glass, metal and cans, paper, plastic or other waste. Recyclables in the wrong bin contaminate collections.
- Live demo: choosing a photo on the upload page and pressing Classify.
- Classifying a photo through the API with POST /api/v1/classify.
- How it works: TrashNet, a fixed 70/15/15 split, MobileNetV3-Small, then an ONNX export checked against PyTorch. The models folder is the contract between training and serving.
- Transfer learning: ImageNet weights plus a new last layer for six classes.
- The training loop: class weights handle imbalance, and label smoothing curbs overconfidence.
- ONNX export with a safety check: the same images go through PyTorch and ONNX Runtime and must agree.
- Inference without PyTorch: only Pillow, NumPy and ONNX Runtime turn a photo into probabilities.
- Choosing when to say "unsure": the lowest threshold with 95% accuracy on validation is 0.4131.
- Uploads are untrusted: content type and size are checked before anything is decoded.
- TrashNet test accuracy: classical baseline 0.730, CNN version 1 0.868, CNN version 2 (final) 0.892.
- 0.892 accuracy on the TrashNet test split, with the confusion matrix.
- Tested, packaged, measured: 575 tests with 100% coverage on the full install, and the Docker image of about 320 MB contains no PyTorch.
- Run it with docker compose up --build. Code and model under the MIT licence. Photos: TrashNet by Gary Thung and Mindy Yang (MIT). Music: Sascha Ende, ende.app (CC BY 4.0).
What it does
You upload a photo and the service answers with a category (cardboard, glass, metal and cans, paper, plastic or other waste), a confidence and the top three guesses. When the model isn’t confident enough, it says it’s unsure instead of guessing. It has a JSON API and a plain upload page that works without JavaScript.
Architecture
Training and serving are separate. The training side compares a classical baseline (colour histograms, LBP and HOG features with logistic regression or an SVM) against a fine-tuned MobileNetV3-Small on TrashNet, then exports the CNN to ONNX with a parity check against PyTorch. The serving side uses only onnxruntime and NumPy behind FastAPI, so the production Docker image contains no PyTorch, and CI checks that.
The hard part
A model that scores well on its own test set can still fail on real photos. On TrashNet the final CNN reaches 0.892 accuracy, against 0.730 for the baseline. On real facility photos from RealWaste it drops to 0.432. I didn’t hide that. Instead I measured calibration, retrained with label smoothing (the calibration error went from 0.353 to 0.108), and picked a confidence threshold of 0.4131. Below it, the service answers “unsure”. On the test set that answers 91.3% of images with 93.9% accuracy.
Decisions
- MobileNetV3-Small with transfer learning: small and fast enough for a CPU.
- ONNX Runtime for serving, recorded in an ADR, so the image stays small.
- A leakage-aware data split: duplicate images are grouped so the same photo can’t end up in both training and test.
- Strict upload validation: size limit (413), the declared format must match the real one (415), and a pixel limit read from the header before decoding.
Testing
38 test files. Without PyTorch installed, 400 pass and 37 torch-dependent ones are skipped; every serving and inference module has 100% statement and branch coverage. A parity test checks that the NumPy preprocessing matches the PyTorch transforms. CI builds the Docker image, starts it and classifies a sample image.
Next
Publishing v0.1.0 as a tagged GitHub release, and a hosted demo.