-
Notifications
You must be signed in to change notification settings - Fork 0
/
graph.py
271 lines (227 loc) · 19.6 KB
/
graph.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
import tensorflow as tf
from utils import image
class GraphGAN:
def __init__(self, device, scope):
self.device = device
self.scope = scope
def define_nodes(self, image_shape, label_shape, noise_shape):
self.image_shape = image_shape
self.label_shape = label_shape
self.noise_shape = noise_shape
with tf.device(self.device):
with tf.name_scope(self.scope+"_placeholders"):
self.image_input = tf.placeholder(tf.float32, shape=[None, image_shape[0], image_shape[1], image_shape[2]], name='image_input')
self.label_input = tf.placeholder(tf.float32, shape=[None, label_shape], name='label_input')
self.noise_input = tf.placeholder(tf.float32, shape=[None, noise_shape], name='noise_input')
self.discriminator_learning_rate = tf.placeholder(tf.float32, shape=[], name='discriminator_learning_rate')
self.generator_learning_rate = tf.placeholder(tf.float32, shape=[], name='generator_learning_rate')
self.training = tf.placeholder(tf.bool, shape=[], name='training')
def build_model(self, model, type):
self.model = model
self.model.build_model(image_input=self.image_input, noise_input=self.noise_input, label_input=self.label_input, type=type, training=self.training)
self.generated_image = self.model.generator_model_output
def build_graph(self, type, regularizer):
self.type = type
self.regularizer = regularizer
# LOSSES
with tf.device(self.device):
with tf.name_scope(self.scope+"_loss"):
# VANILLA GAN
if type=='gan':
with tf.name_scope("generator_loss"):
self.generator_fake_loss = tf.losses.sigmoid_cross_entropy(\
multi_class_labels=tf.ones_like(self.model.discriminator_fake_model_output), \
logits=self.model.discriminator_fake_model_logit, \
weights=1.0, loss_collection="GENERATOR_LOSS", scope='generator_loss')
with tf.name_scope("discriminator_loss"):
self.discriminator_real_loss = tf.losses.sigmoid_cross_entropy(\
multi_class_labels=tf.ones_like(self.model.discriminator_real_model_output), \
logits=self.model.discriminator_real_model_logit, \
weights=1.0, loss_collection="DISCRIMINATOR_LOSS", scope='discriminator_real_loss')
self.discriminator_fake_loss = tf.losses.sigmoid_cross_entropy(\
multi_class_labels=tf.zeros_like(self.model.discriminator_fake_model_output), \
logits=self.model.discriminator_fake_model_logit, \
weights=1.0, loss_collection="DISCRIMINATOR_LOSS", scope='discriminator_fake_loss')
# LEAST-SQUARES GAN
elif type=='lsgan':
with tf.name_scope("generator_loss"):
self.generator_fake_loss = tf.losses.mean_squared_error(\
labels=tf.ones_like(self.model.discriminator_fake_model_output), \
predictions=self.model.discriminator_fake_model_logit, \
weights=0.5, loss_collection="GENERATOR_LOSS", scope='generator_loss')
with tf.name_scope("discriminator_loss"):
self.discriminator_real_loss = tf.losses.mean_squared_error(\
labels=tf.ones_like(self.model.discriminator_real_model_output), \
predictions=self.model.discriminator_real_model_logit, \
weights=0.5, loss_collection="DISCRIMINATOR_LOSS", scope='discriminator_real_loss')
self.discriminator_fake_loss = tf.losses.mean_squared_error(\
labels=tf.zeros_like(self.model.discriminator_fake_model_output), \
predictions=self.model.discriminator_fake_model_logit, \
weights=0.5, loss_collection="DISCRIMINATOR_LOSS", scope='discriminator_fake_loss')
# WASSERSTEIN GAN
elif type=='wgan' or type=='wgan-gp':
with tf.name_scope("generator_loss"):
self.generator_fake_loss = tf.losses.compute_weighted_loss(\
losses=self.model.discriminator_fake_model_logit, \
weights=-1.0, loss_collection="GENERATOR_LOSS", scope='generator_loss')
with tf.name_scope("discriminator_loss"):
self.discriminator_real_loss = tf.losses.compute_weighted_loss(\
losses=self.model.discriminator_real_model_logit, \
weights=-1.0, loss_collection="DISCRIMINATOR_LOSS", scope='discriminator_real_loss')
self.discriminator_fake_loss = tf.losses.compute_weighted_loss(\
losses=self.model.discriminator_fake_model_logit, \
weights=1.0, loss_collection="DISCRIMINATOR_LOSS", scope='discriminator_fake_loss')
# WASSERSTEIN GAN - GRADIENT PENALTY
if 'gp' in type:
with tf.name_scope('gradient_penalty'):
epsilon = tf.random.uniform([], name='epsilon')
gradient_image = tf.identity((epsilon * self.image_input + (1-epsilon) * self.generated_image), name='gradient_image')
discriminator_gradient_model_output, discriminator_gradient_model_logit = self.model.discriminator.build(image_input=gradient_image, label_input=self.label_input, model_scope='discriminator_gradient_image', reuse=True)
gradients = tf.gradients(discriminator_gradient_model_logit, gradient_image, name='gradients')
gradient_norm = tf.norm(gradients[0], ord=2, name='gradient_norm')
self.gradient_penalty = tf.square(gradient_norm - 1)
tf.add_to_collection("DISCRIMINATOR_LOSS", self.gradient_penalty)
# GEOMETRIC GAN
elif type=='geogan':
with tf.name_scope("generator_loss"):
self.generator_fake_loss = tf.losses.compute_weighted_loss(\
losses=self.model.discriminator_fake_model_logit, \
weights=-1.0, loss_collection="GENERATOR_LOSS", scope='generator_loss')
with tf.name_scope("discriminator_loss"):
self.discriminator_real_loss = tf.losses.hinge_loss(\
labels=tf.ones_like(self.model.discriminator_real_model_output), \
logits=self.model.discriminator_real_model_logit, \
weights=1.0, loss_collection="DISCRIMINATOR_LOSS", scope='discriminator_real_loss')
self.discriminator_fake_loss = tf.losses.hinge_loss(\
labels=tf.zeros_like(self.model.discriminator_fake_model_output), \
logits=self.model.discriminator_fake_model_logit, \
weights=1.0, loss_collection="DISCRIMINATOR_LOSS", scope='discriminator_fake_loss')
# AUXILIARY-CLASSIFIER GAN
elif type=='acgan':
with tf.name_scope("generator_loss"):
self.generator_fake_loss = tf.losses.sigmoid_cross_entropy(\
multi_class_labels=tf.ones_like(self.model.discriminator_fake_model_output), \
logits=self.model.discriminator_fake_model_logit, \
weights=1.0, loss_collection="GENERATOR_LOSS", scope='generator_loss')
with tf.name_scope("discriminator_loss"):
self.discriminator_real_loss = tf.losses.sigmoid_cross_entropy(\
multi_class_labels=tf.ones_like(self.model.discriminator_real_model_output), \
logits=self.model.discriminator_real_model_logit, \
weights=1.0, loss_collection="DISCRIMINATOR_LOSS", scope='discriminator_real_loss')
self.discriminator_fake_loss = tf.losses.sigmoid_cross_entropy(\
multi_class_labels=tf.zeros_like(self.model.discriminator_fake_model_output), \
logits=self.model.discriminator_fake_model_logit, \
weights=1.0, loss_collection="DISCRIMINATOR_LOSS", scope='discriminator_fake_loss')
with tf.name_scope("classifier_loss"):
self.classifier_real_loss = tf.losses.softmax_cross_entropy(\
onehot_labels=self.label_input, \
logits=self.model.classifier_real_model_output, \
weights=1.0, loss_collection="CLASSIFIER_LOSS", scope='classifier_real_loss')
self.classifier_fake_loss = tf.losses.softmax_cross_entropy(\
onehot_labels=self.label_input, \
logits=self.model.classifier_fake_model_output, \
weights=1.0, loss_collection="CLASSIFIER_LOSS", scope='classifier_fake_loss')
else:
raise ValueError('unknown gan graph type: {}'.format(type))
# REGULARIZERS
with tf.name_scope('regularizer'):
# MODE-SEEKING GAN
if regularizer=='modeseek':
with tf.name_scope(regularizer):
_img1, _img2 = tf.split(self.generated_image, 2, axis=0, name='image_split')
_noise1, _noise2 = tf.split(self.noise_input, 2, axis=0, name='noise_split')
modeseek_loss = tf.reduce_mean(tf.abs(_img1-_img2), name='generated_image_distance') / tf.reduce_mean(tf.abs(_noise1-_noise2), name='noise_distance')
self.regularizer_loss = 1 / (modeseek_loss + 1e-8)
tf.add_to_collection("GENERATOR_LOSS", self.regularizer_loss)
# SPECTRAL NORMALIZATION
elif regularizer=='spectralnorm':
from custom.regularizers import spectral_normalization
discriminator_kernel = [var for var in tf.trainable_variables(scope='discriminator') if 'kernel' in var.name]
for idx, kernel in enumerate(discriminator_kernel): spectral_normalization(kernel, scope='discriminator_sn_{}'.format(idx), collection="DISCRIMINATOR_OPS")
else:
if regularizer is not None:
raise ValueError('unknown regularizer type {}'.format(regularizer))
else:
pass
self.generator_loss = tf.add_n(tf.get_collection("GENERATOR_LOSS"), name='generator_loss')
self.discriminator_loss = tf.add_n(tf.get_collection("DISCRIMINATOR_LOSS"), name='discriminator_loss')
if type=='acgan':
self.classifier_loss = tf.add_n(tf.get_collection("CLASSIFIER_LOSS"), name='classifier_loss')
# IMAGES
with tf.device(self.device):
with tf.name_scope(self.scope+'_image'):
generated_image_summary = tf.summary.image(name='generated_image', tensor=(image.scale_in(self.generated_image)), max_outputs=64, family='generated_image', collections=["GENERATOR_SUMMARY"])
target_image_summary = tf.summary.image(name='target_image', tensor=(image.scale_in(self.image_input)), max_outputs=64, family='target_image', collections=["GENERATOR_SUMMARY"])
self.image_summary = tf.summary.merge([generated_image_summary, target_image_summary])
# SUMMARIES
with tf.device(self.device):
with tf.name_scope(self.scope+'_summary'+'_op'):
generator_loss_mean, _ = tf.metrics.mean(self.generator_loss, name='generator_loss', updates_collections=["GENERATOR_OPS"])
generator_fake_loss_mean, _ = tf.metrics.mean(self.generator_fake_loss, name='generator_fake_loss', updates_collections=["GENERATOR_OPS"])
discriminator_loss_mean, _ = tf.metrics.mean(self.discriminator_loss, name='discriminator_loss', updates_collections=["DISCRIMINATOR_OPS"])
discriminator_real_loss_mean, _ = tf.metrics.mean(self.discriminator_real_loss, name='discriminator_real_loss', updates_collections=["DISCRIMINATOR_OPS"])
discriminator_fake_loss_mean, _ = tf.metrics.mean(self.discriminator_fake_loss, name='discriminator_fake_loss', updates_collections=["DISCRIMINATOR_OPS"])
if 'gp' in type: gradient_penalty_mean, _ = tf.metrics.mean(self.gradient_penalty, name='gradient_penalty', updates_collections=["DISCRIMINATOR_OPS"])
if regularizer=='modeseek': regularizer_loss_mean, _ = tf.metrics.mean(self.regularizer_loss, name='regularizer_loss', updates_collections=["DISCRIMINATOR_OPS"])
if type=='acgan':
classifier_loss_mean, _ = tf.metrics.mean(self.classifier_loss, name='classifier_loss', updates_collections=["CLASSIFIER_OPS"])
classifier_real_loss_mean, _ = tf.metrics.mean(self.classifier_real_loss, name='classifier_loss', updates_collections=["CLASSIFIER_OPS"])
classifier_fake_loss_mean, _ = tf.metrics.mean(self.classifier_fake_loss, name='classifier_loss', updates_collections=["CLASSIFIER_OPS"])
with tf.name_scope(self.scope+'_summary'):
_ = tf.summary.scalar(name='generator_loss', tensor=generator_loss_mean, collections=["GENERATOR_SUMMARY"], family='01_loss_total')
_ = tf.summary.scalar(name='discriminator_loss', tensor=discriminator_loss_mean, collections=["DISCRIMINATOR_SUMMARY"], family='01_loss_total')
_ = tf.summary.scalar(name='discriminator_real_loss', tensor=discriminator_real_loss_mean, collections=["DISCRIMINATOR_SUMMARY"], family='02_loss_discriminator')
_ = tf.summary.scalar(name='discriminator_fake_loss', tensor=discriminator_fake_loss_mean, collections=["DISCRIMINATOR_SUMMARY"], family='02_loss_discriminator')
_ = tf.summary.scalar(name='generator_learning_rate', tensor=self.generator_learning_rate, collections=["GENERATOR_SUMMARY"], family='03_hyperparameter')
_ = tf.summary.scalar(name='discriminator_learning_rate', tensor=self.discriminator_learning_rate, collections=["DISCRIMINATOR_SUMMARY"], family='03_hyperparameter')
if 'gp' in type: _ = tf.summary.scalar(name='gradient_penalty', tensor=gradient_penalty_mean, collections=["DISCRIMINATOR_SUMMARY"], family='02_loss_discriminator')
if regularizer=='modeseek': _ = tf.summary.scalar(name='regularizer_loss', tensor=regularizer_loss_mean, collections=["DISCRIMINATOR_SUMMARY"], family='04_regularizer')
if type=='acgan':
_ = tf.summary.scalar(name='classifier_loss', tensor=classifier_loss_mean, collections=['CLASSIFIER_SUMMARY'], family='01_loss_total')
_ = tf.summary.scalar(name='classifier_real_loss', tensor=classifier_real_loss_mean, collections=['CLASSIFIER_SUMMARY'], family='05_loss_classifier')
_ = tf.summary.scalar(name='classifier_fake_loss', tensor=classifier_fake_loss_mean, collections=['CLASSIFIER_SUMMARY'], family='05_loss_classifier')
with tf.name_scope(self.scope+'_summary'+'_merge'):
self.generator_summary = tf.summary.merge(tf.get_collection("GENERATOR_SUMMARY"))
self.discriminator_summary = tf.summary.merge(tf.get_collection("DISCRIMINATOR_SUMMARY"))
if type=='acgan':
self.classifier_summary = tf.summary.merge(tf.get_collection("CLASSIFIER_SUMMARY"))
self.merged_summary = tf.summary.merge_all()
# OPTIMIZATIONS
with tf.device(self.device):
with tf.name_scope(self.scope+'_optimize'):
with tf.name_scope("train_variables"):
generator_variables = tf.trainable_variables(scope='generator')
discriminator_variables = tf.trainable_variables(scope='discriminator')
with tf.name_scope("generator_optimize"):
generator_optimizer = tf.train.RMSPropOptimizer(learning_rate=self.generator_learning_rate, name='generator_optimizer')
generator_gradients_and_variables = generator_optimizer.compute_gradients(loss=self.generator_loss, var_list=generator_variables)
with tf.control_dependencies(tf.get_collection(tf.GraphKeys.UPDATE_OPS, scope='generator')+tf.get_collection("GENERATOR_OPS")):
self.generator_optimize = generator_optimizer.apply_gradients(generator_gradients_and_variables, name='generator_train')
with tf.name_scope("discriminator_optimize"):
if type=='wgan':
with tf.name_scope("clip_weight"):
for weight in discriminator_variables:
tf.add_to_collection("DISCRIMINATOR_OPS", weight.assign(tf.clip_by_value(weight, -0.01, 0.01, name='clip'), name='apply_clipping'))
discriminator_optimizer = tf.train.RMSPropOptimizer(learning_rate=self.discriminator_learning_rate, name='discriminator_optimizer')
discriminator_gradients_and_variables = discriminator_optimizer.compute_gradients(loss=self.discriminator_loss, var_list=discriminator_variables)
with tf.control_dependencies(tf.get_collection(tf.GraphKeys.UPDATE_OPS)+tf.get_collection("DISCRIMINATOR_OPS")):
self.discriminator_optimize = discriminator_optimizer.apply_gradients(discriminator_gradients_and_variables, name='discriminator_train')
self.generator_train = [self.generator_summary, self.generator_optimize]
self.discriminator_train = [self.discriminator_summary, self.discriminator_optimize]
if type=='acgan':
with tf.name_scope("classifier_optimize"):
classifier_variables = tf.trainable_variables(scope='classifier') + generator_variables + discriminator_variables
classifier_optimizer = tf.train.AdamOptimizer(learning_rate=self.generator_learning_rate, name='classifier_optimizer')
classifier_gradients_and_variables = classifier_optimizer.compute_gradients(loss=self.classifier_loss, var_list=classifier_variables)
with tf.control_dependencies(tf.get_collection(tf.GraphKeys.UPDATE_OPS)+tf.get_collection("CLASSIFIER_OPS")):
self.classifier_optimize = classifier_optimizer.apply_gradients(classifier_gradients_and_variables, name='classifier_train')
self.classifier_train = [self.classifier_summary, self.classifier_optimize]
# SAVERS
with tf.device("/CPU:0"):
with tf.name_scope(self.scope+'_saver'):
self.generator_saver = tf.train.Saver(var_list=tf.global_variables(scope='generator'), name='generator_saver')
# CREATE COLLECTION FOR IMAGE GENERATION
tf.add_to_collection("TEST_GENERATION_OPS", self.generated_image)
tf.add_to_collection("TEST_GENERATION_OPS", self.label_input)
tf.add_to_collection("TEST_GENERATION_OPS", self.noise_input)
tf.add_to_collection("TEST_GENERATION_OPS", self.training)