pytorch中torch.nn.functional.grid_sample()函数通俗易懂的理解

def grid_sample(input, grid, mode='bilinear', padding_mode='zeros', align_corners=None)

pytorch中torch.nn.functional.grid_sample()函数通俗易懂的理解_第1张图片
函数作用是将input图像特征体通过grid映射到output图像特征体上

  • input:[B, C, H_in, W_in]
  • grid: [B, H_out, W_out, 2]
  • output: [B, C, H_out, W_out]

grid中最后一个维度的2表示在input中的相对索引位置(offset),函数的内部主要执行几件事:

  1. 遍历output图像的所有像素坐标
  2. 比如现在要求output中的(5, 5)坐标的特征向量,若通过查找grid中(5, 5)位置中的offset值为(0.1, 0.2)
  3. 根据 [ o f f s e t x + 1 2 ( W i n − 1 ) , o f f s e t y + 1 2 ( H i n − 1 ) ] [\frac{offset_{x} + 1}{2} (W_{in}-1), \frac{offset_y + 1}{2}(H_{in}-1)] [2offsetx+1(Win1),2offsety+1(Hin1)]即: [ 0.1 + 1 2 ( W i n − 1 ) , 0.2 + 1 2 ( H i n − 1 ) ] [\frac{0.1 + 1}{2} (W_{in}-1), \frac{0.2 + 1}{2}(H_{in}-1)] [20.1+1(Win1),20.2+1(Hin1)]得到到对应input图像上的位置坐标。
  4. 通过双线性插值得到该点的特征向量。
  5. 将该特征向量copy到output图像的(5, 5)位置

注:grid中的offset坐标必须是归一化到[-1, 1]的坐标: o f f s e t x = x ( W i n − 1 ) / 2 − 1 o f f s e t y = y ( H i n − 1 ) / 2 − 1 offset_x = \frac{x}{(W_{in} - 1)/2} -1\\\qquad\\offset_y = \frac{y}{(H_{in} -1)/2}-1 offsetx=(Win1)/2x1offsety=(Hin1)/2y1

你可能感兴趣的:(pytorch踩坑日记,pytorch,深度学习,python)