Experiment 2
Experiment 2
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.
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.
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
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]
Flatten(),
Dense(128, activation='relu'),
Dropout(0.5),
Dense(3, activation='softmax')
])
model.summary()
Output as obtained
Training accuracy: 95.41%
Testing accuracy: 90.67%
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)
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.