Skip to course content
Free computer vision course

Computer Vision and Multimodal AI

Unit 06.03: Preprocessing that must match the pretrained model

Preprocessing is part of the model, not part of your data loader.

Scale, order and normalisation constants must match

The same pixel, prepared three ways: correctly, unnormalised, and at the wrong scale.

The code shows all three.

import numpy as np

MEAN = np.array([0.485, 0.456, 0.406])
STD = np.array([0.229, 0.224, 0.225])

pixel = np.array([200, 180, 40]) / 255.0
correct = (pixel - MEAN) / STD
wrong_no_norm = pixel
wrong_scale = np.array([200, 180, 40]) - MEAN * 255

print(f"{'variant':22} {'values':32}")
print(f"{'correct':22} {np.round(correct, 2)}")
print(f"{'forgot normalisation':22} {np.round(wrong_no_norm, 2)}")
print(f"{'wrong scale':22} {np.round(wrong_scale, 2)}")

print("\nall three run. Only the first matches what the model was trained on.")

# Preprocessing is part of the model, not part of your loader. Resize method,
# scale, channel order and normalisation constants all have to match the
# pretrained checkpoint, and none of them raises when it does not.

All three run. All three produce predictions. Only the first matches the distribution the pretrained weights were fitted to, and the other two are silently degraded.

The specific constants matter - they are properties of the checkpoint, not general truths - and so does the resize method, the channel order and whether values are scaled to 0-1 before normalising.

The mistake this prevents

The mistake is writing the preprocessing from memory. Take it from the checkpoint's own documentation or its provided transform, and assert one known input produces a known output before training anything.

Takeaway

Preprocessing belongs to the checkpoint. Scale, channel order and normalisation constants must match exactly, and a mismatch degrades silently.