liveBook · Manning

Preprocessing

As explained in Quantization and Fast Inference

Image preprocessing is the transformation of an image before model execution, such as resizing or normalizing its pixel values.

Resize and normalize images before model execution. In a measured on-device pipeline, resize took 0.809 ms and normalization took 0.705 ms per image; together with decoding, these stages accounted for 82.3% of the per-image work. section 12.2

EfficientNet-Lite0 on M3

The pipeline decodes JPEG bytes to RGB, resizes to 224×224 with bilinear interpolation, then normalizes to 1CHW using ImageNet mean and standard deviation. Decode averaged 4.205 ms, compared with 0.809 ms for resize and 0.705 ms for normalization. section 12.2

Listing 12.4 Timing each stage and the end-to-end path
import time
import numpy as np
from dataclasses import dataclass
from _common import percentile           # shared helper, lives next to the script
 
@dataclass
class StageTimes:
    decode: float = 0.0
    resize: float = 0.0
    normalize: float = 0.0
    infer: float = 0.0
    post: float = 0.0
    def total(self) -> float:
        return self.decode + self.resize + self.normalize + self.infer + self.post
 
def run_once(predict, path):
    t = StageTimes()
    t0 = time.perf_counter()
    img = _decode(path);     t1 = time.perf_counter()   # JPEG bytes -> RGB
    img = _resize(img);      t2 = time.perf_counter()   # 224x224 bilinear
    x = _normalize(img);     t3 = time.perf_counter()   # 1CHW, ImageNet mean/std
    logits = predict(x);     t4 = time.perf_counter()   # Core ML forward pass
    _ = _post(logits);       t5 = time.perf_counter()   # argmax
    t.decode    = (t1 - t0) * 1000.0
    t.resize    = (t2 - t1) * 1000.0
    t.normalize = (t3 - t2) * 1000.0
    t.infer     = (t4 - t3) * 1000.0
    t.post      = (t5 - t4) * 1000.0
    return t                                            #A
 
predict = _build_predictor("coreml", model_path)        #B
for p in paths[:warmup]:                                #C
    run_once(predict, p)
samples = [run_once(predict, p) for p in paths[warmup:warmup + n]]
 
stage_mean = {f: float(np.mean([getattr(s, f) for s in samples]))
              for f in ("decode", "resize", "normalize", "infer", "post")}
e2e_p95 = percentile([s.total() for s in samples], 0.95)  #D

See how the pipeline times decode, resize, normalization, inference, and postprocessing separately, then aggregates stage timings. section 12.2

How Quantization and Fast Inference covers it

How deep it goes
Measures decode, resize, and normalization separately, without detailing their implementation internals.
Examples
Uses JPEG-to-RGB decoding, 224×224 bilinear resizing, and ImageNet mean-and-standard-deviation normalization.
What you can do after
Diagnose latency by timing preprocessing stages alongside inference and end-to-end execution.
How it connects
Connects image resizing and normalization with image decoding and Core ML inference.

Also explained in

Compare how 25 books explain preprocessing →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Quantization and Fast Inference. The section links point to the parts of the book it draws on.