-
Notifications
You must be signed in to change notification settings - Fork 0
/
lets_gan_celebA.py
778 lines (618 loc) · 24.3 KB
/
lets_gan_celebA.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
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
from __future__ import print_function
import tensorflow as tf
from keras.datasets import cifar10
from keras.preprocessing.image import ImageDataGenerator
from keras.models import *
from keras.layers import *
# from keras.optimizers import *
from keras.utils import np_utils
import keras
import keras.backend as K
import math
import random
import numpy as np
import cv2
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.layers import Conv2DTranspose
from keras.layers import concatenate
from keras.engine.topology import get_source_inputs
from keras.utils.data_utils import get_file
from keras import backend as K
from keras.applications.imagenet_utils import decode_predictions
from keras.applications.imagenet_utils import _obtain_input_shape
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
batch_size = 32
nb_classes = 10
nb_epoch = 200
eps=1e-11
zed = 100
def cifar():
# input image dimensions
img_rows, img_cols = 32, 32
# the CIFAR10 images are RGB
img_channels = 3
# the data, shuffled and split between train and test sets
(X_train, y_train), (X_test, y_test) = cifar10.load_data()
print('X_train shape:', X_train.shape)
print(X_train.shape[0], 'train samples')
print(X_test.shape[0], 'test samples')
# convert class vectors to binary class matrices
Y_train = np_utils.to_categorical(y_train, nb_classes)
Y_test = np_utils.to_categorical(y_test, nb_classes)
X_train = X_train.astype('float32')
X_test = X_test.astype('float32')
X_train /= 255
X_test /= 255
X_train-=0.5
X_test-=0.5
return X_train,Y_train,X_test,Y_test
#print('loading cifar...')
#xt,yt,xv,yv = cifar()
def celebA():
# input image dimensions
img_rows, img_cols = 218, 178
# the CIFAR10 images are RGB
img_channels = 3
# the data, shuffled and split between train and test sets
train=h5py.File('/mnt/Storage/Projects/data/CelebAHDF5/celeba_aligned_cropped_train.hdf5','r')
# valid=h5py.File('/mnt/Storage/Projects/data/CelebAHDF5/celeba_aligned_cropped_valid.hdf5','r')
test=h5py.File('/mnt/Storage/Projects/data/CelebAHDF5/celeba_aligned_cropped_test.hdf5','r')
# # convert class vectors to binary class matrices
# Y_train = np_utils.to_categorical(y_train, nb_classes)
# Y_test = np_utils.to_categorical(y_test, nb_classes)
#
# X_train = X_train.astype('float32')
# X_test = X_test.astype('float32')
#
# X_train /= 255
# X_test /= 255
#
# X_train-=0.5
# X_test-=0.5
return train, test
train_data, test_data = celebA()
def leakyRelu(i):
return LeakyReLU(.2)(i)
def relu(i):
return Activation('relu')(i)
def bn(i):
return BatchNormalization()(i)
def gen2(): # generative network, 2
inp = Input(shape=(zed,))
i = inp
i = Reshape((1,1,zed))(i)
ngf=24
def deconv(i,nop,kw,oh,ow,std=1,tail=True,bm='same'):
global batch_size
i = Deconvolution2D(nop,kw,kw,subsample=(std,std),border_mode=bm,output_shape=(batch_size,oh,ow,nop))(i)
if tail:
i = bn(i)
i = relu(i)
return i
i = deconv(i,nop=ngf*8,kw=4,oh=4,ow=4,std=1,bm='valid')
i = deconv(i,nop=ngf*4,kw=4,oh=8,ow=8,std=2)
i = deconv(i,nop=ngf*2,kw=4,oh=16,ow=16,std=2)
i = deconv(i,nop=ngf*1,kw=4,oh=32,ow=32,std=2)
i = deconv(i,nop=3,kw=4,oh=32,ow=32,std=1,tail=False) # out : 32x32
i = Activation('tanh')(i)
m = Model(input=inp,output=i)
return m
def gen2_keras2(): # generative network, 2
inp = Input(shape=(zed,))
i = inp
i = Reshape((1,1,zed))(i)
ngf=24
def deconv(i,nop,kw,oh,ow,std=1,tail=True,bm='same'):
global batch_size
i = Conv2DTranspose(nop,kernel_size=(kw,kw),strides=(std,std),padding=bm, kernel_initializer='random_uniform')(i)
if tail:
i = bn(i)
i = relu(i)
return i
i = deconv(i,nop=ngf*8,kw=4,oh=4,ow=4,std=1,bm='valid')
i = deconv(i,nop=ngf*4,kw=4,oh=8,ow=8,std=2)
i = deconv(i,nop=ngf*2,kw=4,oh=16,ow=16,std=2)
i = deconv(i,nop=ngf*1,kw=4,oh=32,ow=32,std=2)
i = deconv(i,nop=3,kw=4,oh=32,ow=32,std=1,tail=False) # out : 32x32
i = Activation('tanh')(i)
m = Model(input=inp,output=i)
return m
def gen2_keras2_celebA(): # generative network, 2
inp = Input(shape=(zed,))
i = inp
i = Reshape((1,1,zed))(i)
ngf=24
def deconv(i,nop,kw,kh,std=1,tail=True,bm='same'):
global batch_size
i = Conv2DTranspose(nop,kernel_size=(kw,kh),strides=(std,std),padding=bm, kernel_initializer='random_uniform')(i)
if tail:
i = bn(i)
i = relu(i)
return i
i = deconv(i,nop=32,kw=5,kh=4,std=1,bm='valid')
i = deconv(i,nop=32,kw=4,kh=4,std=5)
i = deconv(i,nop=32,kw=3,kh=3,std=1,bm='valid')
i = deconv(i,nop=32,kw=4,kh=4,std=2)
i = deconv(i,nop=32,kw=4,kh=4,std=2)
i = deconv(i,nop=32,kw=4,kh=4,std=2,bm='valid')
i = deconv(i,nop=3,kw=4,kh=4,std=1,tail=False) # out : 218x178
i = Activation('tanh')(i)
m = Model(input=inp,output=i)
return m
def gen2_mod(): # generative network, 2
inp = Input(shape=(zed,))
i = inp
i = Reshape((1,1,zed))(i)
ngf=24
def deconv(i,nop,kw,kh,std=1,tail=True,bm='same'):
global batch_size
i = Conv2DTranspose(nop,kernel_size=(kw,kh),strides=(std,std),padding=bm, kernel_initializer='random_uniform')(i)
if tail:
i = bn(i)
i = relu(i)
return i
i = deconv(i,nop=ngf*8,kw=5,kh=4,std=1,bm='valid')
i = deconv(i,nop=ngf*8,kw=4,kh=4,std=5)
i = deconv(i,nop=ngf*8,kw=3,kh=3,std=1,bm='valid')
i = deconv(i,nop=ngf*4,kw=4,kh=4,std=2)
i = deconv(i,nop=ngf*2,kw=4,kh=4,std=2)
i = deconv(i,nop=ngf*1,kw=4,kh=4,std=2,bm='valid')
# i = deconv(i,nop=32,kw=5,kh=4,std=1,bm='valid')
# i = deconv(i,nop=32,kw=4,kh=4,std=5)
# i = deconv(i,nop=32,kw=3,kh=3,std=1,bm='valid')
# i = deconv(i,nop=32,kw=4,kh=4,std=2)
# i = deconv(i,nop=32,kw=4,kh=4,std=2)
# i = deconv(i,nop=32,kw=4,kh=4,std=2,bm='valid')
i = deconv(i,nop=3,kw=4,kh=4,std=1,tail=False) # out : 218x178
i = Activation('tanh')(i)
m = Model(input=inp,output=i)
return m
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
def concat_diff_keras2(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
def dis2(): # discriminative network, 2
# inp = Input(shape=(None,None,3))
inp = Input(shape=(32,32,3))
i = inp
ndf=24
def conv(i,nop,kw,std=1,usebn=True,bm='same'):
i = Convolution2D(nop,kw,kw,border_mode=bm,subsample=(std,std))(i)
if usebn:
i = bn(i)
i = 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)
# 1x1
i = Convolution2D(1,2,2,border_mode='valid')(i)
i = Activation('linear',name='conv_exit')(i)
i = Activation('sigmoid')(i)
i = Reshape((1,))(i)
m = Model(input=inp,output=i)
return m
def dis2_keras2(): # discriminative network, 2
# inp = Input(shape=(None,None,3))
# inp = Input(shape=(32,32,3))
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), use_bias=False, kernel_initializer='random_uniform')(i)
if usebn:
i = bn(i)
i = leakyRelu(i)
return i
i = conv(i,ndf*1,3,std=2,usebn=False)
i = concat_diff(i)
i = conv(i,ndf*2,3,std=2)
i = concat_diff(i)
i = conv(i,ndf*4,3,std=2)
i = concat_diff(i)
i = conv(i,ndf*8,4,std=2)
i = concat_diff(i)
# 1x1
i = Conv2D(1,(2,2),padding='valid')(i)
i = Activation('linear',name='conv_exit')(i)
i = Activation('sigmoid')(i)
i = Reshape((1,))(i)
m = Model(input=inp,output=i)
return m
def dis2_keras2_celebA(): # discriminative network, 2
# inp = Input(shape=(None,None,3))
# inp = Input(shape=(32,32,3))
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), use_bias=False, kernel_initializer='random_uniform')(i)
if usebn:
i = bn(i)
i = leakyRelu(i)
return i
i = conv(i,ndf*1,3,std=2,usebn=False)
i = concat_diff(i)
i = conv(i,ndf*2,3,std=2)
i = concat_diff(i)
i = conv(i,ndf*4,3,std=2)
i = concat_diff(i)
i = conv(i,ndf*8,3,std=2)
i = concat_diff(i)
i = conv(i,ndf*8,3,std=2)
i = concat_diff(i)
i = conv(i,ndf*8,3,std=2)
i = concat_diff(i)
# 1x1
i = Conv2D(1,(2,2),padding='valid')(i)
i = Activation('linear',name='conv_exit')(i)
i = Activation('sigmoid')(i)
# i = Reshape((1,))(i)
m = Model(input=inp,output=i)
return m
def dis2_mod(): # discriminative network, 2
# inp = Input(shape=(None,None,3))
# inp = Input(shape=(32,32,3))
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 = bn(i)
i = 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 = Conv2D(32, (3, 3), strides=(2, 2), padding='same', use_bias=False, name='block1_conv1', kernel_initializer='random_uniform')(i)
# i = BatchNormalization(name='block1_conv1_bn')(i)
# i = Activation('relu', name='block1_conv1_act')(i)
# i = concat_diff(i)
# i = Conv2D(32, (3, 3), padding='same', use_bias=False, name='block1_conv2', kernel_initializer='random_uniform')(i)
# i = BatchNormalization(name='block1_conv2_bn')(i)
# i = Activation('relu', name='block1_conv2_act')(i)
# i = concat_diff(i)
# i = Conv2D(128, (3, 3), padding='same', use_bias=False, name='block2_conv1', kernel_initializer='random_uniform')(i)
# i = BatchNormalization(name='block2_conv1_bn')(i)
# i = Activation('relu', name='block2_conv1_act')(i)
# i = concat_diff(i)
# i = Conv2D(128, (3, 3), padding='same', use_bias=False, name='block2_conv2', kernel_initializer='random_uniform')(i)
# i = BatchNormalization(name='block2_conv2_bn')(i)
# i = Activation('relu', name='block2_conv2_act')(i)
# i = concat_diff(i)
# i = Conv2D(256, (3, 3), padding='same', use_bias=False, name='block3_conv1', kernel_initializer='random_uniform')(i)
# i = BatchNormalization(name='block3_conv1_bn')(i)
# i = Activation('relu', name='block3_conv1_act')(i)
# i = concat_diff(i)
# i = Conv2D(256, (3, 3), padding='same', use_bias=False, name='block3_conv2', kernel_initializer='random_uniform')(i)
# i = BatchNormalization(name='block3_conv2_bn')(i)
# i = Activation('relu', name='block3_conv2_act')(i)
# i = concat_diff(i)
#
# i = Conv2D(728, (3, 3), padding='same', use_bias=False, name='block4_conv1', kernel_initializer='random_uniform')(i)
# i = BatchNormalization(name='block4_conv1_bn')(i)
# i = Activation('relu', name='block4_conv1_act')(i)
# i = concat_diff(i)
# i = Conv2D(728, (3, 3), padding='same', use_bias=False, name='block4_conv2', kernel_initializer='random_uniform')(i)
# i = BatchNormalization(name='block4_conv2_bn')(i)
# i = Activation('relu', name='block4_conv2_act')(i)
# i = concat_diff(i)
# residual = Conv2D(32, (2, 2), strides=(1, 1),
# padding='same', use_bias=False, kernel_initializer='random_uniform')(i)
# residual = BatchNormalization()(residual)
#
# i = SeparableConv2D(32, (3, 3), padding='same', use_bias=False, name='block2_sepconv1', kernel_initializer='random_uniform')(i)
# i = BatchNormalization(name='block2_sepconv1_bn')(i)
# i = Activation('relu', name='block2_sepconv2_act')(i)
# i = concat_diff(i)
# i = SeparableConv2D(32, (3, 3), padding='same', use_bias=False, name='block2_sepconv2', kernel_initializer='random_uniform')(i)
# i = BatchNormalization(name='block2_sepconv2_bn')(i)
# i = MaxPooling2D((3, 3), strides=(2, 2), padding='same', name='block2_pool')(i)
# i = layers.add([i, residual])
#
# residual = Conv2D(32, (1, 1), strides=(1, 1),
# padding='same', use_bias=False, kernel_initializer='random_uniform')(i)
# residual = BatchNormalization()(residual)
#
# i = Activation('relu', name='block3_sepconv1_act')(i)
# i = concat_diff(i)
# i = SeparableConv2D(32, (3, 3), padding='same', use_bias=False, name='block3_sepconv1', kernel_initializer='random_uniform')(i)
# i = BatchNormalization(name='block3_sepconv1_bn')(i)
# i = Activation('relu', name='block3_sepconv2_act')(i)
# i = concat_diff(i)
# i = SeparableConv2D(32, (3, 3), padding='same', use_bias=False, name='block3_sepconv2', kernel_initializer='random_uniform')(i)
# i = BatchNormalization(name='block3_sepconv2_bn')(i)
# i = MaxPooling2D((3, 3), strides=(2, 2), padding='same', name='block3_pool')(i)
# i = layers.add([i, residual])
#
# residual = Conv2D(728, (1, 1), strides=(2, 2),
# padding='same', use_bias=False, kernel_initializer='random_uniform')(i)
# residual = BatchNormalization()(residual)
#
# i = Activation('relu', name='block4_sepconv1_act')(i)
# i = concat_diff(i)
# i = SeparableConv2D(728, (3, 3), padding='same', use_bias=False, name='block4_sepconv1', kernel_initializer='random_uniform')(i)
# i = BatchNormalization(name='block4_sepconv1_bn')(i)
# i = Activation('relu', name='block4_sepconv2_act')(i)
# i = concat_diff(i)
# i = SeparableConv2D(728, (3, 3), padding='same', use_bias=False, name='block4_sepconv2', kernel_initializer='random_uniform')(i)
# i = BatchNormalization(name='block4_sepconv2_bn')(i)
#
# i = MaxPooling2D((3, 3), strides=(2, 2), padding='same', name='block4_pool')(i)
# i = layers.add([i, residual])
#
# i = concat_diff(i)
# # 1x1
i = Conv2D(1,(2,2),padding='valid')(i)
i = Activation('linear',name='conv_exit')(i)
i = Activation('sigmoid')(i)
# i = Reshape((1,))(i)
m = Model(input=inp,output=i)
return m
print('generating G...')
#gm = gen2_keras2()
#gm = gen2_keras2_celebA()
gm = gen2_mod()
gm.summary()
print('generating D...')
#dm = dis2_keras2()
#dm = dis2_keras2_celebA()
dm = dis2_mod()
dm.summary()
def gan(g,d):
# initialize a GAN trainer
# this is the fastest way to train a GAN in Keras
# two models are updated simutaneously in one pass
noise = Input(shape=g.input_shape[1:])
real_data = Input(shape=d.input_shape[1:])
generated = g(noise)
gscore = d(generated)
rscore = d(real_data)
def log_eps(i):
return K.log(i+1e-11)
# single side label smoothing: replace 1.0 with 0.9
dloss = - K.mean(log_eps(1-gscore) + .1 * log_eps(1-rscore) + .9 * log_eps(rscore))
gloss = - K.mean(log_eps(gscore))
Adam = tf.train.AdamOptimizer
lr,b1 = 1e-4,.2 # otherwise won't converge.
optimizer = Adam(lr,beta1=b1)
grad_loss_wd = optimizer.compute_gradients(dloss, d.trainable_weights)
update_wd = optimizer.apply_gradients(grad_loss_wd)
grad_loss_wg = optimizer.compute_gradients(gloss, g.trainable_weights)
update_wg = optimizer.apply_gradients(grad_loss_wg)
def get_internal_updates(model):
# get all internal update ops (like moving averages) of a model
inbound_nodes = model.inbound_nodes
input_tensors = []
for ibn in inbound_nodes:
input_tensors+= ibn.input_tensors
updates = [model.get_updates_for(i) for i in input_tensors]
return updates
other_parameter_updates = [get_internal_updates(m) for m in [d,g]]
# those updates includes batch norm.
print('other_parameter_updates for the models(mainly for batch norm):')
print(other_parameter_updates)
train_step = [update_wd, update_wg, other_parameter_updates]
losses = [dloss,gloss]
learning_phase = K.learning_phase()
def gan_feed(sess,batch_image,z_input):
# actual GAN trainer
nonlocal train_step,losses,noise,real_data,learning_phase
res = sess.run([train_step,losses],feed_dict={
noise:z_input,
real_data:batch_image,
learning_phase:True,
# Keras layers needs to know whether
# this run is training or testring (you know, batch norm and dropout)
})
loss_values = res[1]
return loss_values #[dloss,gloss]
return gan_feed
print('generating GAN...')
gan_feed = gan(gm,dm)
print('Ready. enter r() to train')
def r(ep=10000,noise_level=.01):
sess = K.get_session()
np.random.shuffle(xt)
shuffled_cifar = xt
length = len(shuffled_cifar)
for i in range(ep):
noise_level *= 0.99
print('---------------------------')
print('iter',i,'noise',noise_level)
# sample from cifar
j = i % int(length/batch_size)
minibatch = shuffled_cifar[j*batch_size:(j+1)*batch_size]
# minibatch += np.random.normal(loc=0.,scale=noise_level,size=subset_cifar.shape)
z_input = np.random.normal(loc=0.,scale=1.,size=(batch_size,zed))
# train for one step
losses = gan_feed(sess,minibatch,z_input)
print('dloss:{:6.4f} gloss:{:6.4f}'.format(losses[0],losses[1]))
if i==ep-1 or i % 10==0: show()
def r_celebA(class_nbr, ep=10000,noise_level=.01,last_ep=0,dm_weights=None,gm_weights=None,save=None):
sess = K.get_session()
# np.random.shuffle(xt)
# shuffled_cifar = xt
# length = 162770
# length = 3713
# length = 50000
# length = 10000
# class_nbr=4
if dm_weights is not None:
dm.load_weights(dm_weights)
if gm_weights is not None:
gm.load_weights(gm_weights)
idx = train_data['targets'][:,class_nbr].nonzero()[0].tolist()
for i in range(ep-last_ep):
show(save+'.png')
for j in range(batch_size):
# noise_level *= 0.99
print('---------------------------')
print('iter',i+last_ep+1,'batch',j+1)
# sample from cifar
# j = i % int(len(idx)/batch_size)
# IPython.embed()
minibatch = (train_data['features'][idx[j*batch_size:(j+1)*batch_size]]/255)-0.5
# minibatch += np.random.normal(loc=0.,scale=noise_level,size=subset_cifar.shape)
z_input = np.random.normal(loc=0.,scale=1.,size=(batch_size,zed))
# train for one step
losses = gan_feed(sess,minibatch,z_input)
print('dloss:{:6.4f} gloss:{:6.4f}'.format(losses[0],losses[1]))
# if i==ep-1 or i % 10==0:
# show()
if (i+last_ep+1) % 10==0:
# show(save+'.png')
if save is not None:
dm.save_weights('dm_'+save+'.hdf5')
gm.save_weights('gm_'+save+'.hdf5')
def r_celebA_shuffle(class_nbr, ep=10000,noise_level=.01,last_ep=0,dm_weights=None,gm_weights=None,save=None):
sess = K.get_session()
# np.random.shuffle(xt)
# shuffled_cifar = xt
# length = 162770
# length = 3713
# length = 50000
# length = 10000
# class_nbr=4
if dm_weights is not None:
dm.load_weights(dm_weights)
if gm_weights is not None:
gm.load_weights(gm_weights)
idx = train_data['targets'][:,class_nbr].nonzero()[0]
for i in range(ep-last_ep):
show(save+'.png')
for j in range(batch_size):
# noise_level *= 0.99
print('---------------------------')
print('iter',i+last_ep+1,'batch',j+1)
# sample from cifar
np.random.shuffle(idx)
# j = i % int(len(idx)/batch_size)
# IPython.embed()
idx_batch=idx[j*batch_size:(j+1)*batch_size]
idx_batch=np.sort(idx_batch).tolist()
# IPython.embed()
minibatch = (train_data['features'][idx_batch]/255)-0.5
# minibatch += np.random.normal(loc=0.,scale=noise_level,size=subset_cifar.shape)
z_input = np.random.normal(loc=0.,scale=1.,size=(batch_size,zed))
# train for one step
losses = gan_feed(sess,minibatch,z_input)
print('dloss:{:6.4f} gloss:{:6.4f}'.format(losses[0],losses[1]))
# if i==ep-1 or i % 10==0:
# show()
if (i+last_ep+1) % 10==0:
# show(save+'.png')
if save is not None:
dm.save_weights('dm_'+save+'.hdf5')
gm.save_weights('gm_'+save+'.hdf5')
def autoscaler(img):
limit = 400.
# scales = [0.1,0.125,1./6.,0.2,0.25,1./3.,1./2.] + range(100)
scales = np.hstack([1./np.linspace(10,2,num=9), np.linspace(1,100,num=100)])
imgscale = limit/float(img.shape[0])
# imgscale = 1
for s in scales:
if s>=imgscale:
imgscale=s
break
# IPython.embed()
img = cv2.resize(img,dsize=(int(img.shape[0]*imgscale),int(img.shape[1]*imgscale)),interpolation=cv2.INTER_NEAREST)
return img,imgscale
def flatten_multiple_image_into_image(arr):
import cv2
num,uh,uw,depth = arr.shape
patches = int(num+1)
# height = int(math.sqrt(patches)*0.9)
# width = int(patches/height+1)
height = 3
width = 5
img = np.zeros((height*uh+height, width*uw+width, 3),dtype='float32')
index = 0
for row in range(height):
for col in range(width):
if index>=num-1:
break
channels = arr[index]
img[row*uh+row:row*uh+uh+row,col*uw+col:col*uw+uw+col,:] = channels
index+=1
# img,imgscale = autoscaler(img)
imgscale=1
return img,imgscale
def show(save=False):
i = np.random.normal(loc=0.,scale=1.,size=(16,zed))
gened = gm.predict([i])
# IPython.embed()
gened *= 0.5
# IPython.embed()
gened +=0.5
# IPython.embed()
im,ims = flatten_multiple_image_into_image(gened)
# IPython.embed()
if save==False:
cv2.imshow('gened scale:'+str(ims),im[...,::-1])
cv2.waitKey(1)
if save!=False:
cv2.imwrite(save,im[...,::-1]*255)
def save_image(save=False):
i = np.random.normal(loc=0.,scale=1.,size=(16,zed))
gened = gm.predict([i])
# IPython.embed()
gened *= 0.5
# IPython.embed()
gened +=0.5
# IPython.embed()
im,ims = flatten_multiple_image_into_image(gened)
# IPython.embed()
cv2.imshow('gened scale:'+str(ims),im[...,::-1])
cv2.waitKey(1)
if save!=False:
cv2.imwrite(save,im[...,::-1]*255)
#show('test2.png')
#r_celebA(class_nbr=0, ep=10000, last_ep=10000, dm_weights='dm_fce_0.hdf5', gm_weights='gm_fce_0.hdf5', save='fce_0')
#r_celebA(class_nbr=1, ep=10000, last_ep=10000, dm_weights='dm_fce_1.hdf5', gm_weights='gm_fce_1.hdf5', save='fce_1')
#r_celebA(class_nbr=2, ep=10000, last_ep=10000, dm_weights='dm_fce_2.hdf5', gm_weights='gm_fce_2.hdf5', save='fce_2')
r_celebA(class_nbr=3, ep=10000, last_ep=9810, dm_weights='dm_fce_3.hdf5', gm_weights='gm_fce_3.hdf5', save='fce_3')
#r_celebA(class_nbr=4, ep=10000, last_ep=10000, dm_weights='dm_fce_4.hdf5', gm_weights='gm_fce_4.hdf5', save='fce_4')
#r_celebA(class_nbr=4, ep=10000, last_ep=0, dm_weights=None, gm_weights=None, save='fce_4')
#r_celebA(class_nbr=5, ep=10000, last_ep=1120, dm_weights='dm_fce_5.hdf5', gm_weights='gm_fce_5.hdf5', save='fce_5')
#dm.save_weights('')
#show('test2.png')