EDUCBA

EDUCBA

MENUMENU
  • Blog
  • Free Courses
  • All Courses
  • All in One Bundle
  • Login
Home Data Science Data Science Tutorials Keras Tutorial Keras Preprocessing

Keras Preprocessing

Keras Preprocessing

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

  • 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.

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

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.

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:

Keras Preprocessing - dataset

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:

Keras Preprocessing - Image loading

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.

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:

Keras Preprocessing - Converting

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:

Keras Preprocessing - Timeseries

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:

pad sequence function

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.

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:

Image data augmentation

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.

Code:

from tensorflow import keras
from tensorflow.keras.preprocessing import image_dataset_from_directory

Output:

Keras Preprocessing 7

For preprocessing the data model we need to define the training dataset in the below example 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:

define the training dataset

For preprocessing the data model we need to define the validation dataset in the below example 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:

validation dataset

For preprocessing the data model we need to define the dataset model in the below example 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 10

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:

Keras Preprocessing 11

FAQ

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.

Conclusion

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.

Recommended Articles

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 –

  1. Keras Tuner
  2. What is Keras GPU?
  3. Keras Embedding
  4. Keras predict
EVIEWS Training
32+ Hours of HD Videos
11 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5
MYSQL Certification Course
115+ Hours of HD Videos
18 Courses
3 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
CLOUD COMPUTING Certification Course
141+ Hours of HD Videos
23 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5
SPLUNK Training Program
70+ Hours of HD Videos
11 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5
Primary Sidebar
Popular Course in this category
KERAS Training
 40+ Hour of HD Videos
10 Courses
Verifiable Certificate of Completion
  Lifetime Access
4.5
Price

View Course
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more