-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdrcn.py
530 lines (407 loc) · 16.3 KB
/
drcn.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
"""
DRCN main class
Dependency: keras lib
Author: Muhammad Ghifary ([email protected])
"""
from keras.models import Model
from keras.layers import Input, Flatten
from keras.layers.convolutional import Conv2D, MaxPooling2D, UpSampling2D
from keras.layers.pooling import MaxPooling2D
from keras.layers.core import Activation, Dropout, Dense, Reshape
from keras.layers.normalization import BatchNormalization
from keras.optimizers import RMSprop, Adam
from keras.preprocessing.image import ImageDataGenerator
import os
import numpy as np
import time
from myutils import * # contains all helpers for DRCN
class DRCN(object):
def __init__(self, name='svhn-mnist'):
"""
Class constructor
"""
self.name = name
def create_convnet(self, _input, dense_dim=1000, dy=10, nb_filters=[64, 128], kernel_size=(3, 3), pool_size=(2, 2),
dropout=0.5, bn=True, output_activation='softmax', opt='adam'):
"""
Create convnet model / encoder of DRCN
Args:
_input (Tensor) : input layer
dense_dim (int) : dimensionality of the final dense layers
dy (int) : output dimensionality
nb_filter (list) : list of #Conv2D filters
kernel_size (tuple) : Conv2D kernel size
pool_size (tuple) : MaxPool kernel size
dropout (float) : dropout rate
bn (boolean) : batch normalization mode
output_activation (string) : act. function for output layer
opt (string) : optimizer
Store the shared layers into self.enc_functions list
"""
_h = _input
self.enc_functions = [] # to store the shared layers, will be used later for constructing conv. autoencoder
for i, nf in enumerate(nb_filters):
enc_f = Conv2D(nf, kernel_size, padding='same')
_h = enc_f(_h)
self.enc_functions.append(enc_f)
_h = Activation('relu')(_h)
if i < 2:
_h = MaxPooling2D(pool_size=pool_size, padding='same')(_h)
_h = Flatten()(_h)
enc_f = Dense(dense_dim)
_h = enc_f(_h)
self.enc_functions.append(enc_f)
if bn:
_h = BatchNormalization()(_h)
_h = Activation('relu')(_h)
_h = Dropout(dropout)(_h)
enc_f = Dense(dense_dim)
_h = enc_f(_h)
self.enc_functions.append(enc_f)
if bn:
_h = BatchNormalization()(_h)
_feat = Activation('relu')(_h)
_h = Dropout(dropout)(_feat)
_y = Dense(dy, activation=output_activation)(_h)
# convnet
self.convnet_model = Model(input=_input, output=_y)
self.convnet_model.compile(loss='categorical_crossentropy', optimizer=opt)
print(self.convnet_model.summary())
self.feat_model = Model(input=_input, output=_feat)
def create_model(self, input_shape=(1, 32, 32), dense_dim=1000, dy=10, nb_filters=[64, 128], kernel_size=(3, 3), pool_size=(2, 2),
dropout=0.5, bn=True, output_activation='softmax', opt='adam'):
"""
Create DRCN model: convnet model followed by conv. autoencoder
Args:
_input (Tensor) : input layer
dense_dim (int) : dimensionality of the final dense layers
dy (int) : output dimensionality
nb_filter (list) : list of #Conv2D filters
kernel_size (tuple) : Conv2D kernel size
pool_size (tuple) : MaxPool kernel size
dropout (float) : dropout rate
bn (boolean) : batch normalization mode
output_activation (string) : act. function for output layer
opt (string) : optimizer
"""
[d1, d2, c] = input_shape
if opt == 'adam':
opt = Adam(lr=3e-4)
elif opt == 'rmsprop':
opt = RMSprop(lr=1e-4)
_input = Input(shape=input_shape)
# Create ConvNet
self.create_convnet(_input, dense_dim=dense_dim, dy=dy, nb_filters=nb_filters,
kernel_size=kernel_size, pool_size=pool_size, dropout=dropout,
bn=bn, output_activation=output_activation, opt=opt)
# Create ConvAE, encoder functions are shared with ConvNet
_h = _input
# Reconstruct Conv2D layers
for i, nf in enumerate(nb_filters):
_h = self.enc_functions[i](_h)
_h = Activation('relu')(_h)
if i < 2:
_h = MaxPooling2D(pool_size=pool_size, padding='same')(_h)
[_, wflat, hflat, cflat] = _h.get_shape().as_list()
_h = Flatten()(_h)
# Dense layers
for i in range(len(nb_filters), len(self.enc_functions)):
_h = self.enc_functions[i](_h)
_h = Activation('relu')(_h)
# Decoder
_h = Dense(dense_dim)(_h)
_h = Activation('relu')(_h)
_xdec = Dense(wflat*hflat*cflat)(_h)
_xdec = Activation('relu')(_xdec)
_xdec = Reshape((wflat, hflat, nb_filters[-1]))(_xdec)
i = 0
for nf in reversed(nb_filters):
_xdec = Conv2D(nf, kernel_size, padding='same')(_xdec)
_xdec = Activation('relu')(_xdec)
if i > 0:
_xdec = UpSampling2D(size=pool_size)(_xdec)
i += 1
_xdec = Conv2D(c, kernel_size, padding='same', activation=clip_relu)(_xdec)
self.convae_model = Model(input=_input, output=_xdec)
self.convae_model.compile(loss='mse', optimizer=opt)
print(self.convae_model.summary())
def fit_drcn(self, X, Y, Xu, nb_epoch=50, batch_size=128, shuffle=True,
validation_data=None, test_data=None, PARAMDIR=None, CONF=None):
"""
DRCN algorithm:
- i) train convnet on labeled source data, ii) train convae on unlabeled target data
- include data augmentation and denoising
Args:
X (np.array) : [n, d1, d2, c] array of source images
Y (np.array) : [n, dy] array of source labels
Xu (np.array) : [n, d1, d2, c] array of target images
nb_epoch (int) : #iteration of gradient descent
batch_size (int) : # data per batch
shuffle (boolean) : shuffle the data in a batch if True
validation_data (tuple) : tuple of (Xval, Yval) array
test_data : tuple of (Xtest, Ytest) array
PARAMDIR (string) : directory to store the learned weights
CONF (string) : for naming purposes
"""
history = {}
history['losses'] = []
history['accs'] = []
history['gen_losses'] = []
history['val_losses'] = []
history['val_accs'] = []
history['test_losses'] = []
history['test_accs'] = []
history['elapsed_times'] = []
best_ep = 1
# data augmenter and batch iterator for each convnet and convae
ddatagen = ImageDataGenerator(
featurewise_center=False, # set input mean to 0 over the dataset
samplewise_center=False, # set each sample mean to 0
featurewise_std_normalization=False, # divide inputs by std of the dataset
samplewise_std_normalization=False, # divide each input by its std
zca_whitening=False, # apply ZCA whitening
rotation_range=20, # randomly rotate images in the range (degrees, 0 to 180)
width_shift_range=0.2, # randomly shift images horizontally (fraction of total width)
height_shift_range=0.2, # randomly shift images vertically (fraction of total height)
horizontal_flip=False, # randomly flip images
vertical_flip=False
) # randomly flip images
gdatagen = ImageDataGenerator(
featurewise_center=False, # set input mean to 0 over the dataset
samplewise_center=False, # set each sample mean to 0
featurewise_std_normalization=False, # divide inputs by std of the dataset
samplewise_std_normalization=False, # divide each input by its std
zca_whitening=False, # apply ZCA whitening
rotation_range=20, # randomly rotate images in the range (degrees, 0 to 180)
width_shift_range=0.2, # randomly shift images horizontally (fraction of total width)
height_shift_range=0.2, # randomly shift images vertically (fraction of total height)
horizontal_flip=False, # randomly flip images
vertical_flip=False
) # randomly flip images
for e in range(nb_epoch):
start_t = time.time()
# convae training
gen_loss = 0.
n_batch = 0
total_batches = Xu.shape[0] / batch_size
for Xu_batch, Yu_batch in gdatagen.flow(Xu, np.copy(Xu), batch_size=batch_size, shuffle=shuffle):
Xu_batch = get_impulse_noise(Xu_batch, 0.5)
l = self.convae_model.train_on_batch(Xu_batch, Yu_batch)
gen_loss += l
n_batch += 1
if n_batch >= total_batches:
break
gen_loss /= n_batch
history['gen_losses'].append(gen_loss)
# convnet training
loss = 0.
n_batch = 0
total_batches = X.shape[0] / batch_size
for X_batch, Y_batch in ddatagen.flow(X, Y, batch_size=batch_size, shuffle=shuffle):
l = self.convnet_model.train_on_batch(X_batch, Y_batch)
loss += l
n_batch += 1
if n_batch >= total_batches:
break
loss /= n_batch
history['losses'].append(loss)
# calculate accuracy
acc = accuracy(self.convnet_model.predict(X), Y)
history['accs'].append(acc)
elapsed_t = time.time() - start_t
history['elapsed_times'].append(elapsed_t)
val_loss = -1
val_acc = -1
best_val_acc = -1
if validation_data is not None:
(X_val, Y_val) = validation_data
val_loss = 0.
n_batch = 0
for Xv, Yv in iterate_minibatches(X_val, Y_val ,batch_size, shuffle=False):
l = self.convnet_model.test_on_batch(Xv, Yv)
val_loss += l
n_batch += 1
val_loss /= n_batch
history['val_losses'].append(val_loss)
val_acc = accuracy(self.convnet_model.predict(X_val), Y_val)
history['val_accs'].append(val_acc)
test_loss = -1
test_acc = -1
if test_data is not None:
(X_test, Y_test) = test_data
test_loss = 0.
n_batch = 0
for Xt, Yt in iterate_minibatches(X_test, Y_test, batch_size, shuffle=False):
l = self.convnet_model.test_on_batch(Xt, Yt)
test_loss += l
n_batch += 1
test_loss /= n_batch
history['test_losses'].append(test_loss)
test_acc = accuracy(self.convnet_model.predict(X_test), Y_test)
history['test_accs'].append(test_acc)
print('Epoch-%d: (loss: %.3f, acc: %.3f, gen_loss: %.3f), (val_loss: %.3f, val_acc: %.3f), (test_Loss: %.3f, test_acc: %.3f) -- %.2f sec' % \
((e+1), loss, acc, gen_loss, val_loss, val_acc, test_loss, test_acc, elapsed_t))
if PARAMDIR is not None:
if (acc + val_acc) > best_val_acc:
best_val_acc = (acc + val_acc)
best_ep = e + 1
CONFCNN ='%s_cnn' % CONF
save_weights(self.convnet_model, PARAMDIR, CONFCNN)
CONFCAE = '%s_cae' % CONF
save_weights(self.convae_model, PARAMDIR, CONFCAE)
else:
print('do not save, best val_acc: %.3f at %d' % (best_val_acc, best_ep))
# store history
HISTPATH = '/home/wogong/Models/tf-drcn/%s_hist.npy' % CONF
np.save(HISTPATH, history)
# visualization
if validation_data is not None:
(X_val, Y_val) = validation_data
Xsv = X_val[:100]
Xs = postprocess_images(Xsv, omin=0, omax=1)
imgfile = '%s_src.png' % CONF
Xs = np.reshape(Xs, (len(Xs), Xs.shape[3], Xs.shape[1], Xs.shape[2]))
show_images(Xs, filename=imgfile)
Xs_pred = self.convae_model.predict(Xsv)
Xs_pred = postprocess_images(Xs_pred, omin=0, omax=1)
imgfile = '%s_src_pred.png' % CONF
Xs_pred = np.reshape(Xs_pred, (len(Xs_pred), Xs_pred.shape[3], Xs_pred.shape[1], Xs_pred.shape[2]))
show_images(Xs_pred, filename=imgfile)
if test_data is not None:
(X_test, Y_test) = test_data
Xtv = X_test[:100]
Xt = postprocess_images(Xtv, omin=0, omax=1)
imgfile = '%s_tgt.png' % CONF
Xt = np.reshape(Xt, (len(Xt), Xt.shape[3], Xt.shape[1], Xt.shape[2]))
show_images(Xt, filename=imgfile)
Xt_pred = self.convae_model.predict(Xtv)
Xt_pred = postprocess_images(Xt_pred, omin=0, omax=1)
imgfile = '%s_tgt_pred.png' % CONF
Xt_pred = np.reshape(Xt_pred, (len(Xt_pred), Xt_pred.shape[3], Xt_pred.shape[1], Xt_pred.shape[2]))
show_images(Xt_pred, filename=imgfile)
### just in case want to run convnet and convae separately, below are the training modules ###
def fit_convnet(self, X, Y, nb_epoch=50, batch_size=128, shuffle=True,
validation_data=None, test_data=None, PARAMDIR=None, CONF=None):
history = {}
history['losses'] = []
history['accs'] = []
history['val_losses'] = []
history['val_accs'] = []
history['test_losses'] = []
history['test_accs'] = []
history['elapsed_times'] = []
best_ep = 1
for e in range(nb_epoch):
loss = 0.
n_batch = 0
start_t = time.time()
for X_batch, Y_batch in iterate_minibatches(X, Y, batch_size, shuffle=shuffle):
l = self.convnet_model.train_on_batch(X_batch, Y_batch)
loss += l
n_batch += 1
elapsed_t = time.time() - start_t
history['elapsed_times'].append(elapsed_t)
loss /= n_batch
history['losses'].append(loss)
# calculate accuracy
acc = accuracy(self.convnet_model.predict(X), Y)
history['accs'].append(acc)
val_loss = -1
val_acc = -1
best_val_acc = -1
if validation_data is not None:
(X_val, Y_val) = validation_data
val_loss = 0.
n_batch = 0
for Xv, Yv in iterate_minibatches(X_val, Y_val ,batch_size, shuffle=False):
l = self.convnet_model.test_on_batch(Xv, Yv)
val_loss += l
n_batch += 1
val_loss /= n_batch
history['val_losses'].append(val_loss)
val_acc = accuracy(self.convnet_model.predict(X_val), Y_val)
history['val_accs'].append(val_acc)
test_loss = -1
test_acc = -1
if test_data is not None:
(X_test, Y_test) = test_data
test_loss = 0.
n_batch = 0
for Xt, Yt in iterate_minibatches(X_test, Y_test, batch_size, shuffle=False):
l = self.convnet_model.test_on_batch(Xt, Yt)
test_loss += l
n_batch += 1
test_loss /= n_batch
history['test_losses'].append(test_loss)
test_acc = accuracy(self.convnet_model.predict(X_test), Y_test)
history['test_accs'].append(test_acc)
print('Epoch-%d: (loss: %.3f, acc: %.3f), (val_loss: %.3f, val_acc: %.3f), (test_Loss: %.3f, test_acc: %.3f) -- %.2f sec' % \
((e+1), loss, acc, val_loss, val_acc, test_loss, test_acc, elapsed_t))
if PARAMDIR is not None:
if (acc + val_acc) > best_val_acc:
best_val_acc = (acc + val_acc)
best_ep = e + 1
save_weights(self.convnet_model, PARAMDIR, CONF)
else:
print('do not save, best val_acc: %.3f at %d' % (best_val_acc, best_ep))
# store history
HISTPATH = '%s_hist.npy' % CONF
np.save(HISTPATH, history)
def fit_convae(self, X, nb_epoch=50, batch_size=128, shuffle=True,
validation_data=None, test_data=None, PARAMDIR=None, CONF=None):
history = {}
history['losses'] = []
history['val_losses'] = []
history['test_losses'] = []
history['elapsed_times'] = []
best_ep = 1
for e in range(nb_epoch):
loss = 0.
n_batch = 0
start_t = time.time()
for X_batch, Y_batch in iterate_minibatches(X, np.copy(X), batch_size, shuffle=shuffle):
l = self.convae_model.train_on_batch(X_batch, Y_batch)
loss += l
n_batch += 1
elapsed_t = time.time() - start_t
history['elapsed_times'].append(elapsed_t)
loss /= n_batch
history['losses'].append(loss)
val_loss = -1
best_val_loss = 100000
test_loss = -1
print('Epoch-%d: (loss: %.3f), (val_loss: %.3f), (test_Loss: %.3f) -- %.2f sec' % \
((e+1), loss, val_loss,test_loss,elapsed_t))
if PARAMDIR is not None:
if loss < best_val_loss:
best_val_loss = loss
best_ep = e + 1
save_weights(self.convae_model, PARAMDIR, CONF)
else:
print('do not save, best val loss: %.3f at %d' % (best_val_loss, best_ep))
# store history
HISTPATH = '/home/wogong/Models/tf-drcn/%s_hist.npy' % CONF
np.save(HISTPATH, history)
# visualization
if validation_data is not None:
Xtv = validation_data
Xt = postprocess_images(Xtv, omin=0, omax=1)
imgfile = '/home/wogong/Models/tf-drcn/%s_tgt.png' % CONF
Xt = np.reshape(Xt, (len(Xt), Xt.shape[3], Xt.shape[1], Xt.shape[2]))
show_images(Xt, filename=imgfile)
Xt_pred = self.convae_model.predict(Xtv)
Xt_pred = postprocess_images(Xt_pred, omin=0, omax=1)
imgfile = '/home/wogong/Models/tf-drcn/%s_tgt_pred.png' % CONF
Xt_pred = np.reshape(Xt_pred, (len(Xt_pred), Xt_pred.shape[3], Xt_pred.shape[1], Xt_pred.shape[2]))
show_images(Xt_pred, filename=imgfile)
if test_data is not None:
Xsv = test_data
Xs = postprocess_images(Xsv, omin=0, omax=1)
imgfile = '/home/wogong/Models/tf-drcn/%s_src.png' % CONF
Xs = np.reshape(Xs, (len(Xs), Xs.shape[3], Xs.shape[1], Xs.shape[2]))
show_images(Xs, filename=imgfile)
Xs_pred = self.convae_model.predict(Xsv)
Xs_pred = postprocess_images(Xs_pred, omin=0, omax=1)
imgfile = '/home/wogong/Models/tf-drcn/%s_src_pred.png' % CONF
Xs_pred = np.reshape(Xs_pred, (len(Xs_pred), Xs_pred.shape[3], Xs_pred.shape[1], Xs_pred.shape[2]))
show_images(Xs_pred, filename=imgfile)