TinyML Quantization: Reduce Neural Network Size for MCUs

TinyML quantization reduces neural network size by converting FP32 weights into lower-precision formats such as INT8, helping microcontrollers save memory and improve inference efficiency. Techniques such as post-training quantization and quantization-aware training help optimize models while maintaining acceptable prediction accuracy.
Using TensorFlow Lite Micro, developers can deploy quantized models on supported ESP32 and STM32 devices for efficient Edge AI applications.

TinyML makes it possible to run machine learning models on resource-constrained devices such as microcontrollers, sensors, and embedded systems. Instead of sending sensor data to the cloud for every prediction, a TinyML device can process the data locally and produce results with low latency and reduced network dependency.

However, deploying a neural network on a microcontroller is not as simple as training it on a computer. Microcontrollers typically have limited RAM, Flash storage, processing power, and energy. A model that works perfectly on a laptop may be too large to run on an ESP32, STM32, or another embedded device.

This is where TinyML quantization becomes useful.

Quantization reduces the numerical precision used to represent a neural network’s weights and, in many cases, its activations. This can reduce model size, lower memory requirements, and enable efficient inference on supported microcontrollers.

In this guide, you will learn how quantization works, why it matters in TinyML, how INT8 quantization reduces model size, and how to prepare a quantized model for deployment using TensorFlow Lite and TensorFlow Lite for Microcontrollers.

TinyML Quantization_ Reduce Neural Network Size for MCUs

What Is Quantization in TinyML?

Quantization in TinyML is the process of converting a neural network’s numerical values from a higher-precision format, such as 32-bit floating point (FP32), to a lower-precision format, such as 8-bit integer (INT8).

During neural network training, weights and activations are commonly represented using floating-point numbers. These values provide numerical flexibility, but they can require more memory and computation than a microcontroller can efficiently support.

Quantization represents these values using fewer bits.

For example, a model with one million FP32 weights requires approximately 4 MB to store its weights alone. If those weights are represented using INT8, they require approximately 1 MB.

That is a potential 75% reduction in weight storage.

Data typeBits per valueApproximate storage for 1 million weights
FP32 (32-bit float)324 MB
FP16 (16-bit float)162 MB
INT8 (8-bit integer)81 MB
INT4 (4-bit integer)40.5 MB

These figures represent theoretical weight storage using decimal megabytes. Actual model files may include metadata, quantization parameters, operators, and other information. INT4 and FP16 are not universally supported or equally efficient across microcontroller runtimes.

Important: Quantization reduces numerical precision. It does not automatically reduce the number of layers, neurons, or parameters in a neural network.

Why Is Quantization Important for TinyML?

Microcontrollers are designed for embedded applications, not for running large neural networks with unlimited memory and computing resources.

Consider an embedded device that uses a neural network to recognize hand gestures from an accelerometer. The model must process sensor readings and predict the gesture using the memory and processing resources available on the device.

Without optimization, the model might exceed the available Flash storage or RAM.

TinyML model optimization helps address these constraints.

1. Reduces model size

Neural network weights are often stored in Flash memory. Converting FP32 weights to INT8 reduces their storage requirement by approximately 75%, assuming the same number of weights and no additional overhead.

This allows developers to consider models that would otherwise be too large for a particular microcontroller.

2. Reduces memory requirements

Many microcontrollers have only a limited amount of RAM. Although model weights are often stored in Flash, inference also requires RAM for input data, intermediate activations, operator buffers, and other working data.

Integer quantization can reduce activation memory in supported implementations. However, the actual RAM savings depend on the model architecture, tensor dimensions, memory planning, and runtime.

3. Can improve inference speed

Some microcontrollers and processors provide optimized integer arithmetic instructions. When the model uses supported quantized operators, INT8 inference can be faster than floating-point inference.

The improvement depends on the target hardware, runtime, and operator implementation. Quantization does not guarantee faster execution on every device.

