Skip Kaggle dataset download — data already in data/raw/
--skip-setup
Skip .venv creation and dependency install
--skip-verify
Skip architecture verification (src.verify)
--skip-export
Skip multi-format model export after training
Examples
# Fastest local runpythonlocal_train.py--quarter-data
# Half data, include QAT for Androidpythonlocal_train.py--half-data--include-qat
# Full data, lite4 backbone (teacher run for distillation)pythonlocal_train.py--backbonelite4
# Re-run training only (skip setup + download)pythonlocal_train.py--half-data--skip-setup--skip-download
# Smoke test — verifies the full pipeline in secondspythonlocal_train.py--smoke-test
python -m src.train — Core Training Engine
Runs the 3-phase training loop directly. local_train.py wraps this with automated setup and export stages.
python -m src.train [OPTIONS]
Run from the project root directory (not inside src/).
Model Configuration
Flag
Type
Default
Description
--backbone
lite2 | lite4
lite2
Backbone architecture
--weights
path
project root .pth
Custom pretrained .pth weights path
--attention
cbam | se
cbam
Attention module type
Data & I/O
Flag
Type
Default
Description
--data
path
data/raw/
Raw image root directory
--split-dir
path
data/splits/
CSV split output directory
--export-dir
path
outputs/export/portable/
Portable export destination directory
Device & Workers
Flag
Type
Default
Description
--batch-size
int
64
Per-GPU batch size (auto-scaled by local_train.py)
--num-workers
int
4
DataLoader worker processes
--device
str
auto
Force device: cuda or cpu (auto-detects by default)
Enable logit adjustment on top of the sampler (off by default; double-corrects)
--logit-adjust-prior
sampled | raw
sampled
Prior source when logit adjustment is enabled
--rare-threshold
int
30
Breeds below this many train images are excluded from CutMix/MixUp
--contrastive-weight
float
0.2
SupCon weight (0 disables)
--run-tag
str
auto DD-MM-YYYY-HH-MM
Run id for timestamped outputs
Compilation
Flag
Description
--no-compile
Disable torch.compile (auto-set on Windows; use to debug on Linux too)
--no-export
Skip automatic portable export after training completes
Examples
# Sanity check — full pipeline in secondspython-msrc.train--smoke-test
# Quarter-data training (recommended for local GPU)python-msrc.train--quarter-data
# Half-data with overridden epochspython-msrc.train--half-data--phase2-epochs20# Full training with lite4 backbone (teacher run)python-msrc.train--backbonelite4
# Full training, distilled from the lite4 teacherpython-msrc.train--backbonelite2\--teacheroutputs/checkpoints/lite4_phase2_best_<runid>.pt
# Opt-in QAT phase (recovery path; mobile INT8 uses converter PTQ)python-msrc.train--backbonelite2--include-qat
# Override hyperparameters explicitlypython-msrc.train\--backbonelite4\--batch-size32\--phase1-epochs8\--phase2-epochs80\--weight-decay0.01\--label-smoothing0.05\--devicecuda
# CPU-only run (no torch.compile)python-msrc.train--quarter-data--devicecpu--no-compile
python -m src.export — Model Export
Exports a trained checkpoint to desktop or mobile formats.
python -m src.export [OPTIONS]
Flags
Flag
Type
Default
Description
--backbone
lite2 | lite4
lite2
Which backbone's checkpoint to load
--mode
onnx | onnx-int8 | tflite | float16 | portable
required
Export format
--checkpoint
path
auto (<backbone>_phase2_best_<runid>.pt)
Specific .pt file to export
--calibration-images
int
500
Train images used for INT8 calibration (tflite / onnx-int8)
--skip-app-assets
flag
off
Don't copy TFLite artifacts into flutter_app/assets/models/
--onnx-static-batch
flag
off
Fix batch size 1 in the fp32 ONNX export
Export Modes
Mode
Output File
Description
portable
outputs/export/portable/<backbone>_<tag>/
Self-contained folder: model.pt + class JSONs + metadata. Requires BreedClassifier class to load.
Full-integer INT8 PTQ via onnx2tf; input [0,1], normalization baked in; auto-copies into flutter_app/assets/models/
onnx-int8
outputs/export/<backbone>_mobile_int8.onnx
QDQ static INT8 for ONNX Runtime Mobile; per-channel weights, calibrated on real train images
float16
outputs/export/<backbone>_float16.pt
FP16 TorchScript
--mode int8 (x86 PTQ) was removed — it failed conversion and its artifacts were unusable on Android. Running it prints guidance to use tflite / onnx-int8.
Compares the fp32 PyTorch reference against the mobile artifacts on the same data — the accuracy gate every export must pass.
python -m src.parity_check [OPTIONS]
Flags
Flag
Type
Default
Description
--checkpoint
path
auto (<backbone>_phase2_best_<runid>.pt)
fp32 reference checkpoint
--backbone
lite2 | lite4
lite2
Reference backbone
--tflite
path
—
TFLite artifact (fp32 or INT8)
--onnx-int8
path
—
Quantized ONNX (mobile convention: input [0,1])
--onnx
path
—
fp32 ONNX (caller-normalized convention)
--split
val | test
val
Split to evaluate
--limit
int
0 (all)
Max images per runtime
--synthetic
int
0
Skip the dataset; compare N random inputs (max |Δlogit|)
--tolerance
float
0.01
Allowed combined_top1 drop vs fp32
--out
path
outputs/metrics/<backbone>_parity*.json
JSON report path
Output
Accuracy table: binary_acc, cattle_acc, buffalo_acc, combined_top1, combined_top3 per runtime
Deltas vs pytorch_fp32 and a PASS / FAIL verdict against the tolerance
Synthetic mode: per-head max |Δlogit| (guidance < 0.05)
Examples
# Full accuracy parity on the validation splitpython-msrc.parity_check--backbonelite2\--tfliteoutputs/export/lite2_int8.tflite\--onnx-int8outputs/export/lite2_mobile_int8.onnx\--splitval
# Quick artifact-only check without any datasetpython-msrc.parity_check--backbonelite2--synthetic16\--onnx-int8outputs/export/lite2_mobile_int8.onnx
python -m src.evaluate — Model Evaluation
Evaluates a trained checkpoint on the test.csv holdout set.
python -m src.evaluate [OPTIONS]
Flags
Flag
Type
Default
Description
--backbone
lite2 | lite4
lite2
Backbone architecture to evaluate
--checkpoint
path
auto (latest phase2 checkpoint)
Specific .pt file to evaluate
--device
str
auto
Force device: cuda or cpu
Output
Per-class accuracy and F1 scores printed to console
Confusion matrix PNGs saved to outputs/metrics/
Summary JSON saved to outputs/metrics/eval_<backbone>.json
Examples
# Evaluate latest lite2 checkpointpython-msrc.evaluate--backbonelite2
# Evaluate specific checkpointpython-msrc.evaluate--backbonelite2\--checkpointoutputs/checkpoints/lite2_phase2_best_<runid>.pt
# Force CPU evaluationpython-msrc.evaluate--backbonelite2--devicecpu
python test_model.py — Model Tester GUI
Standalone web GUI for visually testing exported models on individual images.
python test_model.py [OPTIONS]
Flags
Flag
Type
Default
Description
--port
int
8501
HTTP port to serve the GUI on
--no-browser
flag
off
Don't automatically open the browser
--dev
flag
on
Developer mode: view full metadata, system logs, edit presenter settings
--present
flag
off
Presenter mode: clean UI, hides technical details and export options
Examples
# Launch with default settings (opens browser at http://localhost:8501)pythontest_model.py
# Use a different portpythontest_model.py--port9000# Headless server (no browser pop-up — useful for SSH sessions)pythontest_model.py--no-browser
Requirements
At least one trained checkpoint in outputs/checkpoints/ or outputs/export/portable/
Class map files in data/splits/ (cattle_classes.json, buffalo_classes.json) — generated automatically during training
python -m src.verify — Architecture Verification
Quick sanity check: loads backbone weights, constructs the model, runs a forward pass, and validates output shapes.
python -m src.verify
No flags. Expected output:
[verify] backbone: lite2
[verify] feature dim: 1280 ✓
[verify] binary head output: (1, 2) ✓
[verify] cattle head output: (1, 57) ✓
[verify] buffalo head output: (1, 18) ✓
[verify] All checks passed.
Web App REST API (server/app.py, port 5000)
The React app and the Colab workflows use the same HTTP contract.
Method
Path
Purpose
GET
/api/health
Liveness, model count, device
POST
/api/predict
Multipart inference: image, model, filename (the original file name)
test_model.py exposes the same semantics with legacy names
(/api/ood-profiles, /api/ood-info, /api/dataset-info,
/api/recalibrate-ood — an alias of /api/ood/recalibrate) and adds
/api/ood-data-dirs.
Guardrail rejection payload
When /api/predict rejects an image, the response includes:
"is_ood":true,"rejection_info":{"rejected":true,"reason_code":"human_or_selfie","stage":"Stage 2: Human / Semantic Check","title":{"en":"...","ta":"...","hi":"..."},"message":{"en":"...","ta":"...","hi":"..."},"instruction":{"en":"...","ta":"...","hi":"..."}}
data_dir may be absolute or relative to the project root. Images can be
flat (Deoni_01.jpg) or nested (cattle/deoni/…); names are matched against
the selected model's class maps.