Edge AI
Shrinking a Detection Model by 78%: A Pruning and Quantization Diary
Taking YOLO11 from 11.4 MB to 2.5 MB without losing the plot. What each compression step actually cost, measured on the device rather than the spec sheet.
A detection model that runs beautifully on a laptop GPU and cannot run at all on the device you need it on is not a working model. It is a demo.
This is a record of compressing YOLO11 for mobile and embedded deployment, the work behind YOLO-OptiMob. Written as the sequence of decisions rather than the tidy summary. The headline is 11.4 MB down to 2.5 MB. The interesting part is what each step cost and how I decided when to stop.
#Start with the constraint, not the model
The first mistake I made on an earlier project was optimising toward "smaller" with no target. You can always go smaller. Without a budget you have no stopping rule, and you will either stop too early or destroy the model.
So the budget came first, derived from the deployment:
- Size: under 4 MB, so it fits comfortably in an Android app bundle alongside everything else.
- Latency: under 120 ms per frame on the target hardware, giving roughly 8 FPS, enough for the interaction being built.
- Accuracy floor: mAP within 5% relative of the baseline. Below that, the downstream logic starts making visibly bad decisions.
Three numbers. Every subsequent decision is measured against them, and the work has a clear definition of done.
#Step one: the dataset, which is not a compression step
Before touching the model, the dataset got attention. Seven object classes, custom collected and labelled.
This matters for compression specifically. A pruned model has less capacity to absorb label noise. Mislabelled examples that a full-size model shrugs off become real accuracy damage after pruning. An afternoon of re-checking the worst-performing class removed more error than one of the later compression tweaks added.
If your dataset is noisy, fix that before you compress. Compression amplifies every weakness already present.
#Step two: pruning, and finding the knee
L1 unstructured pruning, evaluated at 30%, 40% and 50% sparsity. Prune, fine-tune, measure.
| Sparsity | Size | mAP vs baseline | Verdict |
|---|---|---|---|
| 0% | 11.4 MB | , | Baseline |
| 30% | 8.6 MB | -1.1% | Safe, not enough reduction |
| 40% | 7.1 MB | -2.4% | The knee |
| 50% | 5.9 MB | -7.8% | Past the cliff |
The jump from 40% to 50% is the whole story. Up to 40%, accuracy degrades roughly linearly with sparsity. Past it, the curve turns sharply. The model has run out of redundant capacity and is now losing weights that were doing real work.
That shape is typical, and the location of the knee is specific to the architecture and the dataset. It cannot be read from a paper. You have to sweep it. Three training runs is a cheap price for knowing exactly where your cliff is rather than guessing.
One practical note: unstructured pruning gives you a sparse weight tensor, not a smaller computation, unless your runtime exploits sparsity. The size reduction is real once compressed; the speed reduction often is not. I spent an embarrassing amount of time confused about why a 40%-pruned model was not 40% faster. It is not supposed to be. Pruning bought me size and set up quantization; the speed came from elsewhere.
#Step three: INT8 quantization
Post-training INT8 quantization on the 40%-pruned model. 7.1 MB to 2.5 MB, and this is where the latency actually improved, because integer arithmetic is genuinely faster on the target hardware.
Two things mattered more than I expected.
The calibration set. Post-training quantization needs representative data to choose activation ranges. My first attempt used a convenient slice of the training set and lost noticeably more accuracy than it should have. The slice happened to be mostly well-lit images. Activation ranges calibrated on bright scenes clip badly on dark ones. Rebuilding the calibration set to match the real deployment distribution. Lighting, distances, occlusion. Recovered most of the gap. A few hundred genuinely representative images beat a few thousand convenient ones.
Not everything should be quantized. The detection head is more sensitive than the backbone. Keeping the final layers at higher precision while quantizing everything else costs a small amount of size and recovers a meaningful amount of accuracy. Mixed precision is the obvious move and it took me longer than it should have to try it, because "INT8 quantization" sounds like a single global switch.
#Where it landed
Final: 2.5 MB, inside the latency budget on device, accuracy within the 5% floor. Converted to TFLite for Android deployment.
The related ADAS work on a Raspberry Pi hit around 6 FPS for isolated detection inference and about 210 ms for the full perception-to-control loop, detection, lane detection, ultrasonic sensing and actuation.
That second number is the honest one and it is worth dwelling on. The model is one component. Camera capture, preprocessing, the detection pass, post-processing, sensor fusion and the control decision all sit in the loop. Optimising the model from 60 ms to 40 ms is invisible if preprocessing takes 90 ms, and preprocessing is exactly the part nobody profiles because it feels like it should be free.
Profile the loop before you optimise the model. The bottleneck is frequently something you did not think of as computation.
#The step I kept putting off: distillation
Pruning and quantization both take a trained model and remove something from it. They are subtractive, and subtraction has a floor. Past some point you are removing capacity the task genuinely needs, which is exactly what the 50% sparsity row shows.
Distillation is a different move. Instead of shrinking the big model, you train a small one to imitate it, using the large model's full output distribution as the target rather than the hard labels.
The reason this works better than training the small model directly is that the teacher's soft outputs carry more information per example. A hard label says "this is a pedestrian." The teacher's distribution says "this is a pedestrian, and it is somewhat cyclist-like, and not at all vehicle-like", which encodes something about the decision boundary that a one-hot label cannot. The student learns the shape of the problem, not just the answer.
I skipped it initially because it needs another full training loop and the pruning path was already meeting the budget. That was reasonable, and I think it was also the wrong long-term call, because the pruning path is now out of room. The next size reduction has nowhere to come from. 40% is the knee and INT8 is already applied. A distilled student architecture resets that ceiling entirely.
The order I would use next time:
- Train the best large model you can, ignoring deployment constraints entirely. This is the teacher and its only job is to be accurate.
- Distil into a student sized for the target device from the start.
- Prune the student, sweeping to find its knee.
- Quantize with a properly representative calibration set.
- Benchmark the full loop on hardware, under sustained load.
Subtractive methods applied to an architecture that was never meant for the device will get you a long way. They will not get you as far as starting from an architecture chosen for the constraint.
#What I would tell myself at the start
Set the three numbers first. Size, latency, accuracy floor. Without them you are not optimising, you are wandering.
Measure on the device, always. Desktop benchmarks of an edge model are a different experiment. Thermal throttling alone will change your numbers after the first few minutes of sustained inference, and that is the number that matters for anything running continuously.
Sweep, do not guess. The pruning knee is empirical. Three runs tells you more than any amount of reading.
The calibration set is part of the model. Treat it with the same care as the training set. It is not a formality, it directly sets the quantization ranges.
Compression amplifies existing weakness. Noisy labels, an underrepresented class, a distribution gap. All of them get worse. Fix the data first and the compression goes much further.