-
Notifications
You must be signed in to change notification settings - Fork 22
/
models.py
290 lines (251 loc) · 12.8 KB
/
models.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
280
281
282
283
284
285
286
287
288
289
290
import numpy as np
import mxnet as mx
from mxnet import gluon
from mxnet import ndarray as nd
from mxnet.gluon import nn, utils
from mxnet.gluon.nn import Dense, Activation, Conv2D, Conv2DTranspose, \
BatchNorm, LeakyReLU, Flatten, HybridSequential, HybridBlock, Dropout
def set_network(opt, ctx, istest):
depth = opt.depth
if istest:
lr = 0
beta1 = 0
else:
lr = opt.lr
beta1 = opt.beta1
ndf = opt.ndf
ngf = opt.ngf
latent = opt.latent
print(latent)
append = opt.append
netD = None
netD2 = None
netDS = None
trainerD = None
trainerDS = None
trainerD2 = None
# load networks based on opt.ntype (1 - AE 2 - ALOCC 3 - latentD 4 - adnov)
if append:
if opt.ntype > 1:
netD = Discriminator(in_channels=6, n_layers=2, ndf=ndf, istest=istest)
network_init(netD, ctx=ctx)
trainerD = gluon.Trainer(netD.collect_params(), 'adam', {'learning_rate': lr, 'beta1': beta1})
if opt.ntype > 2:
netD2 = LatentDiscriminator(in_channels=6, n_layers=2, ndf=ndf, istest=istest)
network_init(netD2, ctx=ctx)
trainerD2 = gluon.Trainer(netD2.collect_params(), 'adam', {'learning_rate': lr, 'beta1': beta1})
if opt.ntype > 3:
netDS = models.Discriminator(in_channels=6, n_layers=2, ndf=ngf)
network_init(netDS, ctx=ctx)
trainerDS = gluon.Trainer(netDS.collect_params(), 'adam', {'learning_rate': lr, 'beta1': beta1})
else:
if opt.ntype > 1:
netD = Discriminator(in_channels=3, n_layers=2, ndf=ndf, istest=istest)
network_init(netD, ctx=ctx)
trainerD = gluon.Trainer(netD.collect_params(), 'adam', {'learning_rate': lr, 'beta1': beta1})
if opt.ntype > 2:
netD2 = LatentDiscriminator(in_channels=3, n_layers=2, ndf=ndf, istest=istest)
network_init(netD2, ctx=ctx)
trainerD2 = gluon.Trainer(netD2.collect_params(), 'adam', {'learning_rate': lr, 'beta1': beta1})
if opt.ntype > 3:
netDS = Discriminator(in_channels=3, n_layers=2, ndf=ngf)
network_init(netDS, ctx=ctx)
trainerDS = gluon.Trainer(netDS.collect_params(), 'adam', {'learning_rate': lr, 'beta1': beta1})
netEn = Encoder(in_channels=3, n_layers=depth, latent=latent, ndf=ngf, istest=istest)
netDe = Decoder(in_channels=3, n_layers=depth, latent=latent, ndf=ngf, istest=istest)
network_init(netEn, ctx=ctx)
trainerEn = gluon.Trainer(netEn.collect_params(), 'adam', {'learning_rate': lr, 'beta1': beta1})
network_init(netDe, ctx=ctx)
trainerDe = gluon.Trainer(netDe.collect_params(), 'adam', {'learning_rate': lr, 'beta1': beta1})
return netEn, netDe, netD, netD2, netDS, trainerEn, trainerDe, trainerD, trainerD2, trainerDS
# Define Unet generator skip block
class UnetSkipUnit(HybridBlock):
def __init__(self, inner_channels, outer_channels, inner_block=None, innermost=False, outermost=False,
use_dropout=False, use_bias=False):
super(UnetSkipUnit, self).__init__()
with self.name_scope():
self.outermost = outermost
en_conv = Conv2D(channels=inner_channels, kernel_size=5, strides=2, padding=0,
in_channels=outer_channels, use_bias=use_bias)
en_relu = LeakyReLU(alpha=0.2)
en_norm = BatchNorm(momentum=0.1, in_channels=inner_channels)
de_relu = Activation(activation='relu')
de_norm = BatchNorm(momentum=0.1, in_channels=outer_channels)
if innermost:
de_conv = Conv2DTranspose(channels=outer_channels, kernel_size=5, strides=2, padding=0,
in_channels=inner_channels, use_bias=use_bias)
encoder = [en_relu, en_conv]
decoder = [de_relu, de_conv, de_norm]
model = encoder + decoder
elif outermost:
de_conv = Conv2DTranspose(channels=outer_channels, kernel_size=5, strides=2, padding=0,
in_channels=inner_channels)
encoder = [en_conv]
decoder = [de_relu, de_conv, Activation(activation='tanh')]
model = encoder + [inner_block] + decoder
else:
de_conv = Conv2DTranspose(channels=outer_channels, kernel_size=5, strides=2, padding=0,
in_channels=inner_channels, use_bias=use_bias)
encoder = [en_relu, en_conv, en_norm]
decoder = [de_relu, de_conv, de_norm]
model = encoder + [inner_block] + decoder
if use_dropout:
model += [Dropout(rate=0.5)]
self.model = HybridSequential()
with self.model.name_scope():
for block in model:
self.model.add(block)
def hybrid_forward(self, F, x):
if self.outermost:
return self.model(x)
else:
return self.model(x)
# Define Unet generator
class UnetGenerator(HybridBlock):
def __init__(self, in_channels, num_downs, ngf=64, use_dropout=True):
super(UnetGenerator, self).__init__()
# Build unet generator structure
unet = UnetSkipUnit(ngf * 8, ngf * 8, innermost=True)
for _ in range(num_downs - 5):
unet = UnetSkipUnit(ngf * 8, ngf * 8, unet, use_dropout=use_dropout)
unet = UnetSkipUnit(ngf * 8, ngf * 4, unet)
unet = UnetSkipUnit(ngf * 4, ngf * 2, unet)
unet = UnetSkipUnit(ngf * 2, ngf * 1, unet)
unet = UnetSkipUnit(ngf, in_channels, unet, outermost=True)
with self.name_scope():
self.model = unet
def hybrid_forward(self, F, x):
print(np.shape(self.model(x)))
return self.model(x)
# Define the PatchGAN discriminator
class Discriminator(HybridBlock):
def __init__(self, in_channels, ndf=64, n_layers=3, use_sigmoid=False, use_bias=False, istest = False, isthreeway = False):
super(Discriminator, self).__init__()
with self.name_scope():
self.model = HybridSequential()
kernel_size = 5
padding = 0 #int(np.ceil((kernel_size - 1) / 2))
self.model.add(Conv2D(channels=ndf, kernel_size=kernel_size, strides=2,
padding=padding, in_channels=in_channels))
self.model.add(LeakyReLU(alpha=0.2))
nf_mult = 1
for n in range(1, n_layers):
nf_mult_prev = nf_mult
nf_mult = min(2 ** n, 8)
self.model.add(Conv2D(channels=ndf * nf_mult, kernel_size=kernel_size, strides=2,
padding=padding, in_channels=ndf * nf_mult_prev,
use_bias=use_bias))
self.model.add(BatchNorm(momentum=0.1, in_channels=ndf * nf_mult , use_global_stats=istest))
self.model.add(LeakyReLU(alpha=0.2))
nf_mult_prev = nf_mult
nf_mult = min(2 ** n_layers, 8)
self.model.add(Conv2D(channels=ndf * nf_mult, kernel_size=kernel_size, strides=2,
padding=padding, in_channels=ndf * nf_mult_prev,
use_bias=use_bias))
self.model.add(BatchNorm(momentum=0.1, in_channels=ndf * nf_mult, use_global_stats=istest))
self.model.add(LeakyReLU(alpha=0.2))
self.model.add(Conv2D(channels=1, kernel_size=kernel_size, strides=2,
padding=padding, in_channels=ndf * nf_mult))
if isthreeway:
self.model.add(gluon.nn.Dense(3))
elif use_sigmoid:
self.model.add(Activation(activation='sigmoid'))
def hybrid_forward(self, F, x):
out = self.model(x)
#print(np.shape(out))
return out
class LatentDiscriminator(HybridBlock):
def __init__(self, in_channels, ndf=64, n_layers=3, use_sigmoid=False, use_bias=False, istest = False, isthreeway = False):
super(LatentDiscriminator, self).__init__()
with self.name_scope():
self.model = HybridSequential()
self.model.add(gluon.nn.Dense(128))
self.model.add(Activation(activation='relu'))
self.model.add(gluon.nn.Dense(64))
self.model.add(Activation(activation='relu'))
self.model.add(gluon.nn.Dense(32))
self.model.add(Activation(activation='relu'))
self.model.add(gluon.nn.Dense(16))
self.model.add(Activation(activation='sigmoid'))
def hybrid_forward(self, F, x):
out = self.model(x)
return out
class Encoder(HybridBlock):
def __init__(self, in_channels, ndf=64, n_layers=3, use_bias=False, istest=False,latent=256, usetanh = False ):
super(Encoder, self).__init__()
self.model = HybridSequential()
kernel_size = 5
padding = 0 #int(np.ceil((kernel_size - 1) / 2))
self.model.add(Conv2D(channels=ndf, kernel_size=kernel_size, strides=2,
padding=padding, in_channels=in_channels))
self.model.add(LeakyReLU(alpha=0.2))
nf_mult = 2
nf_mult_prev = 1
nf_mult = 1
for n in range(1, n_layers):
nf_mult_prev = nf_mult
nf_mult = 2 ** n
self.model.add(Conv2D(channels=ndf * nf_mult, kernel_size=kernel_size, strides=2,
padding=padding, in_channels=ndf * nf_mult_prev,
use_bias=use_bias))
self.model.add(BatchNorm(momentum=0.1, in_channels=ndf * nf_mult, use_global_stats=istest))
self.model.add(LeakyReLU(alpha=0.2))
nf_mult_prev = nf_mult
nf_mult = 2 ** n_layers
self.model.add(Conv2D(channels=latent, kernel_size=kernel_size, strides=2,
padding=padding, in_channels=ndf * nf_mult_prev,
use_bias=use_bias))
#self.model.add(BatchNorm(momentum=0.1, in_channels =latent, use_global_stats=istest))
if usetanh:
self.model.add(Activation(activation='tanh'))
else:
self.model.add(LeakyReLU(alpha=0.2))
def hybrid_forward(self, F, x):
out = self.model(x)
# print(out)
return out
class Decoder(HybridBlock):
def __init__(self, in_channels, ndf=64, n_layers=3, use_bias=False, istest=False, latent=256, usetanh = False ):
super(Decoder, self).__init__()
self.model = HybridSequential()
kernel_size = 5
padding = 0
nf_mult = 2 ** n_layers
self.model.add(Conv2DTranspose(channels=ndf * nf_mult/2, kernel_size=kernel_size, strides=2,
padding=padding, in_channels=latent,
use_bias=use_bias))
self.model.add(BatchNorm(momentum=0.1, in_channels=ndf * nf_mult / 2, use_global_stats=istest))
self.model.add(Activation(activation='relu'))
for n in range(1, n_layers):
nf_mult = nf_mult / 2
self.model.add(Conv2DTranspose(channels=ndf * nf_mult / 2, kernel_size=kernel_size, strides=2,
padding=padding, in_channels=ndf * nf_mult,
use_bias=use_bias))
self.model.add(BatchNorm(momentum=0.1, in_channels=ndf * nf_mult / 2, use_global_stats=istest))
#self.model.add(LeakyReLU(alpha=0.2))
if n==2:
self.model.add(Dropout(rate=0.5))
self.model.add(Activation(activation='relu'))
self.model.add(Conv2DTranspose(channels=in_channels, kernel_size=kernel_size, strides=2,
padding=padding, in_channels=ndf))
self.model.add(Activation(activation='tanh'))
def hybrid_forward(self, F, x):
out = self.model(x)
# print(out)
return out
def param_init(param, ctx):
if param.name.find('conv') != -1:
if param.name.find('weight') != -1:
param.initialize(init=mx.init.Normal(0.02), ctx=ctx)
else:
param.initialize(init=mx.init.Zero(), ctx=ctx)
elif param.name.find('batchnorm') != -1:
param.initialize(init=mx.init.Zero(), ctx=ctx)
# Initialize gamma from normal distribution with mean 1 and std 0.02
if param.name.find('gamma') != -1:
param.set_data(nd.random_normal(1, 0.02, param.data().shape))
elif param.name.find('dense') != -1:
param.initialize(init=mx.init.Normal(0.02), ctx=ctx)
def network_init(net, ctx):
for param in net.collect_params().values():
param_init(param, ctx)