4. Can reduce energy consumption

Efficient integer operations can reduce the computational cost of inference on suitable hardware. This is valuable for battery-powered sensors, wearables, and remote monitoring devices.

Actual energy savings depend on the processor, clock speed, memory access, inference duration, and power-management configuration.

5. Supports offline embedded intelligence

When a quantized model fits within the device’s resources, the microcontroller can make predictions locally without sending every sensor reading to a cloud server.

Examples include:

  • Gesture recognition using accelerometer data.
  • Keyword spotting in voice-controlled devices.
  • Vibration-based fault detection.
  • Environmental sound classification.
  • Simple sensor anomaly detection.
  • Human activity recognition.

 

registor_now_P

 

How Does Neural Network Quantization Work?

To understand neural network quantization, start with the values stored inside a trained model.

Suppose a neural network contains the following floating-point weights:

-0.82, -0.35, 0.12, 0.47, 0.91

An INT8 model cannot represent these values directly as ordinary floating-point numbers. Instead, quantization maps them to integer values using a scale and, in some quantization schemes, a zero point.

A simplified quantization equation is:

q = round(x / s) + z

Where:

  • x is the original floating-point value.
  • s is the quantization scale.
  • z is the zero point.
  • q is the quantized integer value.

The scale determines how much each integer step represents in the original numerical range. The zero point helps map real zero to an integer value when required by the quantization scheme.

For a simplified symmetric example, assume the scale is 0.01 and the zero point is zero.

A floating-point value of 0.47 becomes:

q = round(0.47 / 0.01) = 47

The integer 47 represents approximately 0.47 when converted back using the scale.

The approximate reconstruction is:

x̂ = s × (q − z)

Because the quantized value uses a limited set of integers, the reconstructed value may differ slightly from the original value. This difference is called quantization error.

The objective is to reduce memory and computational costs while keeping the model’s predictions sufficiently accurate for its application.

Common Types of Quantization in TinyML

There are several approaches to quantization. Understanding their differences helps you choose a suitable method for an embedded machine learning project.

1. Post-training quantization

Post-training quantization is applied after the neural network has been trained. You do not need to retrain the entire model just to apply the basic quantization process.

This makes it a practical starting point for students and developers who already have a trained TensorFlow model.

Two common options are:

  • Dynamic-range quantization: Weights are quantized, while some computations may still use floating-point values. The runtime may determine activation ranges dynamically. The benefits depend on which operators and hardware are supported.
  • Full integer quantization: Weights and supported activations are quantized to integer representations. This is particularly useful when targeting microcontrollers that benefit from integer-only inference.

Full integer quantization generally requires representative calibration data so the converter can estimate activation ranges.

2. Quantization-aware training

Quantization-aware training, or QAT, simulates quantization effects during training using mechanisms such as fake quantization.

The model learns to tolerate the numerical errors introduced by low-precision representations. After training or fine-tuning, it can be converted into a quantized model for supported deployment targets.

QAT is useful when post-training quantization causes an unacceptable drop in model accuracy.

However, QAT requires additional training work and is not necessary for every TinyML project.

3. Float16 quantization

Float16 quantization reduces weight storage by using 16-bit floating-point values instead of 32-bit values.

It can be useful when the target runtime and hardware support the required operations. However, many microcontrollers benefit more from integer inference, and FP16 support varies considerably between devices.

4. INT8 quantization

INT8 quantization represents weights and supported activations using 8-bit integers.

It is one of the most common choices for TinyML because it offers a practical balance between model size, numerical precision, and support in embedded inference tools.

For example, a neural network with 200,000 FP32 weights needs approximately 800 KB for its weights. Representing those weights as INT8 reduces that requirement to approximately 200 KB.

That difference can be significant when working with a microcontroller that has limited Flash storage.

FP32 vs INT8 Quantization: What Is the Difference?

The main difference is the numerical representation used for the model’s values.

