UgaDeep logo
UGADEEP UG · AF · WORLD
← Back to Tutorials
Applied Vision Intermediate 10 min read

Edge Vision AI for Uganda: Quantizing Crop Disease Models for Offline Android

Take a PyTorch cassava & banana leaf classifier from FP32 down to INT8 so smallholder farmers can run diagnostics with zero internet.

By UgaDeep Labs ·
Run in Google Colab ↗

When deploying deep learning outside Kampala—into Mukono, Mbale, Lira, or Mbarara—cloud-only inference breaks the moment cellular reception drops. Edge quantization turns a 45 MB vision model into a crisp < 6 MB offline model that runs in under 100 ms on entry-level Android hardware.

1. Symmetric INT8 Quantization Math

Post-training quantization maps floating-point weights w∈Rw \in \mathbb{R} onto 8-bit signed integers q∈[−127,127]q \in [-127, 127] using a scale factor ss:

s=max⁡(∣w∣)127,q=clip(round(ws),−127,127)s = \frac{\max(|w|)}{127}, \qquad q = \text{clip}\left(\text{round}\left(\frac{w}{s}\right), -127, 127\right)

This reduces model memory bandwidth by ~4x and unlocks fast integer SIMD instructions on mobile ARM CPUs.

2. Export & Dynamic Quantization Workflow

import torch
import torchvision.models as models

# Load a compact MobileNetV3 backbone fine-tuned on 5 cassava disease classes
model = models.mobilenet_v3_small(weights=None)
model.classifier[3] = torch.nn.Linear(model.classifier[3].in_features, 5)
model.eval()

# Export clean ONNX graph for mobile (ONNX Runtime / LiteRT)
dummy = torch.randn(1, 3, 224, 224)
torch.onnx.export(
    model,
    dummy,
    "shamba_vision_fp32.onnx",
    input_names=["leaf_image"],
    output_names=["disease_logits"],
    opset_version=17,
)
print("Exported shamba_vision_fp32.onnx ready for INT8 calibration.")

3. Field-Ready Best Practices

  • Calibrate with real Ugandan lighting: Harsh equatorial sunlight and leaf shadows break models calibrated only on studio images—always include outdoor smartphone calibration samples.
  • Add an “Unknown / Non-Leaf” class or entropy threshold: If predictive entropy H(p)=−∑ipilog⁡piH(p) = -\sum_i p_i \log p_i is high, ask the user to retake the photo closer to the leaf instead of displaying a confident wrong diagnosis.
  • Sync telemetry asynchronously: Store compressed predictions locally and sync anonymized regional outbreak counts to a Cloudflare Worker + D1 endpoint whenever connectivity returns.