Building Vision Models Under Hardware Constraints
TL;DR: Post-training quantisation (PTQ) cut our tiny vision model’s recall from 85% to 71% when we converted it to int8 for an STM32N6 microcontroller. Quantisation-aware training (QAT), 10 epochs at a learning rate of 1e-5, brought recall back to 87% with 92% precision, using one quarter of the bytes per weight. On the board it runs at 41 ms per frame, and its scores match the int8 file on a PC to within about 1%.
Why does hardware decide the model on a microcontroller?
Most model work starts with one question: how accurate can we make it? On a laptop or a cloud server, that is the right question. Memory is plentiful, and a slow model is only a slower model.
On a small device, the question changes. A chip has a fixed amount of memory and a fixed amount of time to process each input. A model that does not fit will not run. So in this scenario, the hardware decides what the model can be, before training starts.
An image model on a microcontroller meets three hard constraints at the same time.
• Memory for the weights. The full model must sit in a small, fixed memory pool.
• Memory for the activations. This is the working memory a model needs while it runs. If it does not fit in the fast on-chip RAM, it spills into slower memory and inference slows down several times over. We cover this in more depth in memory architecture for edge AI.
• Compute. The neural processor on the chip runs only 8-bit integer math. Float layers fall back to the main CPU, which is far slower.
On an STM32N6 board, the limits the model had to fit in were:
• Weights: 1.5 MB
• On-chip RAM for activations: 1.3 MB
• Time per frame: 66 ms
• Number format: int8 only
The last limit is the quiet one. Models are usually trained in 32-bit floats. But the chip wants 8-bit integers. So every weight goes from billions of possible values to 256. The model you train is not the model that runs.
Why run vision on the device instead of the cloud?
It is fair to ask why we do not send every input image to a server and run a large model there.
The answer is that a decision made on the device is a different kind of decision. It happens on every frame, at the moment of capture. It works with no network. And only the useful frames should ever leave the device, to save unnecessary time and compute. We weigh this trade-off in detail in edge vs cloud inference.
At Hoomanely, our mission is to help pet parents understand their pets’ health through everyday signals. Our devices, like EverBowl, sit in the home and collect those signals all day, like the post-meal window after a dog eats. So much of the intelligence must run on the device itself, with small memory and limited compute. Building models that fit this hardware is what makes that possible.
So the model has to live on the chip. The question is how to build it well.
How we designed a detection model around memory and compute limits
Our example is a detection model. It looks at each camera frame and decides if the target is in it. A frame with the target is a positive.
We made three choices before any training started.
1. Choose a network that fits. We picked a small mobile network, at half its normal width, and checked the memory sums on paper first. A large detector was never an option. (More on how we pick architectures in choosing the right model architecture for production.)
2. Shape the input for the scene, not for tradition. Most small models take a square crop. But the camera gives a wide output, and even the edge of the frame is important. So the model takes the full frame, resized.
3. Train on what the device sees. A general pretrained classifier found only 44% of the positives, because it was never trained on frames like these, which are low, close and often dim. So we labelled real frames for fine-tuning, the same lesson we learned when training on bad indoor data.
After normal float training, the model found 85% of the positives in the test set. On a server, the work would end here. On a microcontroller, the hard part starts here.
Post-training quantisation vs quantisation-aware training
The float model now has to become an int8 model. There are two ways to do it.
Quantisation-aware training is a short retraining step that simulates 8-bit rounding inside the network, so the weights learn to work with only 256 levels before the model is exported. Post-training quantisation skips retraining and only rounds an already trained model, which is faster but can lose accuracy on small models.
First way: post-training quantisation (PTQ)
This is the quick way. You take the trained model, show it a few sample images, and round everything to 8 bits. No training is needed.
We tried it first. Recall fell from 85% to 71%. The rounded model found 22 fewer of the 162 positives in the test set.
A small model has very little spare capacity. A large network can absorb rounding errors. A small one cannot, and a few sensitive channels in the first layers are enough to shift the final score. In our case the scores moved down, so real positives slipped under the threshold.
Second way: quantisation-aware training (QAT)
This is the careful way. You retrain the model for a short time with the rounding simulated inside the network. The weights then learn to work with only 256 levels.
This is the step that needs special care, because it can fail without telling you. Our first try ran with no errors, but the tool did not simulate the rounding, so the training had no effect.
The QAT recipe we followed:
• Start from: the trained 32-bit float model
• Ranges: set from 64 calibration images, before training
• Training: 10 epochs, learning rate 1e-5
• Export: full int8, per-channel weights
• Judge by: the exported int8 file, the one that goes to the board
Results: int8 accuracy on the test set and on the board
After QAT, the int8 model found 87% of the positives. That is level with the float model, with one quarter of the bytes per weight.
Positives found at each stage (466 test images from devices the model never saw, 162 positives, decision threshold 0.50):
• General pretrained model: 44% recall
• Float model: 85% recall
• PTQ, int8 with no retraining: 71% recall
• QAT, int8 after 10 epochs: 87% recall, 92% precision
Precision stayed at 92%, with 13 false alarms in 304 images without a positive. Then we put the model on the board and measured it against each constraint:
• Weights: 0.85 MB of the 1.5 MB limit
• Peak activation memory: 1,080 KB of the 1,344 KB limit, all in on-chip RAM
• Inference time: 41 ms per frame, against the 66 ms limit
• Number format: int8, running on the neural processor
A model that fits is not yet a model that is correct. So we ran the same input images through the model on the board and through the int8 file on a PC, and compared the scores.
The board score differed from the PC score by about 1% of the score range on average.
The accuracy numbers above come from the int8 file on the PC test set. The board check shows that the chip reproduces that file closely.
Key takeaways for edge AI teams
• The constraints come first. Memory and compute decide the network and the input size before training starts.
• The number format is a constraint too. Going from floats to int8 changes the model, and small models feel it most.
• Quick quantisation is a test, not a finish. If it loses accuracy, retrain with the rounding in the loop.
• QAT needs care. It can run with no errors and have no effect. Check that the rounding is really simulated.
• Judge the model in the form you ship. Score the exported file, then confirm it on the real hardware.
For more on how we train edge models for messy real-world input, read training edge AI models for real-world chaos.