Introduction to Keras, Part Two: Data Preprocessing | by Samhita Alla | Towards Data Science

In the first part of this series, you’ve implemented various data loading techniques. You’ve seen how to load images, text, CSV files, and NumPy arrays into your Keras workspace.
Now to enable the model to make a rightful usage of your data, you’d have to convert it into an understandable format which could then be interpreted by a deep learning algorithm. To do this, you’d have to preprocess your data.
First import the keras library into your workspace.
from tensorflow import keras
Now you’ll how to preprocess various kinds of data. So here’s the challenge. Every section has the task and its concerned solution. Before you jump to the solution, try solving the given task; that way, you’d be able to understand the preprocessing techniques involved.
Convert text to numerical data — i.e., vectorize text such that every word is mapped to a number.
First things first. Fetch and download the IMDb data (or any text dataset) from an available data source.
Convert the given text directory into tf.data.Dataset by using the method text_dataset_from_directory().
batch_size = 32seed = 42raw_train_ds = keras.preprocessing.text_dataset_from_directory(    os.path.join(dataset_dir, "train"),    batch_size=batch_size,    validation_split=0.2,    subset="training",    seed=seed,)
On running this code, you get the following output:
Found 75000 files belonging to 3 classes.Using 60000 files for training.
You can set the hyperparameters in accordance with the dataset you’re fetching.
To vectorize the text, use the TextVectorization() method.
The max_features is the tentative count of the total number of words you assume to constitute the corpus and sequence_length specifies the length of every output sequence (this is where the padding happens).
Note: Setting the output_mode to binary in TextVectorization outputs one-hot encoded vectors.
Apply vectorize_layer on the corpus,
# Make a text-only dataset (without labels), then call adapttrain_text = raw_train_ds.map(lambda x, y: x)vectorize_layer.adapt(train_text)
and output the numerical representation obtained.
On running this code, you get the text and its encoded representation.
Review tf.Tensor(b'As I\'ve said before I\'m a cheapskate and I\'ll pretty much watch anything I rent but even I couldn\'t endure this piece of junk.I\'ve seen some terrible zombie films ...', shape=(), dtype=string)Label unsupVectorized review (<tf.Tensor: shape=(1, 250), dtype=int64, numpy=array([[  15,  199,  292,  163,  144,    4,    1,    3,  543,  181,   72,   104,  235,   11,  820,   19,   56,   11,  397, 4322,   10,  435, 5,   1,  112,   47,  381,   1038,   91,    8,   59,  150,    5, 148,  191,   91,    3,   10,   14,   29,    5,    2,  243,   5,   2,   1,  13,    2,   20,    7, 1442,  ...]])>, <tf.Tensor: shape=(), dtype=int32, numpy=2>)
Convert a list of sequences into equally padded sequences, all possessing the same length.
Initialize a random list of sequences and use tf.keras.preprocessing.sequence.pad_sequences to pad your sequences.
On running this code, you get the following output:
[[1 2 3][4 5 0][7 0 0]]
Center crop, rescale, and assign a random rotation to images procured from any dataset.
Fetch the images and convert it into tf.data.Dataset.
On running this code, you get the following output:
Downloading data from https://storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz228818944/228813984 [==============================] - 1s 0us/stepFound 3670 files belonging to 5 classes.
Perform the required operations and show the transformed images.
On running this code, you get a set of nine images.
Generate multiple versions of an image by using any data augmentation technique.
Use ImageDataGenerator class to shift an image, both horizontally and vertically.
On running this code, you get the following output (any random image could be picked):
Fetch CSV data and perform feature-wise normalization to plug the data into a normal distribution.
The tf.keras.layers.experimental.preprocessing.Normalization class helps in normalizing the dataset.
On running this code, you get the following output:
var: 1.0000mean: -0.0000
Altogether, here’s the full-fledged code:
Data Preprocessing is a significant step that helps in processing the data in accordance with the network you’d want to build. It helps in proceeding with any kind of data, without actually worrying about its compatibility with the model.
In this article, you learned how to preprocess text, images, and CSV data.
In the next part of this series, you shall learn to build models using the Keras API.
If you think any other preprocessing method is to be included, let me know in the comments. Thanks for reading!