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

Experiment 10 1

This document outlines an experiment focused on building a Convolutional Neural Network (CNN) for image classification using Keras and the CIFAR-10 dataset. It includes a theoretical overview, learning objectives, required materials, and a step-by-step procedure for model building, training, and evaluation. Additionally, it presents practical assignments to deepen understanding of CNN components and performance optimization strategies.

Uploaded by

sya833063
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Experiment 10 1

This document outlines an experiment focused on building a Convolutional Neural Network (CNN) for image classification using Keras and the CIFAR-10 dataset. It includes a theoretical overview, learning objectives, required materials, and a step-by-step procedure for model building, training, and evaluation. Additionally, it presents practical assignments to deepen understanding of CNN components and performance optimization strategies.

Uploaded by

sya833063
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

EXPERIMENT NO.

:-10

AIM: - Building a Convolutional Neural Network for Image Classification.

THEORY:-
1. Introduction/Overview:
This experiment introduces Convolutional Neural Networks (CNNs), a powerful class of neural
networks specifically designed for processing structured grid-like data, such as images. Students will
build a CNN using Keras (a high-level API for TensorFlow) to classify images from a simple dataset.
This hands-on experience will solidify their understanding of convolutional layers, pooling layers, and
fully connected layers.

Learning Objectives:
Understand the architecture of a CNN.
Implement convolutional layers, pooling layers, and fully connected layers using Keras.
Train a CNN for image classification.
Evaluate the performance of the trained model.

2. Materials and Equipment:


Computer with Python 3.7+ installed.
Jupyter Notebook or similar IDE.
TensorFlow and Keras (install using pip install tensorflow).
A simple image dataset (we'll use the CIFAR-10 dataset, readily available through Keras).

3. Procedure:
3.1. Dataset Loading and Preprocessing:
Python
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
import matplotlib.pyplot as plt

# Load the CIFAR-10 dataset


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

# Normalize pixel values to be between 0 and 1


x_train = x_train.astype("float32") / 255.0
x_test = x_test.astype("float32") / 255.0

# One-hot encode the labels


num_classes = 10
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)

print(f"x_train shape: {x_train.shape} - y_train shape: {y_train.shape}")


print(f"x_test shape: {x_test.shape} - y_test shape: {y_test.shape}")

3.2. CNN Model Building:


Python
model = keras.Sequential(
[
keras.Input(shape=(32, 32, 3)), # Input layer (32x32 images with 3 color channels)
layers.Conv2D(32, kernel_size=(3, 3), activation="relu"), # Convolutional layer with 32 filters
layers.MaxPooling2D(pool_size=(2, 2)), # Max pooling layer
layers.Conv2D(64, kernel_size=(3, 3), activation="relu"), # Another Convolutional layer
layers.MaxPooling2D(pool_size=(2, 2)), # Another Max pooling layer
layers.Flatten(), # Flatten the output for the fully connected layers
layers.Dense(num_classes, activation="softmax"), # Output layer with softmax activation for
classification
]
)

model.summary() # Print the model summary

3.3. Model Compilation and Training:


Python
batch_size = 128
epochs = 15

model.compile(loss="categorical_crossentropy", optimizer="adam", metrics=["accuracy"])

model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, validation_split=0.1) # Use a


validation set

score = model.evaluate(x_test, y_test, verbose=0)


print("Test loss:", score[0])
print("Test accuracy:", score[1])

3.4. Visualization (Optional):


You can visualize the filters learned by the convolutional layers (more advanced).

RESULT: - Hence, Building a Convolutional Neural Network for Image Classification.

PRACTICAL ASSIGNMENT:
1. What is the role of convolutional layers? How do they extract features?
2. What is the purpose of pooling layers?
3. Why is a softmax activation function used in the output layer?
4. How does the number of filters and kernel size affect the model's performance? (Experiment with these
parameters).
5. What are some strategies to improve the model's accuracy? (e.g., data augmentation, more layers,
different optimizers).

You might also like