Image preprocessing in Tensorflow

Akshaikp
3 min readOct 13, 2020

--

The input data should be preprocessed for achieving a good result from the model. It is the process of making the data clean and making it suitable for the model. One of the common image preprocessing method is data augmentation. It will help us to make the dataset larger using the existing data.

In Tensorflow, we can make use of ImageDataGenerator (tf.keras.preprocessing.image.ImageDataGenerator)for preprocessing our image data. The ImageDataGenerator can be imported using the following code.

from tensorflow.keras.preprocessing.image import ImageDataGenerator

One of the first step is to normalize the data. Normalization is the process of making the data values to a common scale. In our case, we will preprocess our images by normalizing the pixel value to be in the 0–1 range(Originally all values are in the range 0–255).

from tensorflow.keras.preprocessing.image import ImageDataGeneratortrain_datagen = ImageDataGenerator( rescale = 1.0/255. )test_datagen  = ImageDataGenerator( rescale = 1.0/255. )

some of the other augmentations that can be done using ImageDataGenerator() are:

  • rotation_range: Degree range for random rotations.
tf.keras.preprocessing.image.ImageDataGenerator(rotation_range=0
  • brightness_range: Range for picking a brightness shift value from.
tf.keras.preprocessing.image.ImageDataGenerator(brightness_range=None)
  • shear_range: Shear Intensity (Shear angle in counter-clockwise direction in degrees)
tf.keras.preprocessing.image.ImageDataGenerator(shear_range=0.0)
  • zoom_range: Range for random zoom.
tf.keras.preprocessing.image.ImageDataGenerator(zoom_range=0.0)
  • fill_mode: Points outside the boundaries of the input are filled according to the given mode (“constant”, “nearest”, “reflect” or “wrap”). The default value is ‘nearest’.
tf.keras.preprocessing.image.ImageDataGenerator(fill_mode='nearest')
  • horizontal_flip: Randomly flip inputs horizontally.
tf.keras.preprocessing.image.ImageDataGenerator(horizontal_flip=True)
  • vertical_flip: Randomly flip inputs vertically.
tf.keras.preprocessing.image.ImageDataGenerator(vertical_flip=True)
  • validation_split: Fraction of images that are to be reserved for validation. The value should be between 0–1.
tf.keras.preprocessing.image.ImageDataGenerator(validation_split=0.0)

Sample code

train_datagen = ImageDataGenerator(
rescale=1./255,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True)
test_datagen = ImageDataGenerator(rescale=1./255)
train_generator = train_datagen.flow_from_directory(
'data/train',
target_size=(150, 150),
batch_size=32,
class_mode='binary')
validation_generator = test_datagen.flow_from_directory(
'data/validation',
target_size=(150, 150),
batch_size=32,
class_mode='binary')

In the above code, we created the train_datagen using the imageDatagenerator with the aumentations such as rescale, shear,zoom and horizontal flip. The test_datagen have only rescale augmentation as it is not used for training the model. The training and test set is generated using the flow_from_direcory() which generates a tf.data.Dataset from image files in a directory. The training set is generated from the train directory and the validation set from the validation directory.

Data pre-processing and data augmentation of cat vs Dog dataset

Image classification models require a larger amount of data to train the model. If the dataset is very small, we can make it larger by some random transformations on the images. This will prevent overfitting and helps the model to generalize better.

ImageDataGenerator class in Keras helps us to perform random transformations and normalization operations on the image data during training. The .flow(data, labels) or .flow_from_directory(directory) instantiate generators of augmented image batches.

In this example, we will use the Cat vs Dog dataset from Kaggle. The dataset contains 25,000 images of dogs and cats.

Link to the dataset: https://www.kaggle.com/c/dogs-vs-cats/data

datagen = ImageDataGenerator(
rotation_range=40,
width_shift_range=0.2,
height_shift_range=0.2,
rescale=1./255,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True,
fill_mode='nearest')

Let’s prepare our data. We will use .flow_from_directory() to generate batches of image data (and their labels) directly from our jpgs in their respective folders.

batch_size = 16

train_datagen = ImageDataGenerator(
rescale=1./255,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True)

test_datagen = ImageDataGenerator(rescale=1./255)

train_generator = train_datagen.flow_from_directory(
'data/train',
target_size=(150, 150),
batch_size=batch_size,
class_mode='binary')

validation_generator = test_datagen.flow_from_directory(
'data/validation',
target_size=(150, 150),
batch_size=batch_size,
class_mode='binary')

We can now use these generators to train our model.

model.fit_generator(
train_generator,
steps_per_epoch=2000 // batch_size,
epochs=50,
validation_data=validation_generator,
validation_steps=800 // batch_size)

--

--