-
Notifications
You must be signed in to change notification settings - Fork 58
/
DeepTemporalClustering.py
545 lines (472 loc) · 23.5 KB
/
DeepTemporalClustering.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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
"""
Implementation of the Deep Temporal Clustering model
Main file
@author Florent Forest (FlorentF9)
"""
# Utilities
import os
import csv
import argparse
from time import time
# Keras
from keras.models import Model
from keras.layers import Dense, Reshape, UpSampling2D, Conv2DTranspose, GlobalAveragePooling1D, Softmax
from keras.losses import kullback_leibler_divergence
import keras.backend as K
# scikit-learn
from sklearn.cluster import AgglomerativeClustering, KMeans
# Dataset helper function
from datasets import load_data
# DTC components
from TSClusteringLayer import TSClusteringLayer
from TAE import temporal_autoencoder
from metrics import *
import tsdistances
class DTC:
"""
Deep Temporal Clustering (DTC) model
# Arguments
n_clusters: number of clusters
input_dim: input dimensionality
timesteps: length of input sequences (can be None for variable length)
n_filters: number of filters in convolutional layer
kernel_size: size of kernel in convolutional layer
strides: strides in convolutional layer
pool_size: pooling size in max pooling layer, must divide the time series length
n_units: numbers of units in the two BiLSTM layers
alpha: coefficient in Student's kernel
dist_metric: distance metric between latent sequences
cluster_init: cluster initialization method
"""
def __init__(self, n_clusters, input_dim, timesteps,
n_filters=50, kernel_size=10, strides=1, pool_size=8, n_units=[50, 1],
alpha=1.0, dist_metric='eucl', cluster_init='kmeans', heatmap=False):
assert(timesteps % pool_size == 0)
self.n_clusters = n_clusters
self.input_dim = input_dim
self.timesteps = timesteps
self.n_filters = n_filters
self.kernel_size = kernel_size
self.strides = strides
self.pool_size = pool_size
self.n_units = n_units
self.latent_shape = (self.timesteps // self.pool_size, self.n_units[1])
self.alpha = alpha
self.dist_metric = dist_metric
self.cluster_init = cluster_init
self.heatmap = heatmap
self.pretrained = False
self.model = self.autoencoder = self.encoder = self.decoder = None
if self.heatmap:
self.heatmap_model = None
self.heatmap_loss_weight = None
self.initial_heatmap_loss_weight = None
self.final_heatmap_loss_weight = None
self.finetune_heatmap_at_epoch = None
def initialize(self):
"""
Create DTC model
"""
# Create AE models
self.autoencoder, self.encoder, self.decoder = temporal_autoencoder(input_dim=self.input_dim,
timesteps=self.timesteps,
n_filters=self.n_filters,
kernel_size=self.kernel_size,
strides=self.strides,
pool_size=self.pool_size,
n_units=self.n_units)
clustering_layer = TSClusteringLayer(self.n_clusters,
alpha=self.alpha,
dist_metric=self.dist_metric,
name='TSClustering')(self.encoder.output)
# Heatmap-generating network
if self.heatmap:
n_heatmap_filters = self.n_clusters # one heatmap (class activation map) per cluster
encoded = self.encoder.output
heatmap_layer = Reshape((-1, 1, self.n_units[1]))(encoded)
heatmap_layer = UpSampling2D((self.pool_size, 1))(heatmap_layer)
heatmap_layer = Conv2DTranspose(n_heatmap_filters, (self.kernel_size, 1), padding='same')(heatmap_layer)
# The next one is the heatmap layer we will visualize
heatmap_layer = Reshape((-1, n_heatmap_filters), name='Heatmap')(heatmap_layer)
heatmap_output_layer = GlobalAveragePooling1D()(heatmap_layer)
# A dense layer must be added only if `n_heatmap_filters` is different from `n_clusters`
# heatmap_output_layer = Dense(self.n_clusters, activation='relu')(heatmap_output_layer)
heatmap_output_layer = Softmax()(heatmap_output_layer) # normalize activations with softmax
if self.heatmap:
# Create DTC model
self.model = Model(inputs=self.autoencoder.input,
outputs=[self.autoencoder.output, clustering_layer, heatmap_output_layer])
# Create Heatmap model
self.heatmap_model = Model(inputs=self.autoencoder.input,
outputs=heatmap_layer)
else:
# Create DTC model
self.model = Model(inputs=self.autoencoder.input,
outputs=[self.autoencoder.output, clustering_layer])
@property
def cluster_centers_(self):
"""
Returns cluster centers
"""
return self.model.get_layer(name='TSClustering').get_weights()[0]
@staticmethod
def weighted_kld(loss_weight):
"""
Custom KL-divergence loss with a variable weight parameter
"""
def loss(y_true, y_pred):
return loss_weight * kullback_leibler_divergence(y_true, y_pred)
return loss
def on_epoch_end(self, epoch):
"""
Update heatmap loss weight on epoch end
"""
if epoch > self.finetune_heatmap_at_epoch:
K.set_value(self.heatmap_loss_weight, self.final_heatmap_loss_weight)
def compile(self, gamma, optimizer, initial_heatmap_loss_weight=None, final_heatmap_loss_weight=None):
"""
Compile DTC model
# Arguments
gamma: coefficient of TS clustering loss
optimizer: optimization algorithm
initial_heatmap_loss_weight (optional): initial weight of heatmap loss vs clustering loss
final_heatmap_loss_weight (optional): final weight of heatmap loss vs clustering loss (heatmap finetuning)
"""
if self.heatmap:
self.initial_heatmap_loss_weight = initial_heatmap_loss_weight
self.final_heatmap_loss_weight = final_heatmap_loss_weight
self.heatmap_loss_weight = K.variable(self.initial_heatmap_loss_weight)
self.model.compile(loss=['mse', DTC.weighted_kld(1.0 - self.heatmap_loss_weight), DTC.weighted_kld(self.heatmap_loss_weight)],
loss_weights=[1.0, gamma, gamma],
optimizer=optimizer)
else:
self.model.compile(loss=['mse', 'kld'],
loss_weights=[1.0, gamma],
optimizer=optimizer)
def load_weights(self, weights_path):
"""
Load pre-trained weights of DTC model
# Arguments
weight_path: path to weights file (.h5)
"""
self.model.load_weights(weights_path)
self.pretrained = True
def load_ae_weights(self, ae_weights_path):
"""
Load pre-trained weights of AE
# Arguments
ae_weight_path: path to weights file (.h5)
"""
self.autoencoder.load_weights(ae_weights_path)
self.pretrained = True
def dist(self, x1, x2):
"""
Compute distance between two multivariate time series using chosen distance metric
# Arguments
x1: first input (np array)
x2: second input (np array)
# Return
distance
"""
if self.dist_metric == 'eucl':
return tsdistances.eucl(x1, x2)
elif self.dist_metric == 'cid':
return tsdistances.cid(x1, x2)
elif self.dist_metric == 'cor':
return tsdistances.cor(x1, x2)
elif self.dist_metric == 'acf':
return tsdistances.acf(x1, x2)
else:
raise ValueError('Available distances are eucl, cid, cor and acf!')
def init_cluster_weights(self, X):
"""
Initialize with complete-linkage hierarchical clustering or k-means.
# Arguments
X: numpy array containing training set or batch
"""
assert(self.cluster_init in ['hierarchical', 'kmeans'])
print('Initializing cluster...')
features = self.encode(X)
if self.cluster_init == 'hierarchical':
if self.dist_metric == 'eucl': # use AgglomerativeClustering off-the-shelf
hc = AgglomerativeClustering(n_clusters=self.n_clusters,
affinity='euclidean',
linkage='complete').fit(features.reshape(features.shape[0], -1))
else: # compute distance matrix using dist
d = np.zeros((features.shape[0], features.shape[0]))
for i in range(features.shape[0]):
for j in range(i):
d[i, j] = d[j, i] = self.dist(features[i], features[j])
hc = AgglomerativeClustering(n_clusters=self.n_clusters,
affinity='precomputed',
linkage='complete').fit(d)
# compute centroid
cluster_centers = np.array([features[hc.labels_ == c].mean(axis=0) for c in range(self.n_clusters)])
elif self.cluster_init == 'kmeans':
# fit k-means on flattened features
km = KMeans(n_clusters=self.n_clusters, n_init=10).fit(features.reshape(features.shape[0], -1))
cluster_centers = km.cluster_centers_.reshape(self.n_clusters, features.shape[1], features.shape[2])
self.model.get_layer(name='TSClustering').set_weights([cluster_centers])
print('Done!')
def encode(self, x):
"""
Encoding function. Extract latent features from hidden layer
# Arguments
x: data point
# Return
encoded (latent) data point
"""
return self.encoder.predict(x)
def decode(self, x):
"""
Decoding function. Decodes encoded sequence from latent space.
# Arguments
x: encoded (latent) data point
# Return
decoded data point
"""
return self.decoder.predict(x)
def predict(self, x):
"""
Predict cluster assignment.
"""
q = self.model.predict(x, verbose=0)[1]
return q.argmax(axis=1)
@staticmethod
def target_distribution(q): # target distribution p which enhances the discrimination of soft label q
weight = q ** 2 / q.sum(0)
return (weight.T / weight.sum(1)).T
def predict_heatmap(self, x):
"""
Produces TS clustering heatmap from input sequence.
# Arguments
x: data point
# Return
heatmap
"""
return self.heatmap_model.predict(x, verbose=0)
def pretrain(self, X,
optimizer='adam',
epochs=10,
batch_size=64,
save_dir='results/tmp',
verbose=1):
"""
Pre-train the autoencoder using only MSE reconstruction loss
Saves weights in h5 format.
# Arguments
X: training set
optimizer: optimization algorithm
epochs: number of pre-training epochs
batch_size: training batch size
save_dir: path to existing directory where weights will be saved
"""
print('Pretraining...')
self.autoencoder.compile(optimizer=optimizer, loss='mse')
# Begin pretraining
t0 = time()
self.autoencoder.fit(X, X, batch_size=batch_size, epochs=epochs, verbose=verbose)
print('Pretraining time: ', time() - t0)
self.autoencoder.save_weights('{}/ae_weights-epoch{}.h5'.format(save_dir, epochs))
print('Pretrained weights are saved to {}/ae_weights-epoch{}.h5'.format(save_dir, epochs))
self.pretrained = True
def fit(self, X_train, y_train=None,
X_val=None, y_val=None,
epochs=100,
eval_epochs=10,
save_epochs=10,
batch_size=64,
tol=0.001,
patience=5,
finetune_heatmap_at_epoch=8,
save_dir='results/tmp'):
"""
Training procedure
# Arguments
X_train: training set
y_train: (optional) training labels
X_val: (optional) validation set
y_val: (optional) validation labels
epochs: number of training epochs
eval_epochs: evaluate metrics on train/val set every eval_epochs epochs
save_epochs: save model weights every save_epochs epochs
batch_size: training batch size
tol: tolerance for stopping criterion
patience: patience for stopping criterion
finetune_heatmap_at_epoch: epoch number where heatmap finetuning will start. Heatmap loss weight will
switch from `self.initial_heatmap_loss_weight` to `self.final_heatmap_loss_weight`
save_dir: path to existing directory where weights and logs are saved
"""
if not self.pretrained:
print('Autoencoder was not pre-trained!')
if self.heatmap:
self.finetune_heatmap_at_epoch = finetune_heatmap_at_epoch
# Logging file
logfile = open(save_dir + '/dtc_log.csv', 'w')
fieldnames = ['epoch', 'T', 'L', 'Lr', 'Lc']
if X_val is not None:
fieldnames += ['L_val', 'Lr_val', 'Lc_val']
if y_train is not None:
fieldnames += ['acc', 'pur', 'nmi', 'ari']
if y_val is not None:
fieldnames += ['acc_val', 'pur_val', 'nmi_val', 'ari_val']
logwriter = csv.DictWriter(logfile, fieldnames)
logwriter.writeheader()
y_pred_last = None
patience_cnt = 0
print('Training for {} epochs.\nEvaluating every {} and saving model every {} epochs.'.format(epochs, eval_epochs, save_epochs))
for epoch in range(epochs):
# Compute cluster assignments for training set
q = self.model.predict(X_train)[1]
p = DTC.target_distribution(q)
# Evaluate losses and metrics on training set
if epoch % eval_epochs == 0:
# Initialize log dictionary
logdict = dict(epoch=epoch)
y_pred = q.argmax(axis=1)
if X_val is not None:
q_val = self.model.predict(X_val)[1]
p_val = DTC.target_distribution(q_val)
y_val_pred = q_val.argmax(axis=1)
print('epoch {}'.format(epoch))
if self.heatmap:
loss = self.model.evaluate(X_train, [X_train, p, p], batch_size=batch_size, verbose=False)
else:
loss = self.model.evaluate(X_train, [X_train, p], batch_size=batch_size, verbose=False)
logdict['L'] = loss[0]
logdict['Lr'] = loss[1]
logdict['Lc'] = loss[2]
print('[Train] - Lr={:f}, Lc={:f} - total loss={:f}'.format(logdict['Lr'], logdict['Lc'], logdict['L']))
if X_val is not None:
val_loss = self.model.evaluate(X_val, [X_val, p_val], batch_size=batch_size, verbose=False)
logdict['L_val'] = val_loss[0]
logdict['Lr_val'] = val_loss[1]
logdict['Lc_val'] = val_loss[2]
print('[Val] - Lr={:f}, Lc={:f} - total loss={:f}'.format(logdict['Lr_val'], logdict['Lc_val'], logdict['L_val']))
# Evaluate the clustering performance using labels
if y_train is not None:
logdict['acc'] = cluster_acc(y_train, y_pred)
logdict['pur'] = cluster_purity(y_train, y_pred)
logdict['nmi'] = metrics.normalized_mutual_info_score(y_train, y_pred)
logdict['ari'] = metrics.adjusted_rand_score(y_train, y_pred)
print('[Train] - Acc={:f}, Pur={:f}, NMI={:f}, ARI={:f}'.format(logdict['acc'], logdict['pur'],
logdict['nmi'], logdict['ari']))
if y_val is not None:
logdict['acc_val'] = cluster_acc(y_val, y_val_pred)
logdict['pur_val'] = cluster_purity(y_val, y_val_pred)
logdict['nmi_val'] = metrics.normalized_mutual_info_score(y_val, y_val_pred)
logdict['ari_val'] = metrics.adjusted_rand_score(y_val, y_val_pred)
print('[Val] - Acc={:f}, Pur={:f}, NMI={:f}, ARI={:f}'.format(logdict['acc_val'], logdict['pur_val'],
logdict['nmi_val'], logdict['ari_val']))
logwriter.writerow(logdict)
# check stop criterion
if y_pred_last is not None:
assignment_changes = np.sum(y_pred != y_pred_last).astype(np.float32) / y_pred.shape[0]
y_pred_last = y_pred
if epoch > 0 and assignment_changes < tol:
patience_cnt += 1
print('Assignment changes {} < {} tolerance threshold. Patience: {}/{}.'.format(assignment_changes, tol, patience_cnt, patience))
if patience_cnt >= patience:
print('Reached max patience. Stopping training.')
logfile.close()
break
else:
patience_cnt = 0
# Save intermediate model and plots
if epoch % save_epochs == 0:
self.model.save_weights(save_dir + '/DTC_model_' + str(epoch) + '.h5')
print('Saved model to:', save_dir + '/DTC_model_' + str(epoch) + '.h5')
# Train for one epoch
if self.heatmap:
self.model.fit(X_train, [X_train, p, p], epochs=1, batch_size=batch_size, verbose=False)
self.on_epoch_end(epoch)
else:
self.model.fit(X_train, [X_train, p], epochs=1, batch_size=batch_size, verbose=False)
# Save the final model
logfile.close()
print('Saving model to:', save_dir + '/DTC_model_final.h5')
self.model.save_weights(save_dir + '/DTC_model_final.h5')
if __name__ == "__main__":
# Parsing arguments and setting hyper-parameters
parser = argparse.ArgumentParser(description='train', formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('--dataset', default='CBF', help='UCR/UEA univariate or multivariate dataset')
#parser.add_argument('--validation', default=False, type=bool, help='use train/validation split')
parser.add_argument('--ae_weights', default=None, help='pre-trained autoencoder weights')
parser.add_argument('--n_clusters', default=None, type=int, help='number of clusters')
parser.add_argument('--n_filters', default=50, type=int, help='number of filters in convolutional layer')
parser.add_argument('--kernel_size', default=10, type=int, help='size of kernel in convolutional layer')
parser.add_argument('--strides', default=1, type=int, help='strides in convolutional layer')
parser.add_argument('--pool_size', default=8, type=int, help='pooling size in max pooling layer')
parser.add_argument('--n_units', nargs=2, default=[50, 1], type=int, help='numbers of units in the BiLSTM layers')
parser.add_argument('--gamma', default=1.0, type=float, help='coefficient of clustering loss')
parser.add_argument('--alpha', default=1.0, type=float, help='coefficient in Student\'s kernel')
parser.add_argument('--dist_metric', default='eucl', type=str, choices=['eucl', 'cid', 'cor', 'acf'], help='distance metric between latent sequences')
parser.add_argument('--cluster_init', default='kmeans', type=str, choices=['kmeans', 'hierarchical'], help='cluster initialization method')
parser.add_argument('--heatmap', default=False, type=bool, help='train heatmap-generating network')
parser.add_argument('--pretrain_epochs', default=10, type=int)
parser.add_argument('--epochs', default=100, type=int)
parser.add_argument('--eval_epochs', default=1, type=int)
parser.add_argument('--save_epochs', default=10, type=int)
parser.add_argument('--batch_size', default=64, type=int)
parser.add_argument('--tol', default=0.001, type=float, help='tolerance for stopping criterion')
parser.add_argument('--patience', default=5, type=int, help='patience for stopping criterion')
parser.add_argument('--finetune_heatmap_at_epoch', default=8, type=int, help='epoch where heatmap finetuning starts')
parser.add_argument('--initial_heatmap_loss_weight', default=0.1, type=float, help='initial weight of heatmap loss vs clustering loss')
parser.add_argument('--final_heatmap_loss_weight', default=0.9, type=float, help='final weight of heatmap loss vs clustering loss (heatmap finetuning)')
parser.add_argument('--save_dir', default='results/tmp')
args = parser.parse_args()
print(args)
# Create save directory if not exists
if not os.path.exists(args.save_dir):
os.makedirs(args.save_dir)
# Load data
(X_train, y_train), (X_val, y_val) = load_data(args.dataset), (None, None) # no train/validation split for now
# Find number of clusters
if args.n_clusters is None:
args.n_clusters = len(np.unique(y_train))
# Set default values
pretrain_optimizer = 'adam'
# Instantiate model
dtc = DTC(n_clusters=args.n_clusters,
input_dim=X_train.shape[-1],
timesteps=X_train.shape[1],
n_filters=args.n_filters,
kernel_size=args.kernel_size,
strides=args.strides,
pool_size=args.pool_size,
n_units=args.n_units,
alpha=args.alpha,
dist_metric=args.dist_metric,
cluster_init=args.cluster_init,
heatmap=args.heatmap)
# Initialize model
optimizer = 'adam'
dtc.initialize()
dtc.model.summary()
dtc.compile(gamma=args.gamma, optimizer=optimizer, initial_heatmap_loss_weight=args.initial_heatmap_loss_weight,
final_heatmap_loss_weight=args.final_heatmap_loss_weight)
# Load pre-trained AE weights or pre-train
if args.ae_weights is None and args.pretrain_epochs > 0:
dtc.pretrain(X=X_train, optimizer=pretrain_optimizer,
epochs=args.pretrain_epochs, batch_size=args.batch_size,
save_dir=args.save_dir)
elif args.ae_weights is not None:
dtc.load_ae_weights(args.ae_weights)
# Initialize clusters
dtc.init_cluster_weights(X_train)
# Fit model
t0 = time()
dtc.fit(X_train, y_train, X_val, y_val, args.epochs, args.eval_epochs, args.save_epochs, args.batch_size,
args.tol, args.patience, args.finetune_heatmap_at_epoch, args.save_dir)
print('Training time: ', (time() - t0))
# Evaluate
print('Performance (TRAIN)')
results = {}
q = dtc.model.predict(X_train)[1]
y_pred = q.argmax(axis=1)
if y_train is not None:
results['acc'] = cluster_acc(y_train, y_pred)
results['pur'] = cluster_purity(y_train, y_pred)
results['nmi'] = metrics.normalized_mutual_info_score(y_train, y_pred)
results['ari'] = metrics.adjusted_rand_score(y_train, y_pred)
print(results)