RAFT
RAFT copied to clipboard
Backward Warp (use flow_t->t+1 and frame_t+1 to recover frame_t to ensure no undefined area)
Hello, thank you for sharing such a good work. I am now working to backward warp the frame to construct temporal consistency (use flow_t->t+1 and frame_t+1 to recover frame_t to ensure no undefined area). Is there a good method to warp it? I am not sure how to get the backward flow using forward flow in a efficient way (My implementation takes too long time).
def warp(x, flo):
"""
warp an image/tensor (im2) back to im1, according to the optical flow
x: [B, C, H, W] (im2)
flo: [B, 2, H, W] flow
"""
B, C, H, W = x.size()
# mesh grid
xx = torch.arange(0, W).view(1, -1).repeat(H, 1)
yy = torch.arange(0, H).view(-1, 1).repeat(1, W)
xx = xx.view(1, 1, H, W).repeat(B, 1, 1, 1)
yy = yy.view(1, 1, H, W).repeat(B, 1, 1, 1)
grid = torch.cat((xx, yy), 1).float()
if x.is_cuda:
grid = grid.cuda()
vgrid = grid + flo
# scale grid to [-1,1]
vgrid[:, 0, :, :] = 2.0 * vgrid[:, 0, :, :].clone() / max(W - 1, 1) - 1.0
vgrid[:, 1, :, :] = 2.0 * vgrid[:, 1, :, :].clone() / max(H - 1, 1) - 1.0
vgrid = vgrid.permute(0, 2, 3, 1)
output = F.grid_sample(x, vgrid)
mask = torch.ones(x.size()).to(DEVICE)
mask = F.grid_sample(mask, vgrid)
mask[mask < 0.999] = 0
mask[mask > 0] = 1
return output
Maybe this code can help you.