Hybrid99
Rice quality grading on the edge. A hybrid Classical CV + ML pipeline that fits in under 2MB — no cloud, no latency, no compromise.
“Classical CV handles 80–90% of grains for free. ML only fires for the hard 10–20%. The result is a system that's fast enough for a $35 edge device.”
Why pay the ML tax when math is free?
The 80/20 efficiency rule: classical geometry handles the straightforward cases. ML only activates for the genuinely ambiguous ones.
Free & Instantaneous
Thresholding + contour detection computes area, perimeter, solidity, and circularity. Stones are round → geometry catches them. Single grains get sent straight to the classifier. Zero ML inference cost.
Precision Where It Counts
MicroUNet activates only for touching grain clusters. Input: 256×256 crop. Output: instance masks that feed back into the same classifier path as single grains.
| Metric | Standard ML Pipeline | Hybrid99 |
|---|---|---|
| Model Size | YOLOv8n ~6MB+ | ~1.56MB combined (INT8 target) |
| Inference Path | Every grain through ML | 80% via free geometry |
| Foreign Detection | ML classification | Math: circularity > 0.7 |
| Cluster Handling | Single model | Dedicated MicroUNet segmenter |
| Edge Viable | Marginal | Yes — <100ms target |
When the algorithm fails, you build the tool.
Automatic watershed annotation was unreliable for touching grains. The pivot: synthetic data generation and a custom masking tool for real annotations.
Watershed Annotation
Automatic watershed-based segmentation produced unreliable masks for touching rice grain clusters. Over-segmentation of overlapping grains and sensitivity to lighting made the approach untenable for production-quality labels.
Synthetic + Manual Masking Tool
Built a custom masking tool for precise manual COCO-format annotations with RLE mask encoding. Paired with synthetic cluster generation (compositing individual grains) for initial prototyping. Result: 1,517 production-quality annotated image pairs.
Custom Masking Tool
A purpose-built annotation interface for pixel-level segmentation of rice grain clusters. Outputs individual COCO JSON files with RLE-encoded masks, directly consumable by the MicroUNet training pipeline. Enforces data quality through an accept/reject workflow — only clean annotations make it tomasking_tool/dataset/accepted/.
One model segments. One model classifies.
Each model solves exactly one problem — making size optimization and iteration independent.
MicroUNet
2-level U-Net with skip connections. Pixel-level instance segmentation of touching grain clusters. Fixed a tensor dimension mismatch (ValueError: target [B,H,W] != input [B,1,H,W]) by adding target.unsqueeze(1) in loss computation.
⤢debug_epoch_100.png — Input · Ground Truth · Prediction
TinyMobileClassifier
MobileNet-style depthwise separable CNN. Handles 9 grain classes including rare defects (weevil, stone, shell_dust). Border rejection excludes partial grains touching image edges from training — a subtle but critical data quality decision.
⤢sample_predictions_epoch_60.png — True Label vs. Prediction
Every failure is a data point.
Training curves, confusion matrices, and misclassification logs — raw evidence of engineering depth, not cherry-picked accuracy numbers.
Early stopping triggered at epoch 61. Note the train/val accuracy gap (~17%) — the primary driver is the intrinsic visual similarity between healthy ↔ broken partial grains. Plan: TTA + additional borderline samples.
⤢The healthy↔broken confusion boundary drives the 82.45% accuracy floor. Weevil, stone, and red grains show high confidence due to distinctive visual features.
⤢From research to the edge.
~6.0MB FP32 combined → ~1.56MB INT8 via QAT. Then ONNX and TFLite for cross-platform edge deployment.
INT8 QAT
Quantization-Aware Training for both models. Expected 3.8× size reduction on segmenter. Target: segmenter <0.5MB, classifier <0.2MB. Verify accuracy drop <2%.
ONNX Export
Cross-platform inference. Enables deployment on hardware without PyTorch. Benchmark inference speed on target edge device.
TFLite Export
Mobile and microcontroller deployment. Required for Android-based handheld graders or embedded systems in grain processing facilities.
The pattern is domain-agnostic.
Any domain with small-object detection and a clear size budget can use this exact hybrid pattern. Classical CV filters the easy 80%. ML handles the rest. Swap the classifier labels, retrain on domain data. Pharmaceutical tablet inspection, seed quality grading, PCB defect detection — all viable targets.
Interested in the full pipeline?
Step 06 integration is active. QAT quantization and ONNX export coming next. Let’s connect if you’re working on edge inference, agricultural tech, or small-object detection.