-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain-p2p.py
279 lines (232 loc) · 12.4 KB
/
main-p2p.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
# Adpatation from yenchenlin's pix2pix-tensorflow WIP
from __future__ import division
import os
import sys
from glob import glob
import tensorflow as tf
import numpy as np
from six.moves import xrange
from ops import *
from utils import *
# Use a CGAN with layer_size y class labels for the y_dim and concat it to the inputs of the generator and discriminator
class txt2pic2():
def __init__(self, image_size=256, batch_size=64, sample_size=1):
self.sess = tf.Session()
self.batch_size = batch_size
self.sample_size = sample_size
self.image_size = image_size
self.output_size = image_size
self.y_dim = 1539 # Number of Unique tags
self.z_dim = 100 # Should represent noise
self.gf_dim = 64
self.df_dim = 64
self.c_dim = 3 # 1 for grayscale
self.L1_lambda = 100
# try out Elastic Nets
# Declare the elastic net loss function
# elastic_param1 = tf.constant(1.)
# elastic_param2 = tf.constant(1.)
# l1_a_loss = tf.reduce_mean(tf.abs(A))
# l2_a_loss = tf.reduce_mean(tf.square(A))
# e1_term = tf.multiply(elastic_param1, l1_a_loss)
# e2_term = tf.multiply(elastic_param2, l2_a_loss)
# loss = tf.expand_dims(tf.add(tf.add(tf.reduce_mean(tf.square(y_target - model_output)), e1_term), e2_term), 0)
self.d_bn1 = batch_norm(name='d_bn1')
self.d_bn2 = batch_norm(name='d_bn2')
self.d_bn3 = batch_norm(name='d_bn3')
self.g_bn_e2 = batch_norm(name='g_bn_e2')
self.g_bn_e3 = batch_norm(name='g_bn_e3')
self.g_bn_e4 = batch_norm(name='g_bn_e4')
self.g_bn_e5 = batch_norm(name='g_bn_e5')
self.g_bn_e6 = batch_norm(name='g_bn_e6')
self.g_bn_e7 = batch_norm(name='g_bn_e7')
self.g_bn_e8 = batch_norm(name='g_bn_e8')
self.g_bn_d1 = batch_norm(name='g_bn_d1')
self.g_bn_d2 = batch_norm(name='g_bn_d2')
self.g_bn_d3 = batch_norm(name='g_bn_d3')
self.g_bn_d4 = batch_norm(name='g_bn_d4')
self.g_bn_d5 = batch_norm(name='g_bn_d5')
self.g_bn_d6 = batch_norm(name='g_bn_d6')
self.g_bn_d7 = batch_norm(name='g_bn_d7')
self.checkpoint_dir = "./checkpoint"
self.build_model()
def build_model(self):
self.tags = tf.placeholder(tf.float32, [self.batch_size, self.y_dim], name='tags')
image_dims = [self.image_size, self.image_size, self.c_dim]
self.tags_reshape = tf.reshape(self.tags, [self.batch_size, 1, 1, self.y_dim])
self.inputs = tf.placeholder(tf.float32, [self.batch_size] + image_dims, name='real_images')
self.G = self.generator(self.tags)
self.sampler = self.sampler(self.tags)
self.D, self.D_logits = self.discriminator(tf.concat([self.tags, self.inputs],3), reuse=False) # Real
self.D_, self.D_logits_ = self.discriminator(tf.concat([self.tags, self.G],3), reuse=True) # Fake
self.d_loss_real = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(self.D_logits, tf.ones_like(self.D)))
self.d_loss_fake = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(self.D_logits_, tf.zeros_like(self.D_)))
self.d_loss = self.d_loss_real + self.d_loss_fake
self.g_loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(self.D_logits_, tf.ones_like(self.D_))) + self.L1_lambda * tf.reduce_mean(tf.abs(self.inputs - self.G))
t_vars = tf.trainable_variables()
self.d_vars = [var for var in t_vars if 'd_' in var.name]
self.g_vars = [var for var in t_vars if 'g_' in var.name]
self.d_optim = tf.train.AdamOptimizer(0.0002, beta1=0.5).minimize(self.d_loss, var_list=self.d_vars)
self.g_optim = tf.train.AdamOptimizer(0.0002, beta1=0.5).minimize(self.g_loss, var_list=self.g_vars)
self.saver = tf.train.Saver()
def train(self):
tf.initialize_all_variables().run()
def generator(self, image, y=None):
with tf.variable_scope("generator") as scope:
s = self.output_size
s2, s4, s8, s16, s32, s64, s128 = int(s/2), int(s/4), int(s/8), int(s/16), int(s/32), int(s/64), int(s/128)
e1 = conv2d(image, self.gf_dim, name='g_e1_conv')
# e1 is (128 x 128 x self.gf_dim)
e2 = self.g_bn_e2(conv2d(lrelu(e1), self.gf_dim*2, name='g_e2_conv'))
# e2 is (64 x 64 x self.gf_dim*2)
e3 = self.g_bn_e3(conv2d(lrelu(e2), self.gf_dim*4, name='g_e3_conv'))
# e3 is (32 x 32 x self.gf_dim*4)
e4 = self.g_bn_e4(conv2d(lrelu(e3), self.gf_dim*8, name='g_e4_conv'))
# e4 is (16 x 16 x self.gf_dim*8)
e5 = self.g_bn_e5(conv2d(lrelu(e4), self.gf_dim*8, name='g_e5_conv'))
# e5 is (8 x 8 x self.gf_dim*8)
e6 = self.g_bn_e6(conv2d(lrelu(e5), self.gf_dim*8, name='g_e6_conv'))
# e6 is (4 x 4 x self.gf_dim*8)
e7 = self.g_bn_e7(conv2d(lrelu(e6), self.gf_dim*8, name='g_e7_conv'))
# e7 is (2 x 2 x self.gf_dim*8)
e8 = self.g_bn_e8(conv2d(lrelu(e7), self.gf_dim*8, name='g_e8_conv'))
# e8 is (1 x 1 x self.gf_dim*8)
self.d1, self.d1_w, self.d1_b = deconv2d(tf.nn.relu(e8),
[self.batch_size, s128, s128, self.gf_dim*8], name='g_d1', with_w=True)
d1 = tf.nn.dropout(self.g_bn_d1(self.d1), 0.5)
d1 = tf.concat(3, [d1, e7])
# d1 is (2 x 2 x self.gf_dim*8*2)
self.d2, self.d2_w, self.d2_b = deconv2d(tf.nn.relu(d1),
[self.batch_size, s64, s64, self.gf_dim*8], name='g_d2', with_w=True)
d2 = tf.nn.dropout(self.g_bn_d2(self.d2), 0.5)
d2 = tf.concat(3, [d2, e6])
# d2 is (4 x 4 x self.gf_dim*8*2)
self.d3, self.d3_w, self.d3_b = deconv2d(tf.nn.relu(d2),
[self.batch_size, s32, s32, self.gf_dim*8], name='g_d3', with_w=True)
d3 = tf.nn.dropout(self.g_bn_d3(self.d3), 0.5)
d3 = tf.concat(3, [d3, e5])
# d3 is (8 x 8 x self.gf_dim*8*2)
self.d4, self.d4_w, self.d4_b = deconv2d(tf.nn.relu(d3),
[self.batch_size, s16, s16, self.gf_dim*8], name='g_d4', with_w=True)
d4 = self.g_bn_d4(self.d4)
d4 = tf.concat(3, [d4, e4])
# d4 is (16 x 16 x self.gf_dim*8*2)
self.d5, self.d5_w, self.d5_b = deconv2d(tf.nn.relu(d4),
[self.batch_size, s8, s8, self.gf_dim*4], name='g_d5', with_w=True)
d5 = self.g_bn_d5(self.d5)
d5 = tf.concat(3, [d5, e3])
# d5 is (32 x 32 x self.gf_dim*4*2)
self.d6, self.d6_w, self.d6_b = deconv2d(tf.nn.relu(d5),
[self.batch_size, s4, s4, self.gf_dim*2], name='g_d6', with_w=True)
d6 = self.g_bn_d6(self.d6)
d6 = tf.concat(3, [d6, e2])
# d6 is (64 x 64 x self.gf_dim*2*2)
self.d7, self.d7_w, self.d7_b = deconv2d(tf.nn.relu(d6),
[self.batch_size, s2, s2, self.gf_dim], name='g_d7', with_w=True)
d7 = self.g_bn_d7(self.d7)
d7 = tf.concat(3, [d7, e1])
# d7 is (128 x 128 x self.gf_dim*1*2)
self.d8, self.d8_w, self.d8_b = deconv2d(tf.nn.relu(d7),
[self.batch_size, s, s, self.c_dim], name='g_d8', with_w=True)
# d8 is (256 x 256 x c_dim)
return tf.nn.tanh(self.d8)
def sampler(self, image, y=None):
with tf.variable_scope("generator") as scope:
scope.reuse_variables()
s = self.output_size
s2, s4, s8, s16, s32, s64, s128 = int(s/2), int(s/4), int(s/8), int(s/16), int(s/32), int(s/64), int(s/128)
e1 = conv2d(image, self.gf_dim, name='g_e1_conv')
# e1 is (128 x 128 x self.gf_dim)
e2 = self.g_bn_e2(conv2d(lrelu(e1), self.gf_dim*2, name='g_e2_conv'))
# e2 is (64 x 64 x self.gf_dim*2)
e3 = self.g_bn_e3(conv2d(lrelu(e2), self.gf_dim*4, name='g_e3_conv'))
# e3 is (32 x 32 x self.gf_dim*4)
e4 = self.g_bn_e4(conv2d(lrelu(e3), self.gf_dim*8, name='g_e4_conv'))
# e4 is (16 x 16 x self.gf_dim*8)
e5 = self.g_bn_e5(conv2d(lrelu(e4), self.gf_dim*8, name='g_e5_conv'))
# e5 is (8 x 8 x self.gf_dim*8)
e6 = self.g_bn_e6(conv2d(lrelu(e5), self.gf_dim*8, name='g_e6_conv'))
# e6 is (4 x 4 x self.gf_dim*8)
e7 = self.g_bn_e7(conv2d(lrelu(e6), self.gf_dim*8, name='g_e7_conv'))
# e7 is (2 x 2 x self.gf_dim*8)
e8 = self.g_bn_e8(conv2d(lrelu(e7), self.gf_dim*8, name='g_e8_conv'))
# e8 is (1 x 1 x self.gf_dim*8)
self.d1, self.d1_w, self.d1_b = deconv2d(tf.nn.relu(e8),
[self.batch_size, s128, s128, self.gf_dim*8], name='g_d1', with_w=True)
d1 = tf.nn.dropout(self.g_bn_d1(self.d1), 0.5)
d1 = tf.concat(3, [d1, e7])
# d1 is (2 x 2 x self.gf_dim*8*2)
self.d2, self.d2_w, self.d2_b = deconv2d(tf.nn.relu(d1),
[self.batch_size, s64, s64, self.gf_dim*8], name='g_d2', with_w=True)
d2 = tf.nn.dropout(self.g_bn_d2(self.d2), 0.5)
d2 = tf.concat(3, [d2, e6])
# d2 is (4 x 4 x self.gf_dim*8*2)
self.d3, self.d3_w, self.d3_b = deconv2d(tf.nn.relu(d2),
[self.batch_size, s32, s32, self.gf_dim*8], name='g_d3', with_w=True)
d3 = tf.nn.dropout(self.g_bn_d3(self.d3), 0.5)
d3 = tf.concat(3, [d3, e5])
# d3 is (8 x 8 x self.gf_dim*8*2)
self.d4, self.d4_w, self.d4_b = deconv2d(tf.nn.relu(d3),
[self.batch_size, s16, s16, self.gf_dim*8], name='g_d4', with_w=True)
d4 = self.g_bn_d4(self.d4)
d4 = tf.concat(3, [d4, e4])
# d4 is (16 x 16 x self.gf_dim*8*2)
self.d5, self.d5_w, self.d5_b = deconv2d(tf.nn.relu(d4),
[self.batch_size, s8, s8, self.gf_dim*4], name='g_d5', with_w=True)
d5 = self.g_bn_d5(self.d5)
d5 = tf.concat(3, [d5, e3])
# d5 is (32 x 32 x self.gf_dim*4*2)
self.d6, self.d6_w, self.d6_b = deconv2d(tf.nn.relu(d5),
[self.batch_size, s4, s4, self.gf_dim*2], name='g_d6', with_w=True)
d6 = self.g_bn_d6(self.d6)
d6 = tf.concat(3, [d6, e2])
# d6 is (64 x 64 x self.gf_dim*2*2)
self.d7, self.d7_w, self.d7_b = deconv2d(tf.nn.relu(d6),
[self.batch_size, s2, s2, self.gf_dim], name='g_d7', with_w=True)
d7 = self.g_bn_d7(self.d7)
d7 = tf.concat(3, [d7, e1])
# d7 is (128 x 128 x self.gf_dim*1*2)
self.d8, self.d8_w, self.d8_b = deconv2d(tf.nn.relu(d7),
[self.batch_size, s, s, self.c_dim], name='g_d8', with_w=True)
# d8 is (256 x 256 x c_dim)
return tf.nn.tanh(self.d8)
def discriminator(self, image, reuse=False):
with tf.variable_scope("discriminator") as scope:
if reuse:
tf.get_variable_scope().reuse_variables()
else:
assert tf.get_variable_scope().reuse == False
h0 = lrelu(conv2d(image, self.df_dim, name='d_h0_conv'))
# h0 is (128 x 128 x self.df_dim)
h1 = lrelu(self.d_bn1(conv2d(h0, self.df_dim*2, name='d_h1_conv')))
# h1 is (64 x 64 x self.df_dim*2)
h2 = lrelu(self.d_bn2(conv2d(h1, self.df_dim*4, name='d_h2_conv')))
# h2 is (32x 32 x self.df_dim*4)
h3 = lrelu(self.d_bn3(conv2d(h2, self.df_dim*8, d_h=1, d_w=1, name='d_h3_conv')))
# h3 is (16 x 16 x self.df_dim*8)
h4 = linear(tf.reshape(h3, [self.batch_size, -1]), 1, 'd_h3_lin')
return tf.nn.sigmoid(h4), h4
if __name__ == '__main__':
if len(sys.argv) >= 2:
cmd = sys.argv[1]
if cmd == "train":
try:
size = sys.argv[2]
model = txt2pic2(batch_size=int(size))
model.train()
except IndexError:
model = txt2pic2()
model.train()
elif cmd == "test":
try:
size = sys.argv[2]
model = txt2pic2(image_size=int(size))
model.test()
except IndexError:
size = sys.argv[2]
model = txt2pic2()
model.test()
else:
print("Usage: python main-p2p.py [train, test, (optional) img output size]")
else:
print("Usage: python main-p2p.py [train + (optional) batch_size, test + (optional) img output size]")