understanding the torch.nn.functional.grid_sample op by concrete example

I am debugging a neural network which has a torch.nn.functional.grid.sample operator inside. Using the Pycharm IDE, I can watch the values during debugging. My grid is a 1*15*2 tensor, here are the values in the first batchenter image description here.

My input is a 1*128*16*16 tensor, here are the values in the first channel of the first batch:enter image description here.

My output is 1*128*1*15 tensor, here are the values in the first channel of the first batchenter image description here.

align_corners = False, mode = 'bilinear', padding_mode = 'zero'.

For gird coordinates (-1,-1), I can understand that the value(-4.74179) is sampled from 4 values on the top-left corner with 3 of them being the padded '0's and 1 of them being the value '-18.96716'.(-18.96716/4 = -4.74179).

But for other grid coordinates, I am confused. Taking the value '84.65594' for example, it's corresponding grid coordinate is (-0.45302, 0.53659). I firstly convert them from (-1,1) to (0,15) by adding 1 and then dividing by 2 and then multiplying 15(see official implementation). The converted coordinate is then (4.10235, 11.524425), Upon which I see the four values that should be sampled from are :

(x)44.20010---0.10235---------(y)26.68777 | | | | | | 0.524425---(a,b)-------------------- | | | | | | (w)102.18765---------------------(z)30.03996 

here are my calculation by hand step, Let:

a = 0.10235 b = 0.524425 x = 44.20010 y = 26.68777 z = 30.03996 w = 102.18765 

The interpolated value should then be:

output = a*b*z + (1 - a)*(1 - b)*x + (1 - a)*b*w + (1-b)*a*y = 0.10235*0.524425*30.03996 + (1-0.10235)*(1-0.524425)*44.20010 + (1- 0.10235)*0.524425*102.18765 + (1-0.524425)*0.10235*26.68777 = 69.8852865171 

which isn't 84.65594, I cant't figure out how the value '84.65594' in the output is calculated, please help!

1 Answer

I answer my own question, it turns out that the inconsistency is due to the 'align_corners' flag. My way of calculation is actually under the case when 'align_corners' is true while in the program, this flag is set to be false. For how to calculate sample coordinates, please see this

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like