You are on page 1of 8

No of the Experiment: 03

Name of the Experiment:


Image Classification Using Backpropagation Neural Network Without Using Built-in Function

Objectives:
• To implement image classification using back propagation neural network
• To evaluate the implementation

Theory:

Back Propagation Neural Network:


Backpropagation neural network is a type of artificial neural network that is used to train deep
learning models. The basic idea behind backpropagation is to adjust the weights of the neural
network based on the errors that are propagated backwards from the output layer to the input
layer. During the training process, the network makes predictions on the input data, and the
output is compared with the true values. The difference between the predicted and true values is
the error, which is then propagated backwards through the layers of the network. The
backpropagation algorithm uses this error information to update the weights and biases of the
neurons in each layer, in order to minimize the overall error between the predicted and true
values. The process is repeated over multiple iterations until the error is minimized to a
satisfactory level. Backpropagation is commonly used for supervised learning tasks, such as
image classification, natural language processing, and speech recognition.

How back propagation neural network can help in image classification:


Backpropagation neural network (BPNN) can help in image classification by learning to
recognize patterns in image data and classifying them into different categories or labels. The
network is trained on a dataset of labeled images, where each image is associated with a specific
label or category. During training, the network learns to extract relevant features from the images
and use them to make accurate predictions about the labels of new images. The backpropagation
algorithm is used to adjust the weights of the connections between neurons in the network, based
on the error between the predicted label and the true label of the training data. This process helps
the network to learn and improve its ability to classify images correctly. In a typical image
classification task, the input to the network is an image represented as a matrix of pixel values,
and the output is a vector of probabilities, where each element represents the likelihood of the
image belonging to a particular class. The class with the highest probability is selected as the
predicted label for the image. BPNNs can be effective in image classification tasks because they
are capable of learning complex, non-linear relationships between the input image and its
corresponding label. This allows them to handle the variability and complexity of real-world
image data, and to generalize well to new, unseen images. BPNNs have been used successfully
in a wide range of image classification applications, including object recognition, facial
recognition, medical image analysis, and more.
Implementation:
Here, a dataset is taken with various moods from “Tom & Jerry” cartoon show to see if I can
classify them according to various moods for example: "unknown", "surprised", "sad", "happy",
"angry" and evaluate them:
Unknown
Surprised
Sad
Happy
Angry

Figure 3.1: The partial portion of dataset


Code:
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
import numpy as np
import cv2
import os

class NeuralNetwork:
def __init__(self, input_size, hidden_size, output_size):
self.input_size = input_size
self.hidden_size = hidden_size
self.output_size = output_size
self.weights1 = np.random.rand(input_size, hidden_size)
self.bias1 = np.random.rand(hidden_size)
self.weights2 = np.random.rand(hidden_size, output_size)
self.bias2 = np.random.rand(output_size)

def feedforward(self, inputs):


hidden_layer = sigmoid(np.dot(inputs, self.weights1) + self.bias1)
output_layer = sigmoid(np.dot(hidden_layer, self.weights2) + self.bias2)
return output_layer

def backpropagation(self, inputs, targets, learning_rate):


hidden_layer = sigmoid(np.dot(inputs, self.weights1) + self.bias1)
predictions = sigmoid(np.dot(hidden_layer, self.weights2) + self.bias2)
errors = predictions - targets
delta2 = np.dot(hidden_layer.T, errors * sigmoid_derivative(predictions))
delta1 = np.dot(inputs.T, np.dot(errors * sigmoid_derivative(predictions), self.weights2.T)
* sigmoid_derivative(hidden_layer))
self.weights2 -= learning_rate * delta2
self.bias2 -= learning_rate * np.mean(errors * sigmoid_derivative(predictions), axis=0)
self.weights1 -= learning_rate * delta1
self.bias1 -= learning_rate * np.mean(np.dot(errors * sigmoid_derivative(predictions),
self.weights2.T) * sigmoid_derivative(hidden_layer), axis=0)

def sigmoid(x):
return 1 / (1 + np.exp(-x))

def sigmoid_derivative(x):
return sigmoid(x) * (1 - sigmoid(x))

DATADIR = r"C:\Users\mashi\Downloads\archive\train\train"
CATEGORIES = ["unknown","surprised","sad","happy","angry"]
training_data = []
IMG_SIZE = 100
for category in CATEGORIES:
path = os.path.join(DATADIR, category)
class_num = CATEGORIES.index(category)
for img in os.listdir(path):
img_array = cv2.imread(os.path.join(path, img))
img_array = cv2.cvtColor(img_array, cv2.COLOR_BGR2GRAY)
new_array = cv2.resize(img_array, (IMG_SIZE, IMG_SIZE))
training_data.append([new_array, class_num])

X = []
y = []
for features, label in training_data:
X.append(features.flatten())
y.append(label)
X = np.array(X)
y = np.array(y)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3)

X_train = X_train / 255.0


X_test = X_test / 255.0

num_inputs = IMG_SIZE * IMG_SIZE


num_hidden = 32
num_outputs = len(CATEGORIES)
learning_rate = 0.1
nn = NeuralNetwork(num_inputs, num_hidden, num_outputs)
for i in range(1000):
nn.backpropagation(X_train, np.eye(num_outputs)[y_train], learning_rate)

# Making predictions on test data


predictions = np.argmax(nn.feedforward(X_test), axis=1)
# Convert predictions to integers
predictions = predictions.astype(int)
# Results
e = np.mean(predictions == y_test.reshape(-1, 1))
print(f"Evaluation: {e*100:.2f}%")

