/
model.py
201 lines (157 loc) · 6.05 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
import torch
from torch import nn
from torch import autograd
from torch.autograd import Variable
from torch.nn import functional as F
from torch.nn.parameter import Parameter
import settings
class SEBlock(nn.Module):
def __init__(self, input_dim, reduction):
super().__init__()
mid = int(input_dim / reduction)
self.avg_pool = nn.AdaptiveAvgPool2d(1)
self.fc = nn.Sequential(
nn.Linear(input_dim, reduction),
nn.ReLU(inplace=True),
nn.Linear(reduction, input_dim),
nn.Sigmoid()
)
def forward(self, x):
b, c, _, _ = x.size()
y = self.avg_pool(x).view(b, c)
y = self.fc(y).view(b, c, 1, 1)
return x * y
class NoSEBlock(nn.Module):
def __init__(self, input_dim, reduction):
super().__init__()
def forward(self, x):
return x
SE = SEBlock if settings.use_se else NoSEBlock
class ConvDirec(nn.Module):
def __init__(self, inp_dim, oup_dim, kernel, dilation):
super().__init__()
pad = int(dilation * (kernel - 1) / 2)
self.conv = nn.Conv2d(inp_dim, oup_dim, kernel, padding=pad, dilation=dilation)
self.se = SE(oup_dim, 6)
self.relu = nn.LeakyReLU(0.2)
def forward(self, x, h=None):
x = self.conv(x)
x = self.relu(self.se(x))
return x, None
class ConvRNN(nn.Module):
def __init__(self, inp_dim, oup_dim, kernel, dilation):
super().__init__()
pad_x = int(dilation * (kernel - 1) / 2)
self.conv_x = nn.Conv2d(inp_dim, oup_dim, kernel, padding=pad_x, dilation=dilation)
pad_h = int((kernel - 1) / 2)
self.conv_h = nn.Conv2d(oup_dim, oup_dim, kernel, padding=pad_h)
self.se = SE(oup_dim, 6)
self.relu = nn.LeakyReLU(0.2)
def forward(self, x, h=None):
if h is None:
h = F.tanh(self.conv_x(x))
else:
h = F.tanh(self.conv_x(x) + self.conv_h(h))
h = self.relu(self.se(h))
return h, h
class ConvGRU(nn.Module):
def __init__(self, inp_dim, oup_dim, kernel, dilation):
super().__init__()
pad_x = int(dilation * (kernel - 1) / 2)
self.conv_xz = nn.Conv2d(inp_dim, oup_dim, kernel, padding=pad_x, dilation=dilation)
self.conv_xr = nn.Conv2d(inp_dim, oup_dim, kernel, padding=pad_x, dilation=dilation)
self.conv_xn = nn.Conv2d(inp_dim, oup_dim, kernel, padding=pad_x, dilation=dilation)
pad_h = int((kernel - 1) / 2)
self.conv_hz = nn.Conv2d(oup_dim, oup_dim, kernel, padding=pad_h)
self.conv_hr = nn.Conv2d(oup_dim, oup_dim, kernel, padding=pad_h)
self.conv_hn = nn.Conv2d(oup_dim, oup_dim, kernel, padding=pad_h)
self.se = SE(oup_dim, 6)
self.relu = nn.LeakyReLU(0.2)
def forward(self, x, h=None):
if h is None:
z = F.sigmoid(self.conv_xz(x))
f = F.tanh(self.conv_xn(x))
h = z * f
else:
z = F.sigmoid(self.conv_xz(x) + self.conv_hz(h))
r = F.sigmoid(self.conv_xr(x) + self.conv_hr(h))
n = F.tanh(self.conv_xn(x) + self.conv_hn(r * h))
h = (1 - z) * h + z * n
h = self.relu(self.se(h))
return h, h
class ConvLSTM(nn.Module):
def __init__(self, inp_dim, oup_dim, kernel, dilation):
super().__init__()
pad_x = int(dilation * (kernel - 1) / 2)
self.conv_xf = nn.Conv2d(inp_dim, oup_dim, kernel, padding=pad_x, dilation=dilation)
self.conv_xi = nn.Conv2d(inp_dim, oup_dim, kernel, padding=pad_x, dilation=dilation)
self.conv_xo = nn.Conv2d(inp_dim, oup_dim, kernel, padding=pad_x, dilation=dilation)
self.conv_xj = nn.Conv2d(inp_dim, oup_dim, kernel, padding=pad_x, dilation=dilation)
pad_h = int((kernel - 1) / 2)
self.conv_hf = nn.Conv2d(oup_dim, oup_dim, kernel, padding=pad_h)
self.conv_hi = nn.Conv2d(oup_dim, oup_dim, kernel, padding=pad_h)
self.conv_ho = nn.Conv2d(oup_dim, oup_dim, kernel, padding=pad_h)
self.conv_hj = nn.Conv2d(oup_dim, oup_dim, kernel, padding=pad_h)
self.se = SE(oup_dim, 6)
self.relu = nn.LeakyReLU(0.2)
def forward(self, x, pair=None):
if pair is None:
i = F.sigmoid(self.conv_xi(x))
o = F.sigmoid(self.conv_xo(x))
j = F.tanh(self.conv_xj(x))
c = i * j
h = o * c
else:
h, c = pair
f = F.sigmoid(self.conv_xf(x) + self.conv_hf(h))
i = F.sigmoid(self.conv_xi(x) + self.conv_hi(h))
o = F.sigmoid(self.conv_xo(x) + self.conv_ho(h))
j = F.tanh(self.conv_xj(x) + self.conv_hj(h))
c = f * c + i * j
h = o * F.tanh(c)
h = self.relu(self.se(h))
return h, [h, c]
RecUnit = {
'Conv': ConvDirec,
'RNN': ConvRNN,
'GRU': ConvGRU,
'LSTM': ConvLSTM,
}[settings.uint]
class RESCAN(nn.Module):
def __init__(self):
super().__init__()
channel = settings.channel
self.rnns = nn.ModuleList(
[RecUnit(3, channel, 3, 1)] +
[RecUnit(channel, channel, 3, 2 ** i) for i in range(settings.depth - 3)]
)
self.dec = nn.Sequential(
nn.Conv2d(channel, channel, 3, padding=1),
SE(channel, 6),
nn.LeakyReLU(0.2),
nn.Conv2d(channel, 3, 1),
)
def forward(self, x):
ori = x
old_states = [None for _ in range(len(self.rnns))]
oups = []
for i in range(settings.stage_num):
states = []
for rnn, state in zip(self.rnns, old_states):
x, st = rnn(x, state)
states.append(st)
x = self.dec(x)
if settings.frame == 'Add' and i > 0:
x = x + Variable(oups[-1].data)
oups.append(x)
old_states = states.copy()
x = ori - x
return oups
if __name__ == '__main__':
ts = torch.Tensor(16, 3, 64, 64)
vr = Variable(ts)
net = RESCAN()
print(net)
oups = net(vr)
for oup in oups:
print(oup.size())