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.
- 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 image, text, and sequence data using keras. To use it, we need to import the module by using the import keyword.
Keras Preprocessing Function
The function will vary as per image, text, and time series data. The below example shows the keras image dataset from the directory function as follows.
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 )
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.
tf.keras.preprocessing.image.load_img ( path, target_size = None, color_mode = "rgb", grayscale = False )
Below is an example of preprocessing the image_to_array function. This function is used to convert the instance of PIL into numpy array as follows.
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)
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.
tf.keras.preprocessing.timeseries_dataset_from_array ( data, targets, sequence_length, sequence_stride = 2, sampling_rate = 1, end_index = None, )
The below example shows the preprocessing pad sequence function as follows. This function is used to pad the sequence.
tf.keras.preprocessing.sequence.pad_sequences ( sequences, dtype = "int32", maxlen = None, padding = "pre", value = 0.0 )
Keras Preprocessing Layers
The layer is allowing developers to build the pipeline of native input. The input processing pipeline is used in the code of independent preprocessing. By using 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 that was 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 into the encoded representation which was read from the dense layer. The layer integer lookup will tune integer categorical values into the encoded representation which was 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 the random augmentation which will be transforming the batch of images. In the below example we are defining the keras layers as follows.
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()
Keras Preprocessing Data Model
Basically preprocessing dataset model utility is located at keras preprocessing. To use the preprocessing model we need to import the below module as follows. Basically, we need to import the image dataset from the directory and keras modules as follows.
from tensorflow import keras from tensorflow.keras.preprocessing import image_dataset_from_directory
For preprocessing the data model we need to define the training dataset in the below example we are defining the training dataset as follows.
t_ds = image_dataset_from_directory ( directory = 'keras', batch_size = 32, image_size = (256, 256))
For preprocessing the data model we need to define the validation dataset in the below example we are defining the validation dataset as follows.
v_ds = image_dataset_from_directory ( directory='keras', batch_size=32, image_size=(256, 256))
For preprocessing the data model we need to define the dataset model in the below example we are defining the dataset model as follows.
mod = keras.applications.Xception (weights = None, classes=10) mod.fit(t_ds, epochs = 10, keras = v_ds)
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:
The below example shows text data preprocessing as follows. Here we are providing all arguments as follows.
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, )
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 keras preprocessing layer?
Answer: Basically keras will come with multiple neural networks, such as the convolution layers which we need to define in the training model.
Q3. Which module do we require while using keras preprocessing?
Answer: We need to import the tensorflow and keras module at the time of working with keras preprocessing. The Tensorflow module is important in keras preprocessing.
There are modules for keras and tensorflow augmentation. We can do the preprocessing of image, text, and sequence data by using keras preprocessing. It is the utility that was located at tf.keras preprocessing module, we are using tf.data dataset object for training the model.
This is a guide to Keras Preprocessing. Here we discuss the introduction, keras preprocessing function, layers, data model, and FAQ. You may also have a look at the following articles to learn more –