-
Notifications
You must be signed in to change notification settings - Fork 1
/
module.py
247 lines (180 loc) · 7.39 KB
/
module.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
import tensorflow as tf
import tensorflow_addons as tfa
from layers import Oper2D, Oper2DTranspose
# R2C-GAN models.
# Generator and discriminator networks using operational layers: OpGenerator and OpDiscriminator.
# Using convolutional layers: ConvGenerator and ConvDiscriminator.
# Compact generator and discriminator networks using convolutional layers: ConvCompGenerator and ConvCompDiscriminator.
def OpGenerator(input_shape = (256, 256, 3), q = 1):
dim=64
Norm = tfa.layers.InstanceNormalization
def _residual_block(x):
dim = x.shape[-1]
x1 = tf.nn.tanh(x)
h = x1
h = tf.pad(h, [[0, 0], [1, 1], [1, 1], [0, 0]], mode='REFLECT')
h = Oper2D(dim, 3, q = q, padding='valid', use_bias=False)(h)
h = Norm()(h)
h = tf.nn.tanh(h)
h = tf.pad(h, [[0, 0], [1, 1], [1, 1], [0, 0]], mode='REFLECT')
h = Oper2D(dim, 3, q = q, padding='valid', use_bias=False)(h)
h = Norm()(h)
return tf.nn.tanh(tf.keras.layers.add([x, h]))
h = inputs = tf.keras.Input(shape=input_shape)
dim *= 2
h = Oper2D(dim, 3, strides = 2, q = q, padding='same', use_bias=False)(h)
h = Norm()(h)
h = _residual_block(h)
# Classification branch.
x = tf.keras.layers.MaxPool2D(h.shape[1])(h)
x = tf.keras.layers.Flatten()(x)
y_class = tf.keras.layers.Dense(2, activation = 'softmax')(x)
dim //= 2
h = Oper2DTranspose(3, 3, strides = 2, q = q, padding='same')(h)
h = tf.nn.tanh(h)
return tf.keras.Model(inputs=inputs, outputs=[h, y_class])
def OpDiscriminator(input_shape = (256, 256, 3), q = 1):
dim = 64
Norm = tfa.layers.InstanceNormalization
h = inputs = tf.keras.Input(shape=input_shape)
h = Oper2D(dim, 4, strides = 2, q = q, padding='same')(h)
h = tf.nn.leaky_relu(h, alpha=0.2)
h = Oper2D(2 * dim, 4, strides = 4, q = q, padding='same', use_bias=False)(h)
h = Norm()(h)
h = tf.nn.leaky_relu(h, alpha=0.2)
h = Oper2D(1, 4, strides = 1, q = q, padding='same')(h)
h = tf.nn.leaky_relu(h, alpha=0.2)
return tf.keras.Model(inputs=inputs, outputs=h)
def ConvGenerator(input_shape = (256, 256, 3)):
Norm = tfa.layers.InstanceNormalization
dim=64
n_blocks=9
n_downsamplings=2
def _residual_block(x):
dim = x.shape[-1]
h = x
h = tf.pad(h, [[0, 0], [1, 1], [1, 1], [0, 0]], mode='REFLECT')
h = tf.keras.layers.Conv2D(dim, 3, padding='valid', use_bias=False)(h)
h = Norm()(h)
h = tf.nn.relu(h)
h = tf.pad(h, [[0, 0], [1, 1], [1, 1], [0, 0]], mode='REFLECT')
h = tf.keras.layers.Conv2D(dim, 3, padding='valid', use_bias=False)(h)
h = Norm()(h)
return tf.keras.layers.add([x, h])
# 0
h = inputs = tf.keras.Input(shape=input_shape)
# 1
h = tf.pad(h, [[0, 0], [3, 3], [3, 3], [0, 0]], mode='REFLECT')
h = tf.keras.layers.Conv2D(dim, 7, padding='valid', use_bias=False)(h)
h = Norm()(h)
h = tf.nn.relu(h)
# 2
for _ in range(n_downsamplings):
dim *= 2
h = tf.keras.layers.Conv2D(dim, 3, strides=2, padding='same', use_bias=False)(h)
h = Norm()(h)
h = tf.nn.relu(h)
# 3
for _ in range(n_blocks):
h = _residual_block(h)
if _ == 4:
x = tf.keras.layers.MaxPool2D(h.shape[1])(h)
x = tf.keras.layers.Flatten()(x)
y_class = tf.keras.layers.Dense(2, activation = 'softmax')(x)
# 4
for _ in range(n_downsamplings):
dim //= 2
h = tf.keras.layers.Conv2DTranspose(dim, 3, strides=2, padding='same', use_bias=False)(h)
h = Norm()(h)
h = tf.nn.relu(h)
# 5
h = tf.pad(h, [[0, 0], [3, 3], [3, 3], [0, 0]], mode='REFLECT')
h = tf.keras.layers.Conv2D(3, 7, padding='valid')(h)
h = tf.tanh(h)
return tf.keras.Model(inputs=inputs, outputs=[h, y_class])
def ConvDiscriminator(input_shape = (256, 256, 3), n_downsamplings = 3):
dim=64
dim_ = dim
Norm = tfa.layers.InstanceNormalization
# 0
h = inputs = tf.keras.Input(shape=input_shape)
# 1
h = tf.keras.layers.Conv2D(dim, 4, strides=2, padding='same')(h)
h = tf.nn.leaky_relu(h, alpha=0.2)
for _ in range(n_downsamplings - 1):
dim = min(dim * 2, dim_ * 8)
h = tf.keras.layers.Conv2D(dim, 4, strides=2, padding='same', use_bias=False)(h)
h = Norm()(h)
h = tf.nn.leaky_relu(h, alpha=0.2)
# 2
dim = min(dim * 2, dim_ * 8)
h = tf.keras.layers.Conv2D(dim, 4, strides=1, padding='same', use_bias=False)(h)
h = Norm()(h)
h = tf.nn.leaky_relu(h, alpha=0.2)
# 3
h = tf.keras.layers.Conv2D(1, 4, strides=1, padding='same')(h)
return tf.keras.Model(inputs=inputs, outputs=h)
def ConvCompGenerator(input_shape = (256, 256, 3)):
dim=64
Norm = tfa.layers.InstanceNormalization
def _residual_block(x):
dim = x.shape[-1]
h = x
h = tf.pad(h, [[0, 0], [1, 1], [1, 1], [0, 0]], mode='REFLECT')
h = tf.keras.layers.Conv2D(dim, 3, padding='valid', use_bias=False)(h)
h = Norm()(h)
h = tf.nn.relu(h)
h = tf.pad(h, [[0, 0], [1, 1], [1, 1], [0, 0]], mode='REFLECT')
h = tf.keras.layers.Conv2D(dim, 3, padding='valid', use_bias=False)(h)
h = Norm()(h)
return tf.keras.layers.add([x, h])
# 0
h = inputs = tf.keras.Input(shape=input_shape)
# 2
dim *= 2
h = tf.keras.layers.Conv2D(dim, 3, strides=2, padding='same', use_bias=False)(h)
h = Norm()(h)
h = tf.nn.relu(h)
# 3
h = _residual_block(h)
x = tf.keras.layers.MaxPool2D(h.shape[1])(h)
x = tf.keras.layers.Flatten()(x)
y_class = tf.keras.layers.Dense(2, activation = 'softmax')(x)
# 4
dim //= 2
h = tf.keras.layers.Conv2DTranspose(3, 3, strides = 2, padding='same')(h)
h = tf.nn.tanh(h)
return tf.keras.Model(inputs=inputs, outputs=[h, y_class])
def ConvCompDiscriminator(input_shape = (256, 256, 3)):
dim=64
dim_ = dim
Norm = tfa.layers.InstanceNormalization
# 0
h = inputs = tf.keras.Input(shape=input_shape)
# 1
h = tf.keras.layers.Conv2D(dim, 4, strides=2, padding='same')(h)
h = tf.nn.leaky_relu(h, alpha=0.2)
h = tf.keras.layers.Conv2D(2 * dim, 4, strides = 4, padding='same', use_bias=False)(h)
h = Norm()(h)
h = tf.nn.leaky_relu(h, alpha=0.2)
# Classification and Real or Fake
# 3
h = tf.keras.layers.Conv2D(1, 4, strides = 1, padding='same')(h)
return tf.keras.Model(inputs=inputs, outputs=h)
# Linear dropping learning rate scheduler.
class LinearDecay(tf.keras.optimizers.schedules.LearningRateSchedule):
# if `step` < `step_decay`: use fixed learning rate
# else: linearly decay the learning rate to zero
def __init__(self, initial_learning_rate, total_steps, step_decay):
super(LinearDecay, self).__init__()
self._initial_learning_rate = initial_learning_rate
self._steps = total_steps
self._step_decay = step_decay
self.current_learning_rate = tf.Variable(initial_value=initial_learning_rate, trainable=False, dtype=tf.float32)
def __call__(self, step):
self.current_learning_rate.assign(tf.cond(
step >= self._step_decay,
true_fn=lambda: self._initial_learning_rate * (1 - 1 / (self._steps - self._step_decay) * (step - self._step_decay)),
false_fn=lambda: self._initial_learning_rate
))
return self.current_learning_rate