ElieAljalbout/Clustering-with-Deep-learning

Generic implementation for clustering with deep learning : representation learning (DNN) + clustering

Python

296

1 commits

updated Jul 7, 2018

See the code

README

Deep Learning for Clustering

Code for project "Deep Learning for Clustering" under lab course "Deep Learning for Computer Vision and Biomedicine" - TUM. Depends on numpy, theano, lasagne, scikit-learn, matplotlib.

Contributors

This repository is an implementation of the paper : Elie Aljalbout, Vladimir Golkov, Yawar Siddiqui, Daniel Cremers "Clustering with Deep Learning: Taxonomy and new methods"

Usage

Use the main script for training, visualizing clusters and/or reporting clustering metrics

python main.py <options>
Option
-d DATASET_NAME, --dataset DATASET_NAME (Required) Dataset on which autoencoder is to be trained trained, or metrics/visualizations are to be performed [MNIST,COIL20]
-a ARCH_IDX, --architecture ARCH_IDX(Required) Index of architecture of autoencoder in the json file (archs/)
--pretrain EPOCHSPretrain the autoencoder for specified #epochs specified by architecture on specified dataset
--cluster EPOCHSRefine the autoencoder for specified #epochs with clustering loss, assumes that pretraining results are available
--metricsReport k-means clustering metrics on the clustered latent space, assumes pretrain and cluster based training have been performed
--visualizeVisualize the image space and latent space, assumes pre-training and cluster based training have been performed

Project Structure

Folder / FileDescription
archsContains json files specifying architectures for autoencoder networks used. File mnist.json contains architectures for MNIST dataset. We use the second architecture for the reported results (command line argument -a 1)
coil, mnistContains the datasets COIL20 and MNIST respectively
logsOutput folder for logs generated by the scripts. Named by date and time of script execution
plotsScatter plots showing the raw, pre-trained latent space, and the final latent space clusters
saved_paramsContains saved network parameters and saved representation of inputs in latent space
custom_layers.pyCustom lasagne layers, Unpool2D - which performs inverse max pooling by replicating input pixels as dictated by the filter size, and the ClusteringLayer - a layer that outputs soft cluster assignments based on k-means cluster distance
main.pyThe main python script for training and evaluating the network
misc.pyContains dataset handlers and other utility methods
network.pyContains classes for parsing and building the network from json files and also for training the network

Autoencoder Builder

We've implemented a NetworkBuilder class that can be used to quickly describe the architecture of an autoencoder through a json file. The json specification of the architecture is a dictionary with the following fields

FieldDescription
nameName identifier given to the architecture, used for file naming while saving parameters
batch_sizeBatch size to be used while training the network
use_batch_normWhether to use batch normalization for convolutional/deconvolutional layers
network_typeType of network - convolutional or fully connected
layersA list describing the encoder part of the autoencoder

Further, each item in the layers list is a dictionary with the following fields

FieldDescription
typeCan be Input, Conv2D, MaxPool2D, MaxPool2D*, Dense, Reshape, Deconv2D
num_filtersFor Conv2D/MaxPool2D/MaxPool2D*/Deconv2D layers this field specifies number of filters
filter_sizeDimensions of kernel for the above layers
num_unitsFor Dense layer number of hidden units
non_linearityNon-Linearity function used at output of the layer
conv_modeCan be used to specify the convolution mode like same, valid etc. for convolutional layers
output_non_linearityIf you want a different non linearity function at the output than the one which would be obtained by mirroring

Only the encoder part of the autoencoder needs to be specified, the decoder will be automatically generated by the class.

Example of a network description

{
    "name": "c-5-6_p_c-5-16_p_c-4-120",
    "use_batch_norm": 1,
    "batch_size": 100,
    "layers": [
      {
        "type": "Input",
        "output_shape":[1, 28, 28]
      },
      {
        "type": "Conv2D",
        "num_filters": 50,
        "filter_size": [5, 5],
        "non_linearity": "rectify"
      },
      {
        "type": "MaxPool2D*",
        "filter_size": [2, 2]
      },
      {
        "type": "Conv2D",
        "num_filters": 50,
        "filter_size": [5, 5],
        "non_linearity": "rectify"
      },
      {
        "type": "MaxPool2D*",
        "filter_size": [2, 2]
      },
      {
        "type": "Conv2D",
        "num_filters": 120,
        "filter_size": [4, 4],
        "non_linearity": "linear"
      }
    ]
  }

