Skip to content

Image Classification using CNN with CIFAR-10 Dataset (85% Accuracy)

"Gif"

Import Necessary Libraries

import tensorflow as tf:

  • This imports the TensorFlow library, which is an open-source machine learning framework used for building and training neural networks.

from tensorflow.keras import datasets, layers, models:

  • datasets: This module in Keras provides access to several pre-loaded datasets, like CIFAR-10 or MNIST, which are often used for benchmarking machine learning models.
  • layers: This module provides the building blocks for neural networks, such as convolutional layers (Conv2D), pooling layers (MaxPooling2D), and fully connected layers (Dense).
  • models: This module allows you to define and work with models in Keras. It includes the Sequential model, which is a linear stack of layers, and the Model class used for more complex architectures.

import matplotlib.pyplot as plt:

  • This imports the pyplot interface from the Matplotlib library, which is used for plotting graphs and visualizing data, such as plotting training and validation accuracy or loss over epochs.

from tensorflow.keras.preprocessing.image import ImageDataGenerator:

  • ImageDataGenerator: This class is used to generate batches of tensor image data with real-time data augmentation. The data will be looped over (in batches) indefinitely.

from tensorflow.keras.callbacks import EarlyStopping, LearningRateScheduler, ReduceLROnPlateau:

  • EarlyStopping: A callback to stop training when a monitored metric has stopped improving.
  • LearningRateScheduler: A callback to adjust the learning rate over epochs based on a schedule or function you define.
  • ReduceLROnPlateau: A callback to reduce the learning rate when a metric has stopped improving, which helps in finding a local minimum during training.

from tensorflow.keras.layers import Input, Dense, Conv2D, MaxPooling2D, GlobalAveragePooling2D, Dropout, BatchNormalization:

  • Input: Used to instantiate a Keras tensor, a starting point for a model.
  • Dense: A regular densely-connected neural network layer.
  • Conv2D: A 2D convolutional layer, which is commonly used in processing images.
  • MaxPooling2D: A layer that performs max pooling operation, reducing the spatial dimensions of the input.
  • GlobalAveragePooling2D: A pooling operation that calculates the average output of each feature map in the previous layer.
  • Dropout: A regularization technique that randomly sets input units to 0 with a frequency of rate at each step during training to prevent overfitting.
  • BatchNormalization: A layer that normalizes the activations of the previous layer at each batch, i.e., applies a transformation that maintains the mean activation close to 0 and the activation standard deviation close to 1.

from tensorflow.keras.models import Model:

  • Model: This class is used to instantiate a Keras model. Unlike the Sequential model, the Model class used with the functional API is more flexible and is used for building complex models.

from tensorflow.keras.callbacks import ModelCheckpoint:

  • ModelCheckpoint: A callback to save the Keras model or model weights at some frequency.

from tensorflow.keras.optimizers import Adam:

  • Adam: An optimizer that is an extension to stochastic gradient descent, known for its robust performance in practice.

import numpy as np:

  • This imports the NumPy library, which is a fundamental package for scientific computing in Python. It is used for working with arrays and matrices, among other mathematical operations essential for deep learning tasks.
import tensorflow as tf
from tensorflow.keras import datasets, layers, models
import matplotlib.pyplot as plt
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.callbacks import EarlyStopping, LearningRateScheduler, ReduceLROnPlateau
from tensorflow.keras.layers import Input, Dense, Conv2D, MaxPooling2D, GlobalAveragePooling2D, Dropout, BatchNormalization
from tensorflow.keras.models import Model
from tensorflow.keras.callbacks import ModelCheckpoint
from tensorflow.keras.optimizers import Adam
import numpy as np

Load and Preprocess the Data

  • Loads the CIFAR-10 dataset, which is split into training and testing sets.
  • Normalizes the images by dividing pixel values by 255. This scales the pixel values to a range of 0 to 1, which is beneficial for neural network training.
(train_images, train_labels), (test_images, test_labels) = datasets.cifar10.load_data()
train_images, test_images = train_images / 255.0, test_images / 255.0

Early Stopping

The EarlyStopping callback in Keras is used during training to stop training when a monitored metric has stopped improving. The code snippet you've provided is setting up an EarlyStopping instance with specific parameters to control its behavior:

  • monitor='val_accuracy': This specifies the metric that the callback should monitor. In this case, it's set to 'val_accuracy', which means the callback will monitor the validation accuracy during training. The early stopping mechanism will be triggered based on changes in the validation accuracy.

  • patience=5: This sets the number of epochs with no improvement after which training will be stopped. By specifying patience=5, you're telling the callback to stop training if there is no improvement in the monitored metric for 5 consecutive epochs.

  • restore_best_weights=True: When set to True, this parameter ensures that once training is stopped, the model's weights will revert to the values they had at the epoch where the monitored metric was at its best (highest for accuracy). This means you don't need to manually save the best model during training because the callback will automatically ensure that your model has the best weights at the end of training.

Using the EarlyStopping callback can save time and computational resources by stopping training once it becomes clear that the model is no longer improving. It also helps in preventing overfitting since it prevents the model from continuing to learn from noise in the training data after the point where it has stopped making genuine improvements on the validation data.

early_stopping = EarlyStopping(
    monitor='val_accuracy',  # The metric to monitor
    patience=5,              # How many epochs to wait after the metric has stopped improving
    restore_best_weights=True  # Whether to restore model weights from the epoch with the best value of the monitored metric
)

Reduce Learning Rate

The ReduceLROnPlateau callback in Keras is used to reduce the learning rate when a metric has stopped improving. Here's the breakdown of the parameters for the code snippet you've provided:

  • monitor='val_accuracy': This parameter specifies which metric the callback should monitor. The learning rate will be reduced when the val_accuracy metric stops improving.

  • factor=0.2: When a reduction is triggered, the learning rate will be multiplied by this factor. For instance, if the learning rate is 0.001 and the factor is set to 0.2, the new learning rate will be 0.0002.

  • patience=5: This specifies the number of epochs with no improvement after which the learning rate will be reduced. For example, if val_accuracy doesn't improve for 5 epochs, the callback will reduce the learning rate.

  • verbose=1: When set to 1, the callback will print a message each time the learning rate is reduced, which helps you to see the changes directly in the training log.

  • min_lr=1e-6: This sets the minimum bound for the learning rate. No matter how many times the learning rate is reduced due to the factor, it will not go below this minimum limit.

This callback is particularly useful for fine-tuning a model when approaching a minimum for the loss function. By slowly reducing the learning rate, you allow the model to take smaller steps in the weight space, which can lead to more precise convergence to the minimum.

reduce_lr = ReduceLROnPlateau(
    monitor='val_accuracy', # The metric to be monitored
    factor=0.2,             # The factor by which the learning rate will be reduced. new_lr = lr * factor
    patience=5,             # Number of epochs with no improvement after which learning rate will be reduced.
    verbose=1,              # If 1, the method will print a message each time the learning rate is reduced.
    min_lr=1e-6             # The lower bound on the learning rate.
)

Build the CNN Model