Static int8 passed every benchmark and still lost a shadowed window once tiled; fp16 kept it

I wanted the smallest possible model in an Android app, so I shipped the static int8 export: 3.7 MB instead of 6.5 MB for fp16, and faster on CPU. On my two test sets it was within about 0.01 mIoU of fp16. Then I switched the app to tiled inference, and on a real night photo of a house the int8 model lost a window sitting in a tree’s shadow. The fp16 model, on the same tiles, found it.

TL;DR — Static int8 quantisation can look lossless on aggregate metrics and still fail on the hardest real input, especially when you change how inputs are presented (crops instead of whole images). Recalibrating with min-max on tiles and darkened images did not fix it, and percentile calibration was OOM-killed at a 5 GB cap. If one missing object matters more than 2.8 MB, ship fp16.

The setup

The model is a 6-class facade segmentation network (background, wall, roof, window, door, shutter), LR-ASPP with a MobileNetV3 backbone, exported to ONNX with a fixed 512×512 input. Three exports of the same weights:

Export Size CPU, 4 threads
fp32 12.9 MB 15 ms
fp16 (fp32 inputs and outputs) 6.5 MB 13 ms
int8, static QDQ, per-channel 3.7 MB 10 ms

Static quantisation needs calibration images: the tool runs them through the float model, records the range of every activation, and fixes an int8 scale and zero point per tensor. Tiled inference means running the model on the whole image plus about six overlapping crops and averaging the probabilities; it raised shutter recall from 44 % to 76 %.

The night photo is a house lit by a projector, with four windows I had boxed by hand. Window 2 sits in the shadow of a tree.

What I expected

On whole images the first int8 export kept 95.9 % of labels identical to fp32 and lost 0.009 test mIoU. I assumed tiling would not change that: same model, smaller crops.

What actually happens

Per-window IoU on a real night photo with tiled inference: fp16 0.89, 0.54, 0.61, 0.75; int8 min-max 0.86, 0.00, 0.62, 0.70; the window in the tree shadow drops from 0.54 to 0 in int8

Night photo, 4 windows Global pass only Global + tiles
fp16 4 found, window 2 at 0.66 4 found, 0 false positives
int8, first calibration (40 whole images) 3 found + 1 partial 3 found, window 2 at 0.03, 3 false positives
int8, recalibrated min-max (tiles + darkened) 4 found, window 2 at 0.51 3 found, window 2 at 0.00, 1 false positive
int8 min-max, 640 px model 3 found, window 2 at 0.09 3 found, window 2 at 0.03, 7 false positives

Meanwhile, on the aggregate metrics, the recalibrated int8 model looked fine. With tiling, its mIoU on 25 shutter photos was 0.753 against 0.761 for fp16. Shutter recall and precision were 0.75 / 0.75 against 0.76 / 0.76. Nothing in those tables warned me.

What I tried

The first int8 model was calibrated on 40 whole training images, a third of them darkened. Tiled inference feeds crops whose statistics differ: a crop of a shadowed corner is darker and flatter than any whole image. So I recalibrated on whole images and random half-size crops, from both photo collections, 40 % darkened:

for k, i in enumerate(pick):
    img, msk = load_pair(i, long_side=1024)
    if k % 5 < 2:
        img = darken_day(img, msk, seed=k)          # simulated night
    xs.append(to_tensor(letterbox(img)[0]).unsqueeze(0).numpy())
    h, w = img.shape[:2]; s = min(h, w) // 2         # plus one random tile
    y, x = rng.integers(0, h - s + 1), rng.integers(0, w - s + 1)
    xs.append(to_tensor(letterbox(img[y:y + s, x:x + s])[0]).unsqueeze(0).numpy())

quantize_static(pre, out, Reader(xs), quant_format=QuantFormat.QDQ, per_channel=True,
                activation_type=QuantType.QUInt8, weight_type=QuantType.QInt8,
                calibrate_method=CalibrationMethod.MinMax)

That helped the global pass (window 2 back to 0.51), but under tiling the window still vanished. My guess at the cause: min-max takes the extreme value of each activation, so a few bright pixels, such as a projector’s hot spot, stretch the range, and dim values then share very few int8 steps. Percentile calibration (clip at the 99.99th percentile) is the textbook answer. In a systemd-run unit capped at 5 GB of RAM, it was OOM-killed twice: first with 60 images plus 60 tiles, then with 16 plus 16. Both times it peaked at 5 GB within 30–40 seconds. Min-max on 120 inputs finished without trouble under the same cap.

The fix

There is no clever fix here, only a decision. fp16 halves the size of fp32 with labels 99.9 % identical, and the 512 px fp16 model found all four night-photo windows both with and without tiles, with no false positive. The app plan is now: embed the recalibrated int8 only if it matches fp16 on the hard cases, otherwise fp16 if the size budget allows. On this evidence, that means fp16, 2.8 MB larger.

The evaluation lesson is the transferable one. Keep a small set of hard, real inputs (here one night photo with a hand-boxed shadowed window) and check them per object, under the exact inference pipeline you ship. Aggregate mIoU over 25 daytime photos averaged the failure away.

Why it works

fp16 keeps about three significant decimal digits at any magnitude, so a weak but correct activation in a dark crop survives. Static int8 uses one fixed scale per tensor, chosen from calibration data. A shadowed window in a dark tile produces small activations that fall into a handful of quantisation steps. A logit that was narrowly ahead in float can then fall behind « wall ». The global pass sees the window in a brighter overall context, which is why the global-only int8 result was fine and the tiled one was not. This is my reading of the numbers; I did not inspect per-layer activation ranges.

What I did not test

One real night photo, four windows: this is a case study, not a statistic. I did not try entropy calibration, quantisation-aware training, keeping the first and last layers in float, or dynamic quantisation. I did not measure how much memory percentile calibration would actually need. The timings come from a desktop Ryzen CPU, not a phone.

The models, in fp16 and int8, are being released under CC BY-NC 4.0 on a public model page that is being set up now. Caveat: the backbone was initialised from torchvision’s ImageNet weights, and ImageNet’s terms are for non-commercial research.

Facts

context: Shipping a 512 px ONNX segmentation model on Android CPU with tiled inference
problem: Static int8 (QDQ, per-channel, min-max) matched fp16 on aggregate metrics but lost a shadowed window on a real night photo once tiled; percentile calibration was OOM-killed at 5 GB
solution: Ship fp16 (6.5 MB vs 3.7 MB) and validate per object on hard real inputs under the exact shipped pipeline
verified_on: 2026-10-03
applies_to: [onnxruntime quantize_static, CNN segmentation models, tiled or cropped inference]
does_not_apply_to: [quantisation-aware training, dynamic quantisation, models evaluated only on whole images]

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *

Retour en haut