-
Notifications
You must be signed in to change notification settings - Fork 6
/
model.py
257 lines (187 loc) · 10 KB
/
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
247
248
249
250
251
252
253
254
255
256
from layer import *
import torch
import torch.nn as nn
from torch.nn import init
from torch.optim import lr_scheduler
# U-Net: Convolutional Networks for Biomedical Image Segmentation
# https://arxiv.org/abs/1505.04597
class UNet(nn.Module):
def __init__(self, nch_in, nch_out, nch_ker=64, norm='bnorm'):
super(UNet, self).__init__()
self.nch_in = nch_in
self.nch_out = nch_out
self.nch_ker = nch_ker
self.norm = norm
if norm == 'bnorm':
self.bias = False
else:
self.bias = True
"""
Encoder part
"""
self.enc1_1 = CNR2d(1 * self.nch_in, 1 * self.nch_ker, kernel_size=3, stride=1, norm=self.norm, relu=0.0, drop=[])
self.enc1_2 = CNR2d(1 * self.nch_ker, 1 * self.nch_ker, kernel_size=3, stride=1, norm=self.norm, relu=0.0, drop=[])
self.pool1 = Pooling2d(pool=2, type='avg')
self.enc2_1 = CNR2d(1 * self.nch_ker, 2 * self.nch_ker, kernel_size=3, stride=1, norm=self.norm, relu=0.0, drop=[])
self.enc2_2 = CNR2d(2 * self.nch_ker, 2 * self.nch_ker, kernel_size=3, stride=1, norm=self.norm, relu=0.0, drop=[])
self.pool2 = Pooling2d(pool=2, type='avg')
self.enc3_1 = CNR2d(2 * self.nch_ker, 4 * self.nch_ker, kernel_size=3, stride=1, norm=self.norm, relu=0.0, drop=[])
self.enc3_2 = CNR2d(4 * self.nch_ker, 4 * self.nch_ker, kernel_size=3, stride=1, norm=self.norm, relu=0.0, drop=[])
self.pool3 = Pooling2d(pool=2, type='avg')
self.enc4_1 = CNR2d(4 * self.nch_ker, 8 * self.nch_ker, kernel_size=3, stride=1, norm=self.norm, relu=0.0, drop=[])
self.enc4_2 = CNR2d(8 * self.nch_ker, 8 * self.nch_ker, kernel_size=3, stride=1, norm=self.norm, relu=0.0, drop=[])
self.pool4 = Pooling2d(pool=2, type='avg')
self.enc5_1 = CNR2d(8 * self.nch_ker, 2 * 8 * self.nch_ker, kernel_size=3, stride=1, norm=self.norm, relu=0.0, drop=[])
"""
Decoder part
"""
self.dec5_1 = DECNR2d(2 * 8 * self.nch_ker, 8 * self.nch_ker, kernel_size=3, stride=1, norm=self.norm, relu=0.0, drop=[])
self.unpool4 = UnPooling2d(pool=2, type='nearest')
self.dec4_2 = DECNR2d(2 * 8 * self.nch_ker, 8 * self.nch_ker, kernel_size=3, stride=1, norm=self.norm, relu=0.0, drop=[])
self.dec4_1 = DECNR2d(8 * self.nch_ker, 4 * self.nch_ker, kernel_size=3, stride=1, norm=self.norm, relu=0.0, drop=[])
self.unpool3 = UnPooling2d(pool=2, type='nearest')
self.dec3_2 = DECNR2d(2 * 4 * self.nch_ker, 4 * self.nch_ker, kernel_size=3, stride=1, norm=self.norm, relu=0.0, drop=[])
self.dec3_1 = DECNR2d(4 * self.nch_ker, 2 * self.nch_ker, kernel_size=3, stride=1, norm=self.norm, relu=0.0, drop=[])
self.unpool2 = UnPooling2d(pool=2, type='nearest')
self.dec2_2 = DECNR2d(2 * 2 * self.nch_ker, 2 * self.nch_ker, kernel_size=3, stride=1, norm=self.norm, relu=0.0, drop=[])
self.dec2_1 = DECNR2d(2 * self.nch_ker, 1 * self.nch_ker, kernel_size=3, stride=1, norm=self.norm, relu=0.0, drop=[])
self.unpool1 = UnPooling2d(pool=2, type='nearest')
self.dec1_2 = DECNR2d(2 * 1 * self.nch_ker, 1 * self.nch_ker, kernel_size=3, stride=1, norm=self.norm, relu=0.0, drop=[])
self.dec1_1 = DECNR2d(1 * self.nch_ker, 1 * self.nch_out, kernel_size=3, stride=1, norm=[], relu=[], drop=[], bias=False)
def forward(self, x):
"""
Encoder part
"""
enc1 = self.enc1_2(self.enc1_1(x))
pool1 = self.pool1(enc1)
enc2 = self.enc2_2(self.enc2_1(pool1))
pool2 = self.pool2(enc2)
enc3 = self.enc3_2(self.enc3_1(pool2))
pool3 = self.pool3(enc3)
enc4 = self.enc4_2(self.enc4_1(pool3))
pool4 = self.pool4(enc4)
enc5 = self.enc5_1(pool4)
"""
Encoder part
"""
dec5 = self.dec5_1(enc5)
unpool4 = self.unpool4(dec5)
cat4 = torch.cat([enc4, unpool4], dim=1)
dec4 = self.dec4_1(self.dec4_2(cat4))
unpool3 = self.unpool3(dec4)
cat3 = torch.cat([enc3, unpool3], dim=1)
dec3 = self.dec3_1(self.dec3_2(cat3))
unpool2 = self.unpool2(dec3)
cat2 = torch.cat([enc2, unpool2], dim=1)
dec2 = self.dec2_1(self.dec2_2(cat2))
unpool1 = self.unpool1(dec2)
cat1 = torch.cat([enc1, unpool1], dim=1)
dec1 = self.dec1_1(self.dec1_2(cat1))
x = dec1
return x
# Photo-Realistic Single Image Super-Resolution Using a Generative Adversarial Network
# https://arxiv.org/abs/1609.04802
class ResNet(nn.Module):
def __init__(self, nch_in, nch_out, nch_ker=64, norm='bnorm', nblk=16):
super(ResNet, self).__init__()
self.nch_in = nch_in
self.nch_out = nch_out
self.nch_ker = nch_ker
self.norm = norm
self.nblk = nblk
if norm == 'bnorm':
self.bias = False
else:
self.bias = True
self.enc1 = CNR2d(self.nch_in, self.nch_ker, kernel_size=3, stride=1, padding=1, norm=[], relu=0.0)
res = []
for i in range(self.nblk):
res += [ResBlock(self.nch_ker, self.nch_ker, kernel_size=3, stride=1, padding=1, norm=self.norm, relu=0.0, padding_mode='reflection')]
self.res = nn.Sequential(*res)
self.dec1 = CNR2d(self.nch_ker, self.nch_ker, kernel_size=3, stride=1, padding=1, norm=norm, relu=[])
self.conv1 = Conv2d(self.nch_ker, self.nch_out, kernel_size=3, stride=1, padding=1)
def forward(self, x):
x = self.enc1(x)
x0 = x
x = self.res(x)
x = self.dec1(x)
x = x + x0
x = self.conv1(x)
return x
class Discriminator(nn.Module):
def __init__(self, nch_in, nch_ker=64, norm='bnorm'):
super(Discriminator, self).__init__()
self.nch_in = nch_in
self.nch_ker = nch_ker
self.norm = norm
if norm == 'bnorm':
self.bias = False
else:
self.bias = True
# dsc1 : 256 x 256 x 3 -> 128 x 128 x 64
# dsc2 : 128 x 128 x 64 -> 64 x 64 x 128
# dsc3 : 64 x 64 x 128 -> 32 x 32 x 256
# dsc4 : 32 x 32 x 256 -> 16 x 16 x 512
# dsc5 : 16 x 16 x 512 -> 16 x 16 x 1
self.dsc1 = CNR2d(1 * self.nch_in, 1 * self.nch_ker, kernel_size=4, stride=2, padding=1, norm=self.norm, relu=0.2)
self.dsc2 = CNR2d(1 * self.nch_ker, 2 * self.nch_ker, kernel_size=4, stride=2, padding=1, norm=self.norm, relu=0.2)
self.dsc3 = CNR2d(2 * self.nch_ker, 4 * self.nch_ker, kernel_size=4, stride=2, padding=1, norm=self.norm, relu=0.2)
self.dsc4 = CNR2d(4 * self.nch_ker, 8 * self.nch_ker, kernel_size=4, stride=2, padding=1, norm=self.norm, relu=0.2)
self.dsc5 = CNR2d(8 * self.nch_ker, 1, kernel_size=4, stride=1, padding=1, norm=[], relu=[], bias=False)
# self.dsc1 = CNR2d(1 * self.nch_in, 1 * self.nch_ker, kernel_size=4, stride=2, padding=1, norm=[], relu=0.2)
# self.dsc2 = CNR2d(1 * self.nch_ker, 2 * self.nch_ker, kernel_size=4, stride=2, padding=1, norm=[], relu=0.2)
# self.dsc3 = CNR2d(2 * self.nch_ker, 4 * self.nch_ker, kernel_size=4, stride=2, padding=1, norm=[], relu=0.2)
# self.dsc4 = CNR2d(4 * self.nch_ker, 8 * self.nch_ker, kernel_size=4, stride=1, padding=1, norm=[], relu=0.2)
# self.dsc5 = CNR2d(8 * self.nch_ker, 1, kernel_size=4, stride=1, padding=1, norm=[], relu=[], bias=False)
def forward(self, x):
x = self.dsc1(x)
x = self.dsc2(x)
x = self.dsc3(x)
x = self.dsc4(x)
x = self.dsc5(x)
# x = torch.sigmoid(x)
return x
def init_weights(net, init_type='normal', init_gain=0.02):
"""Initialize network weights.
Parameters:
net (network) -- network to be initialized
init_type (str) -- the name of an initialization method: normal | xavier | kaiming | orthogonal
init_gain (float) -- scaling factor for normal, xavier and orthogonal.
We use 'normal' in the original pix2pix and CycleGAN paper. But xavier and kaiming might
work better for some applications. Feel free to try yourself.
"""
def init_func(m): # define the initialization function
classname = m.__class__.__name__
if hasattr(m, 'weight') and (classname.find('Conv') != -1 or classname.find('Linear') != -1):
if init_type == 'normal':
init.normal_(m.weight.data, 0.0, init_gain)
elif init_type == 'xavier':
init.xavier_normal_(m.weight.data, gain=init_gain)
elif init_type == 'kaiming':
init.kaiming_normal_(m.weight.data, a=0, mode='fan_in')
elif init_type == 'orthogonal':
init.orthogonal_(m.weight.data, gain=init_gain)
else:
raise NotImplementedError('initialization method [%s] is not implemented' % init_type)
if hasattr(m, 'bias') and m.bias is not None:
init.constant_(m.bias.data, 0.0)
elif classname.find('BatchNorm2d') != -1: # BatchNorm Layer's weight is not a matrix; only normal distribution applies.
init.normal_(m.weight.data, 1.0, init_gain)
init.constant_(m.bias.data, 0.0)
print('initialize network with %s' % init_type)
net.apply(init_func) # apply the initialization function <init_func>
def init_net(net, init_type='normal', init_gain=0.02, gpu_ids=[]):
"""Initialize a network: 1. register CPU/GPU device (with multi-GPU support); 2. initialize the network weights
Parameters:
net (network) -- the network to be initialized
init_type (str) -- the name of an initialization method: normal | xavier | kaiming | orthogonal
gain (float) -- scaling factor for normal, xavier and orthogonal.
gpu_ids (int list) -- which GPUs the network runs on: e.g., 0,1,2
Return an initialized network.
"""
if gpu_ids:
assert(torch.cuda.is_available())
net.to(gpu_ids[0])
net = torch.nn.DataParallel(net, gpu_ids) # multi-GPUs
init_weights(net, init_type, init_gain=init_gain)
return net