fig, axs = plt.subplots(3, 5)


fig.suptitle(f"Predictions with {e*100:.2f}% accuracy")
for i in range(15):
img = X_test[i].reshape(IMG_SIZE, IMG_SIZE)
img = cv2.normalize(img, None, alpha=0, beta=255, norm_type=cv2.NORM_MINMAX,
dtype=cv2.CV_8U)
img_3ch = cv2.cvtColor(img, cv2.COLOR_GRAY2RGB) # convert to 3 channels
prediction = CATEGORIES[predictions[i]]
axs[i//5, i%5].imshow(img_3ch)
axs[i//5, i%5].set_title(prediction)
plt.show()

Result:

Figure 3.2: The efficiency and result

Discussion on Code Explanation:


This script is a Python implementation of a simple neural network using backpropagation to
classify grayscale images of faces into five emotion categories (unknown, surprised, sad, happy,
and angry).
• The first few lines of code import the necessary libraries: Matplotlib for plotting, Scikit-
learn for splitting the data into training and testing sets, NumPy for mathematical
operations, OpenCV (cv2) for image processing, and os for working with directories.

• Next, the `NeuralNetwork` class is defined, which takes three arguments: `input_size`
(number of input neurons), `hidden_size` (number of neurons in the hidden layer), and
`output_size` (number of output neurons). The constructor initializes the weights and
biases of the two layers with random values.

• The `feedforward` method takes an input and passes it through the network using the
sigmoid activation function, returning the output layer.

• The `backpropagation` method calculates the errors between the predicted output and
the target output, and adjusts the weights and biases accordingly to minimize the errors.

• The `sigmoid` function returns the sigmoid of a given value, and `sigmoid_derivative`
returns the derivative of the sigmoid function.

• The `DATADIR` and `CATEGORIES` variables define the path to the image data and
the categories of emotions.

• The `training_data` list is created by looping through the image directory, reading in the
images, converting them to grayscale, resizing them to a standard size, and appending
them to the list along with their corresponding emotion category label.

• The `X` and `y` lists are populated by flattening the 2D image arrays into 1D arrays and
appending them to `X`, and appending the emotion category labels to `y`. `X` and `y`
are then converted to NumPy arrays.

• The `train_test_split` function is used to split the data into training and testing sets, with
a test size of 30%.

• The pixel values of the images are normalized to the range [0, 1] by dividing them by
255.0.

• The `num_inputs`, `num_hidden`, `num_outputs`, and `learning_rate` variables are


defined.

• An instance of the `NeuralNetwork` class is created with the defined parameters, and the
backpropagation algorithm is run 1000 times on the training data to train the network.

• The `feedforward` method is used to make predictions on the testing data, and the
`argmax` function is used to find the index of the maximum value in each output vector,
which corresponds to the predicted emotion category. The predictions are then converted
to integers.
• The accuracy of the predictions is calculated by comparing them to the true labels and
taking the mean of the resulting Boolean array.

• Finally, a 3x5 grid of test images and their predicted emotions is plotted using Matplotlib.

Ways of Improving Accuracy:

There are several ways to improve the accuracy of a neural network model. Here are some
suggestions:

• Increasing the size of the hidden layer: The size of the hidden layer affects the capacity
of the model to learn more complex patterns. By increasing the size of the hidden layer,
the model will have more capacity to learn and may be able to capture more subtle
features in the data.

• Adding more hidden layers: Deep neural networks are able to learn complex
representations of data by stacking multiple layers of neurons. Adding more hidden
layers to the neural network may improve its ability to learn complex patterns and
improve its accuracy.

• Changing the activation function: The sigmoid activation function used in this code can
suffer from the "vanishing gradient" problem, where gradients become very small as
they propagate through the layers, making it difficult for the model to learn. Changing
to a different activation function such as ReLU, which does not suffer from this problem,
may improve the model's performance.

• Increasing the number of epochs: The number of training epochs is the number of times
the model goes through the entire training dataset. Increasing the number of epochs may
allow the model to learn more from the data and improve its accuracy. However, it's
important to avoid overfitting by monitoring the model's performance on a validation set
and stopping training when the validation accuracy starts to decrease.

• Data augmentation: Data augmentation techniques can help increase the size of the
training set by creating new variations of existing images. Techniques such as flipping,
rotating, and shifting can help the model generalize better to new images and improve
its accuracy.

• Regularization: Regularization techniques such as L1 and L2 regularization can help


prevent overfitting by adding a penalty term to the loss function that discourages large
weights in the model. This can help the model generalize better to new images and
improve its accuracy.

• Using a pre-trained model: Transfer learning is a technique where a pre-trained model


on a large dataset is used as a starting point for a new task. By using a pre-trained model,
the model can benefit from the knowledge learned from the large dataset and improve
its accuracy on the new task.

Conclusion:
In conclusion, backpropagation neural networks are a powerful tool for image classification
tasks. This implementation uses a simple architecture with one hidden layer and sigmoid
activation function. The model is trained on grayscale images of faces with five categories:
unknown, surprised, sad, happy, and angry. The training set is split into training and testing data,
and the model is trained using backpropagation algorithm. The results show that the model
achieved an accuracy of approximately 70% on the test set. With further optimization of the
architecture and hyperparameters, backpropagation neural networks can be used to achieve high
accuracy in image classification tasks.

You might also like