Skip to content

Instantly share code, notes, and snippets.

@zzilch
Last active July 24, 2023 07:26
Show Gist options
  • Save zzilch/4c02fc1e240b703c4981d72cea53f679 to your computer and use it in GitHub Desktop.
Save zzilch/4c02fc1e240b703c4981d72cea53f679 to your computer and use it in GitHub Desktop.
torch.nn.function.grid_sample
import torch
input = torch.tensor([1,2,3,4]).view(1,1,2,2).float()
"""
Array:A[row,col]=p: H x W -> C
Image:I(x,y)=p: W x H -> C
or [0,1]^2 -> C
or [-1,1]^2 -> C
0.....W
.|1|2|
.|3|4|
H
"""
grid = torch.tensor([
[1,1],
[0,1],
[1,0],
[0,0]]
).view(1,2,2,2).float()
"""
Grid: G[row,col]=(x,y): H x W -> W x H
0.............W
.|(1,1)|(0,1)|
.|(1,0)|(0,0)|
H
"""
grid = grid.mul(2).sub(1)
"""
Grid: G[row,col]=(x,y): H x W -> [-1,1]^2
0..................W
.|( 1, 1) |(-1, 1)|
.|( 1,-1) |(-1,-1)|
H
"""
sampled = torch.nn.functional.grid_sample(input,grid,padding_mode='zeros')
"""
O[row,col] = interpolate( I(G[row,col]) )
= interpolate( I(x,y) )
= interpolate( A[y,x] )
input
-1.....1
.|1|2|
.|3|4|
1
grid
0..................W
.|( 1, 1) |(-1, 1)|
.|( 1,-1) |(-1,-1)|
H
output: bilinear interpolation
-1.....1
.|4|3|
.|2|1|
1
"""
grid = torch.tensor([
[0.5,0.5],
[ 0,0.5],
[0.5, 0],
[ 0, 0]]
).view(1,2,2,2).float().mul(2).sub(1)
"""
Grid: G(row,col)=(x,y): H x W -> W x H
0.................W
.|( 0, 0)|(-1, 0)|
.|( 0,-1)|(-1,-1)|
H
"""
sampled = torch.nn.functional.grid_sample(input,grid,padding_mode='zeros')
"""
O[row,col] = interpolate( I(G[row,col]) )
= interpolate( I(x,y) )
= interpolate( A[y,x] )
input
-1.....1
.|1|2|
.|3|4|
1
grid
0.................W
.|( 0, 0)|(-1, 0)|
.|( 0,-1)|(-1,-1)|
H
output: bilinear interpolation
-1.....1
.|2.5|2.0| 2.5=(1+2+3+4)/4 2.0=(1+3)/2
.|1.5|1.0| 1.5=(1+3)/2 1.0=1/1
1
"""
@ljjTYJR
Copy link

ljjTYJR commented Jul 23, 2023

It seems that the result does not correspond to the real code ?

@zzilch
Copy link
Author

zzilch commented Jul 24, 2023

It seems that the result does not correspond to the real code ?

This was written when pytorch version is 1.0. The default behavior up to version 1.2.0 was align_corners = True. See this https://pytorch.org/docs/stable/generated/torch.nn.functional.grid_sample.html,

@ljjTYJR
Copy link

ljjTYJR commented Jul 24, 2023

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment