r"""Randomly shift the input tensor. Args: x (4D tensor): The input batch of images. offset (int): The maximum offset ratio that is between [0, 1]. The maximum shift is offset * image_size for each direction. mode (str): The resample mode for 'F.grid_sample'.
(x, offset=0.05, mode='bilinear', padding_mode='reflection')
| 175 | |
| 176 | |
| 177 | def random_shift(x, offset=0.05, mode='bilinear', padding_mode='reflection'): |
| 178 | r"""Randomly shift the input tensor. |
| 179 | |
| 180 | Args: |
| 181 | x (4D tensor): The input batch of images. |
| 182 | offset (int): The maximum offset ratio that is between [0, 1]. |
| 183 | The maximum shift is offset * image_size for each direction. |
| 184 | mode (str): The resample mode for 'F.grid_sample'. |
| 185 | padding_mode (str): The padding mode for 'F.grid_sample'. |
| 186 | |
| 187 | Returns: |
| 188 | x (4D tensor) : The randomly shifted image. |
| 189 | """ |
| 190 | assert x.dim() == 4, "Input must be a 4D tensor." |
| 191 | batch_size = x.size(0) |
| 192 | theta = torch.eye(2, 3, device=x.device).unsqueeze(0).repeat( |
| 193 | batch_size, 1, 1) |
| 194 | theta[:, :, 2] = 2 * offset * torch.rand(batch_size, 2) - offset |
| 195 | grid = F.affine_grid(theta, x.size()) |
| 196 | x = F.grid_sample(x, grid, mode=mode, padding_mode=padding_mode) |
| 197 | return x |
| 198 | |
| 199 | |
| 200 | def truncated_gaussian(threshold, size, seed=None, device=None): |
nothing calls this directly
no outgoing calls
no test coverage detected