-
Notifications
You must be signed in to change notification settings - Fork 31
/
Network.py
238 lines (192 loc) · 11.1 KB
/
Network.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
import math
import torch
# import torch.utils.serialization # it was removed in torch v1.0.0 or higher version.
arguments_strModel = 'sintel-final'
SpyNet_model_dir = './models' # The directory of SpyNet's weights
def normalize(tensorInput):
tensorRed = (tensorInput[:, 0:1, :, :] - 0.485) / 0.229
tensorGreen = (tensorInput[:, 1:2, :, :] - 0.456) / 0.224
tensorBlue = (tensorInput[:, 2:3, :, :] - 0.406) / 0.225
return torch.cat([tensorRed, tensorGreen, tensorBlue], 1)
def denormalize(tensorInput):
tensorRed = (tensorInput[:, 0:1, :, :] * 0.229) + 0.485
tensorGreen = (tensorInput[:, 1:2, :, :] * 0.224) + 0.456
tensorBlue = (tensorInput[:, 2:3, :, :] * 0.225) + 0.406
return torch.cat([tensorRed, tensorGreen, tensorBlue], 1)
Backward_tensorGrid = {}
def Backward(tensorInput, tensorFlow, cuda_flag):
if str(tensorFlow.size()) not in Backward_tensorGrid:
tensorHorizontal = torch.linspace(-1.0, 1.0, tensorFlow.size(3)).view(1, 1, 1, tensorFlow.size(3)).expand(tensorFlow.size(0), -1, tensorFlow.size(2), -1)
tensorVertical = torch.linspace(-1.0, 1.0, tensorFlow.size(2)).view(1, 1, tensorFlow.size(2), 1).expand(tensorFlow.size(0), -1, -1, tensorFlow.size(3))
if cuda_flag:
Backward_tensorGrid[str(tensorFlow.size())] = torch.cat([ tensorHorizontal, tensorVertical ], 1).cuda()
else:
Backward_tensorGrid[str(tensorFlow.size())] = torch.cat([tensorHorizontal, tensorVertical], 1)
# end
tensorFlow = torch.cat([ tensorFlow[:, 0:1, :, :] / ((tensorInput.size(3) - 1.0) / 2.0), tensorFlow[:, 1:2, :, :] / ((tensorInput.size(2) - 1.0) / 2.0) ], 1)
return torch.nn.functional.grid_sample(input=tensorInput, grid=(Backward_tensorGrid[str(tensorFlow.size())] + tensorFlow).permute(0, 2, 3, 1), mode='bilinear', padding_mode='border')
# end
class SpyNet(torch.nn.Module):
def __init__(self, cuda_flag):
super(SpyNet, self).__init__()
self.cuda_flag = cuda_flag
class Basic(torch.nn.Module):
def __init__(self, intLevel):
super(Basic, self).__init__()
self.moduleBasic = torch.nn.Sequential(
torch.nn.Conv2d(in_channels=8, out_channels=32, kernel_size=7, stride=1, padding=3),
torch.nn.ReLU(inplace=False),
torch.nn.Conv2d(in_channels=32, out_channels=64, kernel_size=7, stride=1, padding=3),
torch.nn.ReLU(inplace=False),
torch.nn.Conv2d(in_channels=64, out_channels=32, kernel_size=7, stride=1, padding=3),
torch.nn.ReLU(inplace=False),
torch.nn.Conv2d(in_channels=32, out_channels=16, kernel_size=7, stride=1, padding=3),
torch.nn.ReLU(inplace=False),
torch.nn.Conv2d(in_channels=16, out_channels=2, kernel_size=7, stride=1, padding=3)
)
# end
def forward(self, tensorInput):
return self.moduleBasic(tensorInput)
self.moduleBasic = torch.nn.ModuleList([Basic(intLevel) for intLevel in range(4)])
self.load_state_dict(torch.load(SpyNet_model_dir + '/network-' + arguments_strModel + '.pytorch'), strict=False)
def forward(self, tensorFirst, tensorSecond):
tensorFirst = [tensorFirst]
tensorSecond = [tensorSecond]
for intLevel in range(3):
if tensorFirst[0].size(2) > 32 or tensorFirst[0].size(3) > 32:
tensorFirst.insert(0, torch.nn.functional.avg_pool2d(input=tensorFirst[0], kernel_size=2, stride=2))
tensorSecond.insert(0, torch.nn.functional.avg_pool2d(input=tensorSecond[0], kernel_size=2, stride=2))
tensorFlow = tensorFirst[0].new_zeros(tensorFirst[0].size(0), 2,
int(math.floor(tensorFirst[0].size(2) / 2.0)),
int(math.floor(tensorFirst[0].size(3) / 2.0)))
for intLevel in range(len(tensorFirst)):
tensorUpsampled = torch.nn.functional.interpolate(input=tensorFlow, scale_factor=2, mode='bilinear', align_corners=True) * 2.0
# if the sizes of upsampling and downsampling are not the same, apply zero-padding.
if tensorUpsampled.size(2) != tensorFirst[intLevel].size(2):
tensorUpsampled = torch.nn.functional.pad(input=tensorUpsampled, pad=[0, 0, 0, 1], mode='replicate')
if tensorUpsampled.size(3) != tensorFirst[intLevel].size(3):
tensorUpsampled = torch.nn.functional.pad(input=tensorUpsampled, pad=[0, 1, 0, 0], mode='replicate')
# input :[first picture of corresponding level,
# the output of w with input second picture of corresponding level and upsampling flow,
# upsampling flow]
# then we obtain the final flow. 最终再加起来得到intLevel的flow
tensorFlow = self.moduleBasic[intLevel](torch.cat([tensorFirst[intLevel],
Backward(tensorInput=tensorSecond[intLevel],
tensorFlow=tensorUpsampled,
cuda_flag=self.cuda_flag),
tensorUpsampled], 1)) + tensorUpsampled
return tensorFlow
class warp(torch.nn.Module):
def __init__(self, h, w, cuda_flag):
super(warp, self).__init__()
self.height = h
self.width = w
if cuda_flag:
self.addterm = self.init_addterm().cuda()
else:
self.addterm = self.init_addterm()
def init_addterm(self):
n = torch.FloatTensor(list(range(self.width)))
horizontal_term = n.expand((1, 1, self.height, self.width)) # 第一个1是batch size
n = torch.FloatTensor(list(range(self.height)))
vertical_term = n.expand((1, 1, self.width, self.height)).permute(0, 1, 3, 2)
addterm = torch.cat((horizontal_term, vertical_term), dim=1)
return addterm
def forward(self, frame, flow):
"""
:param frame: frame.shape (batch_size=1, n_channels=3, width=256, height=448)
:param flow: flow.shape (batch_size=1, n_channels=2, width=256, height=448)
:return: reference_frame: warped frame
"""
if True:
flow = flow + self.addterm
else:
self.addterm = self.init_addterm()
flow = flow + self.addterm
horizontal_flow = flow[0, 0, :, :].expand(1, 1, self.height, self.width) # 第一个0是batch size
vertical_flow = flow[0, 1, :, :].expand(1, 1, self.height, self.width)
horizontal_flow = horizontal_flow * 2 / (self.width - 1) - 1
vertical_flow = vertical_flow * 2 / (self.height - 1) - 1
flow = torch.cat((horizontal_flow, vertical_flow), dim=1)
flow = flow.permute(0, 2, 3, 1)
reference_frame = torch.nn.functional.grid_sample(frame, flow)
return reference_frame
class ResNet(torch.nn.Module):
"""
Three-layers ResNet/ResBlock
reference: https://blog.csdn.net/chenyuping333/article/details/82344334
"""
def __init__(self, task):
super(ResNet, self).__init__()
self.task = task
self.conv_3x2_64_9x9 = torch.nn.Conv2d(in_channels=3 * 2, out_channels=64, kernel_size=9, padding=8 // 2)
self.conv_3x7_64_9x9 = torch.nn.Conv2d(in_channels=3 * 7, out_channels=64, kernel_size=9, padding=8 // 2)
self.conv_64_64_9x9 = torch.nn.Conv2d(in_channels=64, out_channels=64, kernel_size=9, padding=8 // 2)
self.conv_64_64_1x1 = torch.nn.Conv2d(in_channels=64, out_channels=64, kernel_size=1)
self.conv_64_3_1x1 = torch.nn.Conv2d(in_channels=64, out_channels=3, kernel_size=1)
def ResBlock(self, x, aver):
if self.task == 'interp':
x = torch.nn.functional.relu(self.conv_3x2_64_9x9(x))
x = torch.nn.functional.relu(self.conv_64_64_1x1(x))
elif self.task in ['denoise', 'denoising']:
x = torch.nn.functional.relu(self.conv_3x7_64_9x9(x))
x = torch.nn.functional.relu(self.conv_64_64_1x1(x))
elif self.task in ['sr', 'super-resolution']:
x = torch.nn.functional.relu(self.conv_3x7_64_9x9(x))
x = torch.nn.functional.relu(self.conv_64_64_9x9(x))
x = torch.nn.functional.relu(self.conv_64_64_1x1(x))
else:
raise NameError('Only support: [interp, denoise/denoising, sr/super-resolution]')
x = self.conv_64_3_1x1(x) + aver
return x
def forward(self, frames):
aver = frames.mean(dim=1)
x = frames[:, 0, :, :, :]
for i in range(1, frames.size(1)):
x = torch.cat((x, frames[:, i, :, :, :]), dim=1)
result = self.ResBlock(x, aver)
return result
class TOFlow(torch.nn.Module):
def __init__(self, h, w, task, cuda_flag):
super(TOFlow, self).__init__()
self.height = h
self.width = w
self.task = task
self.cuda_flag = cuda_flag
self.SpyNet = SpyNet(cuda_flag=self.cuda_flag) # SpyNet层
# for param in self.SpyNet.parameters(): # fix
# param.requires_grad = False
self.warp = warp(self.height, self.width, cuda_flag=self.cuda_flag)
self.ResNet = ResNet(task=self.task)
# frames should be TensorFloat
def forward(self, frames):
"""
:param frames: [batch_size=1, img_num, n_channels=3, h, w]
:return:
"""
for i in range(frames.size(1)):
frames[:, i, :, :, :] = normalize(frames[:, i, :, :, :])
if self.cuda_flag:
opticalflows = torch.zeros(frames.size(0), frames.size(1), 2, frames.size(3), frames.size(4)).cuda()
warpframes = torch.empty(frames.size(0), frames.size(1), 3, frames.size(3), frames.size(4)).cuda()
else:
opticalflows = torch.zeros(frames.size(0), frames.size(1), 2, frames.size(3), frames.size(4))
warpframes = torch.empty(frames.size(0), frames.size(1), 3, frames.size(3), frames.size(4))
if self.task == 'interp':
process_index = [0, 1]
opticalflows[:, 1, :, :, :] = self.SpyNet(frames[:, 0, :, :, :], frames[:, 1, :, :, :]) / 2
opticalflows[:, 0, :, :, :] = self.SpyNet(frames[:, 1, :, :, :], frames[:, 0, :, :, :]) / 2
elif self.task in ['denoise', 'denoising', 'sr', 'super-resolution']:
process_index = [0, 1, 2, 4, 5, 6]
for i in process_index:
opticalflows[:, i, :, :, :] = self.SpyNet(frames[:, 3, :, :, :], frames[:, i, :, :, :])
warpframes[:, 3, :, :, :] = frames[:, 3, :, :, :]
else:
raise NameError('Only support: [interp, denoise/denoising, sr/super-resolution]')
for i in process_index:
warpframes[:, i, :, :, :] = self.warp(frames[:, i, :, :, :], opticalflows[:, i, :, :, :])
# warpframes: [batch_size=1, img_num=7, n_channels=3, height=256, width=448]
Img = self.ResNet(warpframes)
# Img: [batch_size=1, n_channels=3, h, w]
Img = denormalize(Img)
return Img