-
Notifications
You must be signed in to change notification settings - Fork 0
/
cnn_generate_confmat_bootstrap.py
598 lines (523 loc) · 25.8 KB
/
cnn_generate_confmat_bootstrap.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
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
# -*- coding: utf-8 -*-
"""
Created on Sat Sep 16 15:53:33 2017
@author: wawan
"""
from __future__ import print_function
from __future__ import absolute_import
import warnings
from keras.layers import Concatenate, Maximum, Average
from keras.layers import Lambda
from keras.models import Model
from keras import layers
from keras.layers import Dense
from keras.layers import Input
from keras.layers import BatchNormalization
from keras.layers import Activation
from keras.layers import Conv2D
from keras.layers import SeparableConv2D
from keras.layers import MaxPooling2D
from keras.layers import GlobalAveragePooling2D
from keras.layers import GlobalMaxPooling2D
from keras.engine.topology import get_source_inputs
from keras.utils.data_utils import get_file
from keras import backend as K
from keras.engine.topology import Layer
from keras import activations
from keras.callbacks import TensorBoard, ModelCheckpoint, CSVLogger
TF_WEIGHTS_PATH = 'https://github.com/fchollet/deep-learning-models/releases/download/v0.4/xception_weights_tf_dim_ordering_tf_kernels.h5'
TF_WEIGHTS_PATH_NO_TOP = 'https://github.com/fchollet/deep-learning-models/releases/download/v0.4/xception_weights_tf_dim_ordering_tf_kernels_notop.h5'
import numpy
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dropout
from keras.layers import Flatten
from keras.utils import np_utils
from keras import optimizers
import IPython
import h5py
from keras.utils.io_utils import HDF5Matrix
from tensorflow.python import debug as tf_debug
from keras import metrics, losses, regularizers
#sess = K.get_session()
#sess = tf_debug.LocalCLIDebugWrapperSession(sess)
#K.set_session(sess)
reg_val = 1e-5
model_homepath = '/home/wawan/git/'
data_homepath = '/mnt/Storage/'
filepath = 'bootstrap_cnn/cnn_weights_best.hdf5'
modelname = model_homepath+'fce_gan/save/stat/bootstrap'
def normalize_pixel(data):
return data/255-.5
def hdf5_generator(dataset,set_type):
i=0
# CelebA data size: train = 162770, valid = 19867, test = 19962
if dataset=='CelebA':
if set_type=='train':
X_data = HDF5Matrix(data_homepath+'Projects/data/CelebAHDF5/celeba_aligned_cropped_train.hdf5', 'features', normalizer=normalize_pixel)
y_data = HDF5Matrix(data_homepath+'Projects/data/CelebAHDF5/celeba_aligned_cropped_train.hdf5', 'targets')
size = 162770
elif set_type=='valid':
X_data = HDF5Matrix(data_homepath+'Projects/data/CelebAHDF5/celeba_aligned_cropped_valid.hdf5', 'features', normalizer=normalize_pixel)
y_data = HDF5Matrix(data_homepath+'Projects/data/CelebAHDF5/celeba_aligned_cropped_valid.hdf5', 'targets')
size = 19867
elif set_type=='test':
X_data = HDF5Matrix(data_homepath+'Projects/data/CelebAHDF5/celeba_aligned_cropped_test.hdf5', 'features', normalizer=normalize_pixel)
y_data = HDF5Matrix(data_homepath+'Projects/data/CelebAHDF5/celeba_aligned_cropped_test.hdf5', 'targets')
size = 19962
if set_type=='train_cls5':
X_data = HDF5Matrix(data_homepath+'Projects/data/CelebAHDF5/celeba_aligned_cropped_train_5cls.hdf5', 'features', normalizer=normalize_pixel)
y_data = HDF5Matrix(data_homepath+'Projects/data/CelebAHDF5/celeba_aligned_cropped_train_5cls.hdf5', 'targets')
size = 122077
elif set_type=='valid_cls5':
X_data = HDF5Matrix(data_homepath+'Projects/data/CelebAHDF5/celeba_aligned_cropped_valid_5cls.hdf5', 'features', normalizer=normalize_pixel)
y_data = HDF5Matrix(data_homepath+'Projects/data/CelebAHDF5/celeba_aligned_cropped_valid_5cls.hdf5', 'targets')
size = 15138
elif set_type=='test_cls5':
X_data = HDF5Matrix(data_homepath+'Projects/data/CelebAHDF5/celeba_aligned_cropped_test_5cls.hdf5', 'features', normalizer=normalize_pixel)
y_data = HDF5Matrix(data_homepath+'Projects/data/CelebAHDF5/celeba_aligned_cropped_test_5cls.hdf5', 'targets')
size = 14724
elif set_type=='all':
X_data = HDF5Matrix(data_homepath+'Projects/data/CelebAHDF5/celeba_aligned_cropped.hdf5', 'features', normalizer=normalize_pixel)
y_data = HDF5Matrix(data_homepath+'Projects/data/CelebAHDF5/celeba_aligned_cropped.hdf5', 'targets')
size = 202599
elif set_type=='try':
X_data = HDF5Matrix('mytestfile.hdf5', 'features')
y_data = HDF5Matrix('mytestfile.hdf5', 'targets')
size = 20
while 1:
X_single = X_data[i%size].reshape((1, 218, 178, 3))
# y_single = y_data[i%size].reshape((1, 40))
y_single = y_data[i%size].reshape((1, 5))
# X_single = X_data[i:i+1]
# y_single = y_data[i:i+1]
yield(X_single, y_single)
i+=1
def load_data_attr(dataset):
if dataset=='mnist':
num_classes = 10
input_shape=(28, 28, 1)
elif dataset=='CelebA':
num_classes = 40
input_shape=(218, 178, 3)
elif dataset=='CelebA_cls5':
num_classes = 5
input_shape=(218, 178, 3)
return num_classes, input_shape
def concat_diff(i): # batch discrimination - increase generation diversity.
# return i
bv = Lambda(lambda i:K.mean(K.abs(i[:] - K.mean(i,axis=0)),axis=-1,keepdims=True))(i)
i = Concatenate()([i,bv])
return i
class ClassDependentCost(Layer):
def __init__(self, output_dim, trainable, **kwargs):
self.output_dim = output_dim
super(ClassDependentCost, self).__init__(**kwargs)
self.sigmoid = activations.get('sigmoid')
self.trainable = trainable
def build(self, input_shape):
# Create a trainable weight variable for this layer.
self.kernel = self.add_weight(name='kernel',
shape=(input_shape[1],),
initializer='he_uniform',
regularizer=regularizers.l2(reg_val),
trainable=self.trainable)
super(ClassDependentCost, self).build(input_shape) # Be sure to call this somewhere!
def call(self, x):
return x * self.sigmoid(self.kernel)
def compute_output_shape(self, input_shape):
return (input_shape[0], self.output_dim)
def cosen_cnn_model(type):
inp = Input(shape=(218,178,3))
i = inp
ndf=24
def conv(i,nop,kw,std=1,usebn=True,bm='same',name=''):
i = Conv2D(nop,kernel_size=(kw,kw),padding=bm,strides=(std,std), kernel_initializer='he_uniform', kernel_regularizer=regularizers.l2(reg_val), bias_regularizer=regularizers.l2(reg_val),name='conv'+name)(i)
if usebn:
i = BatchNormalization(name='bn'+name)(i)
i = Activation('relu',name='relu'+name)(i)
return i
if type=='cost':
cost_trainable = True
elif type=='normal':
cost_trainable = False
i = conv(i,ndf*1,4,std=2,name='0',usebn=False)
i = concat_diff(i)
i = conv(i,ndf*2,4,std=2,name='1')
i = concat_diff(i)
i = conv(i,ndf*4,4,std=2,name='2')
i = concat_diff(i)
i = conv(i,ndf*8,4,std=2,name='3')
i = concat_diff(i)
i = conv(i,ndf*8,4,std=2,name='4')
i = concat_diff(i)
i = conv(i,ndf*8,4,std=2,name='5')
i = concat_diff(i)
i = Flatten()(i)
i = Dense(200, kernel_initializer='he_uniform', kernel_regularizer=regularizers.l2(reg_val), bias_regularizer=regularizers.l2(reg_val),name='dense1')(i)
i = Activation('relu',name='relu_dens1')(i)
# i = Dense(200, kernel_initializer='he_uniform', kernel_regularizer=regularizers.l2(reg_val), bias_regularizer=regularizers.l2(reg_val),name='dense2')(i)
# i = Activation('relu',name='relu_dens2')(i)
i = Dense(num_classes, kernel_initializer='he_uniform', kernel_regularizer=regularizers.l2(reg_val), bias_regularizer=regularizers.l2(reg_val),name='last_dense')(i)
i=ClassDependentCost(5,trainable=cost_trainable)(i)
predictions=Activation('sigmoid')(i)
model = Model(inputs=inp, outputs=predictions)
if type=='cost':
for i in range(34):
model.layers[i].trainable = False
return model
def gan_dis_model():
inp = Input(shape=(218,178,3))
i = inp
ndf=24
def conv(i,nop,kw,std=1,usebn=True,bm='same'):
i = Conv2D(nop,kernel_size=(kw,kw),padding=bm,strides=(std,std), kernel_initializer='random_uniform')(i)
if usebn:
i = BatchNormalization()(i)
i = Activation('relu')(i)
return i
i = conv(i,ndf*1,4,std=2,usebn=False)
i = concat_diff(i)
i = conv(i,ndf*2,4,std=2)
i = concat_diff(i)
i = conv(i,ndf*4,4,std=2)
i = concat_diff(i)
i = conv(i,ndf*8,4,std=2)
i = concat_diff(i)
i = conv(i,ndf*8,4,std=2)
i = concat_diff(i)
i = conv(i,ndf*8,4,std=2)
i = concat_diff(i)
i = Flatten()(i)
i = Dense(200)(i)
i = Activation('relu',name='relu_dens1')(i)
# i = Dense(200)(i)
# i = Activation('relu',name='relu_dens2')(i)
i = Dense(num_classes)(i)
predictions=Activation('sigmoid')(i)
model = Model(inputs=inp, outputs=predictions)
return model
def gan_dis_model_cel5():
inp = Input(shape=(218,178,3))
i = inp
ndf=24
def conv(i,nop,kw,std=1,usebn=True,bm='same',name=''):
i = Conv2D(nop,kernel_size=(kw,kw),padding=bm,strides=(std,std), kernel_initializer='he_uniform', kernel_regularizer=regularizers.l2(reg_val), bias_regularizer=regularizers.l2(reg_val),name='conv'+name)(i)
if usebn:
i = BatchNormalization(name='bn'+name)(i)
i = Activation('relu',name='relu'+name)(i)
return i
# only 5
i = conv(i,ndf*1,4,std=2,name='0',usebn=False)
i = concat_diff(i)
i = conv(i,ndf*2,4,std=2,name='1')
i = concat_diff(i)
i = conv(i,ndf*4,4,std=2,name='2')
i = concat_diff(i)
i = conv(i,ndf*8,4,std=2,name='3')
i = concat_diff(i)
i = conv(i,ndf*8,4,std=2,name='4')
i = concat_diff(i)
i0 = conv(i,ndf*8,4,std=2,name='5_cel0')
i0 = concat_diff(i0)
i0 = Conv2D(38,kernel_size=(1,1),padding='valid',strides=(1,1), kernel_initializer='he_uniform', kernel_regularizer=regularizers.l2(reg_val), bias_regularizer=regularizers.l2(reg_val),name='conv5.5_cel0')(i0)
i1 = conv(i,ndf*8,4,std=2,name='5_cel1')
i1 = concat_diff(i1)
i1 = Conv2D(38,kernel_size=(1,1),padding='valid',strides=(1,1), kernel_initializer='he_uniform', kernel_regularizer=regularizers.l2(reg_val), bias_regularizer=regularizers.l2(reg_val),name='conv5.5_cel1')(i1)
i2 = conv(i,ndf*8,4,std=2,name='5_cel2')
i2 = concat_diff(i2)
i2 = Conv2D(38,kernel_size=(1,1),padding='valid',strides=(1,1), kernel_initializer='he_uniform', kernel_regularizer=regularizers.l2(reg_val), bias_regularizer=regularizers.l2(reg_val),name='conv5.5_cel2')(i2)
i3 = conv(i,ndf*8,4,std=2,name='5_cel3')
i3 = concat_diff(i3)
i3 = Conv2D(39,kernel_size=(1,1),padding='valid',strides=(1,1), kernel_initializer='he_uniform', kernel_regularizer=regularizers.l2(reg_val), bias_regularizer=regularizers.l2(reg_val),name='conv5.5_cel3')(i3)
i4 = conv(i,ndf*8,4,std=2,name='5_cel4')
i4 = concat_diff(i4)
i4 = Conv2D(39,kernel_size=(1,1),padding='valid',strides=(1,1), kernel_initializer='he_uniform', kernel_regularizer=regularizers.l2(reg_val), bias_regularizer=regularizers.l2(reg_val),name='conv5.5_cel4')(i4)
i = Concatenate()([i0,i1,i2,i3,i4])
i = concat_diff(i)
i = Flatten()(i)
i = Dense(200, kernel_initializer='he_uniform', kernel_regularizer=regularizers.l2(reg_val), bias_regularizer=regularizers.l2(reg_val),name='dense1')(i)
i = Activation('relu',name='relu_dens1')(i)
# i = Dense(200, kernel_initializer='he_uniform', kernel_regularizer=regularizers.l2(reg_val), bias_regularizer=regularizers.l2(reg_val),name='dense2')(i)
# i = Activation('relu',name='relu_dens2')(i)
i = Dense(num_classes, kernel_initializer='he_uniform', kernel_regularizer=regularizers.l2(reg_val), bias_regularizer=regularizers.l2(reg_val),name='last_dense')(i)
predictions=Activation('sigmoid')(i)
model = Model(inputs=inp, outputs=predictions)
return model
def gan_dis_model_cel45():
inp = Input(shape=(218,178,3))
i = inp
ndf=24
def conv(i,nop,kw,std=1,usebn=True,bm='same',name=''):
i = Conv2D(nop,kernel_size=(kw,kw),padding=bm,strides=(std,std), kernel_initializer='he_uniform', kernel_regularizer=regularizers.l2(reg_val), bias_regularizer=regularizers.l2(reg_val),name='conv'+name)(i)
if usebn:
i = BatchNormalization(name='bn'+name)(i)
i = Activation('relu',name='relu'+name)(i)
return i
# 4 and 5
i = conv(i,ndf*1,4,std=2,name='0',usebn=False)
i = concat_diff(i)
i = conv(i,ndf*2,4,std=2,name='1')
i = concat_diff(i)
i = conv(i,ndf*4,4,std=2,name='2')
i = concat_diff(i)
i = conv(i,ndf*8,4,std=2,name='3')
i = concat_diff(i)
i0 = conv(i,ndf*8,4,std=2,name='4_cel0')
i0 = concat_diff(i0)
i1 = conv(i,ndf*8,4,std=2,name='4_cel1')
i1 = concat_diff(i1)
i2 = conv(i,ndf*8,4,std=2,name='4_cel2')
i2 = concat_diff(i2)
i3 = conv(i,ndf*8,4,std=2,name='4_cel3')
i3 = concat_diff(i3)
i4 = conv(i,ndf*8,4,std=2,name='4_cel4')
i4 = concat_diff(i4)
i0 = conv(i0,ndf*8,4,std=2,name='5_cel0')
i0 = concat_diff(i0)
i0 = Conv2D(38,kernel_size=(1,1),padding='valid',strides=(1,1), kernel_initializer='he_uniform', kernel_regularizer=regularizers.l2(reg_val), bias_regularizer=regularizers.l2(reg_val),name='conv5.5_cel0')(i0)
i1 = conv(i1,ndf*8,4,std=2,name='5_cel1')
i1 = concat_diff(i1)
i1 = Conv2D(38,kernel_size=(1,1),padding='valid',strides=(1,1), kernel_initializer='he_uniform', kernel_regularizer=regularizers.l2(reg_val), bias_regularizer=regularizers.l2(reg_val),name='conv5.5_cel1')(i1)
i2 = conv(i2,ndf*8,4,std=2,name='5_cel2')
i2 = concat_diff(i2)
i2 = Conv2D(38,kernel_size=(1,1),padding='valid',strides=(1,1), kernel_initializer='he_uniform', kernel_regularizer=regularizers.l2(reg_val), bias_regularizer=regularizers.l2(reg_val),name='conv5.5_cel2')(i2)
i3 = conv(i3,ndf*8,4,std=2,name='5_cel3')
i3 = concat_diff(i3)
i3 = Conv2D(39,kernel_size=(1,1),padding='valid',strides=(1,1), kernel_initializer='he_uniform', kernel_regularizer=regularizers.l2(reg_val), bias_regularizer=regularizers.l2(reg_val),name='conv5.5_cel3')(i3)
i4 = conv(i4,ndf*8,4,std=2,name='5_cel4')
i4 = concat_diff(i4)
i4 = Conv2D(39,kernel_size=(1,1),padding='valid',strides=(1,1), kernel_initializer='he_uniform', kernel_regularizer=regularizers.l2(reg_val), bias_regularizer=regularizers.l2(reg_val),name='conv5.5_cel4')(i4)
i = Concatenate()([i0,i1,i2,i3,i4])
i = concat_diff(i)
i = Flatten()(i)
i = Dense(200, kernel_initializer='he_uniform', kernel_regularizer=regularizers.l2(reg_val), bias_regularizer=regularizers.l2(reg_val),name='dense1')(i)
i = Activation('relu',name='relu_dens1')(i)
# i = Dense(200, kernel_initializer='he_uniform', kernel_regularizer=regularizers.l2(reg_val), bias_regularizer=regularizers.l2(reg_val),name='dense2')(i)
# i = Activation('relu',name='relu_dens2')(i)
i = Dense(num_classes, kernel_initializer='he_uniform', kernel_regularizer=regularizers.l2(reg_val), bias_regularizer=regularizers.l2(reg_val),name='last_dense')(i)
predictions=Activation('sigmoid')(i)
model = Model(inputs=inp, outputs=predictions)
return model
def gan_dis_model_cel345():
inp = Input(shape=(218,178,3))
i = inp
ndf=24
def conv(i,nop,kw,std=1,usebn=True,bm='same',name=''):
i = Conv2D(nop,kernel_size=(kw,kw),padding=bm,strides=(std,std), kernel_initializer='he_uniform', kernel_regularizer=regularizers.l2(reg_val), bias_regularizer=regularizers.l2(reg_val),name='conv'+name)(i)
if usebn:
i = BatchNormalization(name='bn'+name)(i)
i = Activation('relu',name='relu'+name)(i)
return i
# 3, 4, and 5
i = conv(i,ndf*1,4,std=2,name='0',usebn=False)
i = concat_diff(i)
i = conv(i,ndf*2,4,std=2,name='1')
i = concat_diff(i)
i = conv(i,ndf*4,4,std=2,name='2')
i = concat_diff(i)
i0 = conv(i,ndf*8,4,std=2,name='3_cel0')
i0 = concat_diff(i0)
i1 = conv(i,ndf*8,4,std=2,name='3_cel1')
i1 = concat_diff(i1)
i2 = conv(i,ndf*8,4,std=2,name='3_cel2')
i2 = concat_diff(i2)
i3 = conv(i,ndf*8,4,std=2,name='3_cel3')
i3 = concat_diff(i3)
i4 = conv(i,ndf*8,4,std=2,name='3_cel4')
i4 = concat_diff(i4)
i0 = conv(i0,ndf*8,4,std=2,name='4_cel0')
i0 = concat_diff(i0)
i1 = conv(i1,ndf*8,4,std=2,name='4_cel1')
i1 = concat_diff(i1)
i2 = conv(i2,ndf*8,4,std=2,name='4_cel2')
i2 = concat_diff(i2)
i3 = conv(i3,ndf*8,4,std=2,name='4_cel3')
i3 = concat_diff(i3)
i4 = conv(i4,ndf*8,4,std=2,name='4_cel4')
i4 = concat_diff(i4)
i0 = conv(i0,ndf*8,4,std=2,name='5_cel0')
i0 = concat_diff(i0)
i0 = Conv2D(38,kernel_size=(1,1),padding='valid',strides=(1,1), kernel_initializer='he_uniform', kernel_regularizer=regularizers.l2(reg_val), bias_regularizer=regularizers.l2(reg_val),name='conv5.5_cel0')(i0)
i1 = conv(i1,ndf*8,4,std=2,name='5_cel1')
i1 = concat_diff(i1)
i1 = Conv2D(38,kernel_size=(1,1),padding='valid',strides=(1,1), kernel_initializer='he_uniform', kernel_regularizer=regularizers.l2(reg_val), bias_regularizer=regularizers.l2(reg_val),name='conv5.5_cel1')(i1)
i2 = conv(i2,ndf*8,4,std=2,name='5_cel2')
i2 = concat_diff(i2)
i2 = Conv2D(38,kernel_size=(1,1),padding='valid',strides=(1,1), kernel_initializer='he_uniform', kernel_regularizer=regularizers.l2(reg_val), bias_regularizer=regularizers.l2(reg_val),name='conv5.5_cel2')(i2)
i3 = conv(i3,ndf*8,4,std=2,name='5_cel3')
i3 = concat_diff(i3)
i3 = Conv2D(39,kernel_size=(1,1),padding='valid',strides=(1,1), kernel_initializer='he_uniform', kernel_regularizer=regularizers.l2(reg_val), bias_regularizer=regularizers.l2(reg_val),name='conv5.5_cel3')(i3)
i4 = conv(i4,ndf*8,4,std=2,name='5_cel4')
i4 = concat_diff(i4)
i4 = Conv2D(39,kernel_size=(1,1),padding='valid',strides=(1,1), kernel_initializer='he_uniform', kernel_regularizer=regularizers.l2(reg_val), bias_regularizer=regularizers.l2(reg_val),name='conv5.5_cel4')(i4)
i = Concatenate()([i0,i1,i2,i3,i4])
i = concat_diff(i)
i = Flatten()(i)
i = Dense(200, kernel_initializer='he_uniform', kernel_regularizer=regularizers.l2(reg_val), bias_regularizer=regularizers.l2(reg_val),name='dense1')(i)
i = Activation('relu',name='relu_dens1')(i)
# i = Dense(200, kernel_initializer='he_uniform', kernel_regularizer=regularizers.l2(reg_val), bias_regularizer=regularizers.l2(reg_val),name='dense2')(i)
# i = Activation('relu',name='relu_dens2')(i)
i = Dense(num_classes, kernel_initializer='he_uniform', kernel_regularizer=regularizers.l2(reg_val), bias_regularizer=regularizers.l2(reg_val),name='last_dense')(i)
predictions=Activation('sigmoid')(i)
model = Model(inputs=inp, outputs=predictions)
return model
def gan_dis_model_cel2345():
inp = Input(shape=(218,178,3))
i = inp
ndf=24
def conv(i,nop,kw,std=1,usebn=True,bm='same',name=''):
i = Conv2D(nop,kernel_size=(kw,kw),padding=bm,strides=(std,std), kernel_initializer='he_uniform', kernel_regularizer=regularizers.l2(reg_val), bias_regularizer=regularizers.l2(reg_val),name='conv'+name)(i)
if usebn:
i = BatchNormalization(name='bn'+name)(i)
i = Activation('relu',name='relu'+name)(i)
return i
# 2, 3, 4, and 5
i = conv(i,ndf*1,4,std=2,name='0',usebn=False)
i = concat_diff(i)
i = conv(i,ndf*2,4,std=2,name='1')
i = concat_diff(i)
i0 = conv(i,ndf*4,4,std=2,name='2_cel0')
i0 = concat_diff(i0)
i1 = conv(i,ndf*4,4,std=2,name='2_cel1')
i1 = concat_diff(i1)
i2 = conv(i,ndf*4,4,std=2,name='2_cel2')
i2 = concat_diff(i2)
i3 = conv(i,ndf*4,4,std=2,name='2_cel3')
i3 = concat_diff(i3)
i4 = conv(i,ndf*4,4,std=2,name='2_cel4')
i4 = concat_diff(i4)
i0 = conv(i0,ndf*8,4,std=2,name='3_cel0')
i0 = concat_diff(i0)
i1 = conv(i1,ndf*8,4,std=2,name='3_cel1')
i1 = concat_diff(i1)
i2 = conv(i2,ndf*8,4,std=2,name='3_cel2')
i2 = concat_diff(i2)
i3 = conv(i3,ndf*8,4,std=2,name='3_cel3')
i3 = concat_diff(i3)
i4 = conv(i4,ndf*8,4,std=2,name='3_cel4')
i4 = concat_diff(i4)
i0 = conv(i0,ndf*8,4,std=2,name='4_cel0')
i0 = concat_diff(i0)
i1 = conv(i1,ndf*8,4,std=2,name='4_cel1')
i1 = concat_diff(i1)
i2 = conv(i2,ndf*8,4,std=2,name='4_cel2')
i2 = concat_diff(i2)
i3 = conv(i3,ndf*8,4,std=2,name='4_cel3')
i3 = concat_diff(i3)
i4 = conv(i4,ndf*8,4,std=2,name='4_cel4')
i4 = concat_diff(i4)
i0 = conv(i0,ndf*8,4,std=2,name='5_cel0')
i0 = concat_diff(i0)
i0 = Conv2D(38,kernel_size=(1,1),padding='valid',strides=(1,1), kernel_initializer='he_uniform', kernel_regularizer=regularizers.l2(reg_val), bias_regularizer=regularizers.l2(reg_val),name='conv5.5_cel0')(i0)
i1 = conv(i1,ndf*8,4,std=2,name='5_cel1')
i1 = concat_diff(i1)
i1 = Conv2D(38,kernel_size=(1,1),padding='valid',strides=(1,1), kernel_initializer='he_uniform', kernel_regularizer=regularizers.l2(reg_val), bias_regularizer=regularizers.l2(reg_val),name='conv5.5_cel1')(i1)
i2 = conv(i2,ndf*8,4,std=2,name='5_cel2')
i2 = concat_diff(i2)
i2 = Conv2D(38,kernel_size=(1,1),padding='valid',strides=(1,1), kernel_initializer='he_uniform', kernel_regularizer=regularizers.l2(reg_val), bias_regularizer=regularizers.l2(reg_val),name='conv5.5_cel2')(i2)
i3 = conv(i3,ndf*8,4,std=2,name='5_cel3')
i3 = concat_diff(i3)
i3 = Conv2D(39,kernel_size=(1,1),padding='valid',strides=(1,1), kernel_initializer='he_uniform', kernel_regularizer=regularizers.l2(reg_val), bias_regularizer=regularizers.l2(reg_val),name='conv5.5_cel3')(i3)
i4 = conv(i4,ndf*8,4,std=2,name='5_cel4')
i4 = concat_diff(i4)
i4 = Conv2D(39,kernel_size=(1,1),padding='valid',strides=(1,1), kernel_initializer='he_uniform', kernel_regularizer=regularizers.l2(reg_val), bias_regularizer=regularizers.l2(reg_val),name='conv5.5_cel4')(i4)
i = Concatenate()([i0,i1,i2,i3,i4])
i = concat_diff(i)
i = Flatten()(i)
i = Dense(200, kernel_initializer='he_uniform', kernel_regularizer=regularizers.l2(reg_val), bias_regularizer=regularizers.l2(reg_val),name='dense1')(i)
i = Activation('relu',name='relu_dens1')(i)
# i = Dense(200, kernel_initializer='he_uniform', kernel_regularizer=regularizers.l2(reg_val), bias_regularizer=regularizers.l2(reg_val),name='dense2')(i)
# i = Activation('relu',name='relu_dens2')(i)
i = Dense(num_classes, kernel_initializer='he_uniform', kernel_regularizer=regularizers.l2(reg_val), bias_regularizer=regularizers.l2(reg_val),name='last_dense')(i)
predictions=Activation('sigmoid')(i)
model = Model(inputs=inp, outputs=predictions)
return model
# fix random seed for reproducibility
seed = 7
numpy.random.seed(seed)
num_classes, input_shape = load_data_attr('CelebA_cls5')
# build the model
model = gan_dis_model()
# model = gan_dis_model_cel2345()
# model = cosen_cnn_model('normal')
model.load_weights(model_homepath+'fce_gan/save/'+filepath)
model.summary()
prediction=model.predict_generator(hdf5_generator('CelebA','test_cls5'), 14724)
prediction=numpy.transpose(prediction)
prediction[prediction<0.5]=0
prediction[prediction>=0.5]=1
prediction.astype(int)
prediction_flip=numpy.absolute(1-prediction)
y_data = HDF5Matrix(data_homepath+'Projects/data/CelebAHDF5/celeba_aligned_cropped_test_5cls.hdf5', 'targets')
y_data=y_data[:].astype(int)
y_data_flip=numpy.absolute(1-y_data)
confmat=numpy.matmul(prediction, y_data)
confmat_flip=numpy.matmul(prediction_flip, y_data_flip)
ap=numpy.count_nonzero(y_data[:,0])+numpy.count_nonzero(y_data[:,1])+numpy.count_nonzero(y_data[:,2])+numpy.count_nonzero(y_data[:,3])+numpy.count_nonzero(y_data[:,4])
an=numpy.count_nonzero(y_data_flip[:,0])+numpy.count_nonzero(y_data_flip[:,1])+numpy.count_nonzero(y_data_flip[:,2])+numpy.count_nonzero(y_data_flip[:,3])+numpy.count_nonzero(y_data_flip[:,4])
tp=confmat[0,0]+confmat[1,1]+confmat[2,2]+confmat[3,3]+confmat[4,4]
tn=confmat_flip[0,0]+confmat_flip[1,1]+confmat_flip[2,2]+confmat_flip[3,3]+confmat_flip[4,4]
fp=ap-tp
fn=an-tn
precision=tp/ap
recall=tp/(tp+fn)
acc=(tp+tn)/(ap+an)
ap0=numpy.count_nonzero(y_data[:,0])
an0=numpy.count_nonzero(y_data_flip[:,0])
tp0=confmat[0,0]
tn0=confmat_flip[0,0]
fp0=ap0-tp0
fn0=an0-tn0
precision0=tp0/ap0
recall0=tp0/(tp0+fn0)
acc0=(tp0+tn0)/(ap0+an0)
ap1=numpy.count_nonzero(y_data[:,1])
an1=numpy.count_nonzero(y_data_flip[:,1])
tp1=confmat[1,1]
tn1=confmat_flip[1,1]
fp1=ap1-tp1
fn1=an1-tn1
precision1=tp1/ap1
recall1=tp1/(tp1+fn1)
acc1=(tp1+tn1)/(ap1+an1)
ap2=numpy.count_nonzero(y_data[:,2])
an2=numpy.count_nonzero(y_data_flip[:,2])
tp2=confmat[2,2]
tn2=confmat_flip[2,2]
fp2=ap2-tp2
fn2=an2-tn2
precision2=tp2/ap2
recall2=tp2/(tp2+fn2)
acc2=(tp2+tn2)/(ap2+an2)
ap3=numpy.count_nonzero(y_data[:,3])
an3=numpy.count_nonzero(y_data_flip[:,3])
tp3=confmat[3,3]
tn3=confmat_flip[3,3]
fp3=ap3-tp3
fn3=an3-tn3
precision3=tp3/ap3
recall3=tp3/(tp3+fn3)
acc3=(tp3+tn3)/(ap3+an3)
ap4=numpy.count_nonzero(y_data[:,4])
an4=numpy.count_nonzero(y_data_flip[:,4])
tp4=confmat[4,4]
tn4=confmat_flip[4,4]
fp4=ap4-tp4
fn4=an4-tn4
precision4=tp4/ap4
recall4=tp4/(tp4+fn4)
acc4=(tp4+tn4)/(ap4+an4)
save = numpy.array([
[ap, ap0, ap1, ap2, ap3, ap4],
[an, an0, an1, an2, an3, an4],
[tp, tp0, tp1, tp2, tp3, tp4],
[tn, tn0, tn1, tn2, tn3, tn4],
[fp, fp0, fp1, fp2, fp3, fp4],
[fn, fn0, fn1, fn2, fn3, fn4],
[precision, precision0, precision1, precision2, precision3, precision4],
[recall, recall0, recall1, recall2, recall3, recall4],
[acc, acc0, acc1, acc2, acc3, acc4]
])
numpy.savetxt(modelname+'_cnn_stat.csv', save, delimiter=',')
# IPython.embed()