To Implement Transfer Learning for CIFAR-10 dataset classification using VGG-19 architecture.
The CIFAR-10 dataset consists of 60000 32x32 colour images in 10 classes, with 6000 images per class. There are 50000 training images and 10000 test images.
The dataset is divided into five training batches and one test batch, each with 10000 images. The test batch contains exactly 1000 randomly-selected images from each class. The training batches contain the remaining images in random order, but some training batches may contain more images from one class than another. Between them, the training batches contain exactly 5000 images from each class.
Here are the classes in the dataset, as well as 10 random images from each:
VGG19 is a variant of the VGG model which in short consists of 19 layers (16 convolution layers, 3 Fully connected layer, 5 MaxPool layers and 1 SoftMax layer).
Now we have use transfer learning with the help of VGG-19 architecture and use it to classify the CIFAR-10 Dataset
Import tensorflow and preprocessing libraries
Load CIFAR-10 Dataset & use Image Data Generator to increse the size of dataset
Import the VGG-19 as base model & add Dense layers to it
Compile and fit the model
Predict for custom inputs using this model
Developed By: Shafeeq Ahamed. S
Register No: 212221230092
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report,confusion_matrix
from keras import Sequential
from keras.layers import Flatten,Dense,BatchNormalization,Activation,Dropout
from tensorflow.keras import utils
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.callbacks import ReduceLROnPlateau
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from keras.datasets import cifar10
from tensorflow.keras.applications import VGG19
(x_train,y_train),(x_test,y_test)=cifar10.load_data()
y_train_onehot = utils.to_categorical(y_train,10)
y_test_onehot = utils.to_categorical(y_test,10)
base_model = VGG19(include_top=False, weights = "imagenet",
input_shape = (32,32,3))
for layer in base_model.layers:
layer.trainable = False
model = Sequential()
model.add(base_model)
model.add(Flatten())
model.add(Dense(250,activation=("relu")))
model.add(Dropout(0.2))
model.add(Dense(100,activation=("relu")))
model.add(Dropout(0.35))
model.add(Dense(10,activation=("softmax")))
model.summary()
model.compile(optimizer=Adam(learning_rate=0.001),
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
learning_rate_reduction = ReduceLROnPlateau(monitor='val_accuracy',patience=3,
verbose=1,factor=0.5,min_lr=0.00001)
model.fit(x_train, y_train, batch_size=500, epochs=10, validation_data=(x_test, y_test),
callbacks=[learning_rate_reduction])
metrics = pd.DataFrame(model.history.history)
metrics[['loss','val_loss']].plot()
metrics[['accuracy','val_accuracy']].plot()
x_test_predictions = np.argmax(model.predict(x_test), axis=1)
print(confusion_matrix(y_test,x_test_predictions))
print(classification_report(y_test,x_test_predictions))
Training Loss, Validation Loss Vs Iteration | Accuracy, Validation Accuracy Vs Iteration |
---|---|
|
![](https://private-user-images.githubusercontent.com/93427237/241362055-b6dac570-4221-4f55-8e06-68b0c440b6ba.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjIxNjI3MTAsIm5iZiI6MTcyMjE2MjQxMCwicGF0aCI6Ii85MzQyNzIzNy8yNDEzNjIwNTUtYjZkYWM1NzAtNDIyMS00ZjU1LThlMDYtNjhiMGM0NDBiNmJhLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA3MjglMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwNzI4VDEwMjY1MFomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPWRjNGNjYTE1OTUwZTM3Njk1MWU1NzVkZWFlNDZhM2UyZTFiOTI5ZTg0ZGVkZGFjNGQ4ZTU3MjIyMDA5M2I1YjYmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.MunPeL1cYjn93F8TaXrsnoL9B7eWnS6A0F5T7ZGCgJw)
![](https://private-user-images.githubusercontent.com/93427237/241362043-4aeedc50-7454-42ae-9031-cfb94fb9027b.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjIxNjI3MTAsIm5iZiI6MTcyMjE2MjQxMCwicGF0aCI6Ii85MzQyNzIzNy8yNDEzNjIwNDMtNGFlZWRjNTAtNzQ1NC00MmFlLTkwMzEtY2ZiOTRmYjkwMjdiLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA3MjglMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwNzI4VDEwMjY1MFomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTJkM2Y3MGIyZjU1ZjE1OTE4MzBiNzA1OTcxM2U0ZDlmNzVkOTFjN2MyYjdiY2I4NzIxMWExNmYzOGE0YTRjZTEmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.cF8PbkO4eMoEXlxFuVxaIxmMnIMk--kvSHDylp2JuGg)
- We got an Accuracy of 60% with this model.There could be several reasons for not achieving higher accuracy. Here are a few possible explanations:
- VGG19 was originally designed and trained on the ImageNet dataset, which consists of high-resolution images.
- In contrast, the CIFAR10 dataset contains low-resolution images (32x32 pixels).
- The difference in image sizes and content can affect the transferability of the learned features.
- Pretrained models like VGG19 might not be the most suitable choice for CIFAR10 due to this disparity in data characteristics.
- If the CIFAR10 dataset is relatively small, it may not provide enough diverse examples for the model to learn robust representations.
- Deep learning models, such as VGG19, typically require large amounts of data to generalize well.
- In such cases, you could consider exploring other architectures that are specifically designed for smaller datasets, or you might want to look into techniques like data augmentation or transfer learning from models pretrained on similar datasets.
- VGG19 is a deep and computationally expensive model with a large number of parameters.
- If you are limited by computational resources or working with a smaller dataset, the model's capacity might be excessive for the task at hand.
- In such cases, using a smaller model architecture or exploring other lightweight architectures like MobileNet or SqueezeNet could be more suitable and provide better accuracy.
Thus, transfer Learning for CIFAR-10 dataset classification using VGG-19 architecture is successfully implemented.