How to generate a random displacement field followed by gaussian and trainable?

I am using grid_sample function, that

torch.nn.functional.grid_sample(input, grid, mode='bilinear', padding_mode='zeros')

I want to construct a random grid and it trained with the network. The scrip likes

class Network(nn.Module):
    def __init__(self):
          self.rnd_grid_tensor = ... (required_grad=True)

   def forward(self,input):
        output = torch.nn.functional.grid_sample(input, self.rnd_grid_tensor, mode='bilinear', padding_mode='zeros')
        return output

How to generate the rnd_grid_tensor so that the value in the grid followed by gaussian distribution. Thanks

This is my solution but I am not sure is it correct or not

from torch.autograd import Variable
def gaussian(ins, is_training, mean, stddev):
  if is_training:
      noise = Variable(, stddev))
      return ins + noise
  return ins

grid = torch.rand((1,2,16,16), requires_grad=True)
grid = gaussian (grid, True, 1, 0.1)

Do you want a random displacement field where the displacements follow a Gaussian distribution?

If so, what you need to do is generate a tensor with the displacements

disp_field = torch.FloatTensor(1,2,16,16).normal_(mean, std).requires_grad_()

Then add it into an identity grid, which you can create using affine_grid():

id_grid = torch.nn.functional.affine_grid(torch.FloatTensor([[[1, 0, 0],[0, 1, 0]]]), size=(1,2,16,16))

and then pass the sum to grid_sample():

output = torch.nn.functional.grid_sample(input, id_grid + disp_field)

Just note that the mean and standard deviation will be in the grid units of a half-image (ie. a displacement of 1 will be equivalent to (size - 1)/2 pixels).
So if you have the mean and std in pixels, you have to divide them both by (size - 1)/2.

I hope this helps.

1 Like