FeatureFP32 modelINT8 model
Representation32-bit floating point8-bit integer
Weight storage4 bytes per weight1 byte per weight
Weight size reductionBaselineApproximately 75%
Numerical precisionHigherLower
Inference speedHardware-dependentCan be faster on supported hardware
Memory usageOften higherOften lower
AccuracyOriginal trained-model baselineMay change after quantization
TinyML suitabilityDepends on available resourcesOften suitable for constrained devices

A smaller model is not automatically a better model. Developers must measure the final model’s accuracy, inference latency, and memory usage on the intended device.

For instance, an INT8 model that is 75% smaller but misses important fault-detection events may be unsuitable for an industrial monitoring system.

 

Explore Courses - Learn More

 

Practical Example: Quantizing a TensorFlow Model for TinyML

Let’s walk through a practical workflow for preparing a model for a microcontroller.

Assume you have trained a small neural network to classify sensor readings. The model is saved as sensor_model.keras.

Your goal is to convert it into a fully integer-quantized TensorFlow Lite model.

Step 1: Install TensorFlow

Create a Python environment and install a compatible TensorFlow version.

pip install tensorflow

Use a Python version supported by your chosen TensorFlow release.

Step 2: Load the trained model

import tensorflow as tf

model = tf.keras.models.load_model("sensor_model.keras")

The model should already be trained and evaluated. Keep a copy of the original model so you can compare its predictions with the quantized version.

Step 3: Prepare representative data

Full integer post-training quantization needs representative input samples to estimate activation ranges.

For this example, assume your model accepts sensor inputs with a shape of (100, 3), representing 100 time steps and three sensor axes.

import numpy as np

# Replace this with real training or calibration samples.
# Shape: (number_of_samples, 100, 3)
representative_data = np.load("representative_sensor_data.npy")

def representative_dataset():
    for sample in representative_data[:200]:
        sample = sample.astype(np.float32)
        yield [sample[np.newaxis, ...]]

The sample shape must match the model’s actual input shape, including the batch dimension. The values must also follow the same preprocessing and normalization used during training.

The 200 samples are only an example. The right quantity and diversity depend on the model and data distribution.

Step 4: Convert the model to INT8

converter = tf.lite.TFLiteConverter.from_keras_model(model)

converter.optimizations = [
    tf.lite.Optimize.DEFAULT
]

converter.representative_dataset = representative_dataset

converter.target_spec.supported_ops = [
    tf.lite.OpsSet.TFLITE_BUILTINS_INT8
]

converter.inference_input_type = tf.int8
converter.inference_output_type = tf.int8

quantized_model = converter.convert()

with open("sensor_model_int8.tflite", "wb") as file:
    file.write(quantized_model)

print("INT8 model saved successfully.")

This configuration requests integer implementations for supported TensorFlow Lite operators and INT8 input and output tensors. Conversion can fail if the model contains unsupported operations or if the representative dataset does not match the model’s input requirements.

A successful conversion is an important step, but it does not prove that the model will run on every microcontroller.

Step 5: Compare model sizes

import os

fp32_size = os.path.getsize("sensor_model.tflite")
int8_size = os.path.getsize("sensor_model_int8.tflite")

print(f"FP32 model: {fp32_size / 1024:.2f} KB")
print(f"INT8 model: {int8_size / 1024:.2f} KB")

reduction = (1 - int8_size / fp32_size) * 100

print(f"Size reduction: {reduction:.2f}%")

This comparison assumes you also have an FP32 TensorFlow Lite file saved as sensor_model.tflite.

The output reports the actual file-size reduction for your converted models. It may differ from the theoretical 75% reduction because the files can contain metadata, different tensor representations, and other overhead.

Step 6: Validate the quantized model

Evaluate both the original model and the quantized model using the same test dataset.

Check:

  • Classification accuracy or the relevant task-specific metric.
  • False positives and false negatives.
  • Predictions for difficult or unusual input samples.
  • Input and output quantization parameters.
  • Model size and compatibility with the target runtime.

