-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathDSC-Net-L2-MNIST.py
390 lines (301 loc) · 14.4 KB
/
DSC-Net-L2-MNIST.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Oct 9 18:00:12 2018
@author: kowshik
"""
# Code Authors: Pan Ji, University of Adelaide, [email protected]
# Tong Zhang, Australian National University, [email protected]
# Copyright Reserved!
import tensorflow as tf
import numpy as np
from tensorflow.contrib import layers
import scipy.io as sio
from scipy.sparse.linalg import svds
from sklearn import cluster
from sklearn.preprocessing import normalize,minmax_scale
from munkres import Munkres
class ConvAE(object):
def __init__(self, n_input, kernel_size, n_hidden, reg_constant1 = 1.0, re_constant2 = 1.0, batch_size = 200, reg = None, \
denoise = False, model_path = None, restore_path = None, \
logs_path = '/home/pan/workspace-eclipse/deep-subspace-clustering/models_face/logs'):
self.n_input = n_input
self.kernel_size = kernel_size
self.n_hidden = n_hidden
self.batch_size = batch_size
self.reg = reg
self.model_path = model_path
self.restore_path = restore_path
self.iter = 0
#input required to be fed
self.x = tf.placeholder(tf.float32, [None, n_input[0], n_input[1], 1])
self.learning_rate = tf.placeholder(tf.float32, [])
weights = self._initialize_weights()
if denoise == False:
x_input = self.x
latent, shape = self.encoder(x_input, weights)
else:
x_input = tf.add(self.x, tf.random_normal(shape=tf.shape(self.x),
mean = 0,
stddev = 0.2,
dtype=tf.float32))
latent, shape = self.encoder(x_input, weights)
z = tf.reshape(latent, [batch_size, -1])
Coef = weights['Coef']
z_c = tf.matmul(Coef,z)
self.Coef = Coef
latent_c = tf.reshape(z_c, tf.shape(latent))
self.z = z
self.x_r = self.decoder(latent_c, weights, shape)
# l_2 reconstruction loss
self.reconst_cost = 0.5 * tf.reduce_sum(tf.pow(tf.subtract(self.x_r, self.x), 2.0))
tf.summary.scalar("recons_loss", self.reconst_cost)
self.reg_losses = tf.reduce_sum(tf.pow(self.Coef,2.0))
tf.summary.scalar("reg_loss", reg_constant1 * self.reg_losses )
self.selfexpress_losses = 0.5 * tf.reduce_sum(tf.pow(tf.subtract(z_c, z), 2.0))
tf.summary.scalar("selfexpress_loss", re_constant2 * self.selfexpress_losses )
self.loss = self.reconst_cost + reg_constant1 * self.reg_losses + re_constant2 * self.selfexpress_losses
self.merged_summary_op = tf.summary.merge_all()
self.optimizer = tf.train.AdamOptimizer(learning_rate = self.learning_rate).minimize(self.loss) #GradientDescentOptimizer #AdamOptimizer
self.init = tf.global_variables_initializer()
self.sess = tf.InteractiveSession()
self.sess.run(self.init)
self.saver = tf.train.Saver([v for v in tf.trainable_variables() if not (v.name.startswith("Coef"))])
self.summary_writer = tf.summary.FileWriter(logs_path, graph=tf.get_default_graph())
def _initialize_weights(self):
all_weights = dict()
all_weights['enc_w0'] = tf.get_variable("enc_w0", shape=[self.kernel_size[0], self.kernel_size[0], 1, self.n_hidden[0]],
initializer=layers.xavier_initializer_conv2d(),regularizer = self.reg)
all_weights['enc_b0'] = tf.Variable(tf.zeros([self.n_hidden[0]], dtype = tf.float32))
all_weights['enc_w1'] = tf.get_variable("enc_w1", shape=[self.kernel_size[1], self.kernel_size[1], self.n_hidden[0],self.n_hidden[1]],
initializer=layers.xavier_initializer_conv2d(),regularizer = self.reg)
all_weights['enc_b1'] = tf.Variable(tf.zeros([self.n_hidden[1]], dtype = tf.float32))
all_weights['enc_w2'] = tf.get_variable("enc_w2", shape=[self.kernel_size[2], self.kernel_size[2], self.n_hidden[1],self.n_hidden[2]],
initializer=layers.xavier_initializer_conv2d(),regularizer = self.reg)
all_weights['enc_b2'] = tf.Variable(tf.zeros([self.n_hidden[2]], dtype = tf.float32))
all_weights['Coef'] = tf.Variable(1.0e-4 * tf.ones([self.batch_size, self.batch_size],tf.float32), name = 'Coef')
all_weights['dec_w0'] = tf.get_variable("dec_w0", shape=[self.kernel_size[2], self.kernel_size[2], self.n_hidden[1],self.n_hidden[2]],
initializer=layers.xavier_initializer_conv2d(),regularizer = self.reg)
all_weights['dec_b0'] = tf.Variable(tf.zeros([self.n_hidden[1]], dtype = tf.float32))
all_weights['dec_w1'] = tf.get_variable("dec_w1", shape=[self.kernel_size[1], self.kernel_size[1], self.n_hidden[0],self.n_hidden[1]],
initializer=layers.xavier_initializer_conv2d(),regularizer = self.reg)
all_weights['dec_b1'] = tf.Variable(tf.zeros([self.n_hidden[0]], dtype = tf.float32))
all_weights['dec_w2'] = tf.get_variable("dec_w2", shape=[self.kernel_size[0], self.kernel_size[0],1, self.n_hidden[0]],
initializer=layers.xavier_initializer_conv2d(),regularizer = self.reg)
all_weights['dec_b2'] = tf.Variable(tf.zeros([1], dtype = tf.float32))
return all_weights
# Building the encoder
def encoder(self,x, weights):
shapes = []
# Encoder Hidden layer with relu activation #1
shapes.append(x.get_shape().as_list())
layer1 = tf.nn.bias_add(tf.nn.conv2d(x, weights['enc_w0'], strides=[1,2,2,1],padding='SAME'),weights['enc_b0'])
layer1 = tf.nn.relu(layer1)
shapes.append(layer1.get_shape().as_list())
layer2 = tf.nn.bias_add(tf.nn.conv2d(layer1, weights['enc_w1'], strides=[1,2,2,1],padding='SAME'),weights['enc_b1'])
layer2 = tf.nn.relu(layer2)
shapes.append(layer2.get_shape().as_list())
layer3 = tf.nn.bias_add(tf.nn.conv2d(layer2, weights['enc_w2'], strides=[1,2,2,1],padding='SAME'),weights['enc_b2'])
layer3 = tf.nn.relu(layer3)
return layer3, shapes
# Building the decoder
def decoder(self,z, weights, shapes):
# Encoder Hidden layer with relu activation #1
shape_de1 = shapes[2]
layer1 = tf.add(tf.nn.conv2d_transpose(z, weights['dec_w0'], tf.stack([tf.shape(self.x)[0],shape_de1[1],shape_de1[2],shape_de1[3]]),\
strides=[1,2,2,1],padding='SAME'),weights['dec_b0'])
layer1 = tf.nn.relu(layer1)
shape_de2 = shapes[1]
layer2 = tf.add(tf.nn.conv2d_transpose(layer1, weights['dec_w1'], tf.stack([tf.shape(self.x)[0],shape_de2[1],shape_de2[2],shape_de2[3]]),\
strides=[1,2,2,1],padding='SAME'),weights['dec_b1'])
layer2 = tf.nn.relu(layer2)
shape_de3= shapes[0]
layer3 = tf.add(tf.nn.conv2d_transpose(layer2, weights['dec_w2'], tf.stack([tf.shape(self.x)[0],shape_de3[1],shape_de3[2],shape_de3[3]]),\
strides=[1,2,2,1],padding='SAME'),weights['dec_b2'])
layer3 = tf.nn.relu(layer3)
return layer3
def partial_fit(self, X, lr): #
cost, summary, _, Coef = self.sess.run((self.reconst_cost, self.merged_summary_op, self.optimizer, self.Coef), feed_dict = {self.x: X, self.learning_rate: lr})#
self.summary_writer.add_summary(summary, self.iter)
self.iter = self.iter + 1
return cost, Coef
def initlization(self):
self.sess.run(self.init)
def reconstruct(self,X):
return self.sess.run(self.x_r, feed_dict = {self.x:X})
def transform(self, X):
return self.sess.run(self.z, feed_dict = {self.x:X})
def save_model(self):
save_path = self.saver.save(self.sess,self.model_path)
print ("model saved in file: %s" % save_path)
def restore(self):
self.saver.restore(self.sess, self.restore_path)
print ("model restored")
def best_map(L1,L2):
#L1 should be the groundtruth labels and L2 should be the clustering labels we got
Label1 = np.unique(L1)
nClass1 = len(Label1)
Label2 = np.unique(L2)
nClass2 = len(Label2)
nClass = np.maximum(nClass1,nClass2)
G = np.zeros((nClass,nClass))
for i in range(nClass1):
ind_cla1 = L1 == Label1[i]
ind_cla1 = ind_cla1.astype(float)
for j in range(nClass2):
ind_cla2 = L2 == Label2[j]
ind_cla2 = ind_cla2.astype(float)
G[i,j] = np.sum(ind_cla2 * ind_cla1)
m = Munkres()
index = m.compute(-G.T)
index = np.array(index)
c = index[:,1]
newL2 = np.zeros(L2.shape)
for i in range(nClass2):
newL2[L2 == Label2[i]] = Label1[c[i]]
return newL2
def thrC(C,ro):
if ro < 1:
N = C.shape[1]
Cp = np.zeros((N,N))
S = np.abs(np.sort(-np.abs(C),axis=0))
Ind = np.argsort(-np.abs(C),axis=0)
for i in range(N):
cL1 = np.sum(S[:,i]).astype(float)
stop = False
csum = 0
t = 0
while(stop == False):
csum = csum + S[t,i]
if csum > ro*cL1:
stop = True
Cp[Ind[0:t+1,i],i] = C[Ind[0:t+1,i],i]
t = t + 1
else:
Cp = C
return Cp
def build_aff(C):
N = C.shape[0]
Cabs = np.abs(C)
ind = np.argsort(-Cabs,0)
for i in range(N):
Cabs[:,i]= Cabs[:,i] / (Cabs[ind[0,i],i] + 1e-6)
Cksym = Cabs + Cabs.T;
return Cksym
def post_proC(C, K, d, alpha):
# C: coefficient matrix, K: number of clusters, d: dimension of each subspace
C = 0.5*(C + C.T)
r = d*K + 1
U, S, _ = svds(C,r,v0 = np.ones(C.shape[0]))
U = U[:,::-1]
S = np.sqrt(S[::-1])
S = np.diag(S)
U = U.dot(S)
U = normalize(U, norm='l2', axis = 1)
Z = U.dot(U.T)
Z = Z * (Z>0)
L = np.abs(Z ** alpha)
L = L/L.max()
L = 0.5 * (L + L.T)
spectral = cluster.SpectralClustering(n_clusters=K, eigen_solver='arpack', affinity='precomputed',assign_labels='discretize')
spectral.fit(L)
grp = spectral.fit_predict(L) + 1
return grp, L
def err_rate(gt_s, s):
c_x = best_map(gt_s,s)
err_x = np.sum(gt_s[:] != c_x[:])
missrate = err_x.astype(float) / (gt_s.shape[0])
return missrate ,c_x
def build_laplacian(C):
C = 0.5 * (np.abs(C) + np.abs(C.T))
W = np.sum(C,axis=0)
W = np.diag(1.0/W)
L = W.dot(C)
return L
def test_face(Img, Label, CAE, num_class,batch_size):
alpha = max(0.4 - (num_class-1)/10 * 0.1, 0.1)
print (alpha)
total_images= Img.shape[0];
n_batches= (np.ceil(total_images/batch_size)).astype(int)
acc_= [];
pred_=[]
true_=[];
for i in range(n_batches):
#0,39-num_class):
# face_10_subjs = np.array(Img[64*i:64*(i+num_class),:])
# face_10_subjs = face_10_subjs.astype(float)
# label_10_subjs = np.array(Label[64*i:64*(i+num_class)])
# label_10_subjs = label_10_subjs - label_10_subjs.min() + 1
# label_10_subjs = np.squeeze(label_10_subjs)
face_10_subjs= Img[batch_size*i: (i+1)*batch_size,:,:,:]
label_10_subjs= Label[batch_size*i: (i+1)*batch_size,]
#
CAE.initlization()
CAE.restore() # restore from pre-trained model
max_step = 50 + num_class*25# 100+num_class*20
display_step = max_step
lr = 1.0e-3
# fine-tune network
epoch = 0
while epoch < max_step:
epoch = epoch + 1
cost, Coef = CAE.partial_fit(face_10_subjs, lr)#
if epoch % display_step == 0:
print( "epoch: %.1d" % epoch, "cost: %.8f" % (cost/float(batch_size)) )
Coef = thrC(Coef,alpha)
y_x, _ = post_proC(Coef, 10, 10, 5)
missrate_x ,pred1= err_rate(label_10_subjs, y_x)
acc_x = 1 - missrate_x
print( "experiment: %d" % i, "our accuracy: %.4f" % acc_x)
acc_.append(acc_x)
pred_.append(pred1.reshape(-1,1))
true_.append(label_10_subjs.reshape(-1,1))
pred_=np.vstack(np.asarray(pred_))
true_=np.vstack(np.asarray(true_))
acc_ = np.array(acc_)
m = np.mean(acc_)
me = np.median(acc_)
print("%d subjects:" % num_class)
print("Mean: %.4f%%" % ((1-m)*100))
print("Median: %.4f%%" % ((1-me)*100))
print(acc_)
return (1-m), (1-me),pred_,true_
if __name__ == '__main__':
# load images and labels
mnist_256= np.load('Data/mnist_256.npz')
mnist_img=mnist_256['mnist_256']
mnist_labels=mnist_256['mnist_labels']
Img = mnist_img.reshape(-1,16,16,1)
Label= np.ravel(mnist_labels)
# face image clustering
n_input = [16,16]
kernel_size = [5,3,3]
n_hidden = [10,20,30]
all_subjects = [10]#, 15, 20, 25, 30, 35, 38]
avg = []
med = []
iter_loop = 0
# while iter_loop < len(all_subjects):
num_class = all_subjects[iter_loop]
batch_size = 1000#Img.shape[0] #num_class * 64
reg1 = 1.0
reg2 = 0.01#1.0 * 10 ** (num_class / 10.0 - 3.0)
model_path = 'models_face/mymnist.ckpt'
restore_path = 'models_face/mymnist.ckpt'
logs_path = 'logs'
tf.reset_default_graph()
CAE = ConvAE(n_input=n_input, n_hidden=n_hidden, reg_constant1=reg1, re_constant2=reg2, \
kernel_size=kernel_size, batch_size=batch_size, model_path=model_path, restore_path=restore_path, logs_path=logs_path)
avg_i, med_i ,pred_,true_= test_face(Img, Label, CAE, num_class,batch_size)
avg.append(avg_i)
med.append(med_i)
np.savez('clustered_labels_mnist.npz', pred_mnist= pred_,true_mnist=true_)
# iter_loop = iter_loop + 1
#
# iter_loop = 0
# while iter_loop < len(all_subjects):
# num_class = all_subjects[iter_loop]
# print ('%d subjects:' % num_class)
# print ('Mean: %.4f%%' % (avg[iter_loop]*100), 'Median: %.4f%%' % (med[iter_loop]*100) )
# iter_loop = iter_loop + 1
#