0% found this document useful (0 votes)
6 views7 pages

EXP5 VGG16v2

The document outlines the process of training a deep learning model to classify images from the CIFAR-10 dataset using pre-trained models VGG16 and a custom VGG16 model from scratch. It includes steps for data preprocessing, model architecture, compilation, training, and evaluation, emphasizing the use of transfer learning and custom classification heads. Key points highlight the importance of input size adjustment, freezing layers to prevent overfitting, and potential improvements like increasing epochs and data augmentation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views7 pages

EXP5 VGG16v2

The document outlines the process of training a deep learning model to classify images from the CIFAR-10 dataset using pre-trained models VGG16 and a custom VGG16 model from scratch. It includes steps for data preprocessing, model architecture, compilation, training, and evaluation, emphasizing the use of transfer learning and custom classification heads. Key points highlight the importance of input size adjustment, freezing layers to prevent overfitting, and potential improvements like increasing epochs and data augmentation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Train a Deep learning model to classify a given image using pre trained

model of AlexNet and VGGNet and compare their performance.


import tensorflow as tf

from tensorflow.keras.applications import VGG16

from tensorflow.keras.models import Model

from tensorflow.keras.layers import Dense, Flatten, Dropout

from tensorflow.keras.preprocessing.image import ImageDataGenerator

from tensorflow.keras.optimizers import Adam

from tensorflow.keras.utils import to_categorical

from tensorflow.keras.datasets import cifar10

import numpy as np

# Load CIFAR-10 dataset

(x_train, y_train), (x_test, y_test) = cifar10.load_data()

# Resize images to 224x224 (VGG16 input size)

x_train_resized = tf.image.resize(x_train, (224, 224))

x_test_resized = tf.image.resize(x_test, (224, 224))

# Normalize pixel values (scale between 0 and 1)

x_train_resized = x_train_resized / 255.0

x_test_resized = x_test_resized / 255.0

# One-hot encode labels

y_train = to_categorical(y_train, 10)

y_test = to_categorical(y_test, 10)


# Load VGG16 pre-trained model without top layer

base_model = VGG16(weights='imagenet', include_top=False, input_shape=(224, 224,


3))

# Freeze the base model layers

base_model.trainable = False

# Add custom classification head

x = Flatten()(base_model.output)

x = Dense(256, activation='relu')(x)

x = Dropout(0.5)(x)

x = Dense(128, activation='relu')(x)

x = Dropout(0.5)(x)

output = Dense(10, activation='softmax')(x)

# Create the final model

model = Model(inputs=base_model.input, outputs=output)

# Compile the model

model.compile(optimizer=Adam(learning_rate=0.0001),
loss='categorical_crossentropy', metrics=['accuracy'])

# Train the model

model.fit(x_train_resized, y_train, validation_data=(x_test_resized, y_test),


batch_size=32, epochs=10)

# Evaluate the model

test_loss, test_acc = model.evaluate(x_test_resized, y_test)

print(f"Test accuracy: {test_acc:.4f}")


Key Points:

• Input size adjustment: CIFAR-10 images are 32x32, but VGG16 requires
224x224.

• Transfer Learning: Using pre-trained weights from ImageNet.

• Custom Classification Head: Fully connected layers adapted for CIFAR-10.

• Freezing Base Layers: Prevents overfitting and speeds up training.

• Fine-tuning (Optional): Unfreezing some VGG16 layers can improve


performance.
VGG16 model from scratch (without pre-trained weights) on the CIFAR-10 dataset
using Keras.

Key Points:

• VGG16 Architecture: Uses multiple 3×3 convolution layers and max-pooling


layers.

• Input Size: CIFAR-10 images are 32x32, but VGG16 requires 224x224.

• From Scratch: No pre-trained weights are used.

• Final Layers: Fully connected layers adapted for CIFAR-10 (10 classes).

Implementation

import tensorflow as tf

from tensorflow.keras.models import Sequential

from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout

from tensorflow.keras.optimizers import Adam

from tensorflow.keras.utils import to_categorical

from tensorflow.keras.datasets import cifar10

import tensorflow.keras.backend as K

# Load CIFAR-10 dataset

(x_train, y_train), (x_test, y_test) = cifar10.load_data()

# Resize images to 224x224 (VGG16 input size)

x_train_resized = tf.image.resize(x_train, (224, 224))

x_test_resized = tf.image.resize(x_test, (224, 224))

# Normalize pixel values

x_train_resized = x_train_resized / 255.0

x_test_resized = x_test_resized / 255.0


# One-hot encode labels

y_train = to_categorical(y_train, 10)

y_test = to_categorical(y_test, 10)

# Define VGG16 model from scratch

def build_vgg16():

model = Sequential()

# Block 1

model.add(Conv2D(64, (3,3), activation='relu', padding='same', input_shape=(224,


224, 3)))

model.add(Conv2D(64, (3,3), activation='relu', padding='same'))

model.add(MaxPooling2D((2,2), strides=(2,2)))

# Block 2

model.add(Conv2D(128, (3,3), activation='relu', padding='same'))

model.add(Conv2D(128, (3,3), activation='relu', padding='same'))

model.add(MaxPooling2D((2,2), strides=(2,2)))

# Block 3

model.add(Conv2D(256, (3,3), activation='relu', padding='same'))

model.add(Conv2D(256, (3,3), activation='relu', padding='same'))

model.add(Conv2D(256, (3,3), activation='relu', padding='same'))

model.add(MaxPooling2D((2,2), strides=(2,2)))

# Block 4

model.add(Conv2D(512, (3,3), activation='relu', padding='same'))


model.add(Conv2D(512, (3,3), activation='relu', padding='same'))

model.add(Conv2D(512, (3,3), activation='relu', padding='same'))

model.add(MaxPooling2D((2,2), strides=(2,2)))

# Block 5

model.add(Conv2D(512, (3,3), activation='relu', padding='same'))

model.add(Conv2D(512, (3,3), activation='relu', padding='same'))

model.add(Conv2D(512, (3,3), activation='relu', padding='same'))

model.add(MaxPooling2D((2,2), strides=(2,2)))

# Fully Connected Layers

model.add(Flatten())

model.add(Dense(4096, activation='relu'))

model.add(Dropout(0.5))

model.add(Dense(4096, activation='relu'))

model.add(Dropout(0.5))

model.add(Dense(10, activation='softmax')) # CIFAR-10 has 10 classes

return model

# Create model

model = build_vgg16()

# Compile the model

model.compile(optimizer=Adam(learning_rate=0.0001),
loss='categorical_crossentropy', metrics=['accuracy'])

# Train the model


model.fit(x_train_resized, y_train, validation_data=(x_test_resized, y_test),
batch_size=32, epochs=10)

# Evaluate the model

test_loss, test_acc = model.evaluate(x_test_resized, y_test)

print(f"Test accuracy: {test_acc:.4f}")

Explanation:

• Conv2D Layers: Uses 3×3 filters with ReLU activation.

• MaxPooling2D: Reduces spatial dimensions using 2×2 pooling.

• Fully Connected Layers: Uses 4096 neurons with Dropout (0.5) to prevent
overfitting.

• Softmax Output Layer: Since CIFAR-10 has 10 classes.

Improvements:

• Increase epochs (e.g., 50-100 epochs) for better accuracy.

• Use data augmentation for better generalization.

• Try a different learning rate (e.g., 0.00001).

You might also like