ml.preprocessing — ML Preprocessing¶
The ml.preprocessing module contains classes for preprocessing images for use with
machine learning models.
class Normalization – Image Normalization¶
The Normalization object converts image.Image objects into ndarray input tensors
for use with ml.Model.predict(). It is automatically created by the ml.Model object
when an image is passed to ml.Model.predict(), but may be instantiated manually to
control the conversion (scale, mean/stdev, ROI).
- class ml.preprocessing.Normalization(scale: tuple[float, float] = (0.0, 1.0), mean: tuple[float, float, float] = (0.0, 0.0, 0.0), stdev: tuple[float, float, float] = (1.0, 1.0, 1.0), roi: tuple[int, int, int, int] = None)¶
Creates a
Normalizationobject.scaleis the(min, max)range of values that floating-point input tensors expect after normalization (e.g.(0.0, 1.0)or(-1.0, 1.0)). Ignored foruint8andint8input tensors.meanis the per-channel mean(R, G, B)subtracted from the image after scaling. For grayscale tensors the mean is reduced to a single luma value using0.299*R + 0.587*G + 0.114*B. Ignored foruint8andint8input tensors.stdevis the per-channel standard deviation(R, G, B)the image is divided by after the mean is subtracted. For grayscale tensors the stdev is reduced to a single luma value using0.299*R + 0.587*G + 0.114*B. Ignored foruint8andint8input tensors.roiis an optional(x, y, w, h)region of interest within the input image to crop. IfNone, the full image is used. The cropped region is centered, bilinearly scaled (preserving aspect ratio with black padding) to the model’s input tensor dimensions.- __call__(image: image.Image) Normalization¶
- __call__(buffer: bytearray, shape: tuple[int, int, int, int], dtype: int) None
When called with a single
image.Imageargument, returns a newNormalizationobject bound to that image. The bound object is whatml.Model.predict()invokes internally to fill the input tensor. Ifroiwas not set on the original instance, it is initialized to the full image size.When called with
(buffer, shape, dtype), fillsbufferin-place with the normalized input tensor for the previously bound image.shapemust be(1, H, W, C)withCequal to1(grayscale) or3(RGB).dtypeis the ulab numpy type code (e.g.ord('f')for float32,ord('b')for int8,ord('B')for uint8). Float tensors applyscale,mean, andstdev; integer tensors are written directly (with an offset forint8).