Deep Learning : Day 3
Courtesy : https://www.tensorflow.org/tutorials/images/classification
Quick recap
• Upload the data set
• Pre-processing
- Feature engineering
- Fill in missing data
- Encode Categorical & Scale continuous variables
• Build, Train and Evaluate the Model
- Create the Model
- Compile the Model
- Train the Model
- Evaluate the Model
- Predict the Model
High level steps for image
classification
• Upload/Download the data set
• Pre-processing
- Split the dataset
- Configure the dataset to improve performance
- Standardize the data
• Build, Train and Evaluate the Model
- Create the Model
- Compile the Model
- Train the Model
- Evaluate the Model
- Apply techniques to improve the model accuracy
- Re-compile and re-train the model
- Predict the Model
Step 1 – Import all required libraries
import matplotlib.pyplot as plt
import numpy as np
import os
import PIL
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
from tensorflow.keras.models import Sequential
Step 2 – Download and explore the
dataset
import pathlib
dataset_url = "https://storage.googleapis.com/download.tensorflow.org/example_images/
flower_photos.tgz"
data_dir = tf.keras.utils.get_file('flower_photos', origin=dataset_url, untar=True)
data_dir = pathlib.Path(data_dir)
image_count = len(list(data_dir.glob('*/*.jpg')))
print(image_count)
Step 3 – Pre-process the data
Images come in different shapes and sizes. They also come through different
sources.
• A picture of a flower is a natural image
• An X-ray image is not a natural image
Few pre-processing techniques are
• Resize image
• Remove noise(Degradation in image signal caused by external sources)
• Segmentation (Segment the image, separating the background from foreground
objects)
• Morphology(Smoothing edges, remove imperfections from the structure of an
image)
Split the dataset (training &
validation)
• Load these images off disk using the helpful image_dataset_from_directory
utility.
train_ds = tf.keras.preprocessing.image_dataset_from_directory(
data_dir,
validation_split=0.2,
subset="training",
seed=123, # Optional random seed for shuffling and transformations.
image_size=(img_height, img_width),
batch_size=batch_size)
…
val_ds = tf.keras.preprocessing.image_dataset_from_directory(
data_dir,
validation_split=0.2,
subset="validation",
seed=123,
image_size=(img_height, img_width),
batch_size=batch_size)
Configure the dataset for
performance
Dataset.cache() keeps the images in memory after they're loaded off disk during
the first epoch. This will ensure the dataset does not become a bottleneck while
training your model. If your dataset is too large to fit into memory, you can also use
this method to create a performant on-disk cache.
Dataset.prefetch() - transformation uses a background thread and an internal
buffer to prefetch elements from the input dataset ahead of the time they are
requested.
AUTOTUNE = tf.data.experimental.AUTOTUNE # Tune the value dynamically at
runtiume
train_ds = train_ds.cache().shuffle(1000).prefetch(buffer_size=AUTOTUNE)
val_ds = val_ds.cache().prefetch(buffer_size=AUTOTUNE)
Standardize the data
• The RGB channel values are in the [0, 255] range. This is not ideal for a neural
network; in general the input values should be made small.
• Here, you will standardize values to be in the [0, 1] range by using a Rescaling
layer.
normalization_layer = layers.experimental.preprocessing.Rescaling(1./255)
normalized_ds = train_ds.map(lambda x, y: (normalization_layer(x), y))
image_batch, labels_batch = next(iter(normalized_ds))
first_image = image_batch[0]
# Notice the pixels values are now in `[0,1]`.
print(np.min(first_image), np.max(first_image))
Step 4 – Create the model
• The model consists of three convolution blocks with a max pool layer
in each of them.
• There's a fully connected layer with 128 units on top of it that is
activated by a relu activation function.
…
num_classes = 5
model = Sequential([
layers.experimental.preprocessing.Rescaling(1./255, input_shape=(img_height,
img_width, 3)),
layers.Conv2D(16, 3, padding='same', activation='relu'),
layers.MaxPooling2D(),
layers.Conv2D(32, 3, padding='same', activation='relu'),
layers.MaxPooling2D(),
layers.Conv2D(64, 3, padding='same', activation='relu'),
layers.MaxPooling2D(),
layers.Flatten(),
layers.Dense(128, activation='relu'),
layers.Dense(num_classes)
])
Step 5 – Compile the model
• In this example choose the optimizers.Adam optimizer
and losses.SparseCategoricalCrossentropy loss function. To view training and
validation accuracy for each training epoch, pass the metrics argument.
model.compile(optimizer='adam',
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=['accuracy’])
model.summary()
Step 6 – Train the model
epochs=10
history = model.fit(
train_ds,
validation_data=val_ds,
epochs=epochs
)
Step 6 – Validate the model
Create plots of loss and accuracy on the training and validation sets.
acc = history.history['accuracy']
val_acc = history.history['val_accuracy']
loss = history.history['loss']
val_loss = history.history['val_loss']
epochs_range = range(epochs)
plt.figure(figsize=(8, 8))
plt.subplot(1, 2, 1)
plt.plot(epochs_range, acc, label='Training Accuracy')
plt.plot(epochs_range, val_acc, label='Validation Accuracy')
plt.legend(loc='lower right')
plt.title('Training and Validation Accuracy')
Verifying the plot
As you can see from the plots,
training accuracy and validation
accuracy are off by large margin and
the model has achieved only around
60% accuracy on the validation set.
Step 7 – Reduce Overfitting
• When there are a small number of training examples, the model
sometimes learns from noises or unwanted details from training
examples—to an extent that it negatively impacts the performance of
the model on new examples. This phenomenon is known as
overfitting.
• It means that the model will have a difficult time generalizing on a
new dataset.
…
• In the previous plot, the training accuracy is increasing linearly over
time, whereas validation accuracy stalls around 60% in the training
process.
• Also, the difference in accuracy between training and validation
accuracy is noticeable—a sign of overfitting.
• There are multiple ways to fight overfitting in the training process. In
this example, we will use data augmentation and add Dropout to the
model.
Data Augmentation
• Overfitting generally occurs when there are a small number of training
examples.
• Data augmentation takes the approach of generating additional training
data from your existing examples by augmenting them using random
transformations that yield believable-looking images.
…
data_augmentation = keras.Sequential(
[
layers.experimental.preprocessing.RandomFlip("horizontal",
input_shape=(img_height,
img_width,
3)),
layers.experimental.preprocessing.RandomRotation(0.1),
layers.experimental.preprocessing.RandomZoom(0.1),
]
)
Dropout
• When you apply Dropout to a layer it randomly drops out (by setting the activation to zero) a
number of output units from the layer during the training process.
• Dropout takes a fractional number as its input value, in the form such as 0.1, 0.2, 0.4, etc. This
means dropping out 10%, 20% or 40% of the output units randomly from the applied layer.
…
model = Sequential([
data_augmentation,
layers.experimental.preprocessing.Rescaling(1./255),
layers.Conv2D(16, 3, padding='same', activation='relu'),
layers.MaxPooling2D(),
layers.Conv2D(32, 3, padding='same', activation='relu'),
layers.MaxPooling2D(),
layers.Conv2D(64, 3, padding='same', activation='relu'),
layers.MaxPooling2D(),
layers.Dropout(0.2),
layers.Flatten(),
layers.Dense(128, activation='relu'),
layers.Dense(num_classes)
])
Step 8 - Re-compile and re-train the
model
model.compile(optimizer='adam',
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=['accuracy’])
model.summary()
Step 9 - Visualize training results
After applying data augmentation and Dropout,
there is less overfitting than before, and training
and validation accuracy are closer aligned.
Step 10 - Predict on new data
• Finally, let's use our model to classify an image that wasn't included in
the training or validation sets.
Thank You