-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathvideo_net.py
201 lines (160 loc) · 6.64 KB
/
video_net.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
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Function
backward_grid = [{} for _ in range(9)] # 0~7 for GPU, -1 for CPU
class LowerBound(Function):
@staticmethod
def forward(ctx, inputs, bound):
b = torch.ones_like(inputs) * bound
ctx.save_for_backward(inputs, b)
return torch.max(inputs, b)
@staticmethod
def backward(ctx, grad_output):
inputs, b = ctx.saved_tensors
pass_through_1 = inputs >= b
pass_through_2 = grad_output < 0
pass_through = pass_through_1 | pass_through_2
return pass_through.type(grad_output.dtype) * grad_output, None
class GDN(nn.Module):
def __init__(self,
ch,
inverse=False,
beta_min=1e-6,
gamma_init=0.1,
reparam_offset=2**-18):
super().__init__()
self.inverse = inverse
self.beta_min = beta_min
self.gamma_init = gamma_init
self.reparam_offset = reparam_offset
self.build(ch)
def build(self, ch):
self.pedestal = self.reparam_offset**2
self.beta_bound = ((self.beta_min + self.reparam_offset**2)**0.5)
self.gamma_bound = self.reparam_offset
beta = torch.sqrt(torch.ones(ch)+self.pedestal)
self.beta = nn.Parameter(beta)
eye = torch.eye(ch)
g = self.gamma_init*eye
g = g + self.pedestal
gamma = torch.sqrt(g)
self.gamma = nn.Parameter(gamma)
self.pedestal = self.pedestal
def forward(self, inputs):
_, ch, _, _ = inputs.size()
# Beta bound and reparam
beta = LowerBound.apply(self.beta, self.beta_bound)
beta = beta**2 - self.pedestal
# Gamma bound and reparam
gamma = LowerBound.apply(self.gamma, self.gamma_bound)
gamma = gamma**2 - self.pedestal
gamma = gamma.view(ch, ch, 1, 1)
# Norm pool calc
norm_ = nn.functional.conv2d(inputs**2, gamma, beta)
norm_ = torch.sqrt(norm_)
# Apply norm
if self.inverse:
outputs = inputs * norm_
else:
outputs = inputs / norm_
return outputs
def torch_warp(feature, flow):
device_id = -1 if feature.device == torch.device('cpu') else feature.device.index
if str(flow.size()) not in backward_grid[device_id]:
N, _, H, W = flow.size()
tensor_hor = torch.linspace(-1.0, 1.0, W, device=feature.device, dtype=feature.dtype).view(
1, 1, 1, W).expand(N, -1, H, -1)
tensor_ver = torch.linspace(-1.0, 1.0, H, device=feature.device, dtype=feature.dtype).view(
1, 1, H, 1).expand(N, -1, -1, W)
backward_grid[device_id][str(flow.size())] = torch.cat([tensor_hor, tensor_ver], 1)
flow = torch.cat([flow[:, 0:1, :, :] / ((feature.size(3) - 1.0) / 2.0),
flow[:, 1:2, :, :] / ((feature.size(2) - 1.0) / 2.0)], 1)
grid = (backward_grid[device_id][str(flow.size())] + flow)
return torch.nn.functional.grid_sample(input=feature,
grid=grid.permute(0, 2, 3, 1),
mode='bilinear',
padding_mode='border',
align_corners=True)
def flow_warp(im, flow):
warp = torch_warp(im, flow)
return warp
def bilinearupsacling(inputfeature):
inputheight = inputfeature.size()[2]
inputwidth = inputfeature.size()[3]
outfeature = F.interpolate(
inputfeature, (inputheight * 2, inputwidth * 2), mode='bilinear', align_corners=False)
return outfeature
def bilineardownsacling(inputfeature):
inputheight = inputfeature.size()[2]
inputwidth = inputfeature.size()[3]
outfeature = F.interpolate(
inputfeature, (inputheight // 2, inputwidth // 2), mode='bilinear', align_corners=False)
return outfeature
class ResBlock(nn.Module):
def __init__(self, channel, slope=0.01, start_from_relu=True, end_with_relu=False,
bottleneck=False):
super().__init__()
self.leaky_relu = nn.LeakyReLU(negative_slope=slope)
if bottleneck:
self.conv1 = nn.Conv2d(channel, channel // 2, 3, padding=1)
self.conv2 = nn.Conv2d(channel // 2, channel, 3, padding=1)
else:
self.conv1 = nn.Conv2d(channel, channel, 3, padding=1)
self.conv2 = nn.Conv2d(channel, channel, 3, padding=1)
if start_from_relu:
self.first_layer = self.leaky_relu
else:
self.first_layer = nn.Identity()
if end_with_relu:
self.last_layer = self.leaky_relu
else:
self.last_layer = nn.Identity()
def forward(self, x):
out = self.first_layer(x)
out = self.conv1(out)
out = self.leaky_relu(out)
out = self.conv2(out)
out = self.last_layer(out)
return x + out
class MEBasic(nn.Module):
def __init__(self):
super().__init__()
self.relu = nn.ReLU()
self.conv1 = nn.Conv2d(8, 32, 7, 1, padding=3)
self.conv2 = nn.Conv2d(32, 64, 7, 1, padding=3)
self.conv3 = nn.Conv2d(64, 32, 7, 1, padding=3)
self.conv4 = nn.Conv2d(32, 16, 7, 1, padding=3)
self.conv5 = nn.Conv2d(16, 2, 7, 1, padding=3)
def forward(self, x):
x = self.relu(self.conv1(x))
x = self.relu(self.conv2(x))
x = self.relu(self.conv3(x))
x = self.relu(self.conv4(x))
x = self.conv5(x)
return x
class ME_Spynet(nn.Module):
def __init__(self):
super().__init__()
self.L = 4
self.moduleBasic = torch.nn.ModuleList([MEBasic() for _ in range(self.L)])
def forward(self, im1, im2):
batchsize = im1.size()[0]
im1_pre = im1
im2_pre = im2
im1_list = [im1_pre]
im2_list = [im2_pre]
for level in range(self.L - 1):
im1_list.append(F.avg_pool2d(im1_list[level], kernel_size=2, stride=2))
im2_list.append(F.avg_pool2d(im2_list[level], kernel_size=2, stride=2))
shape_fine = im2_list[self.L - 1].size()
zero_shape = [batchsize, 2, shape_fine[2] // 2, shape_fine[3] // 2]
flow = torch.zeros(zero_shape, dtype=im1.dtype, device=im1.device)
for level in range(self.L):
flow_up = bilinearupsacling(flow) * 2.0
img_index = self.L - 1 - level
flow = flow_up + \
self.moduleBasic[level](torch.cat([im1_list[img_index],
flow_warp(im2_list[img_index], flow_up),
flow_up], 1))
return flow