GridSample

使用包含像素坐标的网格张量,将输入张量插值到输出张量中。

像素坐标从输入维度范围归一化到 \([-1,1]\) 范围。对于输入范围之外的值,使用 sample_mode 来确定用于插值的值。

属性

sample_mode 控制如何评估越界网格位置

  • FILL 越界网格位置被赋值为 0。

  • CLAMP 越界网格位置被赋值为最近的边界值。

  • REFLECT 越界网格位置通过输入的边界反射,直到变为界内。

align_corners 控制如何评估角像素。

interpolation_mode 控制使用哪种插值类型

  • NEAREST 最近邻插值。

  • LINEAR 双线性插值。

  • CUBIC 双三次插值。

输入

input: T1 类型的张量

grid: T1 类型的张量

输出

output: T1 类型的张量

数据类型

T1: float16, float32, bfloat16

形状信息

输入形状应为 \([N, C, H, W]\)

网格形状应为 \([N, H_{out}, W_{out}, 2]\)

输出形状应为 \([N, C, H_{out}, W_{out}]\)

体积限制

inputgridoutput 最多可以有 \(2^{31}-1\) 个元素。

示例

input = network.add_input("input", dtype=trt.float32, shape=(1, 1, 3, 3))
grid = network.add_input("grid", dtype=trt.float32, shape=(1, 4, 3, 2))
layer = network.add_grid_sample(input, grid)
layer.align_corners = 0
layer.interpolation_mode = trt.InterpolationMode.LINEAR
layer.sample_mode = trt.SampleMode.FILL
network.mark_output(layer.get_output(0))

inputs[input.name] = np.array([[[[0, 1, 2], [3, 4, 5], [6, 7, 8]]]])

inputs[grid.name] = np.array(
    [
        [
            [[-2.0, -1.83], [-1.67, -1.5], [-1.33, -1.17]],
            [[-1.0, -0.83], [-0.67, -0.5], [-0.33, -0.17]],
            [[0.0, 0.17], [0.33, 0.5], [0.67, 0.83]],
            [[1.0, 1.17], [1.33, 1.5], [1.67, 1.83]],
        ]
    ]
)

outputs[layer.get_output(0).name] = layer.get_output(0).shape
expected[layer.get_output(0).name] = np.array(
    [[[[0.0, 0.0, 0.0], [0.0, 0.746, 2.74], [4.764, 6.744, 6.009], [0.979, 0.0, 0.0]]]]
)

C++ API

有关 C++ IGridSampleLayer 算子的更多信息,请参阅 C++ IGridSampleLayer 文档

Python API

有关 Python IGridSampleLayer 算子的更多信息,请参阅 Python IGridSampleLayer 文档