Dlweek 7
Dlweek 7
No : Date :
EXPERIMENT NO-07
Aim: Train a Deep Learning model to classify a given image using pretrained model of
AlexNet,ZF-Net,VGGnet,GoogleNet,ResNet.
Description: VGG introduced the concept of increasing the number of layers to improve
accuracy. However,increasing the number of layers above 20 could prevent the model from
converging.
The main reason is the vanishing gradient problem—after too many folds, the learning rate is
so low that the model’s weights cannot change. Another issue is gradient explosion. A solution
is gradient clipping, which involves “clipping” the error derivative to a certain threshold during
backward propagation and using these clipped gradients to update the weights. When the error
derivative is rescaled, weights are also rescaled, and this reduces the chance of an overflow or
underflow that can lead to gradient explosion.
The Residual Network (ResNet) architecture uses the concept of skip connections, allowing
inputs to “skip” some convolutional layers. The result is a significant reduction in training time
and improved accuracy. After the model learns a given feature, it won’t attempt to learn it
again—instead, it will focus on learning the new features. It’s a clever approach that can
significantly improve model training.
image.png
!wget --no-check-certificate \
https://storage.googleapis.com/mledu-
datasets/cats_and_dogs_filtered.zip \
-O /tmp/cats_and_dogs_filtered.zip
import os
import zipfile
import tensorflow as tf
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras import layers
from tensorflow.keras import Model
import matplotlib.pyplot as plt
prepare the dataset and separate out the images for pre-trained models for image
classification model:
first divide the folder contents into the train and validation directories. Then, in each directory,
create a separate directory for cats containing only cat images and a separate directory for dogs
base_dir = '/tmp/cats_and_dogs_filtered'
train_dir = os.path.join(base_dir, 'train')
validation_dir = os.path.join(base_dir, 'validation')
fig = plt.gcf()
fig.set_size_inches(ncols*4, nrows*4)
pic_index = 100
train_cat_fnames = os.listdir( train_cats_dir )
train_dog_fnames = os.listdir( train_dogs_dir )
img = mpimg.imread(img_path)
plt.imshow(img)
plt.show()
train_generator = train_datagen.flow_from_directory(
train_dir,
target_size=(224, 224),
batch_size=20,
validation_generator = validation_datagen.flow_from_directory(
validation_dir,
target_size=(224, 224),
batch_size=20,
class_mode='binary'
)
VGG16
from tensorflow.keras.applications.vgg16 import VGG16
# Add a fully connected layer with 512 hidden units and ReLU
activation
x = layers.Dense(512, activation='relu')(x)
model = tf.keras.models.Model(base_model.input, x)
model.compile(optimizer =
tf.keras.optimizers.RMSprop(learning_rate=0.0001), loss =
'binary_crossentropy',metrics = ['acc'])
use the original directories instead of the augmented datasets that are used here. model is
executed with just 10 epochs, but you can also increase them to get better results
NOTE: record the accuracy for different epoch values and compare
Epoch 1/5
/usr/local/lib/python3.10/dist-packages/keras/src/trainers/
data_adapters/py_dataset_adapter.py:121: UserWarning: Your
`PyDataset` class should call `super().__init__(**kwargs)` in its
constructor. `**kwargs` can include `workers`, `use_multiprocessing`,
`max_queue_size`. Do not pass these arguments to `fit()`, as they
will be ignored.
self._warn_if_super_not_called()
plt.subplot(1, 2, 1)
plt.plot(history.history['acc'], label='Train Accuracy')
plt.plot(history.history['val_acc'], label='Validation Accuracy')
plt.title('Model Accuracy')
plt.xlabel('Epochs')
plt.ylabel('Accuracy')
plt.legend()
plt.show()
import pandas as pd
model = tf.keras.models.Model(base_model.input, x)
model.compile(optimizer=tf.keras.optimizers.RMSprop(learning_rate=0.0
001),
loss='binary_crossentropy',
metrics=['acc'])
results.append({
'epochs': epochs,
'train_accuracy': history.history['acc'][-1],
'val_accuracy': history.history['val_acc'][-1],
'train_loss': history.history['loss'][-1],
'val_loss': history.history['val_loss'][-1]
results_df = pd.DataFrame(results)
print(results_df)
ResNet50
from tensorflow.keras.applications import ResNet50
base_model = ResNet50(input_shape=(224, 224, 3), include_top=False,
weights='imagenet')
model.compile(optimizer=tf.keras.optimizers.RMSprop(learning_rate=0.0
001),
loss='binary_crossentropy',
metrics=['acc'])
history = model.fit(
train_generator,
epochs=10,
validation_data=validation_generator,
verbose=1
)
Epoch 1/10
100/100 ━━━━━━━━━━━━━━━━━━━━ 32s 299ms/step - acc: 0.5478 - loss:
0.6794 - val_acc: 0.5690 - val_loss: 0.6776
Epoch 2/10
100/100 ━━━━━━━━━━━━━━━━━━━━ 42s 314ms/step - acc: 0.5540 - loss:
0.6827 - val_acc: 0.5830 - val_loss: 0.6701
Epoch 3/10
100/100 ━━━━━━━━━━━━━━━━━━━━ 32s 304ms/step - acc: 0.5898 - loss:
0.6754 - val_acc: 0.6120 - val_loss: 0.6705
Epoch 4/10
100/100 ━━━━━━━━━━━━━━━━━━━━ 30s 284ms/step - acc: 0.5875 - loss:
0.6776 - val_acc: 0.6300 - val_loss: 0.6538
Epoch 5/10
100/100 ━━━━━━━━━━━━━━━━━━━━ 41s 282ms/step - acc: 0.5963 - loss:
0.6756 - val_acc: 0.5930 - val_loss: 0.6662
Epoch 6/10
100/100 ━━━━━━━━━━━━━━━━━━━━ 43s 307ms/step - acc: 0.5896 - loss:
0.6668 - val_acc: 0.6190 - val_loss: 0.6625
Epoch 7/10
100/100 ━━━━━━━━━━━━━━━━━━━━ 39s 277ms/step - acc: 0.5866 - loss:
0.6776 - val_acc: 0.6290 - val_loss: 0.6553
Epoch 8/10
100/100 ━━━━━━━━━━━━━━━━━━━━ 40s 282ms/step - acc: 0.5554 - loss:
0.6807 - val_acc: 0.6150 - val_loss: 0.6568
Epoch 9/10
100/100 ━━━━━━━━━━━━━━━━━━━━ 29s 278ms/step - acc: 0.5942 - loss:
0.6749 - val_acc: 0.6190 - val_loss: 0.6567
Epoch 10/10
100/100 ━━━━━━━━━━━━━━━━━━━━ 42s 280ms/step - acc: 0.5955 - loss:
0.6757 - val_acc: 0.6080 - val_loss: 0.6646
Page No. Signature of the
Faculty………………………...
Roll No: 1601217333164 Exp. No : Date :
validation_loss, validation_accuracy =
model.evaluate(validation_generator)
plt.figure(figsize=(12, 4))
plt.subplot(1, 2, 1)
plt.plot(history.history['acc'], label='Train Accuracy')
plt.plot(history.history['val_acc'], label='Validation Accuracy')
plt.title('Resnet50 Model Accuracy')
plt.xlabel('Epochs')
plt.ylabel('Accuracy')
plt.legend()
plt.subplot(1, 2, 2)
plt.plot(history.history['loss'], label='Train Loss')
plt.plot(history.history['val_loss'], label='Validation Loss')
plt.title('Resnet50 Model Loss')
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.legend()
plt.show()
Conclusion: The given dataset is trained upon by VGG16 and ResNet50. VGG16 attained an
accuracy of 80% within 5 epochs thus converging faster compared to ResNet50. We could see
a clear increase in the training as well as validation accuracy as we go on increase the no.of
epochs in VGG16. Whereas ResNet50 performed poor comparitively with an accuracy of just
60% after 5 epochs. And as we moved on increasing the no.of epochs, the validation accuracy
started decreasing from 6 epochs and even the model couldn't learn further. Thus resnet50
attained best score after 6 epochs i.e 60%. From this we might conclude that ResNet50 might
further need larger no.of epochs compared to VGG16.