Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

请教一下grid sample的grid最后一维顺序问题 #7

Open
bu1lder opened this issue Jul 11, 2023 · 9 comments
Open

请教一下grid sample的grid最后一维顺序问题 #7

bu1lder opened this issue Jul 11, 2023 · 9 comments

Comments

@bu1lder
Copy link

bu1lder commented Jul 11, 2023

torch.nn.functional.grid_sample的输入grid疑问,交换顺序之前是xy,交换之后是yx,交换之后是否顺序反了?

参数grid中的最后一维的顺序为( x , y ) (x,y)(x,y),但输入图像大小( H , W ) (H,W)(H,W)对应( y _ s i z e , x _ s i z e ) (y_size,x_size)(y_size,x_size)
参考的

@sczhou21
Copy link
Collaborator

您好,交换顺序前gt_box的最后一维顺序为(W,H), 而grid_sample要求是(H, W), 所以需要交换顺序

@bu1lder
Copy link
Author

bu1lder commented Jul 12, 2023

grid中(H, W)是中间二维吧,代码交换的是最后一维
给定 维度为(N,C,Hin,Win) 的input,维度为(N,Hout,Wout,2) 的grid,则该函数output的维度为(N,C,Hout,Wout)。
grid最后一维 2 的顺序为( x , y ),但input大小(Hin,Win)对应( y_size, x_size )
例如
xx = torch.tensor([[[
[1,2,3,4,5,6,7,8,9],
[2,4,6,8,10,12,14,16,18],
[3,6,9,12,15,18,21,24,27],
[4,8,12,16,20,24,28,32,36],
[5,10,15,20,25,30,35,40,45]]]]).float() # 1,1,5,9
grid = torch.tensor([[[[-1,-1],[-1,0],[-1,1],[0,-1],[0,0],[0,1],[1,-1],[1,0],[1,1]]]]).float() # 1,1,9,2
xxx = torch.nn.functional.grid_sample(xx,grid, mode='nearest') #
print(xxx)
输出:tensor([[[[ 1., 3., 5., 5., 15., 25., 9., 27., 45.]]]]) # 1,1,1,9

所以交换最后一维之后会导致采样的点位置错了
grid[:, :, :, [0, 1]] = grid[:, :, :, [1, 0]]
xxx = torch.nn.functional.grid_sample(xx,grid, mode='nearest')
print(xxx)
输出:tensor([[[[ 1., 5., 9., 3., 15., 27., 5., 25., 45.]]]]) # 1,1,1,9

@sczhou21
Copy link
Collaborator

如果我没记错的话,gt_boxes_bev_all在没交换最后一维前的格式是(batch_size, box个数, 9, (W,H)),交换最后一维是需要的

@bu1lder
Copy link
Author

bu1lder commented Jul 12, 2023

如果我没记错的话,gt_boxes_bev_all在没交换最后一维前的格式是(batch_size, box个数, 9, (W,H)),交换最后一维是需要的

最后一维的顺序是(W,H),对应的是input的(Hin,Win)
你看看我上面的例子,(Hin,Win)是(5,9),grid [-1,0]对应的是3,说明-1对应的是Win,输入就是按(W,H)这个顺序,不用交换了

@bu1lder
Copy link
Author

bu1lder commented Jul 12, 2023

(-1,0)对应的是第3行第1列的3,不是第1行第3列的3,这边-1对应的还是Hin

Hin是5,Win是9,-1的意思就是第一列,不就是对应的Win吗
同样的[1,0]对应的是27,第三行第九列,这个1对应的就是第九列,就是Win的size

@sczhou21
Copy link
Collaborator

不好意思有些时间没有梳理这个代码了,具体细节有点记不太清楚,也有可能是在开源的时候重新编写代码引入的bug,我后续check一下,谢谢指出问题

@lihuashengmax
Copy link

附议,我也觉得有问题

@smalltoyfox
Copy link

附议

@smalltoyfox
Copy link

还有一个问题,在后面输入进入torch.nn.functional.grid_sample之前,grid应该归一化吧

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants