| 38 | |
| 39 | |
| 40 | def coords_grid(b, h, w, homogeneous=False, device=None): |
| 41 | y, x = torch.meshgrid(torch.arange(h), torch.arange(w)) # [H, W] |
| 42 | |
| 43 | stacks = [x, y] |
| 44 | |
| 45 | if homogeneous: |
| 46 | ones = torch.ones_like(x) # [H, W] |
| 47 | stacks.append(ones) |
| 48 | |
| 49 | grid = torch.stack(stacks, dim=0).float() # [2, H, W] or [3, H, W] |
| 50 | |
| 51 | grid = grid[None].repeat(b, 1, 1, 1) # [B, 2, H, W] or [B, 3, H, W] |
| 52 | |
| 53 | if device is not None: |
| 54 | grid = grid.to(device) |
| 55 | |
| 56 | return grid |
| 57 | |
| 58 | |
| 59 | def bilinear_sample(img, sample_coords, mode="bilinear", padding_mode="zeros", return_mask=False): |