-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
273 lines (164 loc) · 8.68 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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
import torch
from torch import nn
import numpy as np
from utils.wrapper import ConvWrapper, LinearWrapper
class DConv(nn.Module):
def __init__(self, input_shape, dilation):
super().__init__()
self.input_shape = input_shape
conv1 = ConvWrapper(self.input_shape, 2, (3, 1), dilation=dilation)
conv2 = ConvWrapper(conv1.output_shape, 2, (1, 3), dilation=dilation)
self.dconv = nn.Sequential(conv1,
nn.BatchNorm2d(conv1.out_channels),
nn.PReLU(num_parameters=2, init=0.3),
conv2,
nn.BatchNorm2d(conv2.out_channels),
nn.PReLU(num_parameters=2, init=0.3))
self.output_shape = conv2.output_shape
def forward(self, x):
x = self.dconv(x)
return x
class EncoderBlock(nn.Module):
def __init__(self, input_shape):
super().__init__()
self.input_shape = input_shape
dconv1 = DConv(self.input_shape, 1)
dconv2 = DConv(dconv1.output_shape, 2)
dconv3 = DConv(dconv2.output_shape, 3)
self.conv1 = nn.Sequential(dconv1, dconv2, dconv3)
conv2 = ConvWrapper(self.input_shape, 2, 3, dilation=1)
self.conv2 = nn.Sequential(conv2,
nn.BatchNorm2d(conv2.out_channels))
self.prelu1 = nn.PReLU(num_parameters=4, init=0.3)
concat_shape = [self.input_shape[0], 2 * self.input_shape[1], self.input_shape[2], self.input_shape[3]]
conv3 = ConvWrapper(concat_shape, 2, 1)
self.conv3 = nn.Sequential(conv3, nn.BatchNorm2d(conv3.out_channels))
self.identity = nn.Identity()
self.prelu2 = nn.PReLU(num_parameters=2, init=0.3)
self.output_shape = conv3.output_shape
def forward(self, x):
identity = self.identity(x)
x1 = self.conv1(x)
x2 = self.conv2(x)
x = torch.cat([x1, x2], dim=1)
x = self.prelu1(x)
x = self.conv3(x)
x = x + identity
x = self.prelu2(x)
return x
class Encoder(nn.Module):
def __init__(self, input_shape, reduction):
super(Encoder, self).__init__()
self.input_shape = input_shape
self.input_size = np.prod(self.input_shape[1:])
conv5 = ConvWrapper(self.input_shape, 2, 5)
encoder_block = EncoderBlock(conv5.output_shape)
self.encoder = nn.Sequential(conv5,
nn.BatchNorm2d(conv5.out_channels),
encoder_block)
fc_input_shape = [self.input_shape[0], np.prod(encoder_block.output_shape[1:])]
self.fc = LinearWrapper(fc_input_shape, self.input_size // reduction)
self.output_shape = self.fc.output_shape
def forward(self, x):
x = self.encoder(x)
x = x.view(x.size(0), -1)
x = self.fc(x)
return x
class DecoderBlock(nn.Module):
def __init__(self, input_shape, expansion):
super().__init__()
self.input_shape = input_shape
self.expansion = expansion
out_channels = 8 * expansion
groups = 4 * expansion
conv1 = ConvWrapper(self.input_shape, out_channels, 3, dilation=2)
conv2 = ConvWrapper(conv1.output_shape, out_channels, (1, 3), dilation=3, groups=groups)
conv3 = ConvWrapper(conv2.output_shape, out_channels, (3, 1), dilation=3, groups=groups)
conv4 = ConvWrapper(conv3.output_shape, 2, 3)
self.conv1 = nn.Sequential(conv1,
nn.BatchNorm2d(out_channels),
nn.PReLU(num_parameters=out_channels, init=0.3),
conv2,
nn.BatchNorm2d(out_channels),
nn.PReLU(num_parameters=out_channels, init=0.3),
conv3,
nn.BatchNorm2d(out_channels),
nn.PReLU(num_parameters=out_channels, init=0.3),
conv4)
conv1 = ConvWrapper(self.input_shape, out_channels, (1, 3))
conv2 = ConvWrapper(conv1.output_shape, out_channels, (5, 1), groups=groups)
conv3 = ConvWrapper(conv2.output_shape, out_channels, (1, 5), groups=groups)
conv4 = ConvWrapper(conv3.output_shape, 2, (3, 1))
self.conv2 = nn.Sequential(conv1,
nn.BatchNorm2d(out_channels),
nn.PReLU(num_parameters=out_channels, init=0.3),
conv2,
nn.BatchNorm2d(out_channels),
# nn.ShuffleChannel(groups),
nn.PReLU(num_parameters=out_channels, init=0.3),
conv3,
nn.BatchNorm2d(out_channels),
# nn.shuffleChannel(groups),
nn.PReLU(num_parameters=out_channels, init=0.3),
conv4)
self.identity = nn.Identity()
self.prelu1 = nn.PReLU(num_parameters=4, init=0.3)
self.prelu2 = nn.PReLU(num_parameters=2, init=0.3)
concat_shape = [self.input_shape[0], 2 * self.input_shape[1], self.input_shape[2], self.input_shape[3]]
conv = ConvWrapper(concat_shape, 2, 1)
self.conv3 = nn.Sequential(conv, nn.BatchNorm2d(conv.out_channels))
self.output_shape = conv.output_shape
def forward(self, x):
identity = self.identity(x)
x1 = self.conv1(x)
x2 = self.conv2(x)
x = torch.cat([x1, x2], dim=1)
x = self.prelu1(x)
x = self.conv3(x)
x = x + identity
x = self.prelu2(x)
return x
class Decoder(nn.Module):
def __init__(self, input_shape, output_shape, expansion):
super(Decoder, self).__init__()
self.input_shape = input_shape
self.output_shape = output_shape
self.output_size = np.prod(self.output_shape[1:])
self.fc = LinearWrapper(self.input_shape, self.output_size)
conv5 = ConvWrapper(self.output_shape, 2, 5)
decoderBlock1 = DecoderBlock(conv5.output_shape, expansion)
decoderBlock2 = DecoderBlock(decoderBlock1.output_shape, expansion)
self.decoder = nn.Sequential(conv5,
nn.BatchNorm2d(conv5.out_channels),
nn.PReLU(num_parameters=2, init=0.3),
decoderBlock1,
decoderBlock2)
self.output_shape = decoderBlock2.output_shape
def forward(self, x):
x = self.fc(x)
x = x.view(x.shape[0], *self.output_shape[1:])
x = self.decoder(x)
return x
class DCRNet(nn.Module):
def __init__(self, input_shape, reduction=4, expansion=1):
super(DCRNet, self).__init__()
self.input_shape = input_shape
encoder = Encoder(self.input_shape, reduction)
decoder = Decoder(encoder.output_shape, self.input_shape, expansion)
self.net = nn.Sequential(encoder, decoder, nn.Sigmoid())
self.init_params()
def init_params(self):
for m in self.modules():
if isinstance(m, nn.Conv2d):
nn.init.kaiming_normal_(m.weight, mode='fan_out')
if m.bias is not None:
nn.init.constant_(m.bias, 0)
elif isinstance(m, nn.BatchNorm2d):
nn.init.constant_(m.weight, 1)
nn.init.constant_(m.bias, 0)
elif isinstance(m, nn.Linear):
nn.init.normal_(m.weight, std=0.001)
if m.bias is not None:
nn.init.constant_(m.bias, 0)
def forward(self, x):
return self.net(x)