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

Experiment 2

Uploaded by

mohammed.ansari
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)
30 views7 pages

Experiment 2

Uploaded by

mohammed.ansari
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

Experiment - 2

Name: Ansari Mohammed Shanouf Valijan


Class: B.E. Computer Engineering, Semester - VII
UID: 2021300004
Batch: M

Aim:
To build, train and test a convolutional neural network (CNN) model in order to solve a given
problem.

Objective:
To come up with a convolutional neural network model that takes in an image as the input
and outputs whether the image is that of an airplane, a car or a bird.

Dataset:
For the purpose of training the CNN model, TensorFlow’s predefined ‘CIFAR10’ dataset was
taken into consideration. However, among the 10 classes of objects present in the dataset,
only the images of airplanes, birds and cars were considered.

Theory:
Convolutional Neural Networks (CNNs) have revolutionized the field of machine learning by
providing a powerful tool for visual data analysis. These networks are inspired by the human
visual system, designed to automatically and adaptively learn spatial hierarchies of features
from input images. The core idea behind CNNs is to exploit the spatial structure of images by
applying convolutional filters that scan over the input data, capturing local patterns and
translating them into higher-level features. This process allows CNNs to effectively handle
large images by reducing their dimensionality through pooling operations, which aggregate
information and retain essential features while discarding noise. This hierarchical approach
to feature extraction enables CNNs to achieve remarkable performance in tasks such as image
classification, object detection, and image segmentation.
The architecture of CNNs is characterized by several key layers that work in tandem to process
and interpret visual information. Convolutional layers apply filters to the input data,
producing feature maps that represent the presence of various features across the image.
These filters are learned during the training process, allowing the network to automatically
discover relevant patterns. Pooling layers follow, reducing the spatial dimensions of the
feature maps while preserving the most important information. Finally, fully connected layers
at the end of the network integrate the extracted features to make predictions or
classifications. This combination of convolutional, pooling, and fully connected layers allows
CNNs to achieve high accuracy and generalize well to new, unseen data.

An Example of a CNN Model


CNNs are particularly well-suited for visual tasks due to their ability to learn and recognize
complex patterns with minimal preprocessing. Unlike traditional machine learning methods
that often require extensive feature engineering, CNNs can learn relevant features directly
from raw data. This capability is enhanced by their depth and the ability to handle multiple
layers of abstraction, from simple edges and textures to complex shapes and objects.
Furthermore, CNNs benefit from transfer learning, where pre-trained models on large
datasets can be fine-tuned for specific tasks, significantly reducing training time and resource
requirements. This adaptability and efficiency make CNNs a cornerstone in modern computer
vision applications and a driving force behind many advancements in artificial intelligence.

An Example Convolution Operation (Filter Size – 3x3, Stride – 2)


Overfitting and Underfitting in the Context of CNNs
In the context of Convolutional Neural Networks (CNNs), overfitting and underfitting are
critical concepts that influence the performance of a model. Overfitting occurs when a CNN
learns the training data too well, capturing not just the underlying patterns but also the noise
and specific details that do not generalize to new, unseen data. This happens when the model
is too complex relative to the amount and variety of training data, resulting in high accuracy
on the training set but poor performance on validation or test data. Techniques such as
dropout, regularization, and data augmentation are commonly used to mitigate overfitting by
preventing the model from becoming too specialized to the training data.

On the other hand, underfitting happens when a CNN is too simple to capture the underlying
patterns in the data, leading to poor performance on both the training and test sets. This can
occur when the model architecture is too shallow or lacks sufficient complexity to learn from
the data effectively. Underfitting can be addressed by increasing the network's capacity
through additional layers or filters, improving the quality and quantity of the training data, or
employing more sophisticated learning techniques. Balancing the complexity of the model
with the amount and quality of data is essential for achieving good generalization
performance and avoiding both overfitting and underfitting.

How can we identify them in a CNN model?


Identifying overfitting and underfitting in a Convolutional Neural Network (CNN) involves
monitoring performance metrics and visualizing training progress. Overfitting can be
detected by comparing the accuracy or loss on the training set versus the validation set. If the
training accuracy continues to improve while the validation accuracy stagnates or decreases,
it indicates that the model is memorizing the training data rather than generalizing.
Conversely, underfitting is evident when both the training and validation metrics remain poor,
suggesting that the model is not learning effectively from the data. Visualizations such as
learning curves, which plot training and validation loss over epochs, are particularly useful for
diagnosing these issues. Additionally, performance on a held-out test set can provide insight
into how well the model generalizes to unseen data, helping to confirm whether the issues
of overfitting or underfitting are present.

What can be done for their mitigation?


