TD 1 : Image Classification
MOD 4.6 Deep Learning & Artificial Intelligence: an introduction
Description
This project's goal is to implement image classification using two different models: k-nearest neighbors (KNN) and multilayer perceptron (MLP) neural network in Python. We will used the CIFAR-10 dataset, consisting of 60,000 color images of size 32x32 divided into 10 classes.
This project is composed of three python files : "read_cifar.py", "knn.py" and "mlp.py". And two directories : "data" (that contains the batches with the data) and "results" (that contains two PNG files showing examples of th training of the two models.
Visuals
The project generates accuracy plots ("knn.png" and "mlp.png") in the "results" directory, showing the performance of the implemented models.
Usage
In the file "read_cifar.py", the functions permit to read the dataset and shuffle it. The dataset is also split into training and testing sets. The split parameter determines the proportion of the data to be used for training.
In the file "knn.py", the functions evaluates the classification rate (accuracy) of the k-nearest neighbors algorithm using the provided training and testing data along with the number of neighbors (k). Initially, the distance_matrix function computes the distance matrix between your training dataset and the dataset that holds images whose labels you aim to predict. Subsequently, by employing the knn_predict function, you have the flexibility to specify the hyperparameter k and utilize the precomputed distance matrix to forecast the labels for your test dataset. Lastly, the evaluate_knn function assesses the accuracy of the k-nearest neighbors algorithm implemented earlier on our dataset, considering a specific value for the hyperparameter k. We notice that when k=1, it yields the highest accuracy, whereas k=2 results in the lowest. The accuracy hovers around 34% for the remaining values of k.
In the files "mlp.py", the focus is on implementing and training a multilayer perceptron (MLP) for classification tasks. It delivers training accuracies across epochs and the final testing accuracy. Users can employ these functions to effectively train and evaluate MLP classifiers for their specific classification datasets. The MLP undergoes learning steps at each epoch, where each step comprises a forward pass that generates predictions, a calculation of the loss, a backward pass to compute gradients for backpropagation, and the updating of the MLP's parameters. Both the learn_once_mse and learn_once_cross_entropy functions encapsulate an entire learning step, differing only in the loss calculation function they employ.