-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathDiscoGAN.py
275 lines (207 loc) · 16.6 KB
/
DiscoGAN.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
import tensorflow as tf
import matplotlib.pyplot as plt
# Hyperparameters
initializer = tf.truncated_normal_initializer(stddev=0.02)
learning_rate = 0.0002
batch_size = 256
epoch = 100000
lambda = 10
# Read image files
shoes_filename_queue = tf.train.string_input_producer(tf.train.match_filenames_once("./shoes/*.jpg"), capacity=200)
bags_filename_queue = tf.train.string_input_producer(tf.train.match_filenames_once("./bags/*.jpg"), capacity=200)
image_reader = tf.WholeFileReader()
_, shoes_file = image_reader.read(shoes_filename_queue)
_, bags_file = image_reader.read(bags_filename_queue)
shoes_image = tf.image.decode_jpeg(shoes_file)
bags_image = tf.image.decode_jpeg(bags_file)
shoes_image = tf.cast(tf.reshape(shoes_image,shape=[64,64,3]),dtype=tf.float32)/255.0
bags_image = tf.cast(tf.reshape(bags_image,shape=[64,64,3]),dtype=tf.float32)/255.0
num_preprocess_threads = 1
min_queue_examples = 256
batch_shoes = tf.train.shuffle_batch([shoes_image],
batch_size=batch_size,
num_threads=num_preprocess_threads,
capacity=min_queue_examples + 3 * batch_size,
min_after_dequeue=min_queue_examples)
batch_bags = tf.train.shuffle_batch([bags_image],
batch_size=batch_size,
num_threads=num_preprocess_threads,
capacity=min_queue_examples + 3 * batch_size,
min_after_dequeue=min_queue_examples)
# Functions for training
def lrelu(x, leak=0.2, name="lrelu"):
with tf.variable_scope(name):
f1 = 0.5 * (1 + leak)
f2 = 0.5 * (1 - leak)
return f1 * x + f2 * abs(x)
def discriminator_shoes(tensor,reuse=False):
with tf.variable_scope("discriminator_s"):
conv1 = tf.contrib.layers.conv2d(inputs=tensor, num_outputs=32, kernel_size=4, stride=2, padding="SAME", \
reuse=reuse, activation_fn=lrelu,weights_initializer=initializer,scope="d_conv1") # 32 x 32 x 32
conv2 = tf.contrib.layers.conv2d(inputs=conv1, num_outputs=64, kernel_size=4, stride=2, padding="SAME", \
reuse=reuse, activation_fn=lrelu,normalizer_fn=tf.contrib.layers.batch_norm,\
weights_initializer=initializer,scope="d_conv2") # 16 x 16 x 64
conv3 = tf.contrib.layers.conv2d(inputs=conv2, num_outputs=128, kernel_size=4, stride=2, padding="SAME", \
reuse=reuse, activation_fn=lrelu,normalizer_fn=tf.contrib.layers.batch_norm,\
weights_initializer=initializer,scope="d_conv3") # 8 x 8 x 128
conv4 = tf.contrib.layers.conv2d(inputs=conv3, num_outputs=256, kernel_size=4, stride=2, padding="SAME", \
reuse=reuse, activation_fn=lrelu,normalizer_fn=tf.contrib.layers.batch_norm,\
weights_initializer=initializer,scope="d_conv4") # 4 x 4 x 256
conv5 = tf.contrib.layers.conv2d(inputs=conv4, num_outputs=512, kernel_size=4, stride=2, padding="SAME", \
reuse=reuse, activation_fn=lrelu,normalizer_fn=tf.contrib.layers.batch_norm,\
weights_initializer=initializer,scope="d_conv5") # 2 x 2 x 512
fc1 = tf.reshape(conv5, shape=[batch_size, 2*2*512])
fc1 = tf.contrib.layers.fully_connected(inputs=fc1, num_outputs=512,reuse=reuse, activation_fn=lrelu, \
normalizer_fn=tf.contrib.layers.batch_norm, \
weights_initializer=initializer,scope="d_fc1")
fc2 = tf.contrib.layers.fully_connected(inputs=fc1, num_outputs=1, reuse=reuse, activation_fn=tf.nn.sigmoid,\
weights_initializer=initializer,scope="d_fc2")
return fc2
def discriminator_bags(tensor,reuse=False):
with tf.variable_scope("discriminator_b"):
conv1 = tf.contrib.layers.conv2d(inputs=tensor, num_outputs=32, kernel_size=4, stride=2, padding="SAME", \
reuse=reuse, activation_fn=lrelu,weights_initializer=initializer,scope="d_conv1") # 32 x 32 x 32
conv2 = tf.contrib.layers.conv2d(inputs=conv1, num_outputs=64, kernel_size=4, stride=2, padding="SAME", \
reuse=reuse, activation_fn=lrelu,normalizer_fn=tf.contrib.layers.batch_norm,\
weights_initializer=initializer,scope="d_conv2") # 16 x 16 x 64
conv3 = tf.contrib.layers.conv2d(inputs=conv2, num_outputs=128, kernel_size=4, stride=2, padding="SAME", \
reuse=reuse, activation_fn=lrelu,normalizer_fn=tf.contrib.layers.batch_norm,\
weights_initializer=initializer,scope="d_conv3") # 8 x 8 x 128
conv4 = tf.contrib.layers.conv2d(inputs=conv3, num_outputs=256, kernel_size=4, stride=2, padding="SAME", \
reuse=reuse, activation_fn=lrelu,normalizer_fn=tf.contrib.layers.batch_norm,\
weights_initializer=initializer,scope="d_conv4") # 4 x 4 x 256
conv5 = tf.contrib.layers.conv2d(inputs=conv4, num_outputs=512, kernel_size=4, stride=2, padding="SAME", \
reuse=reuse, activation_fn=lrelu,normalizer_fn=tf.contrib.layers.batch_norm,\
weights_initializer=initializer,scope="d_conv5") # 2 x 2 x 512
fc1 = tf.reshape(conv5, shape=[batch_size, 2*2*512])
fc1 = tf.contrib.layers.fully_connected(inputs=fc1, num_outputs=512,reuse=reuse, activation_fn=lrelu, \
normalizer_fn=tf.contrib.layers.batch_norm, \
weights_initializer=initializer,scope="d_fc1")
fc2 = tf.contrib.layers.fully_connected(inputs=fc1, num_outputs=1, reuse=reuse, activation_fn=tf.nn.sigmoid,\
weights_initializer=initializer,scope="d_fc2")
return fc2
def generator_sb(image,reuse=False):
with tf.variable_scope("generator_sb"):
conv1 = tf.contrib.layers.conv2d(inputs=image, num_outputs=32, kernel_size=4, stride=2, padding="SAME", \
reuse=reuse, activation_fn=lrelu,weights_initializer=initializer,scope="d_conv1") # 32 x 32 x 32
conv2 = tf.contrib.layers.conv2d(inputs=conv1, num_outputs=64, kernel_size=4, stride=2, padding="SAME", \
reuse=reuse, activation_fn=lrelu,normalizer_fn=tf.contrib.layers.batch_norm,\
weights_initializer=initializer,scope="d_conv2") # 16 x 16 x 64
conv3 = tf.contrib.layers.conv2d(inputs=conv2, num_outputs=128, kernel_size=4, stride=2, padding="SAME", \
reuse=reuse, activation_fn=lrelu,normalizer_fn=tf.contrib.layers.batch_norm,\
weights_initializer=initializer,scope="d_conv3") # 8 x 8 x 128
conv4 = tf.contrib.layers.conv2d(inputs=conv3, num_outputs=256, kernel_size=4, stride=2, padding="SAME", \
reuse=reuse, activation_fn=lrelu,normalizer_fn=tf.contrib.layers.batch_norm,\
weights_initializer=initializer,scope="d_conv4") # 4 x 4 x 256
conv_trans1 = tf.contrib.layers.conv2d(conv4, num_outputs=4*128, kernel_size=4, stride=1, padding="SAME", \
activation_fn=tf.nn.relu, normalizer_fn=tf.contrib.layers.batch_norm, \
weights_initializer=initializer,scope="g_conv1")
conv_trans1 = tf.reshape(conv_trans1, shape=[batch_size,8,8,128])
conv_trans2 = tf.contrib.layers.conv2d(conv_trans1, num_outputs=4*64, kernel_size=4, stride=1, padding="SAME", \
activation_fn=tf.nn.relu,normalizer_fn=tf.contrib.layers.batch_norm, \
weights_initializer=initializer,scope="g_conv2")
conv_trans2 = tf.reshape(conv_trans2, shape=[batch_size,16,16,64])
conv_trans3 = tf.contrib.layers.conv2d(conv_trans2, num_outputs=4*32, kernel_size=4, stride=1, padding="SAME", \
activation_fn=tf.nn.relu, normalizer_fn=tf.contrib.layers.batch_norm, \
weights_initializer=initializer,scope="g_conv3")
conv_trans3 = tf.reshape(conv_trans3, shape=[batch_size,32,32,32])
conv_trans4 = tf.contrib.layers.conv2d(conv_trans3, num_outputs=4*16, kernel_size=4, stride=1, padding="SAME", \
activation_fn=tf.nn.relu,normalizer_fn=tf.contrib.layers.batch_norm, \
weights_initializer=initializer,scope="g_conv4")
conv_trans4 = tf.reshape(conv_trans4, shape=[batch_size,64,64,16])
recon_bag = tf.contrib.layers.conv2d(conv_trans4, num_outputs=3, kernel_size=4, stride=1, padding="SAME", \
activation_fn=tf.nn.relu,scope="g_conv5")
return recon_bag
def generator_bs(image,reuse=False):
with tf.variable_scope("generator_bs"):
conv1 = tf.contrib.layers.conv2d(inputs=image, num_outputs=32, kernel_size=4, stride=2, padding="SAME", \
reuse=reuse, activation_fn=lrelu,weights_initializer=initializer,scope="d_conv1") # 32 x 32 x 32
conv2 = tf.contrib.layers.conv2d(inputs=conv1, num_outputs=64, kernel_size=4, stride=2, padding="SAME", \
reuse=reuse, activation_fn=lrelu,normalizer_fn=tf.contrib.layers.batch_norm,\
weights_initializer=initializer,scope="d_conv2") # 16 x 16 x 64
conv3 = tf.contrib.layers.conv2d(inputs=conv2, num_outputs=128, kernel_size=4, stride=2, padding="SAME", \
reuse=reuse, activation_fn=lrelu,normalizer_fn=tf.contrib.layers.batch_norm,\
weights_initializer=initializer,scope="d_conv3") # 8 x 8 x 128
conv4 = tf.contrib.layers.conv2d(inputs=conv3, num_outputs=256, kernel_size=4, stride=2, padding="SAME", \
reuse=reuse, activation_fn=lrelu,normalizer_fn=tf.contrib.layers.batch_norm,\
weights_initializer=initializer,scope="d_conv4") # 4 x 4 x 256
conv_trans1 = tf.contrib.layers.conv2d(conv4, num_outputs=4*128, kernel_size=4, stride=1, padding="SAME", \
activation_fn=tf.nn.relu, normalizer_fn=tf.contrib.layers.batch_norm, \
weights_initializer=initializer,scope="g_conv1")
conv_trans1 = tf.reshape(conv_trans1, shape=[batch_size,8,8,128])
conv_trans2 = tf.contrib.layers.conv2d(conv_trans1, num_outputs=4*64, kernel_size=4, stride=1, padding="SAME", \
activation_fn=tf.nn.relu,normalizer_fn=tf.contrib.layers.batch_norm, \
weights_initializer=initializer,scope="g_conv2")
conv_trans2 = tf.reshape(conv_trans2, shape=[batch_size,16,16,64])
conv_trans3 = tf.contrib.layers.conv2d(conv_trans2, num_outputs=4*32, kernel_size=4, stride=1, padding="SAME", \
activation_fn=tf.nn.relu, normalizer_fn=tf.contrib.layers.batch_norm, \
weights_initializer=initializer,scope="g_conv3")
conv_trans3 = tf.reshape(conv_trans3, shape=[batch_size,32,32,32])
conv_trans4 = tf.contrib.layers.conv2d(conv_trans3, num_outputs=4*16, kernel_size=4, stride=1, padding="SAME", \
activation_fn=tf.nn.relu,normalizer_fn=tf.contrib.layers.batch_norm, \
weights_initializer=initializer,scope="g_conv4")
conv_trans4 = tf.reshape(conv_trans4, shape=[batch_size,64,64,16])
recon_shoe = tf.contrib.layers.conv2d(conv_trans4, num_outputs=3, kernel_size=4, stride=1, padding="SAME", \
activation_fn=tf.nn.relu,scope="g_conv5")
return recon_shoe
# Generation & Discrimination
gen_b_fake = generator_sb(batch_shoes)
gen_s_fake = generator_bs(batch_bags)
recon_s = generator_bs(gen_b_fake,reuse=True)
recon_b = generator_sb(gen_s_fake,reuse=True)
disc_s_fake = discriminator_shoes(gen_s_fake)
disc_b_fake = discriminator_bags(gen_b_fake)
disc_s_real = discriminator_shoes(batch_shoes,reuse=True)
disc_b_real = discriminator_bags(batch_bags,reuse=True)
# Loss Caculation
const_loss_s = tf.reduce_sum(tf.losses.mean_squared_error(batch_shoes,recon_s))
const_loss_b = tf.reduce_sum(tf.losses.mean_squared_error(batch_bags,recon_b))
# Instead of cross entropy loss used in paper, I've used LSGAN loss for experiment
gen_s_loss = tf.reduce_sum(tf.square(disc_s_fake-1))/2
gen_b_loss = tf.reduce_sum(tf.square(disc_b_fake-1))/2
disc_s_loss = tf.reduce_sum(tf.square(disc_s_real-1) + tf.square(disc_s_fake))/2
disc_b_loss = tf.reduce_sum(tf.square(disc_b_real-1) + tf.square(disc_b_fake))/2
gen_loss = lambda * (const_loss_s + const_loss_b) + gen_s_loss + gen_b_loss
disc_loss = disc_s_loss + disc_b_loss
# Compute & Apply gradients
gen_sb_variables = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope="generator_sb")
gen_bs_variables = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope="generator_bs")
dis_s_variables = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope="discriminator_s")
dis_b_variables = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope="discriminator_b")
d_optimizer = tf.train.RMSPropOptimizer(learning_rate=learning_rate)
g_optimizer = tf.train.RMSPropOptimizer(learning_rate=learning_rate)
d_grads = d_optimizer.compute_gradients(disc_loss,dis_s_variables + dis_b_variables) #Only update the weights for the discriminator network.
g_grads = g_optimizer.compute_gradients(gen_loss,gen_sb_variables + gen_bs_variables) #Only update the weights for the generator network.
update_D = d_optimizer.apply_gradients(d_grads)
update_G = g_optimizer.apply_gradients(g_grads)
init = tf.global_variables_initializer()
saver = tf.train.Saver()
with tf.Session() as sess:
sess.run(init)
try:
saver.restore(sess=sess, save_path="./model/model.ckpt")
print("\n--------model restored--------\n")
except:
print("\n--------model Not restored--------\n")
pass
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(coord=coord)
for i in range(epoch):
for j in range(2):
_ = sess.run([update_G])
_, _, g_loss, d_loss, reconed_b, reconed_s, fake_s, fake_b,s_image, b_image = sess.run([update_G,update_D,
gen_loss, disc_loss,
recon_b,recon_s,
gen_s_fake,gen_b_fake,
batch_shoes,batch_bags])
if i % 100 == 0:
saver.save(sess, './model/model.ckpt')
print("{}th iter gen loss:{} disc loss:{}".format(i,g_loss/batch_size, d_loss/batch_size))
plt.imsave("./result/{}th_recon_bag.png".format(i),reconed_b[0])
plt.imsave("./result/{}th_recon_shoe.png".format(i),reconed_s[0])
plt.imsave("./result/{}th_origin_shoe.png".format(i),s_image[0])
plt.imsave("./result/{}th_origin_bag.png".format(i),b_image[0])
plt.imsave("./result/{}th_gen_shoe.png".format(i),fake_s[0])
plt.imsave("./result/{}th_gen_bag.png".format(i),fake_b[0])
coord.request_stop()
coord.join(threads)