Principle:Tensorflow Tfjs Data Preprocessing Layers
| Knowledge Sources | |
|---|---|
| Domains | Deep_Learning, Preprocessing, Data_Augmentation |
| Last Updated | 2026-02-10 06:00 GMT |
Overview
Model-integrated preprocessing and data augmentation layers that transform raw inputs (images, categories) into model-ready representations as part of the computation graph.
Description
Preprocessing layers embed data transformation logic directly into the model graph, ensuring consistent preprocessing between training and inference. TensorFlow.js implements:
Image preprocessing:
- Rescaling: Scales pixel values by a factor (e.g., 1/255 to normalize to [0,1])
- Resizing: Resizes images to a target height and width using bilinear interpolation
- CenterCrop: Crops images from the center to a target size
Data augmentation (training only):
- RandomHeight: Randomly adjusts image height within a factor range
- RandomWidth: Randomly adjusts image width within a factor range
Categorical encoding:
- CategoryEncoding: Converts integer category indices to one-hot, multi-hot, or count encodings
Augmentation layers are active only during training (controlled by the training flag) and pass data through unchanged during inference.
Usage
Use preprocessing layers as the first layers of your model to create self-contained models that handle their own data transformation. This eliminates the need for separate preprocessing pipelines and ensures the model can be deployed with raw input data.
Theoretical Basis
Pseudo-code Logic:
# Preprocessing pipeline as model layers:
model = tf.sequential([
tf.layers.rescaling({scale: 1/255}), # Normalize pixels
tf.layers.resizing({height: 224, width: 224}), # Resize to model input
tf.layers.conv2d({filters: 32, kernelSize: 3}),
# ... rest of model
])
# During training, augmentation is active:
augmented = randomHeight.call(image, {training: true}) # Randomly resized
# During inference, augmentation is bypassed:
original = randomHeight.call(image, {training: false}) # Unchanged