For integer inputs, you may need to transform preprocessed floating-point inputs using the model’s input scale and zero point. Do not simply cast sensor values to int8 without applying the required quantization mapping.

How to Deploy a Quantized Model on ESP32

After converting and validating your model, the next step is deployment.

The ESP32 is a popular platform for TinyML experiments because it combines microcontroller capabilities with connectivity features. Actual model capacity and inference performance vary across ESP32 variants.

A typical deployment workflow is:

  1. Train a neural network using TensorFlow or Keras.
  2. Convert the model into a quantized TensorFlow Lite model.
  3. Validate its accuracy and inspect the operators it requires.
  4. Convert the .tflite model into a C/C++ array or otherwise package it for the embedded application.
  5. Integrate it with a compatible inference runtime, such as TensorFlow Lite for Microcontrollers where supported.
  6. Compile and flash the firmware.
  7. Run inference on real sensor data and measure resource usage.

For example, an accelerometer-based gesture recognition application might collect motion readings, prepare a window of sensor data, invoke the model, and display the predicted gesture.

Important memory considerations

A quantized model’s file size is not the same as its total RAM requirement.

An embedded application may need memory for:

  • Model weights in Flash.
  • Input and output tensors.
  • Intermediate activation tensors.
  • The inference runtime’s tensor arena.
  • Sensor buffers and application variables.
  • Stack usage and other firmware components.

The tensor arena is a region of RAM used by TensorFlow Lite for Microcontrollers to hold tensors and other inference data. Its size depends on the model’s operators, tensor shapes, and memory-planning requirements.

Even when an INT8 model fits in Flash, it can still fail to run if the application does not have enough RAM.

Best practice: Test the final firmware on the actual microcontroller instead of relying only on the .tflite file size.

Does Quantization Reduce Model Accuracy?

Quantization can reduce model accuracy because it maps a range of floating-point values to a smaller set of representable numbers.

The impact varies by model architecture, task, data distribution, and quantization method. Some models retain almost the same accuracy after INT8 conversion, while others experience a meaningful decline.

For example, a sensor-classification model might achieve 95% accuracy in FP32 and 94.5% in INT8. In another application, quantization could cause a much larger drop.

These values are illustrative, not benchmark results.

To minimize accuracy loss:

  • Use representative calibration data that reflects real inputs.
  • Preserve the same preprocessing pipeline.
  • Evaluate the converted model on held-out test data.
  • Inspect per-class metrics instead of accuracy alone.
  • Consider quantization-aware training if post-training quantization performs poorly.
  • Test on the actual deployment runtime.

For safety-critical or industrial applications, also assess the consequences of incorrect predictions rather than relying only on aggregate accuracy.

Quantization vs Pruning vs Knowledge Distillation

Quantization is not the only method for reducing the resource requirements of a neural network.

TechniqueHow it worksMain benefit
QuantizationUses lower-precision numerical representationsReduces storage and can improve integer inference efficiency
PruningRemoves or zeros selected weights or connectionsCan reduce computation or storage when supported by the format and runtime
Knowledge distillationTrains a smaller student model to imitate a larger teacher modelCan produce a smaller model with useful predictive performance
Architecture optimizationDesigns or selects a smaller networkReduces parameters and computation at the model-design stage

These methods address different aspects of model efficiency.

For example, a developer could train a compact neural network, apply INT8 quantization, and then evaluate whether further pruning or architecture changes are necessary.

However, unstructured pruning does not always reduce actual model size or inference latency unless the storage format and execution runtime can take advantage of the sparsity. Similarly, a distilled model still needs testing and optimization for its target device.

Common Mistakes When Applying TinyML Quantization

Students often encounter deployment problems because model conversion is treated as the final step.

Mistake 1: Assuming every INT8 model is faster

