Skip to content

Image Preprocessing

Define an image preprocessing pipeline to extract features for training your model. Pass the same pipeline to dg.export and it compiles directly into your model binary, so deployed inference runs on raw sensor frames with no preprocessing mismatch.

For complete working examples, see the Tutorials page.

from dg import preprocess

pipe = preprocess.ImagePipeline([
    preprocess.Crop(192, 192),
    preprocess.Downsample(factor=2),
    preprocess.Grayscale(),
], height=240, width=320, channels=3)

# Extract features once (sensor_frames: uint8 [batch, 240, 320, 3] in HWC layout)
features = pipe.features(sensor_frames, batch_size=2048)

# Train your model
model = MyImageModel()
qmodel = dg.enable_quantization(model, (features[:2],))
# ... train / calibrate ...
quantized = dg.freeze_quantization(qmodel)

# Export with preprocessing
schema = dg.export(quantized, preprocess=pipe)

batch_size controls how many samples process per call; lower it if memory is tight.

Available transformations

Image preprocessing pipelines typically include the following steps. See the API reference for all available steps and their parameters:

  • Cropping – region-of-interest extraction
  • Downsampling – integer-factor box average
  • Grayscale conversion – weighted channel sum (BT.601 luma)