def forward(self, x, flow): x = self.conv1(x) flow = F.interpolate(flow, scale_factor=0.5, mode="bilinear", align_corners=False, recompute_scale_factor=False) * 0.5 f1 = warp(x, flow) x = self.conv2(x) flow = F.interpolate(flow, scale_factor=0.5, mode="bilinear", align_corners=False, recompute_scale_factor=False) * 0.5 f2 = warp(x, flow) x = self.conv3(x) flow = F.interpolate(flow, scale_factor=0.5, mode="bilinear", align_corners=False, recompute_scale_factor=False) * 0.5 f3 = warp(x, flow) x = self.conv4(x) flow = F.interpolate(flow, scale_factor=0.5, mode="bilinear", align_corners=False, recompute_scale_factor=False) * 0.5 f4 = warp(x, flow) return [f1, f2, f3, f4]
def forward(self, x): flow0 = self.block0(x) F1 = flow0 F1_large = F.interpolate(F1, scale_factor=2.0, mode="bilinear", align_corners=False) * 2.0 warped_img0 = warp(x[:, :3], F1_large[:, :2]) warped_img1 = warp(x[:, 3:], F1_large[:, 2:4]) flow1 = self.block1(torch.cat((warped_img0, warped_img1, F1_large), 1)) F2 = (flow0 + flow1) F2_large = F.interpolate(F2, scale_factor=2.0, mode="bilinear", align_corners=False) * 2.0 warped_img0 = warp(x[:, :3], F2_large[:, :2]) warped_img1 = warp(x[:, 3:], F2_large[:, 2:4]) flow2 = self.block2(torch.cat((warped_img0, warped_img1, F2_large), 1)) F3 = (flow0 + flow1 + flow2) return F3, [F1, F2, F3]
def forward(self, x): x = F.interpolate(x, scale_factor=0.5, mode="bilinear", align_corners=False) flow0 = self.block0(x) F1 = flow0 warped_img0 = warp(x[:, :3], F1, self.cFlag) warped_img1 = warp(x[:, 3:], -F1, self.cFlag) flow1 = self.block1(torch.cat((warped_img0, warped_img1, F1), 1)) F2 = (flow0 + flow1) warped_img0 = warp(x[:, :3], F2, self.cFlag) warped_img1 = warp(x[:, 3:], -F2, self.cFlag) flow2 = self.block2(torch.cat((warped_img0, warped_img1, F2), 1)) F3 = (flow0 + flow1 + flow2) return F3, [F1, F2, F3]
def forward(self, img0, img1, flow, c0, c1, flow_gt): warped_img0 = warp(img0, flow[:, :2]) warped_img1 = warp(img1, flow[:, 2:4]) if flow_gt == None: warped_img0_gt, warped_img1_gt = None, None else: warped_img0_gt = warp(img0, flow_gt[:, :2]) warped_img1_gt = warp(img1, flow_gt[:, 2:4]) s0 = self.down0(torch.cat((img0, img1, warped_img0, warped_img1), 1)) s1 = self.down1(torch.cat((s0, c0[0], c1[0]), 1)) s2 = self.down2(torch.cat((s1, c0[1], c1[1]), 1)) s3 = self.down3(torch.cat((s2, c0[2], c1[2]), 1)) x = self.up0(torch.cat((s3, c0[3], c1[3]), 1)) x = self.up1(torch.cat((x, s2), 1)) x = self.up2(torch.cat((x, s1), 1)) x = self.up3(torch.cat((x, s0), 1)) x = self.conv(x) return x, warped_img0, warped_img1, warped_img0_gt, warped_img1_gt
def forward(self, x, UHD=False): if UHD: x = F.interpolate(x, scale_factor=0.25, mode="bilinear", align_corners=False) else: x = F.interpolate(x, scale_factor=0.5, mode="bilinear", align_corners=False) flow0 = self.block0(x) F1 = flow0 warped_img0 = warp(x[:, :3], F1) warped_img1 = warp(x[:, 3:], -F1) flow1 = self.block1(torch.cat((warped_img0, warped_img1, F1), 1)) F2 = (flow0 + flow1) warped_img0 = warp(x[:, :3], F2) warped_img1 = warp(x[:, 3:], -F2) flow2 = self.block2(torch.cat((warped_img0, warped_img1, F2), 1)) F3 = (flow0 + flow1 + flow2) warped_img0 = warp(x[:, :3], F3) warped_img1 = warp(x[:, 3:], -F3) flow3 = self.block3(torch.cat((warped_img0, warped_img1, F3), 1)) F4 = (flow0 + flow1 + flow2 + flow3) return F4, [F1, F2, F3, F4]
def forward(self, x, scale=1.0): if scale != 1.0: x = F.interpolate(x, scale_factor=scale, mode="bilinear", align_corners=False) flow0 = self.block0(x) F1 = flow0 F1_large = F.interpolate( F1, scale_factor=2.0, mode="bilinear", align_corners=False) * 2.0 warped_img0 = warp(x[:, :3], F1_large[:, :2]) warped_img1 = warp(x[:, 3:], F1_large[:, 2:4]) flow1 = self.block1(torch.cat((warped_img0, warped_img1, F1_large), 1)) F2 = (flow0 + flow1) F2_large = F.interpolate( F2, scale_factor=2.0, mode="bilinear", align_corners=False) * 2.0 warped_img0 = warp(x[:, :3], F2_large[:, :2]) warped_img1 = warp(x[:, 3:], F2_large[:, 2:4]) flow2 = self.block2(torch.cat((warped_img0, warped_img1, F2_large), 1)) F3 = (flow0 + flow1 + flow2) F3_large = F.interpolate( F3, scale_factor=2.0, mode="bilinear", align_corners=False) * 2.0 warped_img0 = warp(x[:, :3], F3_large[:, :2]) warped_img1 = warp(x[:, 3:], F3_large[:, 2:4]) flow3 = self.block3(torch.cat((warped_img0, warped_img1, F3_large), 1)) F4 = (flow0 + flow1 + flow2 + flow3) if scale != 1.0: F4 = F.interpolate(F4, scale_factor=1 / scale, mode="bilinear", align_corners=False) / scale return F4, [F1, F2, F3, F4]
def forward(self, x, scale=[4, 2, 1], timestep=0.5): img0 = x[:, :3] img1 = x[:, 3:6] gt = x[:, 6:] # In inference time, gt is None flow_list = [] merged = [] mask_list = [] warped_img0 = img0 warped_img1 = img1 flow = None loss_distill = 0 stu = [self.block0, self.block1, self.block2] for i in range(3): if flow != None: flow_d, mask_d = stu[i](torch.cat( (img0, img1, warped_img0, warped_img1, mask), 1), flow, scale=scale[i]) flow = flow + flow_d mask = mask + mask_d else: flow, mask = stu[i](torch.cat((img0, img1), 1), None, scale=scale[i]) mask_list.append(torch.sigmoid(mask)) flow_list.append(flow) warped_img0 = warp(img0, flow[:, :2]) warped_img1 = warp(img1, flow[:, 2:4]) merged_student = (warped_img0, warped_img1) merged.append(merged_student) if gt.shape[1] == 3: flow_d, mask_d = self.block_tea(torch.cat( (img0, img1, warped_img0, warped_img1, mask, gt), 1), flow, scale=1) flow_teacher = flow + flow_d warped_img0_teacher = warp(img0, flow_teacher[:, :2]) warped_img1_teacher = warp(img1, flow_teacher[:, 2:4]) mask_teacher = torch.sigmoid(mask + mask_d) merged_teacher = warped_img0_teacher * mask_teacher + warped_img1_teacher * ( 1 - mask_teacher) else: flow_teacher = None merged_teacher = None for i in range(3): merged[i] = merged[i][0] * mask_list[i] + merged[i][1] * ( 1 - mask_list[i]) if gt.shape[1] == 3: loss_mask = ((merged[i] - gt).abs().mean(1, True) > (merged_teacher - gt).abs().mean(1, True) + 0.01).float().detach() loss_distill += ((flow_teacher.detach() - flow_list[i]).abs() * loss_mask).mean() c0 = self.contextnet(img0, flow[:, :2]) c1 = self.contextnet(img1, flow[:, 2:4]) tmp = self.unet(img0, img1, warped_img0, warped_img1, mask, flow, c0, c1) res = tmp[:, :3] * 2 - 1 merged[2] = torch.clamp(merged[2] + res, 0, 1) return flow_list, mask_list[ 2], merged, flow_teacher, merged_teacher, loss_distill