Clustering using pretrained deep models

MNIST clustering using pretrained ResNet 18 and KMeans.

In this task, you are going to cluster MNIST dataset using a pretrained ResNet 18 model and k-means.

Load the dataset

Load the MNIST dataset bellow. You can use either torchvision.datasets.MNIST or sklearn.datasets.fetch_openml() or any other way to load the dataset. Note that you won't need a validation set.

In [ ]:
 

Get ResNet 18

Instantiate ResNet 18 model (pretrained on imagenet) from torchvision's model zoo.

In [ ]:
 

Remove the decider layer

Replace the fc layer of the resnet with an Identity layer so that we use the hidden features of the last layer.

In [ ]:
 

Cluster the data

Use MiniBatchKMeans to cluster the MNIST dataset in 64 clusters.

In [ ]:
 

Assign clusters to test set.

Predict cluster for test set samples.

In [ ]:
 

Draw clusters

Draw 10 random samples per each cluster from the test set.

In [ ]: