| 6 | |
| 7 | |
| 8 | def warp(tenInput, tenFlow): |
| 9 | k = (str(tenFlow.device), str(tenFlow.size())) |
| 10 | if k not in backwarp_tenGrid: |
| 11 | tenHorizontal = ( |
| 12 | torch.linspace(-1.0, 1.0, tenFlow.shape[3], device=device) |
| 13 | .view(1, 1, 1, tenFlow.shape[3]) |
| 14 | .expand(tenFlow.shape[0], -1, tenFlow.shape[2], -1) |
| 15 | ) |
| 16 | tenVertical = ( |
| 17 | torch.linspace(-1.0, 1.0, tenFlow.shape[2], device=device) |
| 18 | .view(1, 1, tenFlow.shape[2], 1) |
| 19 | .expand(tenFlow.shape[0], -1, -1, tenFlow.shape[3]) |
| 20 | ) |
| 21 | backwarp_tenGrid[k] = torch.cat([tenHorizontal, tenVertical], 1).to(device) |
| 22 | |
| 23 | tenFlow = torch.cat( |
| 24 | [ |
| 25 | tenFlow[:, 0:1, :, :] / ((tenInput.shape[3] - 1.0) / 2.0), |
| 26 | tenFlow[:, 1:2, :, :] / ((tenInput.shape[2] - 1.0) / 2.0), |
| 27 | ], |
| 28 | 1, |
| 29 | ) |
| 30 | |
| 31 | g = (backwarp_tenGrid[k] + tenFlow).permute(0, 2, 3, 1) |
| 32 | return torch.nn.functional.grid_sample( |
| 33 | input=tenInput, grid=g, mode="bilinear", padding_mode="border", align_corners=True |
| 34 | ) |