Experiment 3
Experiment 3
Aim:
To use a pre-trained CNN model for a given problem.
Objective:
▪ To use the pre-trained models like ResNet, VGGNet, etc for a classification task.
Theory:
VGG-16 Architecture
Pretrained Convolutional Neural Network (CNN) models such as ResNet and VGGNet are
powerful tools in deep learning, particularly for tasks like image classification, object
detection, and more. These models have been trained on large datasets, like ImageNet, and
possess learned features that can be reused for similar tasks, reducing the need for vast
amounts of data or computational resources. Leveraging pretrained models allows for
transfer learning, where the knowledge gained from one task (e.g., distinguishing between a
thousand classes of objects) is applied to a new, more specific task. By fine-tuning the
pretrained weights on new datasets, the model can be adapted efficiently to the target
problem.
ResNet (Residual Network) and VGGNet are two popular architectures for transfer learning.
VGGNet, known for its simplicity, uses deep stacks of convolutional layers, each with a small
kernel size, making it easy to understand and implement. However, the deeper architecture
leads to many parameters, making training and inference computationally expensive. ResNet,
on the other hand, introduces skip connections, allowing information to bypass certain layers.
This innovation helps overcome the vanishing gradient problem in very deep networks,
allowing models with hundreds of layers to converge efficiently. Thus, ResNet is often
preferred for deeper architectures because of its improved training stability.
Using pretrained models like VGGNet or ResNet comes with multiple advantages, including
faster convergence and potentially higher accuracy, as the network starts with weights that
already encode useful patterns. These models are particularly valuable in cases where labeled
data is limited. Instead of training a network from scratch, researchers can use the pretrained
model as a feature extractor, only retraining the final classification layer for their specific task.
This method speeds up the process and reduces the computational resources required,
making pretrained models a go-to choice for many real-world applications.
Problem Statement:
Using VGG-16 as the base pre-trained CNN network, develop a model that requires less
amount of training to properly classify a given image as that of a cat or a dog.
Implementation:
Pretrained CNN Notebook
Importing the required libraries
import tensorflow as tf
from tensorflow.keras.applications import VGG16
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.layers import Dense, Flatten
from tensorflow.keras.models import Model
import matplotlib.pyplot as plt
import matplotlib.pyplot as plt
Organizing the dataset consisting of images of cats and dogs (3000 images)
dataset_url = 'https://storage.googleapis.com/mledu-
datasets/cats_and_dogs_filtered.zip'
data_dir = tf.keras.utils.get_file('cats_and_dogs', origin=dataset_url,
extract=True)
data_dir = data_dir.replace('cats_and_dogs', 'cats_and_dogs_filtered')
train_dir = f"{data_dir}/train"
validation_dir = f"{data_dir}/validation"
Defining data generators that would be used to produce batches of images for the purpose
of training and testing
train_datagen = ImageDataGenerator(rescale=1./255)
validation_datagen = ImageDataGenerator(rescale=1./255)
train_generator = train_datagen.flow_from_directory(
train_dir, target_size=(224, 224), batch_size=32, class_mode='binary')
validation_generator = validation_datagen.flow_from_directory(
validation_dir, target_size=(224, 224), batch_size=32, class_mode='binary')
Freezing specific layers from the pretrained model to avoid changing specific weights
model = Model(inputs=base_model.input, outputs=predictions)
for layer in base_model.layers:
layer.trainable = False
prediction = model.predict(img_array)
print("Predicted class:", "Dog" if prediction > 0.5 else "Cat")
plt.imshow(img)
plt.axis('off')
plt.show()
Inferences:
▪ The VGG-16 pre-trained convolutional network proved to be really beneficial in
building a classifier for identifying cats’ and dogs’ images while using minimal data for
training.
▪ The concerned model built was found to have a validation accuracy of 89.20% at the
end of training.
▪ Upon plotting the training history of the model, it can be noticed that the validation
accuracy of the model was comparatively higher than the training accuracy which may
be due to weight parameters of the pre-trained VGG-16 model. However, these
accuracies converge towards the end of training.