-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_ResCycleGAN.py
282 lines (215 loc) · 9.89 KB
/
test_ResCycleGAN.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
from __future__ import print_function, division
import os
from glob import glob
import numpy as np
from keras_contrib.layers.normalization import InstanceNormalization
from keras.layers import Input, Dropout, Concatenate, Multiply
from keras.layers.advanced_activations import LeakyReLU
from keras.layers.convolutional import UpSampling2D, Conv2D
from keras.models import Model
from keras.optimizers import Adam
import datetime
import matplotlib.pyplot as plt
from data_loader import DataLoader
class CycleGAN():
def __init__(self):
# Input shape
self.img_rows = 256
self.img_cols = 256
self.channels = 3
self.img_shape = (self.img_rows, self.img_cols, self.channels)
self.var_img_shape = (None, None, self.channels)
# Calculate output shape of D (PatchGAN)
patch = int(self.img_rows / 2**4)
self.disc_patch = (patch, patch, 1)
# Number of filters in the first layer of G and D
self.gf = 32
self.df = 64
# Configure data loader
self.modelName = '/ResCycleGAN'
self.dataset_name = 'A_to_B'
self.data_loader = DataLoader(dataset_name=self.dataset_name,
img_res=(self.img_rows, self.img_cols),
is_zeroMean=False)
# is_zeroMean=False)
# self.is_res = False
self.is_res = True
self.is_blockwise = False
# Loss weights
self.lambda_cycle = 10.0 # Cycle-consistency loss
self.lambda_id = 1.0 # Identity loss
optimizer = Adam(0.001, 0.9)
# Build and compile the discriminators
self.d_A = self.build_discriminator()
self.d_B = self.build_discriminator()
# print(' ')
# print('* '*30 + 'Discriminators Network' + ' *'*30)
# self.d_A.summary()
# print(' ')
self.d_A.compile(loss='mse',
optimizer=optimizer,
metrics=['accuracy'])
self.d_B.compile(loss='mse',
optimizer=optimizer,
metrics=['accuracy'])
# Build and compile the generators
self.g_AB = self.build_generator()
self.g_BA = self.build_generator()
self.g_AB.compile(loss='binary_crossentropy', optimizer=optimizer)
self.g_BA.compile(loss='binary_crossentropy', optimizer=optimizer)
# print(' ')
# print('* '*30 + 'Generators Network' + ' *'*30)
# self.g_AB.summary()
# print(' ')
# Input images from both domains
img_A = Input(shape=self.var_img_shape)
img_B = Input(shape=self.var_img_shape)
# Translate images to the other domain
fake_B = self.g_AB(img_A)
fake_A = self.g_BA(img_B)
# Translate images back to original domain
reconstr_A = self.g_BA(fake_B)
reconstr_B = self.g_AB(fake_A)
# For the combined model we will only train the generators
self.d_A.trainable = False
self.d_B.trainable = False
# Discriminators determines validity of translated images
valid_A = self.d_A(fake_A)
valid_B = self.d_B(fake_B)
self.combined = Model([img_A, img_B], [valid_A, valid_B, fake_B, fake_A, \
reconstr_A, reconstr_B])
self.combined.compile(loss=['mse', 'mse', 'mae', 'mae', 'mae', 'mae'],
loss_weights=[1, 1, self.lambda_id, self.lambda_id, \
self.lambda_cycle, self.lambda_cycle],
optimizer=optimizer)
# print(' ')
# print('* '*30 + 'Overall Network' + ' *'*30)
# self.combined.summary()
# print(' ')
def build_generator(self):
"""U-Net Generator"""
def conv2d(layer_input, filters, f_size=4):
"""Layers used during downsampling"""
d = Conv2D(filters, kernel_size=f_size, strides=2, padding='same')(layer_input)
d = LeakyReLU(alpha=0.2)(d)
d = InstanceNormalization()(d)
return d
def deconv2d(layer_input, skip_input, filters, f_size=4, dropout_rate=0):
"""Layers used during upsampling"""
u = UpSampling2D(size=2)(layer_input)
u = Conv2D(filters, kernel_size=f_size, strides=1, padding='same', activation='relu')(u)
if dropout_rate:
u = Dropout(dropout_rate)(u)
u = InstanceNormalization()(u)
u = Concatenate()([u, skip_input])
return u
# Image input
d0 = Input(shape=self.var_img_shape)
# Downsampling
d1 = conv2d(d0, self.gf)
d2 = conv2d(d1, self.gf*2)
d3 = conv2d(d2, self.gf*4)
d4 = conv2d(d3, self.gf*8)
# Upsampling
u1 = deconv2d(d4, d3, self.gf*4)
u2 = deconv2d(u1, d2, self.gf*2)
u3 = deconv2d(u2, d1, self.gf)
u4 = UpSampling2D(size=2)(u3)
if not self.is_res:
output_img = Conv2D(self.channels, kernel_size=4, strides=1, padding='same', activation='tanh')(u4)
else:
out_cor = Conv2D(self.channels, kernel_size=4, strides=1, padding='same', activation='relu')(u4)
output_img = Multiply()([d0, out_cor])
return Model(d0, output_img)
def build_discriminator(self):
def d_layer(layer_input, filters, f_size=4, normalization=True):
"""Discriminator layer"""
d = Conv2D(filters, kernel_size=f_size, strides=2, padding='same')(layer_input)
d = LeakyReLU(alpha=0.2)(d)
if normalization:
d = InstanceNormalization()(d)
return d
img = Input(shape=self.var_img_shape)
d1 = d_layer(img, self.df, normalization=False)
d2 = d_layer(d1, self.df*2)
d3 = d_layer(d2, self.df*4)
d4 = d_layer(d3, self.df*8)
validity = Conv2D(1, kernel_size=4, strides=1, padding='same')(d4)
return Model(img, validity)
def test(self, modelNo, batch_size):
start_time = datetime.datetime.now()
# ------------------
# Load weights
# ------------------
self.load_weights(modelNo)
# ------------------
# test Generators
# ------------------
self.save_imgs(modelNo, batch_size)
elapsed_time = datetime.datetime.now() - start_time
# Plot the progress
print ('* '*30)
print ("Elaspsed time: %s" % (elapsed_time))
print ('* '*30)
def save_imgs(self, modelNo, batch_size):
savePath = "./images/"+ self.dataset_name + self.modelName + '/CL_' + str(self.lambda_cycle) + '_IL_' + str(self.lambda_id) + '/' + str(modelNo) + '/'
if not os.path.exists(savePath):
os.makedirs(savePath)
path1 = glob('./datasets/%s/%s/*.*g' % (self.dataset_name, 'testA'))
#path2 = glob('./datasets/%s/%s/*' % (self.dataset_name, 'topcon'))
# batch_images1 = np.random.choice(path1, size=batch_size)
# batch_images2 = np.random.choice(path2, size=batch_size)
batch_images1 = path1
# batch_images2 = path1
k = 0
for img_path in batch_images1:
# print()
# print ('- '*30)
imgs_A = self.data_loader.load_full_img(batch_images1[k], blockwise = self.is_blockwise, is_ext = True)
# imgs_B = self.data_loader.load_full_img(batch_images2[k], blockwise = self.is_blockwise)
# Translate images to the other domain
fake_B = self.g_AB.predict(imgs_A, batch_size=1)
# fake_A = self.g_BA.predict(imgs_B, batch_size=1)
# Translate back to original domain
# reconstr_A = self.g_BA.predict(fake_B, batch_size=1)
# reconstr_B = self.g_AB.predict(fake_A, batch_size=1)
resName = ['Original', 'Translated', 'Reconstructed']
#path = savePath + "%d_A_%s.png" % (k, resName[0])
#self.data_loader.imwrite(path, imgs_A, blockwise = self.is_blockwise)
# path = savePath + "%d_A_%s.png" % (k, resName[1])
path = savePath + os.path.basename(img_path)
self.data_loader.imwrite(path, fake_B, blockwise = self.is_blockwise)
# path = savePath + "%d_A_%s.png" % (k, resName[2])
# self.data_loader.imwrite(path, reconstr_A, blockwise = self.is_blockwise)
# path = savePath + "%d_B_%s.png" % (k, resName[0])
# self.data_loader.imwrite(path, imgs_B, blockwise = self.is_blockwise)
# path = savePath + "%d_B_%s.png" % (k, resName[1])
# self.data_loader.imwrite(path, fake_A, blockwise = self.is_blockwise)
# path = savePath + "%d_B_%s.png" % (k, resName[2])
# self.data_loader.imwrite(path, reconstr_B, blockwise = self.is_blockwise)
k = k+1
# print ('- '*30)
# print()
def load_weights(self, modelNo):
start_time = datetime.datetime.now()
# ------------------
# Load weights of Generators and Discriminators
# ------------------
if modelNo != 0:
saveWtPath = "./weights/"+ self.dataset_name + self.modelName + '/CL_' + str(self.lambda_cycle) + '_IL_' + str(self.lambda_id) + '/'
self.d_A.load_weights(saveWtPath + str(modelNo) + '_d_A.hdf5')
self.d_B.load_weights(saveWtPath + str(modelNo) + '_d_B.hdf5')
self.g_AB.load_weights(saveWtPath + str(modelNo) + '_d_AB.hdf5')
self.g_BA.load_weights(saveWtPath + str(modelNo) + '_d_BA.hdf5')
elapsed_time = datetime.datetime.now() - start_time
# Elapsed time
print ('* '*30)
print ("Time for loading weights: %s" % (elapsed_time))
print ('* '*30)
def my_loss(self, y_true, y_pred):
# Dummy
l1_loss = K.mean(K.abs(y_pred - y_true))
return l1_loss
if __name__ == '__main__':
gan = CycleGAN()
gan.test(modelNo=200000, batch_size=20)