MCPcopy Create free account
hub / github.com/OpenTalker/StyleHEAT / random_shift

Function random_shift

utils/misc.py:177–197  ·  view source on GitHub ↗

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

Source from the content-addressed store, hash-verified

175
176
177def 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
200def truncated_gaussian(threshold, size, seed=None, device=None):

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected