Args: feat (torch.Tensor): [B, C, H, W] image features uv (torch.Tensor): [B, 2, N] uv coordinates in the image plane, range [-1, 1] Returns: samples[:, :, :, 0] (torch.Tensor): [B, C, N] image features at the uv coordinates
(feat, uv)
| 135 | |
| 136 | |
| 137 | def interpolate(feat, uv): |
| 138 | """ |
| 139 | Args: |
| 140 | feat (torch.Tensor): [B, C, H, W] image features |
| 141 | uv (torch.Tensor): [B, 2, N] uv coordinates |
| 142 | in the image plane, range [-1, 1] |
| 143 | Returns: |
| 144 | samples[:, :, :, 0] (torch.Tensor): |
| 145 | [B, C, N] image features at the uv coordinates |
| 146 | """ |
| 147 | if uv.shape[-1] != 2: |
| 148 | uv = uv.transpose(1, 2) # [B, N, 2] |
| 149 | uv = uv.unsqueeze(2) # [B, N, 1, 2] |
| 150 | # NOTE: for newer PyTorch, it seems that training |
| 151 | # results are degraded due to implementation diff in F.grid_sample |
| 152 | # for old versions, simply remove the aligned_corners argument. |
| 153 | if int(torch.__version__.split('.')[1]) < 4: |
| 154 | samples = torch.nn.functional.grid_sample(feat, uv) # [B, C, N, 1] |
| 155 | else: |
| 156 | samples = torch.nn.functional.grid_sample( |
| 157 | feat, uv, align_corners=True) # [B, C, N, 1] |
| 158 | return samples[:, :, :, 0] # [B, C, N] |
| 159 | |
| 160 | |
| 161 | def _softmax(tensor, temperature, dim=-1): |
no outgoing calls
no test coverage detected