INT8 inference can be faster on supported hardware, but performance depends on the processor, operators, and runtime. Benchmark the model on the target device.

Mistake 2: Using unrepresentative calibration data

If calibration samples do not reflect real-world inputs, the estimated activation ranges may be unsuitable. Include realistic examples covering the expected input range.

Mistake 3: Ignoring preprocessing differences

A model trained with normalized sensor values may produce incorrect predictions if firmware sends raw sensor readings without equivalent preprocessing.

Mistake 4: Checking only Flash usage

A small model can still require substantial RAM for intermediate tensors and buffers. Measure both Flash and RAM requirements.

Mistake 5: Skipping accuracy evaluation

Always compare the quantized model against the original model using a separate test dataset. Verify the metrics that matter for the actual application.

Mistake 6: Assuming every TensorFlow Lite model works with TFLite Micro

TensorFlow Lite for Microcontrollers supports a limited set of operators and has deployment constraints. Check operator compatibility, memory requirements, and target-specific build instructions before selecting the model architecture.

Best Practices for Reducing Neural Network Size for Microcontrollers

If you are starting a TinyML project, follow this practical sequence:

  1. Choose a suitable model architecture. Start with a compact network designed for your task and available hardware.
  2. Establish a baseline. Measure FP32 accuracy, model size, inference latency, and memory requirements.
  3. Apply post-training quantization. Try INT8 quantization first when the target runtime supports it.
  4. Use representative calibration data. Ensure samples reflect the input distribution expected in deployment.
  5. Validate model quality. Compare the quantized model against the original using appropriate test metrics.
  6. Inspect operator compatibility. Confirm the required operations are supported by your microcontroller’s runtime.
  7. Measure on hardware. Check inference time, Flash use, RAM use, and power consumption where relevant.
  8. Use QAT if needed. If quantization causes unacceptable accuracy loss, consider quantization-aware training or a different model architecture.

This process makes optimization measurable rather than relying on theoretical file-size estimates.

Conclusion

TinyML quantization is an important technique for deploying neural networks on microcontrollers with limited memory and processing resources. By converting model weights and supported activations from FP32 to INT8, developers can often reduce weight storage by approximately 75% and may improve inference efficiency on compatible hardware.

However, successful deployment involves more than shrinking the model file. You must also preserve acceptable accuracy, verify operator compatibility, account for RAM usage, and benchmark the final implementation on the target device.

For students learning embedded systems and Edge AI, a practical starting project is to train a small sensor-classification model, convert it using full integer post-training quantization, and deploy it on a supported ESP32 or STM32 platform.

By understanding the relationship between model precision, memory consumption, inference speed, and accuracy, you can build TinyML applications that make effective use of constrained embedded hardware.

Post-training INT8 quantization is a useful starting point because it can optimize an existing trained model without requiring a complete retraining process. If accuracy drops too much, evaluate calibration quality, model architecture, and quantization-aware training.

 

Talk to Academic Advisor

Frequently Asked Questions

TinyML quantization reduces the numerical precision used to represent neural network weights and, in supported configurations, activations. It helps reduce model storage and can improve inference efficiency on resource-constrained microcontrollers.

INT8 uses one byte per weight instead of four bytes for FP32. Therefore, weight storage can decrease by approximately 75%, although the total model file may not shrink by exactly that amount.

Post-training quantization is applied after training and can be a relatively straightforward optimization step. Quantization-aware training simulates quantization effects during training so the model can adapt to reduced numerical precision.

Yes, compatible quantized models can run on suitable ESP32 variants using an appropriate embedded inference runtime. Deployment depends on available memory, supported operators, and the specific chip and software configuration.

No. Some models retain nearly the same accuracy, while others experience a noticeable decline. Evaluate the quantized model using representative test data before deploying it.

Author

Embedded Systems and IOT Trainer– IIES

Updated On: 15-10-26


10+ years of hands-on experience delivering practical training in Embedded Systems and it's design