This would generate the network 50[5x5] 50[5x5]_bn max[2x2] 50[5x5] 50[5x5]_bn max[2x2] **120[4x4] 120[4x4]_bn **50[4x4] 50[4x4]_bn ups*[2x2] 50[5x5] 50[5x5]_bn ups*[2x2] 1[5x5]

Experiments and Results

We trained and tested the network on two datasets - MNIST and COIL20

DatasetImage sizeNumber of samplesNumber of clusters
MNIST28x28x16000010
COIL20128x128x1144020

Clustering was performed with two different loss functions -

  • Loss = KL-Divergence(soft assignment distribution, target distribution) + Autoencoder Reconstruction loss , where the target distribution is a distribution that improves cluster purity and puts more emphasis on data points assigned with a high confidence. For more details check out the DEC paper [1].
  • Loss = k-Means loss + Autoencoder Reconstruction loss

MNIST

Our network
Clustering spaceClustering AccuracyNormalized Mutual Information
Image pixels0.5420.480
Autoencoder0.7600.667
Autoencoder + k-Means Loss0.7810.796
Autoencoder + KLDiv Loss0.8590.825
Other networks
MethodClustering AccuracyNormalized Mutual Information
DEC0.8430.800
DCN0.8300.810
CNN-RC-0.915
CNN-FD-0.876
DBC0.9640.917

Note: The commit b34743114f68624b5371cd0d4c059b141422902f gives upto 0.96 accuracy and 0.92 NMI on the MNIST dataset. We will include it to the main branch once we can get better results with the COIL architecture

Latent space visualizations
Pixel space

Autoencoder

Autoencoder Latent Space Evolution (video)

Autoencoder

Autoencoder + KLDivergence

Autoencoder + KLDivergence Latent Space Evolution (video)

Autoencoder

Autoencoder + k-Means

COIL20

Our network
Clustering spaceClustering AccuracyNormalized Mutual Information
Image pixels0.6890.793
Autoencoder0.7390.828
Autoencoder + k-Means Loss0.7450.846
Autoencoder + KLDiv Loss0.7620.848
Other networks
MethodClustering AccuracyNormalized Mutual Information
DEN0.7250.870
CNN-RC-1.000
DBC0.7930.895
Latent space visualizations
Pixel space

Autoencoder

Autoencoder + k-Means

Autoencoder + KLDivergence

clustering
deep-learning
machine-learning

Contributors

ElieAljalbout

1 commits

ElieAljalbout/Clustering-with-Deep-learning

Generic implementation for clustering with deep learning : representation learning (DNN) + clustering

Python

296

1 commits

updated Jul 7, 2018

See the code

README

Deep Learning for Clustering

Code for project "Deep Learning for Clustering" under lab course "Deep Learning for Computer Vision and Biomedicine" - TUM. Depends on numpy, theano, lasagne, scikit-learn, matplotlib.

Contributors

This repository is an implementation of the paper : Elie Aljalbout, Vladimir Golkov, Yawar Siddiqui, Daniel Cremers "Clustering with Deep Learning: Taxonomy and new methods"

Usage

Use the main script for training, visualizing clusters and/or reporting clustering metrics

python main.py <options>
Option
-d DATASET_NAME, --dataset DATASET_NAME (Required) Dataset on which autoencoder is to be trained trained, or metrics/visualizations are to be performed [MNIST,COIL20]
-a ARCH_IDX, --architecture ARCH_IDX(Required) Index of architecture of autoencoder in the json file (archs/)
--pretrain EPOCHSPretrain the autoencoder for specified #epochs specified by architecture on specified dataset
--cluster EPOCHSRefine the autoencoder for specified #epochs with clustering loss, assumes that pretraining results are available
--metricsReport k-means clustering metrics on the clustered latent space, assumes pretrain and cluster based training have been performed
--visualizeVisualize the image space and latent space, assumes pre-training and cluster based training have been performed

Project Structure

Folder / FileDescription
archsContains json files specifying architectures for autoencoder networks used. File mnist.json contains architectures for MNIST dataset. We use the second architecture for the reported results (command line argument -a 1)
coil, mnistContains the datasets COIL20 and MNIST respectively
logsOutput folder for logs generated by the scripts. Named by date and time of script execution
plotsScatter plots showing the raw, pre-trained latent space, and the final latent space clusters
saved_paramsContains saved network parameters and saved representation of inputs in latent space
custom_layers.pyCustom lasagne layers, Unpool2D - which performs inverse max pooling by replicating input pixels as dictated by the filter size, and the ClusteringLayer - a layer that outputs soft cluster assignments based on k-means cluster distance
main.pyThe main python script for training and evaluating the network
misc.pyContains dataset handlers and other utility methods
network.pyContains classes for parsing and building the network from json files and also for training the network

