-
Notifications
You must be signed in to change notification settings - Fork 163
/
Copy pathfgan_model.py
246 lines (179 loc) · 7.39 KB
/
fgan_model.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
import numpy as np
import tensorflow as tf
import awesome_gans.modules as t
tf.set_random_seed(777)
class FGAN:
def __init__(
self,
s,
batch_size=64,
height=28,
width=28,
channel=1,
sample_num=8 * 8,
sample_size=8,
z_dim=128,
dfc_unit=256,
gfc_unit=1024,
lr=2e-4,
divergence_method='KL',
use_tricky_g_loss=False,
):
"""
# General Settings
:param s: TF Session
:param batch_size: training batch size, default 64
:param height: input image height, default 28
:param width: input image width, default 28
:param channel: input image channel, default 1
# Output Settings
:param sample_num: the number of sample images, default 64
:param sample_size: sample image size, default 8
# Model Settings
:param z_dim: z noise dimension, default 128
:param dfc_unit: the number of fully connected units used at disc, default 256
:param gfc_unit: the number of fully connected units used at gen, default 1024
# Training Settings
:param lr: learning rate, default 2e-4
:param divergence_method: the method of f-divergences, default 'KL'
:param use_tricky_g_loss: use g_loss referred in f-GAN Section 3.2, default False
"""
self.s = s
self.batch_size = batch_size
self.height = height
self.width = width
self.channel = channel
self.sample_size = sample_size
self.sample_num = sample_num
self.image_shape = [self.height, self.width, self.channel]
self.n_input = self.height * self.width * self.channel
self.z_dim = z_dim
self.dfc_unit = dfc_unit
self.gfc_unit = gfc_unit
# pre-defined
self.d_loss = 0.0
self.g_loss = 0.0
self.g = None
self.d_op = None
self.g_op = None
self.merged = None
self.writer = None
self.saver = None
# Placeholders
self.x = tf.placeholder(tf.float32, shape=[None, self.n_input], name='x-images')
self.z = tf.placeholder(tf.float32, shape=[None, self.z_dim], name='z-noise')
# Training Options
self.beta1 = 0.5
self.lr = lr
self.divergence = divergence_method
self.use_tricky_g_loss = use_tricky_g_loss
self.bulid_fgan() # build f-GAN model
def discriminator(self, x, reuse=None):
with tf.variable_scope('discriminator', reuse=reuse):
x = t.dense(x, self.dfc_unit, name='disc-fc-1')
x = tf.nn.elu(x)
x = t.dense(x, self.dfc_unit, name='disc-fc-2')
x = tf.nn.elu(x)
x = tf.layers.flatten(x)
x = t.dense(x, 1, name='disc-fc-3')
return x
def generator(self, z, reuse=None, is_train=True):
with tf.variable_scope('generator', reuse=reuse):
x = t.dense(z, self.gfc_unit, name='gen-fc-1')
x = t.batch_norm(x, is_train=is_train, name='gen-bn-1')
x = tf.nn.relu(x)
x = t.dense(x, self.gfc_unit, name='gen-fc-2')
x = t.batch_norm(x, is_train=is_train, name='gen-bn-2')
x = tf.nn.relu(x)
x = t.dense(x, self.n_input, name='gen-fc-3')
x = tf.nn.sigmoid(x)
return x
def bulid_fgan(self):
# Generator
self.g = self.generator(self.z)
# Discriminator
d_real = self.discriminator(self.x)
d_fake = self.discriminator(self.g, reuse=True)
# Losses
if self.divergence == 'GAN':
def activation(x):
return -tf.reduce_mean(-t.safe_log(1.0 + tf.exp(-x)))
def conjugate(x):
return -tf.reduce_mean(-t.safe_log(1.0 - tf.exp(x)))
elif self.divergence == 'KL': # tf.distribution.kl_divergence
def activation(x):
return -tf.reduce_mean(x)
def conjugate(x):
return -tf.reduce_mean(tf.exp(x - 1.0))
elif self.divergence == 'Reverse-KL':
def activation(x):
return -tf.reduce_mean(-tf.exp(x))
def conjugate(x):
return -tf.reduce_mean(-1.0 - x) # remove log
elif self.divergence == 'JS':
def activation(x):
return -tf.reduce_mean(tf.log(2.0) - t.safe_log(1.0 + tf.exp(-x)))
def conjugate(x):
return -tf.reduce_mean(-t.safe_log(2.0 - tf.exp(x)))
elif self.divergence == 'JS-Weighted':
def activation(x):
return -tf.reduce_mean(-np.pi * np.log(np.pi) - t.safe_log(1.0 + tf.exp(-x)))
def conjugate(x):
return -tf.reduce_mean((1.0 - np.pi) * t.safe_log((1.0 - np.pi) / (1.0 - np.pi * tf.exp(x / np.pi))))
elif self.divergence == 'Squared-Hellinger':
def activation(x):
return -tf.reduce_mean(1.0 - tf.exp(x))
def conjugate(x):
return -tf.reduce_mean(x / (1.0 - x))
elif self.divergence == 'Pearson':
def activation(x):
return -tf.reduce_mean(x)
def conjugate(x):
return -tf.reduce_mean(tf.square(x) / 4.0 + x)
elif self.divergence == 'Neyman':
def activation(x):
return -tf.reduce_mean(1.0 - tf.exp(x))
def conjugate(x):
return -tf.reduce_mean(2.0 - 2.0 * tf.sqrt(1.0 - x))
elif self.divergence == 'Jeffrey':
from scipy.special import lambertw
def activation(x):
return -tf.reduce_mean(x)
def conjugate(x):
lambert_w = lambertw(self.s.run(tf.exp(1.0 - x))) # need to be replaced with another tensor func
return -tf.reduce_mean(lambert_w + 1.0 / lambert_w + x - 2.0)
elif self.divergence == 'Total-Variation':
def activation(x):
return -tf.reduce_mean(tf.nn.tanh(x) / 2.0)
def conjugate(x):
return -tf.reduce_mean(x)
else:
raise NotImplementedError("[-] Not Implemented f-divergence %s" % self.divergence)
d_real_loss = activation(d_real)
d_fake_loss = conjugate(d_fake)
self.d_loss = d_real_loss - d_fake_loss
if self.use_tricky_g_loss:
self.g_loss = activation(d_fake)
else:
self.g_loss = d_fake_loss
# Summary
tf.summary.scalar("loss/d_real_loss", d_real_loss)
tf.summary.scalar("loss/d_fake_loss", d_fake_loss)
tf.summary.scalar("loss/d_loss", self.d_loss)
tf.summary.scalar("loss/g_loss", self.g_loss)
# Collect trainer values
t_vars = tf.trainable_variables()
d_params = [v for v in t_vars if v.name.startswith('d')]
g_params = [v for v in t_vars if v.name.startswith('g')]
# Optimizer
self.d_op = tf.train.AdamOptimizer(learning_rate=self.lr, beta1=self.beta1).minimize(
self.d_loss, var_list=d_params
)
self.g_op = tf.train.AdamOptimizer(learning_rate=self.lr, beta1=self.beta1).minimize(
self.g_loss, var_list=g_params
)
# Merge summary
self.merged = tf.summary.merge_all()
# Model Saver
self.saver = tf.train.Saver(max_to_keep=1)
self.writer = tf.summary.FileWriter('./model/%s/' % self.divergence, self.s.graph)