├── requirements.txt ├── .gitignore ├── metrics.py ├── README.md └── LICENSE /requirements.txt: -------------------------------------------------------------------------------- 1 | numpy 2 | tensorflow 3 | keras 4 | scipy 5 | matplotlib 6 | sklearn 7 | seaborn -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # ignore the results of running code 'results/' 2 | results/*/* 3 | results/*.csv 4 | !/results/exp1/results.csv 5 | 6 | *.pyc 7 | 8 | *.hdf5 9 | *.ipynb_checkpoints 10 | *.p 11 | *.h5 12 | *.HDF5 13 | __pycache__ -------------------------------------------------------------------------------- /metrics.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | from sklearn.metrics import normalized_mutual_info_score, adjusted_rand_score 3 | 4 | nmi = normalized_mutual_info_score 5 | ari = adjusted_rand_score 6 | 7 | 8 | def acc(y_true, y_pred): 9 | """ 10 | Calculate clustering accuracy. Require scikit-learn installed 11 | 12 | # Arguments 13 | y: true labels, numpy.array with shape `(n_samples,)` 14 | y_pred: predicted labels, numpy.array with shape `(n_samples,)` 15 | 16 | # Return 17 | accuracy, in [0,1] 18 | """ 19 | y_true = y_true.astype(np.int64) 20 | assert y_pred.size == y_true.size 21 | D = max(y_pred.max(), y_true.max()) + 1 22 | w = np.zeros((D, D), dtype=np.int64) 23 | for i in range(y_pred.size): 24 | w[y_pred[i], y_true[i]] += 1 25 | from sklearn.utils.linear_assignment_ import linear_assignment 26 | ind = linear_assignment(w.max() - w) 27 | return sum([w[i, j] for i, j in ind]) * 1.0 / y_pred.size -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # [How to do Unsupervised Clustering with Keras](https://www.dlology.com/blog/how-to-do-unsupervised-clustering-with-keras/) | DLology Blog 2 | 3 | 4 | ## How to Run 5 | Require [Python 3.5+](https://www.python.org/ftp/python/3.6.4/python-3.6.4.exe) and [Jupyter notebook](https://jupyter.readthedocs.io/en/latest/install.html) installed 6 | ### Clone or download this repo 7 | ``` 8 | git clone https://github.com/Tony607/Keras_Deep_Clustering 9 | ``` 10 | ### Install required libraries 11 | `pip3 install -r requirements.txt` 12 | 13 | 14 | In the project start a command line run 15 | ``` 16 | jupyter notebook 17 | ``` 18 | In the opened browser window open 19 | ``` 20 | Keras-DEC.ipynb 21 | ``` 22 | If you want to skip the training, you can try the pre-trained weights from the releases, [results.zip](https://github.com/Tony607/Keras_Deep_Clustering/releases/download/V0.1/results.zip). Extract 23 | `results` folders to the root of the project directory. 24 | 25 | Happy coding! Leave a comment if you have any question. 26 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | LICENSE 2 | 3 | The MIT License (MIT) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | --------------------------------------------------------------------------------