Experiment No: 02. (C) Date: ……. /…….
/……………
DESIGN AND ANALYSIS OF A CNN ARCHITECTURE FOR OBJECT
RECOGNITION
Aim
To write a python program to analyse the CNN architecture for object recognition.
Equipment Required
PC with python software
Algorithm
Step 1: Import the relevant libraries
Step 2: Read the dataset
Step 3: Load Pre-Trained VGG model
Step 4: Add Custom top layers
Step 5: Fine tune the model
Step 6. Evaluate the model
Step 7: Stop Programming
Theory
Transfer learning is an approach where we use a pre-trained model (trained on a large
dataset) as a starting point for a different task. Instead of training a model from scratch, we
reuse the learned features and adapt them to our specific problem. Suppose we have a pre-
trained model (e.g., a neural network) that has learned useful features from a large dataset
(e.g., ImageNet for image classification). We transfer the weights (learned parameters) from
this pre-trained model to a new model designed for a different task (e.g., classifying specific
objects in medical images). The main goal is to improve the performance on the new task
using the gained knowledge from the source task. VGG16 is a deep CNN architecture
developed by the Visual Geometry Group (VGG) at the University of Oxford. It has 16
weight layers, including 13 convolutional layers and 3 fully connected layers. The model was
trained on the ImageNet dataset, which contains millions of images across 1000 classes. It
gained prominence for its simplicity, uniform structure, and impressive performance on
large-scale image recognition tasks.
1
Program
import tensorflow as tf
from tensorflow.keras.applications import VGG16
from tensorflow.keras.layers import Dense, Flatten
from tensorflow.keras.models import Model
from tensorflow.keras.datasets import cifar10
from tensorflow.keras.utils import to_categorical
# Load CIFAR-10 dataset
(train_images, train_labels), (val_images, val_labels) = cifar10.load_data()
# Preprocess data (normalize pixel values, convert labels to one-hot encoding)
train_images = train_images / 255.0
val_images = val_images / 255.0
train_labels = to_categorical(train_labels, num_classes=10)
val_labels = to_categorical(val_labels, num_classes=10)
# Load pre-trained VGG16 model (excluding top layers)
base_model = VGG16(weights='imagenet', include_top=False, input_shape=(32, 32, 3))
# Add custom top layers
x = Flatten()(base_model.output)
x = Dense(256, activation='relu')(x)
output = Dense(10, activation='softmax')(x) # 10 classes for CIFAR-10
# Create the final model
model = Model(inputs=base_model.input, outputs=output)
# Compile the model
model.compile(optimizer='adam',
loss='categorical_crossentropy',
metrics=['accuracy'])
# Train the model
model.fit(train_images, train_labels, epochs=10, validation_data=(val_images, val_labels))
2
Inference:
Result: