The task of the project is to perform unsupervised learning on Cifar 10 dataset. There are two task, the first is to perform K-means clustering on the raw data from scratch. The second is to perform K-means clustering on a representation generated by the Auto-Encoder method using library functions. The code should be written in Python using Keras.
The Cifar 10 dataset has a training set of 50,000 examples, and a test set of 10,000 examples. Each example is a 32x32 image, associated with a label from 10 classes. Each image is 32 pixels in height and 32 pixels in width, for a total of 1024 pixels in total. This pixel-value is an integer between 0 and 255. The training and test data sets have 1025 columns including the labels.
We will first load the cifar10 dataset and store the test and train data.
Next we will converts the images from RGB to Grayscale. The size of the last dimension becomes 1, containing the Grayscale value of the pixels.
To avoid making the computation more complex we will normalize the values to range 0 to 1. This is done by dividing the data by 255 (since the pixels range is 255).
Now we will reshape the images from a square of 32 X 32 pixels to 1024.
We will define 10 clusters and then initialize random centroids.
The next step is to define methods for updating and forming clusters.
We will now calculate the difference between the old and the new centroids. Until the difference is the least we will keep updating our centroids and form clusters with those updated centroids.
Now we will access the quality of the clusters using ASC (Average Silhouette Coefficient) and DI (Dunn’s Index) evaluation metrics.
For Dunn’s Index, I installed the validclust package separately.
The general idea of Auto-Encoders is that we have to set an encoder and a decoder as neural networks and to learn the best encoding-decoding scheme using an iterative optimisation process. So, at each iteration we feed the autoencoder architecture (the encoder followed by the decoder) with some data, we compare the encoded-decoded output with the initial data and back-propagate the error through the architecture to update the weights of the networks.
Here we have used encoder with two dense layers. One with 1024 for which we have flattened the x train data and then the other with 64. We will then define encoder model. This model will be further used for predicting the x train data. Similarly we have defined for the decoder part but in a reverse manner. That is the first layer has 64 then 1024.
We will then compile and fit our AutoEncoder Model using Adam optimizer and loss =’mse’.
Now using the Encoder model defined previously we will predict the x train data. Using K-Mean we generate clusters from the sparse representations generated by the Auto-Encoders.
Lastly we will access the quality of the clusters using ASC (Average Silhouette Coefficient).