Authors: Francois Chollet, Mark Omernick
View on TensorFlow.org
|
Run in Google Colab
|
View source on GitHub
|
View on keras.io
|
Keras preprocessing
The Keras preprocessing layers API allows developers to build Keras-native input processing pipelines. These input processing pipelines can be used as independent preprocessing code in non-Keras workflows, combined directly with Keras models, and exported as part of a Keras SavedModel.
With Keras preprocessing layers, you can build and export models that are truly end-to-end: models that accept raw images or raw structured data as input; models that handle feature normalization or feature value indexing on their own.
Available preprocessing
Text preprocessing
tf.keras.layers.TextVectorization: turns raw strings into an encoded representation that can be read by anEmbeddinglayer orDenselayer.
Numerical features preprocessing
tf.keras.layers.Normalization: performs feature-wise normalization of input features.tf.keras.layers.Discretization: turns continuous numerical features into integer categorical features.
Categorical features preprocessing
tf.keras.layers.CategoryEncoding: turns integer categorical features into one-hot, multi-hot, or count dense representations.tf.keras.layers.Hashing: performs categorical feature hashing, also known as the "hashing trick".tf.keras.layers.StringLookup: turns string categorical values into an encoded representation that can be read by anEmbeddinglayer orDenselayer.tf.keras.layers.IntegerLookup: turns integer categorical values into an encoded representation that can be read by anEmbeddinglayer orDenselayer.
Image preprocessing
These layers are for standardizing the inputs of an image model.
tf.keras.layers.Resizing: resizes a batch of images to a target size.tf.keras.layers.Rescaling: rescales and offsets the values of a batch of images (e.g. go from inputs in the[0, 255]range to inputs in the[0, 1]range.tf.keras.layers.CenterCrop: returns a center crop of a batch of images.
Image data augmentation
These layers apply random augmentation transforms to a batch of images. They are only active during training.
tf.keras.layers.RandomCroptf.keras.layers.RandomFliptf.keras.layers.RandomTranslationtf.keras.layers.RandomRotationtf.keras.layers.RandomZoomtf.keras.layers.RandomContrast
The adapt() method
Some preprocessing layers have an internal state that can be computed based on a sample of the training data. The list of stateful preprocessing layers is:
TextVectorization: holds a mapping between string tokens and integer indicesStringLookupandIntegerLookup: hold a mapping between input values and integer indices.Normalization: holds the mean and standard deviation of the features.Discretization: holds information about value bucket boundaries.
Crucially, these layers are non-trainable. Their state is not set during training; it must be set before training, either by initializing them from a precomputed constant, or by "adapting" them on data.
You set the state of a preprocessing layer by exposing it to training data, via the
adapt() method:
import numpy as np
import tensorflow as tf
from tensorflow import keras
View on TensorFlow.org
Run in Google Colab
View source on GitHub
View on keras.io