Professional Documents
Culture Documents
Objectives:
• To implement image classification using back propagation neural network
• To evaluate the implementation
Theory:
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 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)
Result:
• 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.
• 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.
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.
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.