forked from lrjconan/deep_parsimonious
-
Notifications
You must be signed in to change notification settings - Fork 1
/
eval_clustering.py
189 lines (140 loc) · 5.58 KB
/
eval_clustering.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
"""eval_clustering.py
Usage:
eval_clustering.py <exp_id>
"""
import os
import time
import math
import numpy as np
import tensorflow as tf
import nn_cell_lib as nn
import exp_config as cg
from docopt import docopt
from mini_batch_iter import MiniBatchIterator
from CIFAR_input import read_CIFAR10, read_CIFAR100
from CIFAR_models import (baseline_model, clustering_model, distilled_model,
hybrid_model)
from scipy.cluster.vq import kmeans2
from scipy.spatial import distance
EPS = 1.0e-16
def get_num(x):
return len(np.nonzero(x)[0])
def compute_mutual_information(cluster_label, class_label, num_cluster, num_class):
MI = 0.0
N = float(len(cluster_label))
for ii in xrange(num_class):
for jj in xrange(num_cluster):
idx_cluster = cluster_label == jj
idx_class = class_label == ii
W = get_num(idx_cluster)
C = get_num(idx_class)
J = get_num(idx_cluster & idx_class)
MI += J / N * np.log((N * J + EPS) / (W * C + EPS))
return MI
def compute_entropy(class_label, num_class):
H = 0.0
N = float(len(class_label))
for ii in xrange(num_class):
idx_class = class_label == ii
C = get_num(idx_class)
H -= C / N * np.log((C + EPS) / N)
return H
def compute_normalized_mutual_information(cluster_label, class_label, num_cluster, num_class):
MI = compute_mutual_information(
cluster_label, class_label, num_cluster, num_class)
H1 = compute_entropy(class_label, num_class)
H2 = compute_entropy(cluster_label, num_cluster)
NMI = 2 * MI / (H1 + H2)
return NMI
def Kmeans(data, num_K):
centroid, label = kmeans2(
data=data, k=num_K, iter=100, minit='points', missing='warn')
return centroid, label
def main():
# get exp parameters
args = docopt(__doc__)
param = getattr(cg, args['<exp_id>'])()
# read data from file
if param['dataset_name'] == 'CIFAR10':
input_data = read_CIFAR10(param['data_folder'])
elif param['dataset_name'] == 'CIFAR100':
input_data = read_CIFAR100(param['data_folder'])
else:
raise ValueError('Unsupported dataset name!')
print 'Reading data done!'
# build model
test_op_names = ['embeddings']
if param['model_name'] == 'baseline':
model_ops = baseline_model(param)
elif param['model_name'] == 'parsimonious':
model_ops = clustering_model(param)
elif param['model_name'] == 'distilled':
with tf.variable_scope('dist') as dist_var_scope:
model_ops = distilled_model(param)
elif param['model_name'] in ['hybrid_spatial', 'hybrid_sample']:
with tf.variable_scope('hybrid') as dist_var_scope:
model_ops = hybrid_model(param)
else:
raise ValueError('Unsupported model name!')
test_ops = [model_ops[i] for i in test_op_names]
print 'Building model done!'
# run model
input_data['train_img'] = np.concatenate(
[input_data['train_img'], input_data['val_img']], axis=0)
input_data['train_label'] = np.concatenate(
[input_data['train_label'], input_data['val_label']])
num_train_img = input_data['train_img'].shape[0]
max_test_iter = int(math.ceil(num_train_img / param['bat_size']))
test_iterator = MiniBatchIterator(
idx_start=0, bat_size=param['bat_size'], num_sample=num_train_img,
train_phase=False, is_permute=False)
config = tf.ConfigProto(allow_soft_placement=True)
sess = tf.Session(config=config)
saver = tf.train.Saver()
saver.restore(sess, os.path.join(
param['test_folder'], param['test_model_name']))
print 'Graph initialization done!'
if param['model_name'] == 'parsimonious':
param['num_layer_cnn'] = len(
[xx for xx in param['num_cluster_cnn'] if xx])
param['num_layer_mlp'] = len(
[xx for xx in param['num_cluster_mlp'] if xx])
num_layer_reg = param['num_layer_cnn'] + param['num_layer_mlp']
cluster_center = sess.run(model_ops['cluster_center'])
else:
num_layer_cnn = len(param['act_func_cnn'])
num_layer_mlp = len(param['act_func_mlp'])
num_layer_reg = num_layer_cnn + num_layer_mlp
cluster_center = [None] * num_layer_reg
embeddings = [[] for _ in xrange(num_layer_reg)]
for test_iter in xrange(max_test_iter):
idx_bat = test_iterator.get_batch()
bat_imgs = (input_data['train_img'][idx_bat, :, :, :].astype(
np.float32) - input_data['mean_img']) / 255.0
feed_data = {model_ops['input_images']: bat_imgs}
results = sess.run(test_ops, feed_dict=feed_data)
test_results = {}
for res, name in zip(results, test_op_names):
test_results[name] = res
for ii, ee in enumerate(test_results['embeddings']):
if ii < 3:
continue
embeddings[ii] += [ee]
for ii in xrange(num_layer_reg):
if ii < 3:
continue
embeddings[ii] = np.concatenate(embeddings[ii], axis=0)
# kmeans
centroid, tmp_label = Kmeans(embeddings[ii], 100)
cluster_center[ii] = centroid
# deep clustering
pdist = distance.cdist(
cluster_center[ii], embeddings[ii], 'sqeuclidean')
tmp_label = np.argsort(pdist, axis=0)[0]
sort_idx = np.argsort(pdist, axis=1)
NMI = compute_normalized_mutual_information(tmp_label, input_data['train_label'].astype(
np.int), cluster_center[ii].shape[0], param['label_size'])
print 'NMI = {}'.format(NMI)
sess.close()
if __name__ == '__main__':
main()