Spaces:
Runtime error
Runtime error
File size: 21,039 Bytes
f670afc |
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 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 |
# Copyright (C) 2021 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
#
# This work is made available under the Nvidia Source Code License-NC.
# To view a copy of this license, check out LICENSE.md
import torch
from torch.nn import init
import torch.nn as nn
import resample2d
import channelnorm
import numpy as np
from imaginaire.third_party.flow_net.flownet2.networks import flownet_c
from imaginaire.third_party.flow_net.flownet2.networks import flownet_s
from imaginaire.third_party.flow_net.flownet2.networks import flownet_sd
from imaginaire.third_party.flow_net.flownet2.networks import flownet_fusion
from imaginaire.third_party.flow_net.flownet2.networks.submodules import \
tofp16, tofp32
'Parameter count = 162,518,834'
class FlowNet2(nn.Module):
def __init__(self, args, use_batch_norm=False, div_flow=20.):
super(FlowNet2, self).__init__()
self.batch_norm = use_batch_norm
self.div_flow = div_flow
self.rgb_max = args.rgb_max
self.args = args
self.channelnorm = channelnorm.ChannelNorm()
# First Block (FlowNetC)
self.flownetc = flownet_c.FlowNetC(
args, use_batch_norm=self.batch_norm)
self.upsample1 = nn.Upsample(scale_factor=4, mode='bilinear',
align_corners=False)
self.args = args
# if args.fp16:
# self.resample1 = nn.Sequential(
# tofp32(), resample2d.Resample2d(), tofp16())
# else:
self.resample1 = resample2d.Resample2d()
# Block (FlowNetS1)
self.flownets_1 = flownet_s.FlowNetS(
args, use_batch_norm=self.batch_norm)
self.upsample2 = nn.Upsample(scale_factor=4, mode='bilinear',
align_corners=False)
# if args.fp16:
# self.resample2 = nn.Sequential(
# tofp32(), resample2d.Resample2d(), tofp16())
# else:
self.resample2 = resample2d.Resample2d()
# Block (FlowNetS2)
self.flownets_2 = flownet_s.FlowNetS(
args, use_batch_norm=self.batch_norm)
# Block (FlowNetSD)
self.flownets_d = flownet_sd.FlowNetSD(
args, use_batch_norm=self.batch_norm)
self.upsample3 = nn.Upsample(scale_factor=4, mode='nearest')
self.upsample4 = nn.Upsample(scale_factor=4, mode='nearest')
# if args.fp16:
# self.resample3 = nn.Sequential(
# tofp32(), resample2d.Resample2d(), tofp16())
# else:
self.resample3 = resample2d.Resample2d()
# if args.fp16:
# self.resample4 = nn.Sequential(
# tofp32(), resample2d.Resample2d(), tofp16())
# else:
self.resample4 = resample2d.Resample2d()
# Block (FLowNetFusion)
self.flownetfusion = flownet_fusion.FlowNetFusion(
args, use_batch_norm=self.batch_norm)
for m in self.modules():
if isinstance(m, nn.Conv2d):
if m.bias is not None:
init.uniform_(m.bias)
init.xavier_uniform_(m.weight)
if isinstance(m, nn.ConvTranspose2d):
if m.bias is not None:
init.uniform_(m.bias)
init.xavier_uniform_(m.weight)
def init_deconv_bilinear(self, weight):
f_shape = weight.size()
height, width = f_shape[-2], f_shape[-1]
f = np.ceil(width / 2.0)
c = (2 * f - 1 - f % 2) / (2.0 * f)
bilinear = np.zeros([height, width])
for x in range(width):
for y in range(height):
value = (1 - abs(x / f - c)) * (1 - abs(y / f - c))
bilinear[x, y] = value
min_dim = min(f_shape[0], f_shape[1])
weight.data.fill_(0.)
for i in range(min_dim):
weight.data[i, i, :, :] = torch.from_numpy(bilinear)
return
def forward(self, inputs):
rgb_mean = inputs.contiguous().view(inputs.size()[:2] + (-1,)).mean(
dim=-1).view(inputs.size()[:2] + (1, 1, 1,))
x = (inputs - rgb_mean) / self.rgb_max
x1 = x[:, :, 0, :, :]
x2 = x[:, :, 1, :, :]
x = torch.cat((x1, x2), dim=1)
# flownetc
flownetc_flow2 = self.flownetc(x)[0]
flownetc_flow = self.upsample1(flownetc_flow2 * self.div_flow)
# warp img1 to img0;
# magnitude of diff between img0 and and warped_img1,
if self.args.fp16:
resampled_img1 = self.resample1(tofp32()(x[:, 3:, :, :]),
flownetc_flow)
resampled_img1 = tofp16()(resampled_img1)
else:
resampled_img1 = self.resample1(x[:, 3:, :, :], flownetc_flow)
diff_img0 = x[:, :3, :, :] - resampled_img1
norm_diff_img0 = self.channelnorm(diff_img0)
# concat img0, img1, img1->img0, flow, diff-mag ;
concat1 = torch.cat(
(x, resampled_img1, flownetc_flow / self.div_flow, norm_diff_img0),
dim=1)
# flownets1
flownets1_flow2 = self.flownets_1(concat1)[0]
flownets1_flow = self.upsample2(flownets1_flow2 * self.div_flow)
# warp img1 to img0 using flownets1;
# magnitude of diff between img0 and and warped_img1
if self.args.fp16:
resampled_img1 = self.resample2(tofp32()(x[:, 3:, :, :]),
flownets1_flow)
resampled_img1 = tofp16()(resampled_img1)
else:
resampled_img1 = self.resample2(x[:, 3:, :, :], flownets1_flow)
diff_img0 = x[:, :3, :, :] - resampled_img1
norm_diff_img0 = self.channelnorm(diff_img0)
# concat img0, img1, img1->img0, flow, diff-mag
concat2 = torch.cat(
(x,
resampled_img1,
flownets1_flow /
self.div_flow,
norm_diff_img0),
dim=1)
# flownets2
flownets2_flow2 = self.flownets_2(concat2)[0]
flownets2_flow = self.upsample4(flownets2_flow2 * self.div_flow)
norm_flownets2_flow = self.channelnorm(flownets2_flow)
if self.args.fp16:
diff_flownets2_flow = self.resample4(tofp32()(x[:, 3:, :, :]),
flownets2_flow)
diff_flownets2_flow = tofp16()(diff_flownets2_flow)
else:
diff_flownets2_flow = self.resample4(x[:, 3:, :, :], flownets2_flow)
diff_flownets2_img1 = self.channelnorm(
(x[:, :3, :, :] - diff_flownets2_flow))
# flownetsd
flownetsd_flow2 = self.flownets_d(x)[0]
flownetsd_flow = self.upsample3(flownetsd_flow2 / self.div_flow)
norm_flownetsd_flow = self.channelnorm(flownetsd_flow)
if self.args.fp16:
diff_flownetsd_flow = self.resample3(tofp32()(x[:, 3:, :, :]),
flownetsd_flow)
diff_flownetsd_flow = tofp16()(diff_flownetsd_flow)
else:
diff_flownetsd_flow = self.resample3(x[:, 3:, :, :], flownetsd_flow)
diff_flownetsd_img1 = self.channelnorm(
(x[:, :3, :, :] - diff_flownetsd_flow))
# concat img1 flownetsd, flownets2, norm_flownetsd,
# norm_flownets2, diff_flownetsd_img1, diff_flownets2_img1
concat3 = torch.cat((x[:, :3, :, :], flownetsd_flow, flownets2_flow,
norm_flownetsd_flow, norm_flownets2_flow,
diff_flownetsd_img1, diff_flownets2_img1), dim=1)
flownetfusion_flow = self.flownetfusion(concat3)
return flownetfusion_flow
class FlowNet2C(flownet_c.FlowNetC):
def __init__(self, args, use_batch_norm=False, div_flow=20):
super(
FlowNet2C,
self).__init__(
args,
use_batch_norm=use_batch_norm,
div_flow=20)
self.rgb_max = args.rgb_max
def forward(self, inputs):
rgb_mean = inputs.contiguous().view(inputs.size()[:2] + (-1,)).mean(
dim=-1).view(inputs.size()[:2] + (1, 1, 1,))
x = (inputs - rgb_mean) / self.rgb_max
x1 = x[:, :, 0, :, :]
x2 = x[:, :, 1, :, :]
# FlownetC top input stream
out_conv1a = self.conv1(x1)
out_conv2a = self.conv2(out_conv1a)
out_conv3a = self.conv3(out_conv2a)
# FlownetC bottom input stream
out_conv1b = self.conv1(x2)
out_conv2b = self.conv2(out_conv1b)
out_conv3b = self.conv3(out_conv2b)
# Merge streams
out_corr = self.corr(out_conv3a, out_conv3b) # False
out_corr = self.corr_activation(out_corr)
# Redirect top input stream and concatenate
out_conv_redir = self.conv_redir(out_conv3a)
in_conv3_1 = torch.cat((out_conv_redir, out_corr), 1)
# Merged conv layers
out_conv3_1 = self.conv3_1(in_conv3_1)
out_conv4 = self.conv4_1(self.conv4(out_conv3_1))
out_conv5 = self.conv5_1(self.conv5(out_conv4))
out_conv6 = self.conv6_1(self.conv6(out_conv5))
flow6 = self.predict_flow6(out_conv6)
flow6_up = self.upsampled_flow6_to_5(flow6)
out_deconv5 = self.deconv5(out_conv6)
concat5 = torch.cat((out_conv5, out_deconv5, flow6_up), 1)
flow5 = self.predict_flow5(concat5)
flow5_up = self.upsampled_flow5_to_4(flow5)
out_deconv4 = self.deconv4(concat5)
concat4 = torch.cat((out_conv4, out_deconv4, flow5_up), 1)
flow4 = self.predict_flow4(concat4)
flow4_up = self.upsampled_flow4_to_3(flow4)
out_deconv3 = self.deconv3(concat4)
concat3 = torch.cat((out_conv3_1, out_deconv3, flow4_up), 1)
flow3 = self.predict_flow3(concat3)
flow3_up = self.upsampled_flow3_to_2(flow3)
out_deconv2 = self.deconv2(concat3)
concat2 = torch.cat((out_conv2a, out_deconv2, flow3_up), 1)
flow2 = self.predict_flow2(concat2)
if self.training:
return flow2, flow3, flow4, flow5, flow6
else:
return self.upsample1(flow2 * self.div_flow)
class FlowNet2S(flownet_s.FlowNetS):
def __init__(self, args, use_batch_norm=False, div_flow=20):
super(FlowNet2S, self).__init__(args, input_channels=6,
use_batch_norm=use_batch_norm)
self.rgb_max = args.rgb_max
self.div_flow = div_flow
def forward(self, inputs):
rgb_mean = inputs.contiguous().view(inputs.size()[:2] + (-1,)).mean(
dim=-1).view(inputs.size()[:2] + (1, 1, 1,))
x = (inputs - rgb_mean) / self.rgb_max
x = torch.cat((x[:, :, 0, :, :], x[:, :, 1, :, :]), dim=1)
out_conv1 = self.conv1(x)
out_conv2 = self.conv2(out_conv1)
out_conv3 = self.conv3_1(self.conv3(out_conv2))
out_conv4 = self.conv4_1(self.conv4(out_conv3))
out_conv5 = self.conv5_1(self.conv5(out_conv4))
out_conv6 = self.conv6_1(self.conv6(out_conv5))
flow6 = self.predict_flow6(out_conv6)
flow6_up = self.upsampled_flow6_to_5(flow6)
out_deconv5 = self.deconv5(out_conv6)
concat5 = torch.cat((out_conv5, out_deconv5, flow6_up), 1)
flow5 = self.predict_flow5(concat5)
flow5_up = self.upsampled_flow5_to_4(flow5)
out_deconv4 = self.deconv4(concat5)
concat4 = torch.cat((out_conv4, out_deconv4, flow5_up), 1)
flow4 = self.predict_flow4(concat4)
flow4_up = self.upsampled_flow4_to_3(flow4)
out_deconv3 = self.deconv3(concat4)
concat3 = torch.cat((out_conv3, out_deconv3, flow4_up), 1)
flow3 = self.predict_flow3(concat3)
flow3_up = self.upsampled_flow3_to_2(flow3)
out_deconv2 = self.deconv2(concat3)
concat2 = torch.cat((out_conv2, out_deconv2, flow3_up), 1)
flow2 = self.predict_flow2(concat2)
if self.training:
return flow2, flow3, flow4, flow5, flow6
else:
return self.upsample1(flow2 * self.div_flow)
class FlowNet2SD(flownet_sd.FlowNetSD):
def __init__(self, args, use_batch_norm=False, div_flow=20):
super(FlowNet2SD, self).__init__(args, use_batch_norm=use_batch_norm)
self.rgb_max = args.rgb_max
self.div_flow = div_flow
def forward(self, inputs):
rgb_mean = inputs.contiguous().view(inputs.size()[:2] + (-1,)).mean(
dim=-1).view(inputs.size()[:2] + (1, 1, 1,))
x = (inputs - rgb_mean) / self.rgb_max
x = torch.cat((x[:, :, 0, :, :], x[:, :, 1, :, :]), dim=1)
out_conv0 = self.conv0(x)
out_conv1 = self.conv1_1(self.conv1(out_conv0))
out_conv2 = self.conv2_1(self.conv2(out_conv1))
out_conv3 = self.conv3_1(self.conv3(out_conv2))
out_conv4 = self.conv4_1(self.conv4(out_conv3))
out_conv5 = self.conv5_1(self.conv5(out_conv4))
out_conv6 = self.conv6_1(self.conv6(out_conv5))
flow6 = self.predict_flow6(out_conv6)
flow6_up = self.upsampled_flow6_to_5(flow6)
out_deconv5 = self.deconv5(out_conv6)
concat5 = torch.cat((out_conv5, out_deconv5, flow6_up), 1)
out_interconv5 = self.inter_conv5(concat5)
flow5 = self.predict_flow5(out_interconv5)
flow5_up = self.upsampled_flow5_to_4(flow5)
out_deconv4 = self.deconv4(concat5)
concat4 = torch.cat((out_conv4, out_deconv4, flow5_up), 1)
out_interconv4 = self.inter_conv4(concat4)
flow4 = self.predict_flow4(out_interconv4)
flow4_up = self.upsampled_flow4_to_3(flow4)
out_deconv3 = self.deconv3(concat4)
concat3 = torch.cat((out_conv3, out_deconv3, flow4_up), 1)
out_interconv3 = self.inter_conv3(concat3)
flow3 = self.predict_flow3(out_interconv3)
flow3_up = self.upsampled_flow3_to_2(flow3)
out_deconv2 = self.deconv2(concat3)
concat2 = torch.cat((out_conv2, out_deconv2, flow3_up), 1)
out_interconv2 = self.inter_conv2(concat2)
flow2 = self.predict_flow2(out_interconv2)
if self.training:
return flow2, flow3, flow4, flow5, flow6
else:
return self.upsample1(flow2 * self.div_flow)
class FlowNet2CS(nn.Module):
def __init__(self, args, use_batch_norm=False, div_flow=20.):
super(FlowNet2CS, self).__init__()
self.use_batch_norm = use_batch_norm
self.div_flow = div_flow
self.rgb_max = args.rgb_max
self.args = args
self.channelnorm = channelnorm.ChannelNorm()
# First Block (FlowNetC)
self.flownetc = flownet_c.FlowNetC(
args, use_batch_norm=self.use_batch_norm)
self.upsample1 = nn.Upsample(scale_factor=4, mode='bilinear',
align_corners=False)
self.args = args
# if args.fp16:
# self.resample1 = nn.Sequential(
# tofp32(), resample2d.Resample2d(), tofp16())
# else:
self.resample1 = resample2d.Resample2d()
# Block (FlowNetS1)
self.flownets_1 = flownet_s.FlowNetS(
args, use_batch_norm=self.use_batch_norm)
self.upsample2 = nn.Upsample(scale_factor=4, mode='bilinear',
align_corners=False)
for m in self.modules():
if isinstance(m, nn.Conv2d):
if m.bias is not None:
init.uniform(m.bias)
init.xavier_uniform(m.weight)
if isinstance(m, nn.ConvTranspose2d):
if m.bias is not None:
init.uniform(m.bias)
init.xavier_uniform(m.weight)
def forward(self, inputs):
rgb_mean = inputs.contiguous().view(inputs.size()[:2] + (-1,)).mean(
dim=-1).view(inputs.size()[:2] + (1, 1, 1,))
x = (inputs - rgb_mean) / self.rgb_max
x1 = x[:, :, 0, :, :]
x2 = x[:, :, 1, :, :]
x = torch.cat((x1, x2), dim=1)
# flownetc
flownetc_flow2 = self.flownetc(x)[0]
flownetc_flow = self.upsample1(flownetc_flow2 * self.div_flow)
# warp img1 to img0;
# magnitude of diff between img0 and and warped_img1,
if self.args.fp16:
resampled_img1 = self.resample1(tofp32()(x[:, 3:, :, :]),
flownetc_flow)
resampled_img1 = tofp16()(resampled_img1)
else:
resampled_img1 = self.resample1(x[:, 3:, :, :], flownetc_flow)
diff_img0 = x[:, :3, :, :] - resampled_img1
norm_diff_img0 = self.channelnorm(diff_img0)
# concat img0, img1, img1->img0, flow, diff-mag ;
concat1 = torch.cat(
(x, resampled_img1, flownetc_flow / self.div_flow, norm_diff_img0),
dim=1)
# flownets1
flownets1_flow2 = self.flownets_1(concat1)[0]
flownets1_flow = self.upsample2(flownets1_flow2 * self.div_flow)
return flownets1_flow
class FlowNet2CSS(nn.Module):
def __init__(self, args, use_batch_norm=False, div_flow=20.):
super(FlowNet2CSS, self).__init__()
self.use_batch_norm = use_batch_norm
self.div_flow = div_flow
self.rgb_max = args.rgb_max
self.args = args
self.channelnorm = channelnorm.ChannelNorm()
# First Block (FlowNetC)
self.flownetc = flownet_c.FlowNetC(
args, use_batch_norm=self.use_batch_norm)
self.upsample1 = nn.Upsample(scale_factor=4, mode='bilinear',
align_corners=False)
self.args = args
# if args.fp16:
# self.resample1 = nn.Sequential(
# tofp32(), resample2d.Resample2d(), tofp16())
# else:
self.resample1 = resample2d.Resample2d()
# Block (FlowNetS1)
self.flownets_1 = flownet_s.FlowNetS(
args, use_batch_norm=self.use_batch_norm)
self.upsample2 = nn.Upsample(scale_factor=4, mode='bilinear',
align_corners=False)
# if args.fp16:
# self.resample2 = nn.Sequential(
# tofp32(), resample2d.Resample2d(), tofp16())
# else:
self.resample2 = resample2d.Resample2d()
# Block (FlowNetS2)
self.flownets_2 = flownet_s.FlowNetS(
args, use_batch_norm=self.use_batch_norm)
self.upsample3 = nn.Upsample(scale_factor=4, mode='nearest',
align_corners=False)
for m in self.modules():
if isinstance(m, nn.Conv2d):
if m.bias is not None:
init.uniform(m.bias)
init.xavier_uniform(m.weight)
if isinstance(m, nn.ConvTranspose2d):
if m.bias is not None:
init.uniform(m.bias)
init.xavier_uniform(m.weight)
def forward(self, inputs):
rgb_mean = inputs.contiguous().view(inputs.size()[:2] + (-1,)).mean(
dim=-1).view(inputs.size()[:2] + (1, 1, 1,))
x = (inputs - rgb_mean) / self.rgb_max
x1 = x[:, :, 0, :, :]
x2 = x[:, :, 1, :, :]
x = torch.cat((x1, x2), dim=1)
# flownetc
flownetc_flow2 = self.flownetc(x)[0]
flownetc_flow = self.upsample1(flownetc_flow2 * self.div_flow)
# Warp img1 to img0;
# Magnitude of diff between img0 and and warped_img1,
if self.args.fp16:
resampled_img1 = self.resample1(tofp32()(x[:, 3:, :, :]),
flownetc_flow)
resampled_img1 = tofp16()(resampled_img1)
else:
resampled_img1 = self.resample1(x[:, 3:, :, :], flownetc_flow)
diff_img0 = x[:, :3, :, :] - resampled_img1
norm_diff_img0 = self.channelnorm(diff_img0)
# concat img0, img1, img1->img0, flow, diff-mag ;
concat1 = torch.cat(
(x, resampled_img1, flownetc_flow / self.div_flow, norm_diff_img0),
dim=1)
# flownets1
flownets1_flow2 = self.flownets_1(concat1)[0]
flownets1_flow = self.upsample2(flownets1_flow2 * self.div_flow)
# Warp img1 to img0 using flownets1;
# magnitude of diff between img0 and and warped_img1
if self.args.fp16:
resampled_img1 = self.resample2(tofp32()(x[:, 3:, :, :]),
flownets1_flow)
resampled_img1 = tofp16()(resampled_img1)
else:
resampled_img1 = self.resample2(x[:, 3:, :, :], flownets1_flow)
diff_img0 = x[:, :3, :, :] - resampled_img1
norm_diff_img0 = self.channelnorm(diff_img0)
# concat img0, img1, img1->img0, flow, diff-mag
concat2 = torch.cat(
(x,
resampled_img1,
flownets1_flow /
self.div_flow,
norm_diff_img0),
dim=1)
# flownets2
flownets2_flow2 = self.flownets_2(concat2)[0]
flownets2_flow = self.upsample3(flownets2_flow2 * self.div_flow)
return flownets2_flow
|