-
Notifications
You must be signed in to change notification settings - Fork 144
/
model.py
266 lines (225 loc) · 10.3 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
"""
Copyright (c) 2019 NAVER Corp.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"""
import torch
import torch.nn as nn
import numpy as np
def get_wav(in_channels, pool=True):
"""wavelet decomposition using conv2d"""
harr_wav_L = 1 / np.sqrt(2) * np.ones((1, 2))
harr_wav_H = 1 / np.sqrt(2) * np.ones((1, 2))
harr_wav_H[0, 0] = -1 * harr_wav_H[0, 0]
harr_wav_LL = np.transpose(harr_wav_L) * harr_wav_L
harr_wav_LH = np.transpose(harr_wav_L) * harr_wav_H
harr_wav_HL = np.transpose(harr_wav_H) * harr_wav_L
harr_wav_HH = np.transpose(harr_wav_H) * harr_wav_H
filter_LL = torch.from_numpy(harr_wav_LL).unsqueeze(0)
filter_LH = torch.from_numpy(harr_wav_LH).unsqueeze(0)
filter_HL = torch.from_numpy(harr_wav_HL).unsqueeze(0)
filter_HH = torch.from_numpy(harr_wav_HH).unsqueeze(0)
if pool:
net = nn.Conv2d
else:
net = nn.ConvTranspose2d
LL = net(in_channels, in_channels,
kernel_size=2, stride=2, padding=0, bias=False,
groups=in_channels)
LH = net(in_channels, in_channels,
kernel_size=2, stride=2, padding=0, bias=False,
groups=in_channels)
HL = net(in_channels, in_channels,
kernel_size=2, stride=2, padding=0, bias=False,
groups=in_channels)
HH = net(in_channels, in_channels,
kernel_size=2, stride=2, padding=0, bias=False,
groups=in_channels)
LL.weight.requires_grad = False
LH.weight.requires_grad = False
HL.weight.requires_grad = False
HH.weight.requires_grad = False
LL.weight.data = filter_LL.float().unsqueeze(0).expand(in_channels, -1, -1, -1)
LH.weight.data = filter_LH.float().unsqueeze(0).expand(in_channels, -1, -1, -1)
HL.weight.data = filter_HL.float().unsqueeze(0).expand(in_channels, -1, -1, -1)
HH.weight.data = filter_HH.float().unsqueeze(0).expand(in_channels, -1, -1, -1)
return LL, LH, HL, HH
class WavePool(nn.Module):
def __init__(self, in_channels):
super(WavePool, self).__init__()
self.LL, self.LH, self.HL, self.HH = get_wav(in_channels)
def forward(self, x):
return self.LL(x), self.LH(x), self.HL(x), self.HH(x)
class WaveUnpool(nn.Module):
def __init__(self, in_channels, option_unpool='cat5'):
super(WaveUnpool, self).__init__()
self.in_channels = in_channels
self.option_unpool = option_unpool
self.LL, self.LH, self.HL, self.HH = get_wav(self.in_channels, pool=False)
def forward(self, LL, LH, HL, HH, original=None):
if self.option_unpool == 'sum':
return self.LL(LL) + self.LH(LH) + self.HL(HL) + self.HH(HH)
elif self.option_unpool == 'cat5' and original is not None:
return torch.cat([self.LL(LL), self.LH(LH), self.HL(HL), self.HH(HH), original], dim=1)
else:
raise NotImplementedError
class WaveEncoder(nn.Module):
def __init__(self, option_unpool):
super(WaveEncoder, self).__init__()
self.option_unpool = option_unpool
self.pad = nn.ReflectionPad2d(1)
self.relu = nn.ReLU(inplace=True)
self.conv0 = nn.Conv2d(3, 3, 1, 1, 0)
self.conv1_1 = nn.Conv2d(3, 64, 3, 1, 0)
self.conv1_2 = nn.Conv2d(64, 64, 3, 1, 0)
self.pool1 = WavePool(64)
self.conv2_1 = nn.Conv2d(64, 128, 3, 1, 0)
self.conv2_2 = nn.Conv2d(128, 128, 3, 1, 0)
self.pool2 = WavePool(128)
self.conv3_1 = nn.Conv2d(128, 256, 3, 1, 0)
self.conv3_2 = nn.Conv2d(256, 256, 3, 1, 0)
self.conv3_3 = nn.Conv2d(256, 256, 3, 1, 0)
self.conv3_4 = nn.Conv2d(256, 256, 3, 1, 0)
self.pool3 = WavePool(256)
self.conv4_1 = nn.Conv2d(256, 512, 3, 1, 0)
def forward(self, x):
skips = {}
for level in [1, 2, 3, 4]:
x = self.encode(x, skips, level)
return x
def encode(self, x, skips, level):
assert level in {1, 2, 3, 4}
if self.option_unpool == 'sum':
if level == 1:
out = self.conv0(x)
out = self.relu(self.conv1_1(self.pad(out)))
out = self.relu(self.conv1_2(self.pad(out)))
skips['conv1_2'] = out
LL, LH, HL, HH = self.pool1(out)
skips['pool1'] = [LH, HL, HH]
return LL
elif level == 2:
out = self.relu(self.conv2_1(self.pad(x)))
out = self.relu(self.conv2_2(self.pad(out)))
skips['conv2_2'] = out
LL, LH, HL, HH = self.pool2(out)
skips['pool2'] = [LH, HL, HH]
return LL
elif level == 3:
out = self.relu(self.conv3_1(self.pad(x)))
out = self.relu(self.conv3_2(self.pad(out)))
out = self.relu(self.conv3_3(self.pad(out)))
out = self.relu(self.conv3_4(self.pad(out)))
skips['conv3_4'] = out
LL, LH, HL, HH = self.pool3(out)
skips['pool3'] = [LH, HL, HH]
return LL
else:
return self.relu(self.conv4_1(self.pad(x)))
elif self.option_unpool == 'cat5':
if level == 1:
out = self.conv0(x)
out = self.relu(self.conv1_1(self.pad(out)))
return out
elif level == 2:
out = self.relu(self.conv1_2(self.pad(x)))
skips['conv1_2'] = out
LL, LH, HL, HH = self.pool1(out)
skips['pool1'] = [LH, HL, HH]
out = self.relu(self.conv2_1(self.pad(LL)))
return out
elif level == 3:
out = self.relu(self.conv2_2(self.pad(x)))
skips['conv2_2'] = out
LL, LH, HL, HH = self.pool2(out)
skips['pool2'] = [LH, HL, HH]
out = self.relu(self.conv3_1(self.pad(LL)))
return out
else:
out = self.relu(self.conv3_2(self.pad(x)))
out = self.relu(self.conv3_3(self.pad(out)))
out = self.relu(self.conv3_4(self.pad(out)))
skips['conv3_4'] = out
LL, LH, HL, HH = self.pool3(out)
skips['pool3'] = [LH, HL, HH]
out = self.relu(self.conv4_1(self.pad(LL)))
return out
else:
raise NotImplementedError
class WaveDecoder(nn.Module):
def __init__(self, option_unpool):
super(WaveDecoder, self).__init__()
self.option_unpool = option_unpool
if option_unpool == 'sum':
multiply_in = 1
elif option_unpool == 'cat5':
multiply_in = 5
else:
raise NotImplementedError
self.pad = nn.ReflectionPad2d(1)
self.relu = nn.ReLU(inplace=True)
self.conv4_1 = nn.Conv2d(512, 256, 3, 1, 0)
self.recon_block3 = WaveUnpool(256, option_unpool)
if option_unpool == 'sum':
self.conv3_4 = nn.Conv2d(256*multiply_in, 256, 3, 1, 0)
else:
self.conv3_4_2 = nn.Conv2d(256*multiply_in, 256, 3, 1, 0)
self.conv3_3 = nn.Conv2d(256, 256, 3, 1, 0)
self.conv3_2 = nn.Conv2d(256, 256, 3, 1, 0)
self.conv3_1 = nn.Conv2d(256, 128, 3, 1, 0)
self.recon_block2 = WaveUnpool(128, option_unpool)
if option_unpool == 'sum':
self.conv2_2 = nn.Conv2d(128*multiply_in, 128, 3, 1, 0)
else:
self.conv2_2_2 = nn.Conv2d(128*multiply_in, 128, 3, 1, 0)
self.conv2_1 = nn.Conv2d(128, 64, 3, 1, 0)
self.recon_block1 = WaveUnpool(64, option_unpool)
if option_unpool == 'sum':
self.conv1_2 = nn.Conv2d(64*multiply_in, 64, 3, 1, 0)
else:
self.conv1_2_2 = nn.Conv2d(64*multiply_in, 64, 3, 1, 0)
self.conv1_1 = nn.Conv2d(64, 3, 3, 1, 0)
def forward(self, x, skips):
for level in [4, 3, 2, 1]:
x = self.decode(x, skips, level)
return x
def decode(self, x, skips, level):
assert level in {4, 3, 2, 1}
if level == 4:
out = self.relu(self.conv4_1(self.pad(x)))
LH, HL, HH = skips['pool3']
original = skips['conv3_4'] if 'conv3_4' in skips.keys() else None
out = self.recon_block3(out, LH, HL, HH, original)
_conv3_4 = self.conv3_4 if self.option_unpool == 'sum' else self.conv3_4_2
out = self.relu(_conv3_4(self.pad(out)))
out = self.relu(self.conv3_3(self.pad(out)))
return self.relu(self.conv3_2(self.pad(out)))
elif level == 3:
out = self.relu(self.conv3_1(self.pad(x)))
LH, HL, HH = skips['pool2']
original = skips['conv2_2'] if 'conv2_2' in skips.keys() else None
out = self.recon_block2(out, LH, HL, HH, original)
_conv2_2 = self.conv2_2 if self.option_unpool == 'sum' else self.conv2_2_2
return self.relu(_conv2_2(self.pad(out)))
elif level == 2:
out = self.relu(self.conv2_1(self.pad(x)))
LH, HL, HH = skips['pool1']
original = skips['conv1_2'] if 'conv1_2' in skips.keys() else None
out = self.recon_block1(out, LH, HL, HH, original)
_conv1_2 = self.conv1_2 if self.option_unpool == 'sum' else self.conv1_2_2
return self.relu(_conv1_2(self.pad(out)))
else:
return self.conv1_1(self.pad(x))