0% found this document useful (0 votes)
2 views

Dl Lab 7 Excuted

The document outlines a process for using the pre-trained VGG16 convolutional neural network for image classification on the CIFAR-10 dataset. It includes steps for data loading, model creation, training, and visualization of results. The model is trained for 5 epochs, achieving a final training accuracy of approximately 48.65% and a validation accuracy of about 48.82%.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Dl Lab 7 Excuted

The document outlines a process for using the pre-trained VGG16 convolutional neural network for image classification on the CIFAR-10 dataset. It includes steps for data loading, model creation, training, and visualization of results. The model is trained for 5 epochs, achieving a final training accuracy of approximately 48.65% and a validation accuracy of about 48.82%.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

1.

Aim: Use a pre-trained convolution neural


network (VGG16) for image classification.
Source code:
# Import necessary libraries
import numpy as np
import tensorflow as tf
from tensorflow.keras.datasets import cifar10
from tensorflow.keras.applications import VGG16
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Flatten
from tensorflow.keras.optimizers import RMSprop
import matplotlib.pyplot as plt
# Load CIFAR-10 datasetvalues
train_images = train_(train_images, train_labels), (test_images, test_labels)
=cifar10.load_data
# Normalize pixel images.astype('float32') / 255.0()
test_images = test_images.astype('float32') / 255.0

# Load pre-trained VGG16 model without top (fully connected) layers


vgg_base = VGG16(weights='imagenet', include_top=False, input_shape=(32, 32, 3))

# Freeze the convolutional layers


vgg_base.trainable = False

# Create a new model with the pre-trained VGG16 base and additional layers
model = Sequential([
vgg_base,
Flatten(),
Dense(256, activation='relu'),
Dense(10, activation='softmax')
])

# Compile the model


model.compile(optimizer=RMSprop(learning_rate=2e-5),
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])

# Display training images


plt.figure(figsize=(10, 10))
for i inrange(25):
plt.subplot(5, 5, i + 1)
plt.xticks([])
plt.yticks([])
plt.grid(False)
plt.imshow(train_images[i])
plt.xlabel(get_class_names()[train_labels[i][0]])
plt.show()

1
plt.figure(figsize=(10, 10))
for i inrange(25):
plt.subplot(5, 5, i + 1)
plt.xticks([])
plt.yticks([])
plt.grid(False)
plt.imshow(test_images[i])
plt.xlabel(get_class_names()[test_labels[i][0]])
plt.show()

2
# Train the model
history = model.fit(train_images, train_labels, epochs=5, batch_size=64,
validation_data=(test_images, test_labels))
Epoch 1/5
782/782━━━━━━━━━━━━━━━━━━━━16s 15ms/step - accuracy: 0.1848 - loss:
2.2483 - val_accuracy: 0.3618 - val_loss: 1.9465
Epoch 2/5
782/782━━━━━━━━━━━━━━━━━━━━8s 11ms/step - accuracy: 0.3839 - loss:
1.8907 - val_accuracy: 0.4210 - val_loss: 1.7493
Epoch 3/5
782/782━━━━━━━━━━━━━━━━━━━━9s 12ms/step - accuracy: 0.4352 - loss:
1.7138 - val_accuracy: 0.4516 - val_loss: 1.6360
Epoch 4/5
782/782━━━━━━━━━━━━━━━━━━━━9s 10ms/step - accuracy: 0.4634 - loss:
1.6136 - val_accuracy: 0.4702 - val_loss: 1.5637
Epoch 5/5
782/782━━━━━━━━━━━━━━━━━━━━10s 11ms/step - accuracy: 0.4865 - loss:
1.5350 - val_accuracy: 0.4882 - val_loss: 1.5121

3
# Plot accuracy graph
plt.plot(history.history['accuracy'], label='Training Accuracy')
plt.plot(history.history['val_accuracy'], label='Validation Accuracy')
plt.xlabel('Epoch')
plt.ylabel('Accuracy')
plt.legend()
plt.show()

# Function to get class names


defget_class_names():
return {
0: 'airplane',
1: 'automobile',
2: 'bird',
3: 'cat',
4: 'deer',
5: 'dog',
6: 'frog',
7: 'horse',
8: 'ship',
9: 'truck'
}

You might also like