-
Notifications
You must be signed in to change notification settings - Fork 1
/
ODDL_MNIST.py
1664 lines (1322 loc) · 66.5 KB
/
ODDL_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
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
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import time
from utils import *
#from scipy.misc import imsave as ims
#from ops import *
#from utils import *
#from Utlis2 import *
import random as random
from glob import glob
import os, gzip
from glob import glob
from Basic_structure import *
from mnist_hand import *
from CIFAR10 import *
#import keras as K
import tensorflow.keras as K
#
from tensorflow.keras import layers
#from skimage.measure import compare_ssim
import skimage as skimage
from tensorflow_probability import distributions as tfd
#os.environ["CUDA_VISIBLE_DEVICES"] = '5'
import numpy.linalg as la
#import Fid_tf2 as fid2
#from inception import *
def file_name(file_dir):
t1 = []
file_dir = "F:/Third_Experiment/Multiple_GAN_codes/data/images_background/"
for root, dirs, files in os.walk(file_dir):
for a1 in dirs:
b1 = "F:/Third_Experiment/Multiple_GAN_codes/data/images_background/" + a1 + "/renders/*.png"
b1 = "F:/Third_Experiment/Multiple_GAN_codes/data/images_background/" + a1
for root2, dirs2, files2 in os.walk(b1):
for c1 in dirs2:
b2 = b1 + "/" + c1 + "/*.png"
img_path = glob(b2)
t1.append(img_path)
cc = []
for i in range(len(t1)):
a1 = t1[i]
for p1 in a1:
cc.append(p1)
return cc
def Classifier(name, image, z_dim=20, reuse=False):
with tf.compat.v1.variable_scope(name) as scope:
if reuse:
scope.reuse_variables()
batch_size = 64
kernel = 3
z_dim = 256
image = tf.compat.v1.reshape(image, (-1, 28 * 28))
#image = tf.compat.v1.concat((image, y), axis=1)
net = tf.compat.v1.nn.relu(bn(linear(image, 400, scope='g_fc1'), is_training=True, scope='g_bn1'))
batch_size = 64
kernel = 3
z_dim = 256
h5 = linear(image, 400, 'e_h5_lin')
h5 = lrelu(h5)
continous_len = 10
logoutput = linear(h5, continous_len, 'e_log_sigma_sq')
return logoutput
def Image_classifier(inputs, scopename, is_training=True, reuse=False):
with tf.variable_scope(scopename, reuse=reuse):
batch_norm_params = {'is_training': is_training, 'decay': 0.9, 'updates_collections': None}
with slim.arg_scope([slim.conv2d, slim.fully_connected],
normalizer_fn=slim.batch_norm,
normalizer_params=batch_norm_params):
x = tf.reshape(inputs, [-1, 32, 32, 3])
# For slim.conv2d, default argument values are like
# normalizer_fn = None, normalizer_params = None, <== slim.arg_scope changes these arguments
# padding='SAME', activation_fn=nn.relu,
# weights_initializer = initializers.xavier_initializer(),
# biases_initializer = init_ops.zeros_initializer,
net = slim.conv2d(x, 32, [5, 5], scope='conv1')
net = slim.max_pool2d(net, [2, 2], scope='pool1')
net = slim.conv2d(net, 64, [5, 5], scope='conv2')
net = slim.max_pool2d(net, [2, 2], scope='pool2')
net = slim.flatten(net, scope='flatten3')
# For slim.fully_connected, default argument values are like
# activation_fn = nn.relu,
# normalizer_fn = None, normalizer_params = None, <== slim.arg_scope changes these arguments
# weights_initializer = initializers.xavier_initializer(),
# biases_initializer = init_ops.zeros_initializer,
net = slim.fully_connected(net, 1024, scope='fc3')
net = slim.dropout(net, is_training=is_training, scope='dropout3') # 0.5 by default
outputs = slim.fully_connected(net, 10, activation_fn=None, normalizer_fn=None, scope='fco')
return outputs
def CodeImage_classifier(s, scopename, reuse=False):
with tf.variable_scope(scopename, reuse=reuse):
input = s
# initializers
w_init = tf.contrib.layers.variance_scaling_initializer()
b_init = tf.constant_initializer(0.)
n_hidden = 500
keep_prob = 0.9
# 1st hidden layer
w0 = tf.get_variable('w0', [input.get_shape()[1], n_hidden], initializer=w_init)
b0 = tf.get_variable('b0', [n_hidden], initializer=b_init)
h0 = tf.matmul(s, w0) + b0
h0 = tf.nn.tanh(h0)
h0 = tf.nn.dropout(h0, keep_prob)
n_output = 4
# output layer-mean
wo = tf.get_variable('wo', [h0.get_shape()[1], n_output], initializer=w_init)
bo = tf.get_variable('bo', [n_output], initializer=b_init)
y1 = tf.matmul(h0, wo) + bo
y = tf.nn.softmax(y1)
return y1, y
def sample_gumbel(shape, eps=1e-20):
"""Sample from Gumbel(0, 1)"""
U = tf.random_uniform(shape, minval=0, maxval=1)
return -tf.log(-tf.log(U + eps) + eps)
def my_gumbel_softmax_sample(logits, cats_range, temperature=0.1):
""" Draw a sample from the Gumbel-Softmax distribution"""
y = logits + sample_gumbel(tf.shape(logits))
logits_with_noise = tf.nn.softmax(y / temperature)
return logits_with_noise
def load_mnist(dataset_name):
data_dir = os.path.join("./data", dataset_name)
def extract_data(filename, num_data, head_size, data_size):
with gzip.open(filename) as bytestream:
bytestream.read(head_size)
buf = bytestream.read(data_size * num_data)
data = np.frombuffer(buf, dtype=np.uint8).astype(np.float)
return data
data = extract_data(data_dir + '/train-images-idx3-ubyte.gz', 60000, 16, 28 * 28)
trX = data.reshape((60000, 28, 28, 1))
data = extract_data(data_dir + '/train-labels-idx1-ubyte.gz', 60000, 8, 1)
trY = data.reshape((60000))
data = extract_data(data_dir + '/t10k-images-idx3-ubyte.gz', 10000, 16, 28 * 28)
teX = data.reshape((10000, 28, 28, 1))
data = extract_data(data_dir + '/t10k-labels-idx1-ubyte.gz', 10000, 8, 1)
teY = data.reshape((10000))
trY = np.asarray(trY)
teY = np.asarray(teY)
X = np.concatenate((trX, teX), axis=0)
y = np.concatenate((trY, teY), axis=0).astype(np.int)
seed = 547
np.random.seed(seed)
np.random.shuffle(X)
np.random.seed(seed)
np.random.shuffle(y)
y_vec = np.zeros((len(y), 10), dtype=np.float)
for i, label in enumerate(y):
y_vec[i, y[i]] = 1.0
return X / 255., y_vec
def My_Encoder_mnist(image, z_dim, name, batch_size=64, reuse=False):
with tf.variable_scope(name) as scope:
if reuse:
scope.reuse_variables()
len_discrete_code = 4
is_training = True
x = image
net = lrelu(conv2d(x, 64, 4, 4, 2, 2, name='c_conv1'))
net = lrelu(bn(conv2d(net, 128, 4, 4, 2, 2, name='c_conv2'), is_training=is_training, scope='c_bn2'))
net = tf.reshape(net, [batch_size, -1])
net = lrelu(bn(linear(net, 1024, scope='c_fc3'), is_training=is_training, scope='c_bn3'))
net = lrelu(bn(linear(net, 64, scope='e_fc11'), is_training=is_training, scope='c_bn11'))
z_mean = linear(net, z_dim, 'e_mean')
z_log_sigma_sq = linear(net, z_dim, 'e_log_sigma_sq')
z_log_sigma_sq = tf.nn.softplus(z_log_sigma_sq)
return z_mean, z_log_sigma_sq
def My_Classifier_mnist(image, z_dim, name, batch_size=64, reuse=False):
with tf.variable_scope(name) as scope:
if reuse:
scope.reuse_variables()
len_discrete_code = 4
is_training = True
# z_dim = 32
x = image
net = lrelu(conv2d(x, 64, 4, 4, 2, 2, name='c_conv1'))
net = lrelu(bn(conv2d(net, 128, 4, 4, 2, 2, name='c_conv2'), is_training=is_training, scope='c_bn2'))
net = tf.reshape(net, [batch_size, -1])
net = lrelu(bn(linear(net, 1024, scope='c_fc3'), is_training=is_training, scope='c_bn3'))
net = lrelu(bn(linear(net, 64, scope='e_fc11'), is_training=is_training, scope='c_bn11'))
out_logit = linear(net, len_discrete_code, scope='e_fc22')
softmaxValue = tf.nn.softmax(out_logit)
return out_logit, softmaxValue
def MINI_Classifier(s, scopename, reuse=False):
keep_prob = 1.0
with tf.variable_scope(scopename, reuse=reuse):
input = s
n_output = 10
n_hidden = 500
# initializers
w_init = tf.contrib.layers.variance_scaling_initializer()
b_init = tf.constant_initializer(0.)
# 1st hidden layer
w0 = tf.get_variable('w0', [input.get_shape()[1], n_hidden], initializer=w_init)
b0 = tf.get_variable('b0', [n_hidden], initializer=b_init)
h0 = tf.matmul(s, w0) + b0
h0 = tf.nn.tanh(h0)
h0 = tf.nn.dropout(h0, keep_prob)
n_output = 10
# output layer-mean
wo = tf.get_variable('wo', [h0.get_shape()[1], n_output], initializer=w_init)
bo = tf.get_variable('bo', [n_output], initializer=b_init)
y1 = tf.matmul(h0, wo) + bo
y = tf.nn.softmax(y1)
return y1, y
# Create model of CNN with slim api
class LifeLone_MNIST(object):
def __init__(self):
self.data_stream_batch = 10
self.batch_size = 64
self.input_height = 32
self.input_width = 32
self.c_dim = 3
self.z_dim = 50
self.len_discrete_code = 4
self.epoch = 200
self.classifierLearnRate = 0.000001
self.learning_rate = 1e-4
self.beta1 = 0.5
self.beta = 1.0
(Xtrain, ytrain), (Xtest, ytest) = keras.datasets.mnist.load_data()
Ntrain = Xtrain.shape[0]
Ntest = Xtest.shape[0]
# ---- reshape to vectors
Xtrain = Xtrain.reshape(Ntrain, -1) / 255
Xtest = Xtest.reshape(Ntest, -1) / 255
#Xtest = utils.bernoullisample(Xtest)
# ---- do the training
start = time.time()
best = float(-np.inf)
# Split MNIST into Five tasks
y_train = keras.utils.to_categorical(ytrain, num_classes=10)
ytest = keras.utils.to_categorical(ytest, num_classes=10)
arr1, labelArr1, arr2, labelArr2, arr3, labelArr3, arr4, labelArr4, arr5, labelArr5 = Split_dataset_by5(Xtrain,
y_train)
arr1_test, labelArr1_test, arr2_test, labelArr2_test, arr3_test, labelArr3_test, arr4_test, labelArr4_test, arr5_test, labelArr5_test = Split_dataset_by5(
Xtest,
ytest)
totalSet = np.concatenate((arr1, arr2, arr3, arr4, arr5),
axis=0)
totalSetLabel = np.concatenate((labelArr1, labelArr2, labelArr3, labelArr4, labelArr5),
axis=0)
self.totalSet = totalSet
self.totalSetLabel = totalSetLabel
testingSet = np.concatenate((arr1_test, arr2_test, arr3_test, arr4_test, arr5_test),
axis=0)
testingSetLabel = np.concatenate(
(labelArr1_test, labelArr2_test, labelArr3_test, labelArr4_test, labelArr5_test), axis=0)
self.arr1_test = arr1_test
self.arr2_test = arr2_test
self.arr3_test = arr3_test
self.arr4_test = arr4_test
self.arr5_test = arr5_test
self.labelArr1_test = labelArr1_test
self.labelArr2_test = labelArr2_test
self.labelArr3_test = labelArr3_test
self.labelArr4_test = labelArr4_test
self.labelArr5_test = labelArr5_test
self.testX = testingSet
self.testY = testingSetLabel
self.data_textX = np.concatenate((arr1_test,arr2_test,arr3_test,arr4_test,arr5_test),axis=0)
self.data_textY = np.concatenate((labelArr1_test,labelArr2_test,labelArr3_test,labelArr4_test,labelArr5_test),axis=0)
self.superEncoderArr = []
self.subEncoderArr = []
self.superGeneratorArr = []
self.subGeneratorArr = []
self.zArr = []
self.latentXArr = []
self.KLArr = []
self.recoArr = []
self.componentCount = 0
self.lossArr = []
self.memoryArr = []
self.parameterArr = []
self.label = tf.placeholder(tf.float32, [self.batch_size, 10])
self.KDlabel = tf.placeholder(tf.float32, [self.batch_size, 10])
self.StudentLogitInput = tf.placeholder(tf.float32, [self.batch_size, 10])
self.ClassifierPrediction = []
self.ClassifierLogits = []
self.Give_Feature = 0
self.GeneratorArr = []
self.TeacherFeatures = []
self.TeacherPredictions = []
self.TeacherLossArr = []
self.VAEOptimArr = []
self.ClassifierOptimArr = []
def Random_Data(self,data):
n_examples = np.shape(data)[0]
index = [i for i in range(n_examples)]
random.shuffle(index)
data = data[index]
return data
def Create_Student(self,index,selectedComponentIndex):
beta = 0.01
SuperGeneratorStr = "SuperGenerator" + str(index)
SubGeneratorStr = "SubGenerator" + str(index)
SuperEncoder = "SuperEncoder" + str(index)
SubEncoder = "SubEncoder" + str(index)
classifierName = "StudentClassifier" + str(index)
is_training = True
SuperGeneratorStr = "SuperGenerator" + str(index)
SubGeneratorStr = "SubGenerator" + str(index)
SuperEncoder = "SuperEncoder" + str(index)
SubEncoder = "SubEncoder" + str(index)
classifierName = "StuClassifier" + str(index)
discriminatorName = "discriminator" + str(index)
generatorName = "GAN_generator" + str(index)
is_training = True
sharedEncoderName = "StusharedEncoder" + str(index)
encoderName = "StuEncoder" + str(index)
sharedDecoderName = "sharedDecoder" + str(index)
decoderName = "StuDecoder" + str(index)
# Classifier
logits = Classifier(classifierName, self.inputs, self.z_dim, reuse=False)
label_softmax = tf.nn.softmax(logits)
self.StuPredictions = tf.argmax(label_softmax, 1)
self.StudentClassLoss = tf.reduce_mean(
tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=self.label))
z_shared = self.shoaared_encoder(sharedEncoderName, self.inputs, self.z_dim, reuse=False)
q_mu, q_std = self.encoder(encoderName, z_shared, self.z_dim, reuse=False)
n_samples = 1
qzx = tfd.Normal(q_mu, q_std + 1e-6)
z = qzx.sample(n_samples)
z = tf.reshape(z, (self.batch_size, -1))
x_shared = self.shared_decoder(sharedDecoderName, z, self.z_dim, reuse=False)
logits = self.decoder(decoderName, x_shared, self.z_dim, reuse=False)
gen_x_shared = self.shared_decoder(sharedDecoderName, self.z, self.z_dim, reuse=True)
gen_logits = self.decoder(decoderName, gen_x_shared, self.z_dim, reuse=True)
#self.GeneratorArr.append(gen_logits)
self.StuGenerator = gen_logits
reco = tf.reshape(logits, (self.batch_size, 28, 28, 1))
myInput = tf.reshape(self.inputs, (self.batch_size, 28, 28, 1))
reconstruction_loss1 = tf.reduce_mean(tf.reduce_sum(tf.square(reco - myInput), [1, 2, 3]))
KL_divergence1 = 0.5 * tf.reduce_sum(
tf.square(q_mu) + tf.square(q_std) - tf.log(
1e-8 + tf.square(q_std)) - 1,
1)
KL_divergence1 = tf.reduce_mean(KL_divergence1)
self.StudentvaeLoss = reconstruction_loss1 + self.beta * KL_divergence1
#self.lossArr.append(self.vaeLoss)
T_vars = tf.trainable_variables()
classifierParameters = [var for var in T_vars if var.name.startswith(classifierName)]
var1 = [var for var in T_vars if var.name.startswith(sharedEncoderName)]
var2 = [var for var in T_vars if var.name.startswith(encoderName)]
var3 = [var for var in T_vars if var.name.startswith(sharedDecoderName)]
var4 = [var for var in T_vars if var.name.startswith(decoderName)]
VAE_parameters = var1 + var2 + var3 + var4
with tf.variable_scope("foo", reuse=tf.AUTO_REUSE):
self.Student_Classifier_optim = tf.train.GradientDescentOptimizer(self.classifierLearnRate).minimize(self.StudentClassLoss, var_list=classifierParameters)
self.StudentVAE_optim1 = tf.train.AdamOptimizer(learning_rate=1e-4) \
.minimize(self.StudentvaeLoss, var_list=VAE_parameters)
def Create_subloss(self, G, name):
name = "discriminator1"
epsilon = tf.random_uniform([], 0.0, 1.0)
x_hat = epsilon * self.inputs + (1 - epsilon) * G
d_hat = Discriminator_SVHN_WGAN(x_hat, name, reuse=True)
scale = 10.0
ddx = tf.gradients(d_hat, x_hat)[0]
ddx = tf.sqrt(tf.reduce_sum(tf.square(ddx), axis=1))
ddx = tf.reduce_mean(tf.square(ddx - 1.0) * scale)
return ddx
def shoaared_encoder(self,name, x, z_dim=20, reuse=False):
with tf.variable_scope(name, reuse=reuse):
l1 = tf.layers.dense(x, 200, activation=tf.nn.tanh)
return l1
def encoder(self,name, x, z_dim=50, reuse=False):
with tf.variable_scope(name, reuse=reuse):
l1 = tf.layers.dense(x, 200, activation=tf.nn.tanh)
#l2 = tf.layers.dense(l1, 200, activation=tf.nn.relu)
mu = tf.layers.dense(l1, z_dim, activation=None)
sigma = tf.layers.dense(l1, z_dim, activation=tf.exp)
return mu, sigma
def shared_decoder(self,name,z, z_dim=50, reuse=False):
with tf.variable_scope(name, reuse=reuse):
l1 = tf.layers.dense(z, 200, activation=tf.nn.tanh)
return l1
def decoder(self,name,z, z_dim=50, reuse=False):
with tf.variable_scope(name, reuse=reuse):
l1 = tf.layers.dense(z, 200, activation=tf.nn.relu)
#l2 = tf.layers.dense(l1, 200, activation=tf.nn.relu)
x_hat = tf.layers.dense(
l1, self.data_dim, activation=None)
return x_hat
def Create_VAEs(self,index,selectedComponentIndex):
beta = 0.01
SuperGeneratorStr = "SuperGenerator" + str(index)
SubGeneratorStr = "SubGenerator" + str(index)
SuperEncoder = "SuperEncoder" + str(index)
SubEncoder = "SubEncoder" + str(index)
classifierName = "Classifier" + str(index)
discriminatorName = "discriminator" + str(index)
generatorName = "GAN_generator" + str(index)
is_training = True
sharedEncoderName = "sharedEncoder" + str(index)
encoderName = "Encoder" + str(index)
sharedDecoderName = "sharedDecoder" + str(index)
decoderName = "Decoder" + str(index)
if self.componentCount == 0:
# Classifier
logits = Classifier(classifierName, self.inputs, self.z_dim, reuse=False)
label_softmax = tf.nn.softmax(logits)
predictions = tf.argmax(label_softmax, 1)
self.TeacherPredictions.append(predictions)
TeacherClassLoss = tf.reduce_mean(
tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=self.label))
z_shared = self.shoaared_encoder(sharedEncoderName, self.inputs, self.z_dim, reuse=False)
q_mu, q_std = self.encoder(encoderName, z_shared, self.z_dim, reuse=False)
n_samples = 1
qzx = tfd.Normal(q_mu, q_std + 1e-6)
z = qzx.sample(n_samples)
z = tf.reshape(z,(self.batch_size,-1))
x_shared = self.shared_decoder(sharedDecoderName, z, self.z_dim, reuse=False)
logits = self.decoder(decoderName, x_shared, self.z_dim, reuse=False)
gen_x_shared = self.shared_decoder(sharedDecoderName, self.z, self.z_dim, reuse=True)
gen_logits = self.decoder(decoderName, gen_x_shared, self.z_dim, reuse=True)
self.GeneratorArr.append(gen_logits)
reco = tf.reshape(logits, (self.batch_size, 28, 28, 1))
myInput = tf.reshape(self.inputs, (self.batch_size, 28, 28, 1))
reconstruction_loss1 = tf.reduce_mean(tf.reduce_sum(tf.square(reco - myInput), [1, 2, 3]))
KL_divergence1 = 0.5 * tf.reduce_sum(
tf.square(q_mu) + tf.square(q_std) - tf.log(
1e-8 + tf.square(q_std)) - 1,
1)
KL_divergence1 = tf.reduce_mean(KL_divergence1)
vaeLoss = reconstruction_loss1 + self.beta *KL_divergence1
self.lossArr.append(vaeLoss)
T_vars = tf.trainable_variables()
classifierParameters = [var for var in T_vars if var.name.startswith(classifierName)]
var1 = [var for var in T_vars if var.name.startswith(sharedEncoderName)]
var2 = [var for var in T_vars if var.name.startswith(encoderName)]
var3 = [var for var in T_vars if var.name.startswith(sharedDecoderName)]
var4 = [var for var in T_vars if var.name.startswith(decoderName)]
VAE_parameters = var1 + var2 + var3 + var4
with tf.variable_scope("foo", reuse=tf.AUTO_REUSE):
VAE_optim1 = tf.train.AdamOptimizer(learning_rate=self.learning_rate) \
.minimize(vaeLoss, var_list=VAE_parameters)
'''
self.Teacher_optim1 = tf.train.AdamOptimizer(learning_rate=self.c) \
.minimize(self.TeacherClassLoss, var_list=classifierParameters)
'''
Teacher_optim1 = tf.train.AdamOptimizer(learning_rate=self.learning_rate).minimize(TeacherClassLoss, var_list=classifierParameters)
self.VAEOptimArr.append(VAE_optim1)
self.ClassifierOptimArr.append(Teacher_optim1)
self.componentCount = self.componentCount + 1
else:
# Classifier
logits = Classifier(classifierName, self.inputs, self.z_dim, reuse=False)
label_softmax = tf.nn.softmax(logits)
predictions = tf.argmax(label_softmax, 1)
self.TeacherPredictions.append(predictions)
TeacherClassLoss = tf.reduce_mean(
tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=self.label))
z_shared = self.shoaared_encoder(sharedEncoderName, self.inputs, self.z_dim, reuse=False)
q_mu, q_std = self.encoder(encoderName, z_shared, self.z_dim, reuse=False)
n_samples = 1
qzx = tfd.Normal(q_mu, q_std + 1e-6)
z = qzx.sample(n_samples)
z = tf.reshape(z, (self.batch_size, -1))
x_shared = self.shared_decoder(sharedDecoderName, z, self.z_dim, reuse=False)
logits = self.decoder(decoderName, x_shared, self.z_dim, reuse=False)
gen_x_shared = self.shared_decoder(sharedDecoderName, self.z, self.z_dim, reuse=True)
gen_logits = self.decoder(decoderName, gen_x_shared, self.z_dim, reuse=True)
self.GeneratorArr.append(gen_logits)
reco = tf.reshape(logits, (self.batch_size, 28, 28, 1))
myInput = tf.reshape(self.inputs, (self.batch_size, 28, 28, 1))
reconstruction_loss1 = tf.reduce_mean(tf.reduce_sum(tf.square(reco - myInput), [1, 2, 3]))
KL_divergence1 = 0.5 * tf.reduce_sum(
tf.square(q_mu) + tf.square(q_std) - tf.log(
1e-8 + tf.square(q_std)) - 1,
1)
KL_divergence1 = tf.reduce_mean(KL_divergence1)
vaeLoss = reconstruction_loss1 + self.beta * KL_divergence1
self.lossArr.append(vaeLoss)
T_vars = tf.trainable_variables()
classifierParameters = [var for var in T_vars if var.name.startswith(classifierName)]
var1 = [var for var in T_vars if var.name.startswith(sharedEncoderName)]
var2 = [var for var in T_vars if var.name.startswith(encoderName)]
var3 = [var for var in T_vars if var.name.startswith(sharedDecoderName)]
var4 = [var for var in T_vars if var.name.startswith(decoderName)]
VAE_parameters = var1 + var2 + var3 + var4
with tf.variable_scope("foo", reuse=tf.AUTO_REUSE):
VAE_optim1 = tf.train.AdamOptimizer(learning_rate=self.learning_rate) \
.minimize(vaeLoss, var_list=VAE_parameters)
'''
self.Teacher_optim1 = tf.train.AdamOptimizer(learning_rate=self.c) \
.minimize(self.TeacherClassLoss, var_list=classifierParameters)
'''
Teacher_optim1 = tf.train.AdamOptimizer(learning_rate=self.learning_rate).minimize(
TeacherClassLoss, var_list=classifierParameters)
self.VAEOptimArr.append(VAE_optim1)
self.ClassifierOptimArr.append(Teacher_optim1)
self.componentCount = self.componentCount + 1
global_vars = tf.global_variables()
is_not_initialized = self.sess.run([tf.is_variable_initialized(var) for var in global_vars])
not_initialized_vars = [v for (v, f) in zip(global_vars, is_not_initialized) if not f]
self.sess.run(tf.variables_initializer(not_initialized_vars))
#set weights for the new component
'''
print(selectedComponentIndex)
parent_SubGeneratorStr = "SubGenerator"+ str(selectedComponentIndex)
parent_SubEncoder = "SubEncoder"+ str(selectedComponentIndex)
parent_vars1 = [var for var in T_vars if var.name.startswith(parent_SubGeneratorStr)]
parent_vars2 = [var for var in T_vars if var.name.startswith(parent_SubEncoder)]
arr1 = []
arr1_value = []
for var in vars1:
arr1.append(var)
for var in parent_vars1:
arr1_value.append(var)
print(np.shape(arr1))
print(np.shape(arr1_value))
for i in range(np.shape(arr1)[0]):
tf.assign(arr1[i],arr1_value[i])
arr1 = []
arr1_value = []
for var in vars2:
arr1.append(var)
for var in parent_vars2:
arr1_value.append(var)
for i in range(np.shape(arr1)[0]):
tf.assign(arr1[i],arr1_value[i])
'''
def SelectComponentByData(self,data):
mycount = int(np.shape(data)[0] / self.batch_size)
losses = []
for i in range(np.shape(self.lossArr)[0]):
sumLoss = 0
for j in range(mycount):
batch = data[j * self.batch_size:(j + 1) * self.batch_size]
loss1 = self.sess.run(self.lossArr[i],feed_dict={self.inputs:batch})
sumLoss = sumLoss + loss1
sumLoss = sumLoss / mycount
losses.append(sumLoss)
print("index")
print(losses)
losses = np.array(losses)
index = np.argmin(losses)
#index = index + 1
return index
def Reconstruction_ByIndex(self,index,test):
count = int(np.shape(test)[0] / self.batch_size)
realTest = []
recoArr = []
for i in range(count):
batch = test[i * self.batch_size : (i+1) * self.batch_size]
reco = self.sess.run(self.recoArr[index],feed_dict={self.inputs:batch})
for j in range(self.batch_size):
realTest.append(batch[j])
recoArr.append(reco[j])
realTest = np.array(realTest)
recoArr = np.array(recoArr)
return realTest,recoArr
def Calculate_Accuracy_ByALL_Batch(self, testX,testY, index):
totalCount = np.shape(testX)[0]
myPro = []
totalBatchCount = int(totalCount / self.batch_size)
for i in range(totalBatchCount):
batch = testX[i*self.batch_size:(i+1)*self.batch_size]
index = self.SelectComponentByBatch(batch)
pred = self.sess.run(self.TeacherPredictions[index],feed_dict={self.inputs:batch})
for j in range(self.batch_size):
myPro.append(pred[j])
target = [np.argmax(one_hot) for one_hot in testY]
sumError = 0
accCount = 0
target = target[0:np.shape(myPro)[0]]
for i in range(np.shape(myPro)[0]):
if myPro[i] == target[i]:
accCount = accCount + 1
totalCount = np.shape(myPro)[0]
acc = float(accCount / totalCount)
return acc
def ClassficationEvaluation(self):
index1 = self.SelectComponentByData(self.train_arr1)
test1 = self.train_arr1
test1, reco1 = self.Reconstruction_ByIndex(index1, test1)
label1 = self.train_labelArr1[0:np.shape(reco1)[0]]
index2 = self.SelectComponentByData(self.train_arr2)
test2 = self.train_arr2
test2, reco2 = self.Reconstruction_ByIndex(index2, test2)
label2 = self.train_labelArr2[0:np.shape(reco2)[0]]
index3 = self.SelectComponentByData(self.train_arr3)
test3 = self.train_arr3
test3, reco3 = self.Reconstruction_ByIndex(index3, test3)
label3 = self.train_labelArr3[0:np.shape(reco3)[0]]
index4 = self.SelectComponentByData(self.train_arr4)
test4 = self.train_arr4
test4, reco4 = self.Reconstruction_ByIndex(index4, test4)
label4 = self.train_labelArr4[0:np.shape(reco4)[0]]
index5 = self.SelectComponentByData(self.train_arr5)
test5 = self.train_arr5
test5, reco5 = self.Reconstruction_ByIndex(index5, test5)
label5 = self.train_labelArr5[0:np.shape(reco5)[0]]
index6 = self.SelectComponentByData(self.train_arr6)
test6 = self.train_arr6
test6, reco6 = self.Reconstruction_ByIndex(index6, test6)
label6 = self.train_labelArr6[0:np.shape(reco6)[0]]
index7 = self.SelectComponentByData(self.train_arr7)
test7 = self.train_arr7
test7, reco7 = self.Reconstruction_ByIndex(index7, test7)
label7 = self.train_labelArr7[0:np.shape(reco7)[0]]
index8 = self.SelectComponentByData(self.train_arr8)
test8 = self.train_arr8
test8, reco8 = self.Reconstruction_ByIndex(index8, test8)
label8 = self.train_labelArr8[0:np.shape(reco8)[0]]
index9 = self.SelectComponentByData(self.train_arr9)
test9 = self.train_arr9
test9, reco9 = self.Reconstruction_ByIndex(index9, test9)
label9 = self.train_labelArr8[0:np.shape(reco9)[0]]
index10 = self.SelectComponentByData(self.train_arr10)
test10 = self.train_arr10
test10, reco10 = self.Reconstruction_ByIndex(index10, test10)
label10 = self.train_labelArr10[0:np.shape(reco10)[0]]
arrLabels = []
arrReco = []
arrLabels.append(label1)
arrLabels.append(label2)
arrLabels.append(label3)
arrLabels.append(label4)
arrLabels.append(label5)
arrLabels.append(label6)
arrLabels.append(label7)
arrLabels.append(label8)
arrLabels.append(label9)
arrLabels.append(label10)
arrReco.append(reco1)
arrReco.append(reco2)
arrReco.append(reco3)
arrReco.append(reco4)
arrReco.append(reco5)
arrReco.append(reco6)
arrReco.append(reco7)
arrReco.append(reco8)
arrReco.append(reco9)
arrReco.append(reco10)
arrLabels, arrReco = self.Combined_data(arrLabels, arrReco)
from keras.utils import np_utils
from keras.models import Sequential
from keras.layers import Dense, Activation
from keras.optimizers import RMSprop
model = Sequential([
Dense(2048, input_dim=32*32*3), # input_dim即为28* 28=784,output_dim为32,即传出来只有32个feature
Activation('relu'), # 变成非线性化的数据
Dense(1024, input_dim=2048),
Activation('relu'), # 变成非线性化的数据
Dense(512, input_dim=1024),
Activation('relu'), # 变成非线性化的数据
Dense(256, input_dim=512),
Activation('relu'), # 变成非线性化的数据
Dense(10), # input即为上一层的output,故定义output_dim是10个feature就可以
Activation('softmax') # 使用softmax进行分类处理
])
rmsprop = RMSprop(lr=0.001, rho=0.9, epsilon=1e-08, decay=0.0)
model.compile( # 激活model
optimizer=rmsprop, # 若是要使用默认的参数,即optimizer=‘rmsprop'即可
loss='categorical_crossentropy', # crossentropy协方差
metrics=['accuracy'])
arrReco = np.reshape(arrReco,(-1,32*32*3))
test = np.reshape(self.cifar_test_x,(-1,32*32*3))
model.fit(arrReco, arrLabels, epochs=100, batch_size=64) # 使用fit功能来training;epochs表示训练的轮数;
loss, accuracy = model.evaluate(test, self.cifar_test_label)
return accuracy
def build_model(self):
min_value = 1e-10
# some parameters
image_dims = [self.input_height, self.input_width, self.c_dim]
bs = self.batch_size
self.data_dim = 28*28
self.inputs = tf.placeholder(tf.float32, [self.batch_size, self.data_dim])
self.KDinputs = tf.placeholder(tf.float32, [self.batch_size, self.data_dim])
self.z = tf.placeholder(tf.float32, [self.batch_size, self.z_dim], name='z')
self.y = tf.placeholder(tf.float32, [self.batch_size, self.len_discrete_code])
self.labels = tf.placeholder(tf.float32, [self.batch_size, 10])
self.weights = tf.placeholder(tf.float32, [self.batch_size, 4])
self.index = tf.placeholder(tf.int32, [self.batch_size])
self.gan_inputs = tf.placeholder(tf.float32, [bs] + image_dims)
self.gan_domain = tf.placeholder(tf.float32, [self.batch_size, 4])
self.gan_domain_labels = tf.placeholder(tf.float32, [self.batch_size, 1])
# GAN networks
self.Create_VAEs(1,0)
#self.Create_VAEs(2,0)
#self.Create_Student(1,0)
def Generate_PreviousSamples(self, num):
b_num = int(num / self.batch_size)
mylist = []
for i in range(b_num):
# update GAN
batch_z = np.random.uniform(-1, 1, [self.batch_size, self.z_dim]).astype(np.float32)
gan1 = self.sess.run(
self.GAN_gen1,
feed_dict={self.z: batch_z})
print(np.shape(gan1))
for ttIndex in range(self.batch_size):
mylist.append(gan1[ttIndex])
mylist = np.array(mylist)
return mylist
def Combined_data(self,arr1,arr2):
r1 = []
r2 = []
for i in range(np.shape(arr1)[0]):
b1 = arr1[i]
b2 = arr2[i]
for j in range(np.shape(b1)[0]):
r1.append(b1[j])
r2.append(b2[j])
r1 = np.array(r1)
r2 = np.array(r2)
return r1,r2
def predict(self):
# define the classifier
label_logits = Image_classifier(self.inputs, "label_classifier", reuse=True)
label_softmax = tf.nn.softmax(label_logits)
predictions = tf.argmax(label_softmax, 1, name="predictions")
return predictions
def Give_predictedLabels(self, testX):
totalN = np.shape(testX)[0]
myN = int(totalN / self.batch_size)
myPrediction = self.predict()
totalPredictions = []
myCount = 0
for i in range(myN):
my1 = testX[self.batch_size * i:self.batch_size * (i + 1)]
predictions = self.sess.run(myPrediction, feed_dict={self.inputs: my1})
for k in range(self.batch_size):
totalPredictions.append(predictions[k])
totalPredictions = np.array(totalPredictions)
totalPredictions = K.utils.to_categorical(totalPredictions)
return totalPredictions
def domain_predict(self):
z_mean1, z_log_sigma_sq1 = Encoder_SVHN(self.inputs, "encoder", batch_size=64, reuse=True)
continous_variables1 = z_mean1 + z_log_sigma_sq1 * tf.random_normal(tf.shape(z_mean1), 0, 1, dtype=tf.float32)
domain_logit, domain_class = CodeImage_classifier(continous_variables1, "encoder_domain", reuse=True)
domain_logit = tf.nn.softmax(domain_logit)
predictions = tf.argmax(domain_logit, 1)
return predictions
def Give_RealReconstruction(self):
z_mean1, z_log_sigma_sq1 = Encoder_SVHN(self.inputs, "encoder", batch_size=64, reuse=True)
continous_variables1 = z_mean1 + z_log_sigma_sq1 * tf.random_normal(tf.shape(z_mean1), 0, 1, dtype=tf.float32)
domain_logit, domain_class = CodeImage_classifier(continous_variables1, "encoder_domain", reuse=True)
log_y = tf.log(tf.nn.softmax(domain_logit) + 1e-10)
discrete_real = my_gumbel_softmax_sample(log_y, np.arange(self.len_discrete_code))
code = tf.concat((continous_variables1, discrete_real), axis=1)
VAE1 = Generator_SVHN("VAE_Generator", code, reuse=True)
reconstruction_loss1 = tf.reduce_mean(tf.reduce_sum(tf.square(VAE1 - self.inputs), [1, 2, 3]))
return reconstruction_loss1
def Calculate_ReconstructionErrors(self, testX):
p1 = int(np.shape(testX)[0] / self.batch_size)
myPro = self.Give_RealReconstruction()
sumError = 0
for i in range(p1):
g = testX[i * self.batch_size:(i + 1) * self.batch_size]
sumError = sumError + self.sess.run(myPro, feed_dict={self.inputs: g})
sumError = sumError / p1
return sumError
def random_pick(self, some_list, probabilities):
x = random.uniform(0, 1)
cumulative_probability = 0.0
for item, item_probability in zip(some_list, probabilities):
cumulative_probability += item_probability
if x < cumulative_probability: break
return item
def Regular_Matrix(self,matrix):
count = np.shape(matrix)[0]
#avoid negative value for each element
for i in range(count):
for j in range(count):
if matrix[i,j] < 0:
matrix[i,j] = 0.05
#normalize the probability for each row
for i in range(count):
row = matrix[i,0:count]
sum1 = self.ReturnSum(row)
for j in range(count):
row[j] = row[j] / sum1
self.proMatrix[i,j] = row[j]
def ReturnSum(self,arr):
count = np.shape(arr)[0]
sum1 = 0
for i in range(count):
sum1 += arr[i]
return sum1
def Give_Reconstruction_ByAnySamples(self,set):
recoList = []
count = np.shape(set)[0]
for i in range(count):
batch = set[i]
index2,batch2 = self.SelectComponentByBatch(batch)
reco = self.sess.run(self.recoArr[index2 - 1],feed_dict={self.inputs:batch2})
recoList.append(reco[0])
recoList = np.array(recoList)
return recoList
def KeepSize(self,arr,size):
mycount = np.shape(arr)[0]
mycount = int(mycount/self.batch_size)
lossArr = []
arr = np.array(arr)
for i in range(mycount):
batch = arr[i*self.batch_size : (i+1)*self.batch_size]
loss = self.sess.run(self.VAE_multi,feed_dict={self.inputs:batch})
for j in range(self.batch_size):
lossArr.append(loss[j])