Produce freshness assessment from a single photo, using multi-task deep learning.
- Learned tasks: freshness (Fresh / Stale) and produce type (apple, banana, bitter gourd, capsicum, orange, tomato).
- Heuristic fields: quality grade and shelf-life days are derived from P(fresh). They are not learned or validated, and the API marks them
*_is_heuristic: true. - Evaluation: a leakage-free split grouped by source photo. The Kaggle dataset ships many augmented copies of each photo, and a per-file split leaks them into test. See
DECISIONS.md§0 andpaper/.
- Backbone: EfficientNet-B0 by default, or MobileNetV3-Large (ImageNet-pretrained, via timm). The paper's chosen model and the app use MobileNetV3-Large.
- Heads: freshness (2 classes) and produce type (6 classes), trained with an equal-weight cross-entropy loss
- OOD gate: energy score on the produce-type head, thresholded at 95% validation TPR (stored in
model_meta.json). The app's gates are set differently (DECISIONS.md §0.1–0.2). - Detector (app v2.1): SSDlite320-MobileNetV3 with one "produce" class (
src/detection/) - Training: PyTorch Lightning, mixed precision, warmup + cosine LR, early stopping, CSV logs per run
- API: FastAPI with rate limiting, API key auth, input validation and SQLite logging
- Frontends: Streamlit (with Grad-CAM) and an offline Flutter app (
mobile_app/)
src/
├── data/build_splits.py # grouped leakage-free split -> data/metadata_v2.json
├── data/dataset.py # FruitDataset + Albumentations transforms
├── models/ # FreshTrackModel (PyTorch Lightning)
├── training/train.py # one run -> models/runs/<name>/
├── training/evaluate.py # metrics.json + model_meta.json per run
├── training/run_experiment.py # full matrix + baseline -> results/
├── training/export_onnx.py # classifier -> mobile_app/assets/model/
├── detection/ # produce detector: data, train, evaluate, export
├── api/ # FastAPI inference server
├── app.py # Streamlit frontend
└── config.py # labels, heuristics, paths
mobile_app/ # Flutter Android app, runs both models on the phone
paper/ # IEEE paper; numbers are generated from results/
tests/ # pytest suite
pip install -r requirements.txt
cp .env.example .env # set API_KEY, CORS_ORIGINS, ...- Download the Kaggle datasets "Fresh and Stale Images of Fruits and Vegetables" and "Fruit and Vegetable Image Recognition" into
data/downloads/. - Build the splits, run the experiments and generate the paper's numbers:
python -m src.data.build_splits # grouped split + external evaluation set
python -m src.training.run_experiment # 5 configs x 3 seeds + HSV baseline (resumable)
python paper/make_tables.py # LaTeX macros/tables/figure from results/results/summary.md holds the headline table. Every run records the metadata SHA-256, git SHA and seed in models/runs/<run>/run_config.json.
The app (v2.1) scans in two stages: a small detector finds each fruit or vegetable,
then the classifier checks a crop around each one (DECISIONS.md §0.2). The served
classifier is deploy_mnv3_v210, not the research model reported in the paper
(DECISIONS.md §0.1–0.2). The API still classifies the whole photo.
# classifier: v2.0.1 deployment model, then v2.1 fine-tuned on crops of real scenes
python -m src.training.train --name deploy_mnv3_v201 --metadata data/metadata_deploy.json \
--strong_aug --backbone mobilenetv3_large_100 --epochs 12 --num_workers 2
python -m src.detection.data coco # then: autolabel, cutouts, composite, build, clfcrops
python -m src.training.train --name deploy_mnv3_v210 --metadata data/metadata_deploy_v210.json \
--strong_aug --epochs 8 --lr 1e-4 --num_workers 2 --init_from models/runs/deploy_mnv3_v201/best.ckpt
python -m src.training.gate_sweep models/runs/deploy_mnv3_v210 --match models/runs/deploy_mnv3_v201
cp models/runs/deploy_mnv3_v210/best.ckpt models/checkpoints/freshtrack_v2.ckpt
cp models/runs/deploy_mnv3_v210/model_meta.json models/checkpoints/model_meta.json
python -m src.training.export_onnx # classifier -> Android app
# detector: train, calibrate the crop gate and evaluate everything, export
python -m src.detection.train
python -m src.detection.evaluate # -> results/detection_eval.json
python -m src.detection.export # detector -> Android app
uvicorn src.api.main:app --host 0.0.0.0 --port 8000
streamlit run src/app.pyDocker: the image does not contain the model, so mount it at runtime.
docker build -t freshtrack-api .
docker run -p 8000:8000 --env-file .env \
-v "$PWD/models/checkpoints:/app/models/checkpoints:ro" freshtrack-apiAndroid package in.rvitm.freshtrack. It runs the detector and the classifier on the phone with ONNX Runtime, so it needs no server and no internet (the release build does not request the INTERNET permission). Scan history, with a crop of each item, is kept in a local SQLite database.
python -m src.training.export_onnx # classifier + metadata + parity fixture -> mobile_app/
python -m src.detection.export # detector + metadata + parity fixtures -> mobile_app/
cd mobile_app
flutter pub get
flutter analyze && flutter test # 52 tests
flutter build apk --release --split-per-abi # arm64 APK ~60 MB- Measurements (APK size, RAM, cold start, latency, parity with PyTorch): see
docs/mobile_on_device_report.mdandresults/mobile_metrics.json. - Release signing: put
storeFile,storePassword,keyAliasandkeyPasswordinandroid/key.properties. Without that file, release builds fall back to the debug key.key.propertiesand*.jksare gitignored. - Screenshots:
mobile_app/screenshots/(v2.1.0, Android emulator, networking switched off).
| Method | Path | Description |
|---|---|---|
| GET | / |
Status check |
| GET | /health |
Model + device health |
| POST | /predict |
Image → freshness, produce type, heuristic quality/shelf-life, OOD score |
| POST | /feedback |
Submit a corrected freshness label (Fresh/Stale) |
| GET | /history, /stats |
Logged predictions |
/predict returns {"error": "OBJECT_NOT_RECOGNIZED", ...} with status 200 when the OOD gate rejects the image.
Authentication: pass the X-API-Key header when API_KEY is set. Rate limits: /predict 30/min, /feedback 10/min.
pytest tests/ -v- Freshness is binary (Fresh/Stale). The data has no intermediate ripeness stages.
- Quality grade and shelf life are heuristics, not predictions: no dataset used here has graded or time-to-spoilage labels.
- Training images are mostly single items on plain backgrounds. Accuracy on cluttered market photos is untested.
- Only 6 produce types are supported. The OOD gate is meant to reject others, but its near-OOD rejection rate is imperfect (see
results/).