To mitigate overfitting in Convolutional Neural Networks (CNNs), several strategies can be
employed. Regularization techniques such as L1 or L2 regularization can penalize large
weights, reducing the model's complexity and helping it generalize better. Dropout layers,
which randomly deactivate a portion of neurons during training, further prevent the network
from becoming overly reliant on specific features. Data augmentation methods, like rotations
and flips, increase the diversity of the training dataset, which aids in generalization.
Additionally, early stopping involves halting training when the validation performance stops
improving, while cross-validation helps ensure that the model performs consistently across
different subsets of data. To address underfitting, one can start by increasing the model's
complexity through additional layers or more filters to capture more detailed patterns in the
data. Improving the quality and quantity of the training data, or using advanced architectures
and pre-trained models, can also enhance the model's ability to learn effectively. Adjusting
the learning rate and optimizing hyperparameters are crucial for improving the model’s
convergence and performance. These adjustments help ensure that the model is neither too
simplistic nor incapable of learning the underlying patterns in the data.

Implementation:
Following is a step-by-step implementation that was carried out in an attempt to solve the
problem at hand-
Link to notebook -> ConvolutionalNeuralNetwork

Importing the necessary libraries


import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout
from tensorflow.keras.utils import to_categorical
from tensorflow.keras.datasets import cifar10
import matplotlib.pyplot as plt
import numpy as np

Loading the dataset and splitting into train-test sets


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

Considering only first 3 classes (airplane, bird, car) for training and testing
selected_classes = [0, 1, 2]
train_indices = [i for i, label in enumerate(y_train) if label[0] in
selected_classes]
test_indices = [i for i, label in enumerate(y_test) if label[0] in
selected_classes]
x_train, y_train = x_train[train_indices], y_train[train_indices]
x_test, y_test = x_test[test_indices], y_test[test_indices]

Performing one-hot encoding on the output categories


y_train = to_categorical(y_train, num_classes=3)
y_test = to_categorical(y_test, num_classes=3)

Normalizing the images under consideration from the dataset


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

Defining the CNN model


model = Sequential([
Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3)),
MaxPooling2D(pool_size=(2, 2)),

Conv2D(64, (3, 3), activation='relu'),


MaxPooling2D(pool_size=(2, 2)),

Conv2D(128, (3, 3), activation='relu'),


MaxPooling2D(pool_size=(2, 2)),

Flatten(),
Dense(128, activation='relu'),
Dropout(0.5),
Dense(3, activation='softmax')
])

model.summary()

Model summary as obtained-

Compiling the model


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

Training the model


history = model.fit(x_train, y_train,
epochs=10,
batch_size=64,
validation_data=(x_test, y_test))

Evaluating the model


train_loss, train_acc = model.evaluate(x_train, y_train)
print(f'Training accuracy: {train_acc*100:.2f}%')

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


print(f'Testing accuracy: {test_acc*100:.2f}%')

Output as obtained
Training accuracy: 95.41%
Testing accuracy: 90.67%

Plotting the training history


plt.figure(figsize=(12, 4))

plt.subplot(1, 2, 1)
plt.plot(history.history['accuracy'], label='Train Accuracy')
plt.plot(history.history['val_accuracy'], label='Validation Accuracy')
plt.title('Model Accuracy')
plt.xlabel('Epoch')
plt.ylabel('Accuracy')
plt.legend(loc='upper left')

plt.subplot(1, 2, 2)
plt.plot(history.history['loss'], label='Train Loss')
plt.plot(history.history['val_loss'], label='Validation Loss')
plt.title('Model Loss')
plt.xlabel('Epoch')
plt.ylabel('Loss')
plt.legend(loc='upper left')

plt.tight_layout()
plt.show()

Plots as obtained
Making a prediction using the trained model
example_image = x_test[0]
example_image = np.expand_dims(example_image, axis=0)

predicted_probabilities = model.predict(example_image)
predicted_class = np.argmax(predicted_probabilities, axis=1)

class_names = ['airplane', 'car', 'bird']


print(f"Predicted class: {class_names[predicted_class[0]]}")
print(f"True class: {class_names[np.argmax(y_test[0])]}")

Output as obtained
Predicted class: airplane
True class: airplane

Inferences:
▪ The trained model was found to have an accuracy of 95.41 percent on training data,
while it demonstrated an accuracy of 90.67 percent on test data.
▪ From the plots of model accuracy as obtained from the data collected during the
training process, one can infer that initially, the accuracy was increasing at a faster rate
(till epoch 4).
▪ Further, the increase in accuracy was dimmed, but was still increasing at an
approximately linear rate.
▪ Similar trends were observed in the loss, rapidly decreasing initially, followed by a
diminution at a constant rate.

You might also like