-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVAE.py
238 lines (192 loc) · 7.58 KB
/
VAE.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
# -*- coding: utf-8 -*-
"""
Created on Mon Feb 25 15:08:21 2019
@author: IIST
"""
from keras.models import Sequential, Model
from keras.layers import *
from keras.optimizers import *
import os
from keras import metrics, backend as K
from PIL import Image
import numpy as np
#filenames = list()
#labels = list()
#
#images = os.path.join("img_align_celeba_png/")
#annot = os.path.join("list_attr_celeba_png.txt")
#
#with open(annot) as in_file:
# count_datapoints = int(in_file.readline())
# plaintext_labels = in_file.readline().split()
#
# for line in in_file:
# splitted = line.split()
#
# filenames.append(os.path.join(images, splitted[0]))
#
# properties_celebrity = [float(x) for x in splitted[1:]]
# properties_celebrity = [max(0.0, x) for x in properties_celebrity]
# labels.append(properties_celebrity)
#assert len(filenames) == len(labels)
#
## print(labels[:4])
## print(filenames[:4])
#
#show_number = len(filenames)
#datasetlist = []
## print(plaintext_labels)
#for filename, properties in zip(filenames[:show_number], labels[:show_number]):
# image = Image.open(os.path.join(filename))
# image = image.resize([64, 64], Image.ANTIALIAS)
# image = (np.array(image) - 127.5 / 127.5)
# # image = plt.imread(image)
# # plt.imshow(image)
# # plt.show()
# datasetlist.append(image)
# print(filename)
# # print(properties)
#
#dataset = np.array(datasetlist)
#print(dataset.shape)
def sampling(args):
mean, logsigma = args
epsilon = K.random_normal(shape=(K.shape(mean)[0], 512), mean=0., stddev=1.0)
return mean + K.exp(logsigma / 2) * epsilon
def encoder(kernel, filter, rows, columns, channel):
X = Input(shape=(rows, columns, channel))
Y = Input(shape=(2,2,1024))
model = Conv2D(filters=filter, kernel_size=kernel, strides=2, padding='same')(X) #64*64*32
model = BatchNormalization(epsilon=1e-5)(model)
model = LeakyReLU(alpha=0.2)(model)
model = Conv2D(filters=filter*2, kernel_size=kernel, strides=2, padding='same')(X) #32*32*64
model = BatchNormalization(epsilon=1e-5)(model)
model = LeakyReLU(alpha=0.2)(model)
model = Conv2D(filters=filter*4, kernel_size=kernel, strides=2, padding='same')(model)#16*16*128
model = BatchNormalization(epsilon=1e-5)(model)
model = LeakyReLU(alpha=0.2)(model)
model = Conv2D(filters=filter*8, kernel_size=kernel, strides=2, padding='same')(model) #8*8*256
model = BatchNormalization(epsilon=1e-5)(model)
model = LeakyReLU(alpha=0.2)(model)
model = Conv2D(filters=filter*16, kernel_size=kernel, strides=2, padding='same')(model) #4*4*512
model = BatchNormalization(epsilon=1e-5)(model)
model = LeakyReLU(alpha=0.2)(model)
model = Conv2D(filters=filter*32, kernel_size=kernel, strides=2, padding='same')(model) #2*2*1024
model = BatchNormalization(epsilon=1e-5)(model)
model = LeakyReLU(alpha=0.2)(model)
model = Concatenate()([model, Y])
model = Flatten()(model)
mean = Dense(512)(model)
logsigma = Dense(512, activation='tanh')(model)
latent = Lambda(sampling, output_shape=(512,))([mean, logsigma])
meansigma = Model([X], [mean, logsigma, latent])
return meansigma
def decgen(kernel, filter, rows, columns, channel):
X = Input(shape=(512,))
model = Dense(filter*8*rows*columns)(X)
model = Reshape((rows, columns, filter * 8))(model)
model = BatchNormalization(epsilon=1e-5)(model)
model = Activation('relu')(model)
model = Conv2DTranspose(filters=filter*4, kernel_size=kernel, strides=2, padding='same')(model)
model = BatchNormalization(epsilon=1e-5)(model)
model = Activation('relu')(model)
model = Conv2DTranspose(filters=filter*2, kernel_size=kernel, strides=2, padding='same')(model)
model = BatchNormalization(epsilon=1e-5)(model)
model = Activation('relu')(model)
model = Conv2DTranspose(filters=filter, kernel_size=kernel, strides=2, padding='same')(model)
model = BatchNormalization(epsilon=1e-5)(model)
model = Activation('relu')(model)
model = Conv2DTranspose(filters=channel, kernel_size=kernel, strides=2, padding='same')(model)
model = Activation('tanh')(model)
model = Model(X, model)
return model
def discriminator(kernel, filter, rows, columns, channel):
X = Input(shape=(rows, columns, channel))
model = Conv2D(filters=filter*2, kernel_size=kernel, strides=2, padding='same')(X)
model = LeakyReLU(alpha=0.2)(model)
model = Conv2D(filters=filter*4, kernel_size=kernel, strides=2, padding='same')(model)
model = BatchNormalization(epsilon=1e-5)(model)
model = LeakyReLU(alpha=0.2)(model)
model = Conv2D(filters=filter*8, kernel_size=kernel, strides=2, padding='same')(model)
model = BatchNormalization(epsilon=1e-5)(model)
model = LeakyReLU(alpha=0.2)(model)
model = Conv2D(filters=filter*8, kernel_size=kernel, strides=2, padding='same')(model)
dec = BatchNormalization(epsilon=1e-5)(model)
dec = LeakyReLU(alpha=0.2)(dec)
dec = Flatten()(dec)
dec = Dense(1, activation='sigmoid')(dec)
output = Model([X], [dec, model])
return output
batch_size = 512
rows = 64
columns = 64
channel = 3
epochs = 20000
datasize = len(dataset)
noise = np.random.normal(0, 1, (batch_size, 256))
# optimizers
SGDop = SGD(lr=0.0003)
ADAMop = Adam(lr=0.0002)
# encoder
E = encoder(5, 32, rows, columns, channel)
E.compile(optimizer=SGDop, loss='mse')
E.summary()
# generator/decoder
G = decgen(5, 32, rows, columns, channel)
G.compile(optimizer=SGDop, loss='mse')
G.summary()
# discriminator
D = discriminator(5, 32, rows, columns, channel)
D.compile(optimizer=SGDop, loss='mse')
D.summary()
D_fixed = discriminator(5, 32, rows, columns, channel)
D_fixed.compile(optimizer=SGDop, loss='mse')
# VAE
X = Input(shape=(rows, columns, channel))
# latent_rep = E(X)[0]
# output = G(latent_rep)
E_mean, E_logsigma, Z = E(X)
# Z = Input(shape=(512,))
# Z2 = Input(shape=(batch_size, 512))
output = G(Z)
G_dec = G(E_mean + E_logsigma)
D_fake, F_fake = D(output)
D_fromGen, F_fromGen = D(G_dec)
D_true, F_true = D(X)
VAE = Model(X, output)
kl = - 0.5 * K.sum(1 + E_logsigma - K.square(E_mean) - K.exp(E_logsigma), axis=-1)
crossent = 64 * metrics.mse(K.flatten(X), K.flatten(output))
VAEloss = K.mean(crossent + kl)
VAE.add_loss(VAEloss)
VAE.compile(optimizer=SGDop)
for epoch in range(epochs):
latent_vect = E.predict(dataset)[0]
encImg = G.predict(latent_vect)
fakeImg = G.predict(noise)
DlossTrue = D_true.train_on_batch(dataset, np.ones((batch_size, 1)))
DlossEnc = D_fromGen.train_on_batch(encImg, np.ones((batch_size, 1)))
DlossFake = D_fake.train_on_batch(fakeImg, np.zeros((batch_size, 1)))
cnt = epoch
while cnt > 3:
cnt = cnt - 4
if cnt == 0:
GlossEnc = G.train_on_batch(latent_vect, np.ones((batch_size, 1)))
GlossGen = G.train_on_batch(noise, np.ones((batch_size, 1)))
Eloss = VAE.train_on_batch(dataset, None)
chk = epoch
while chk > 50:
chk = chk - 51
if chk == 0:
D.save_weights('discriminator.h5')
G.save_weights('generator.h5')
E.save_weights('encoder.h5')
print("epoch number", epoch + 1)
print("loss:")
print("D:", DlossTrue, DlossEnc, DlossFake)
print("G:", GlossEnc, GlossGen)
print("VAE:", Eloss)
print('Training done,saving weights')
D.save_weights('discriminator.h5')
G.save_weights('generator.h5')
E.save_weights('encoder.h5')
print('end')