Autoencoder Builder

We've implemented a NetworkBuilder class that can be used to quickly describe the architecture of an autoencoder through a json file. The json specification of the architecture is a dictionary with the following fields

FieldDescription
nameName identifier given to the architecture, used for file naming while saving parameters
batch_sizeBatch size to be used while training the network
use_batch_normWhether to use batch normalization for convolutional/deconvolutional layers
network_typeType of network - convolutional or fully connected
layersA list describing the encoder part of the autoencoder

Further, each item in the layers list is a dictionary with the following fields

FieldDescription
typeCan be Input, Conv2D, MaxPool2D, MaxPool2D*, Dense, Reshape, Deconv2D
num_filtersFor Conv2D/MaxPool2D/MaxPool2D*/Deconv2D layers this field specifies number of filters
filter_sizeDimensions of kernel for the above layers
num_unitsFor Dense layer number of hidden units
non_linearityNon-Linearity function used at output of the layer
conv_modeCan be used to specify the convolution mode like same, valid etc. for convolutional layers
output_non_linearityIf you want a different non linearity function at the output than the one which would be obtained by mirroring

Only the encoder part of the autoencoder needs to be specified, the decoder will be automatically generated by the class.

Example of a network description

{
    "name": "c-5-6_p_c-5-16_p_c-4-120",
    "use_batch_norm": 1,
    "batch_size": 100,
    "layers": [
      {
        "type": "Input",
        "output_shape":[1, 28, 28]
      },
      {
        "type": "Conv2D",
        "num_filters": 50,
        "filter_size": [5, 5],
        "non_linearity": "rectify"
      },
      {
        "type": "MaxPool2D*",
        "filter_size": [2, 2]
      },
      {
        "type": "Conv2D",
        "num_filters": 50,
        "filter_size": [5, 5],
        "non_linearity": "rectify"
      },
      {
        "type": "MaxPool2D*",
        "filter_size": [2, 2]
      },
      {
        "type": "Conv2D",
        "num_filters": 120,
        "filter_size": [4, 4],
        "non_linearity": "linear"
      }
    ]
  }

This would generate the network 50[5x5] 50[5x5]_bn max[2x2] 50[5x5] 50[5x5]_bn max[2x2] **120[4x4] 120[4x4]_bn **50[4x4] 50[4x4]_bn ups*[2x2] 50[5x5] 50[5x5]_bn ups*[2x2] 1[5x5]

Experiments and Results

We trained and tested the network on two datasets - MNIST and COIL20

DatasetImage sizeNumber of samplesNumber of clusters
MNIST28x28x16000010
COIL20128x128x1144020

Clustering was performed with two different loss functions -

  • Loss = KL-Divergence(soft assignment distribution, target distribution) + Autoencoder Reconstruction loss , where the target distribution is a distribution that improves cluster purity and puts more emphasis on data points assigned with a high confidence. For more details check out the DEC paper [1].
  • Loss = k-Means loss + Autoencoder Reconstruction loss

MNIST

Our network
Clustering spaceClustering AccuracyNormalized Mutual Information
Image pixels0.5420.480
Autoencoder0.7600.667
Autoencoder + k-Means Loss0.7810.796
Autoencoder + KLDiv Loss0.8590.825
Other networks
MethodClustering AccuracyNormalized Mutual Information
DEC0.8430.800
DCN0.8300.810
CNN-RC-0.915
CNN-FD-0.876
DBC0.9640.917

Note: The commit b34743114f68624b5371cd0d4c059b141422902f gives upto 0.96 accuracy and 0.92 NMI on the MNIST dataset. We will include it to the main branch once we can get better results with the COIL architecture

Latent space visualizations
Pixel space

Autoencoder

Autoencoder Latent Space Evolution (video)

Autoencoder

Autoencoder + KLDivergence

Autoencoder + KLDivergence Latent Space Evolution (video)

Autoencoder

Autoencoder + k-Means

COIL20

Our network
Clustering spaceClustering AccuracyNormalized Mutual Information
Image pixels0.6890.793
Autoencoder0.7390.828
Autoencoder + k-Means Loss0.7450.846
Autoencoder + KLDiv Loss0.7620.848
Other networks
MethodClustering AccuracyNormalized Mutual Information
DEN0.7250.870
CNN-RC-1.000
DBC0.7930.895
Latent space visualizations
Pixel space

Autoencoder

Autoencoder + k-Means

Autoencoder + KLDivergence

clustering
deep-learning
machine-learning

Contributors

ElieAljalbout

1 commits

Languages

Python

100.0%