forked from LynnHo/CycleGAN-Tensorflow-2
-
Notifications
You must be signed in to change notification settings - Fork 1
/
module.py
146 lines (113 loc) · 4.78 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
import tensorflow as tf
import tfa.normalizations as tfalayers
import tensorflow.keras as keras
# ==============================================================================
# = networks =
# ==============================================================================
def _get_norm_layer(norm):
if norm == 'none':
return lambda: lambda x: x
elif norm == 'batch_norm':
return keras.layers.BatchNormalization
elif norm == 'instance_norm':
return tfalayers.InstanceNormalization
pass
elif norm == 'layer_norm':
return keras.layers.LayerNormalization
pass
class Pad(keras.layers.Layer):
def __init__(self, paddings, mode='CONSTANT', constant_values=0, **kwargs):
super(Pad, self).__init__(**kwargs)
self.paddings = paddings
self.mode = mode
self.constant_values = constant_values
def call(self, inputs):
return tf.pad(inputs, self.paddings, mode=self.mode, constant_values=self.constant_values)
def ResnetGenerator(input_shape=(256, 256, 3),
output_channels=3,
dim=64,
n_downsamplings=2,
n_blocks=9,
norm='instance_norm'):
Norm = _get_norm_layer(norm)
def _residual_block(x):
dim = x.shape[-1]
h = x
h = Pad([[0, 0], [1, 1], [1, 1], [0, 0]], mode='REFLECT')(h)
h = keras.layers.Conv2D(dim, 3, padding='valid', use_bias=False)(h)
h = Norm()(h)
h = keras.layers.ReLU()(h)
h = Pad([[0, 0], [1, 1], [1, 1], [0, 0]], mode='REFLECT')(h)
h = keras.layers.Conv2D(dim, 3, padding='valid', use_bias=False)(h)
h = Norm()(h)
return keras.layers.add([x, h])
# 0
h = inputs = keras.Input(shape=input_shape)
# 1
h = Pad([[0, 0], [3, 3], [3, 3], [0, 0]], mode='REFLECT')(h)
h = keras.layers.Conv2D(dim, 7, padding='valid', use_bias=False)(h)
h = Norm()(h)
h = keras.layers.ReLU()(h)
# 2
for _ in range(n_downsamplings):
dim *= 2
h = keras.layers.Conv2D(dim, 3, strides=2, padding='same', use_bias=False)(h)
h = Norm()(h)
h = keras.layers.ReLU()(h)
# 3
for _ in range(n_blocks):
h = _residual_block(h)
# 4
for _ in range(n_downsamplings):
dim //= 2
h = keras.layers.Conv2DTranspose(dim, 3, strides=2, padding='same', use_bias=False)(h)
h = Norm()(h)
h = keras.layers.ReLU()(h)
# 5
h = Pad([[0, 0], [3, 3], [3, 3], [0, 0]], mode='REFLECT')(h)
h = keras.layers.Conv2D(output_channels, 7, padding='valid')(h)
h = keras.layers.Activation('tanh')(h)
return keras.Model(inputs=inputs, outputs=h)
def ConvDiscriminator(input_shape=(256, 256, 3),
dim=64,
n_downsamplings=3,
norm='instance_norm'):
dim_ = dim
Norm = _get_norm_layer(norm)
# 0
h = inputs = keras.Input(shape=input_shape)
# 1
h = keras.layers.Conv2D(dim, 4, strides=2, padding='same')(h)
h = keras.layers.LeakyReLU(alpha=0.2)(h)
for _ in range(n_downsamplings - 1):
dim = min(dim * 2, dim_ * 8)
h = keras.layers.Conv2D(dim, 4, strides=2, padding='same', use_bias=False)(h)
h = Norm()(h)
h = keras.layers.LeakyReLU(alpha=0.2)(h)
# 2
dim = min(dim * 2, dim_ * 8)
h = keras.layers.Conv2D(dim, 4, strides=1, padding='same', use_bias=False)(h)
h = Norm()(h)
h = keras.layers.LeakyReLU(alpha=0.2)(h)
# 3
h = keras.layers.Conv2D(1, 4, strides=1, padding='same')(h)
return keras.Model(inputs=inputs, outputs=h)
# ==============================================================================
# = learning rate scheduler =
# ==============================================================================
class LinearDecay(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