GridSample

Interpolates an input tensor into an output tensor using a grid tensor containing pixel coordinates.

The pixel coordinates are normalized from the input dimensions range into the \([-1,1]\) range. For values outside the input range, sample_mode is used to determine the value to use for the interpolation.

Attributes

sample_mode Controls how out-of-bound grid locations are evaluated:

  • FILL Out-of-bound grid locations are assigned to 0.

  • CLAMP Out-of-bound grid locations are assigned the nearest border value.

  • REFLECT Out-of-bound grid locations are reflected by the input’s borders until becoming inbounds.

align_corners Controls how corner pixels are evaluated.

interpolation_mode Controls what kind of interpolation is used:

  • NEAREST Nearest interpolation.

  • LINEAR Bilinear interpolation.

  • CUBIC Bicubic interpolation.

Inputs

input: tensor of type T1

grid: tensor of type T1

Outputs

output: tensor of type T1

Data Types

T1: float16, float32, bfloat16

Shape Information

Input shape should be of \([N, C, H, W]\)

Grid shape should be of \([N, H_out, W_out, 2]\)

Output shape should be of \([N, C, H_out, W_out]\)

Examples

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

For more information about the C++ IGridSampleLayer operator, refer to the C++ IGridSampleLayer documentation

Python API

For more information about the Python IGridSampleLayer operator, refer to the Python IGridSampleLayer documentation