INT8 Quantization: Shrinking Neural Networks for the Edge
- Edge AI
- TensorFlow Lite
- Quantization
- Machine Learning
Across two projects — PredictEdge (predictive maintenance on a Raspberry Pi) and FaceGuard (face recognition on a phone) — I kept hitting the same wall: the model worked great on my laptop and was useless on the target device. The fix both times was quantization.
The problem with FP32
By default, neural-network weights are 32-bit floats. That's fine on a GPU, but on a Raspberry Pi or a mid-range phone it means:
- 4× the memory of an 8-bit model.
- Slow inference, because there's no fast floating-point throughput.
- Battery drain you can't hide behind a spinner.
What INT8 quantization does
Quantization maps those 32-bit floats to 8-bit integers. Each tensor gets a scale and a zero-point so values can be reconstructed approximately:
real_value ≈ scale × (int8_value − zero_point)
You lose a little precision, but you gain ~4× smaller models and integer-only math that edge hardware loves.
Doing it with TensorFlow Lite
The part that surprised me: post-training quantization gets you most of the way with almost no effort, as long as you give it a representative dataset to calibrate the ranges.
converter = tf.lite.TFLiteConverter.from_keras_model(model)
converter.optimizations = [tf.lite.Optimize.DEFAULT]
converter.representative_dataset = rep_data_gen # calibration samples
converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS_INT8]
converter.inference_input_type = tf.int8
converter.inference_output_type = tf.int8
tflite_model = converter.convert()
The representative dataset is the secret sauce — without good calibration samples, the activation ranges are wrong and accuracy falls off a cliff.
The trade-off, measured
For PredictEdge's RUL model, INT8 cut the model size dramatically with only a small accuracy hit — exactly the deal you want at the edge. For FaceGuard, quantized MobileFaceNet ran inference in tens of milliseconds on-device.
What I learned
- Measure accuracy after quantization, on-device, not just in the notebook.
- Calibration data matters more than the model architecture for the final accuracy.
- "Make it small enough to run where it's needed" is its own engineering discipline — and a genuinely fun one.