Updated June 20, 2023
Introduction to Keras Preprocessing
Keras preprocessing is the utility that was located at tf.keras preprocessing module; we are using the tf.data dataset object for training the model. It is a data augmentation and preprocessing module for the Keras library of deep learning. This module provides the utility to work with text, image, and sequence data. We can import it directly from the up-to-date installation of Keras.
Key Takeaways
- The preprocessing layer is specifically designed for using early stages in the neural network. We can use image preprocessing, such as resizing for rotating the image to adjust the contrast.
- When preprocessing, the layer is supported for larger neural network functions.
What is Preprocessing?
In the preprocessing layer, API allows developers to build the processing pipeline of Keras native input. We can use these pipelines to preprocess independent code into the non-keras workflow. We can combine the same direction with the models. When working with ML programs related to the images, we need to collect the specified images for training the data.
There are many ways to do preprocessing. We can use the external libraries or write our function for the same. There is a module for Keras and TensorFlow augmentation. We can preprocess images, text, and sequence data using Keras. We need to import the module by using the import keyword to use it.
Keras Preprocessing Function
The function will vary per image, text, and time series data. The below example shows the Keras image dataset from the directory function as follows.
Code:
tf.keras.preprocessing.image_dataset_from_directory (
directory,
labels = "inferred",
label_mode = "int",
class_names = None,
color_mode = "rgb",
batch_size = 32,
image_size = (256, 256),
shuffle = True,
crop_to_aspect_ratio = False,
**kwargs
)
Output:
The preprocessing load_img function is used to load the image. We are loading the image in PIL format. The below example shows preprocessing load_img function as follows.
Code:
tf.keras.preprocessing.image.load_img (
path,
target_size = None,
color_mode = "rgb",
grayscale = False
)
Output:
Below is an example of preprocessing the image_to_array function. This function converts the instance of PIL into a numpy array as follows.
Code:
from PIL import Image
img = np.random.random (size=(250, 250, 3))
img1 = tf.keras.preprocessing.image.array_to_img (img)
arr = tf.keras.preprocessing.image.img_to_array (img1)
Output:
The below example shows that preprocessing timeseries dataset from array functions as follows. This function will create a dataset of sliding windows at the time when timeseries are provided from an array.
Code:
tf.keras.preprocessing.timeseries_dataset_from_array (
data,
targets,
sequence_length,
sequence_stride = 2,
sampling_rate = 1,
end_index = None,
)
Output:
The below example shows the preprocessing pad sequence function as follows. This function is used to pad the sequence.
Code:
tf.keras.preprocessing.sequence.pad_sequences (
sequences,
dtype = "int32",
maxlen = None,
padding = "pre",
value = 0.0
)
Output:
Keras Preprocessing Layers
The layer allows developers to build the pipeline of native input. The input processing pipeline is used in the code of independent preprocessing. By using a layer of it, we can build the export model. Below is the available preprocessing layer as follows.
1. Text Preprocessing
The layer text vectorization will turn a raw string into the representation which was encoded and which was read by using a dense layer or embedding layer.
2. Numerical Feature Preprocessing
The layer of normalization will perform input feature-wise normalization. The discretization layer will turn continuous numerical features into the features of integers.
3. Categorical Feature Preprocessing
The layer of category encoding will turn categorical features into an encoded representation based on dense representation. The layer of hashing performs categorical hashing, which is known as that trick of hashing.
The layer of string lookup contains the categorical values in the encoded representation, which was read from the dense layer. The layer integer lookup will tune integer categorical values into the encoded representation ready in the dense layer.
4. Image Preprocessing
The layer resizing will resize the batch of images from the target size. The layer rescaling will rescale the offset values for the batch images. The layer of the center crop will return to the center crop of the image batch.
5. Image Data Augmentation
This layer will apply random augmentation, transforming the image batch. In the below example, we are defining the Keras layers as follows.
Code:
out_height, out_width = 128, 256
size = tf.keras.layers.Resizing(out_height, out_width)
fig, ax = plt.subplots(2, 3, figsize=(6,4))
for images, labels in ds.take(1):
for i in range(3):
…..
plt.show()
Output:
Keras Preprocessing Data Model
Preprocessing dataset model utility is located at Keras preprocessing. To use the preprocessing model, we must import the following module. We must import the image dataset from the directory and Keras modules.
Code:
from tensorflow import keras
from tensorflow.keras.preprocessing import image_dataset_from_directory
Output:
We need to define the training dataset in the example below to preprocess the data model. We are defining the training dataset as follows.
Code:
t_ds = image_dataset_from_directory (
directory = 'keras',
batch_size = 32,
image_size = (256, 256))
Output:
We need to define the validation dataset in the example below to preprocess the data model. We are defining the validation dataset as follows.
Code:
v_ds = image_dataset_from_directory (
directory='keras',
batch_size=32,
image_size=(256, 256))
Output:
We need to define the dataset model in the example below to preprocess the data model. We are defining the dataset model as follows.
Code:
mod = keras.applications.Xception (weights = None, classes=10)
mod.fit(t_ds, epochs = 10, keras = v_ds)
Output:
Keras Preprocessing with Text
For the text data preprocessing, we are using the text dataset from the directory function. This function will generate the tf.Data.Dataset from the text files of the specified directory. The text dataset from the directory function will create tf dataset.
While using the preprocessing with text, we are using the below parameters as follows:
- Labels
- Directory
- Label_mode
- Class_names
- Max_length
- Batch_size
- Shuffle
- Seed
- Validation_split
- Subset
- Follow_links
The below example shows text data preprocessing as follows. Here we are providing all arguments as follows.
Code:
tf.keras.preprocessing.text_dataset_from_directory (
directory = "keras",
labels = "inferred",
label_mode = "int",
class_names = None,
batch_size = 32,
shuffle = True,
seed = None,
follow_links = False,
)
Output:
FAQs
Given below are the FAQs mentioned:
Q1. What is the use of Keras preprocessing?
Answer: It is nothing but the data preprocessing and module of data augmentation into the Keras library of deep learning. It will provide the working utilities.
Q2. What is the use of the Keras preprocessing layer?
Answer: Keras will come with multiple neural networks, such as the convolution layers we must define in the training model.
Q3. Which module do we require while using Keras preprocessing?
Answer: When working with Keras preprocessing, we must import the TensorFlow and Keras modules. The Tensorflow module is important in Keras preprocessing.
Conclusion
There are modules for keras and TensorFlow augmentation. We can preprocess images, text, and sequence data using Keras. It is the utility located at tf.keras preprocessing module; we are using the tf.data dataset object to train the model.
Recommended Articles
This is a guide to Keras Preprocessing. Here we discuss the introduction, keras preprocessing function, layers, data model, and FAQs. You may also have a look at the following articles to learn more –