RAFT icon indicating copy to clipboard operation
RAFT copied to clipboard

Backward Warp (use flow_t->t+1 and frame_t+1 to recover frame_t to ensure no undefined area)

Open lxa9867 opened this issue 4 years ago • 2 comments

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).

lxa9867 avatar Dec 21 '20 02:12 lxa9867

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.

mingqizhang avatar Dec 21 '20 10:12 mingqizhang