Resize

Resize an input tensor into an output tensor. The output shape is either set directly using shape or calculated by the scales parameters.

Attributes

resize_mode Controls what kind of resize is used, defaults to NEAREST.

  • NEAREST Nearest neighbor resizing on the innermost m dimensions of an input of rank N where \(0\leq m \leqmin(3,N)\).

  • LINEAR Linear resizing on the innermost m dimensions of an input of rank N where \(0 \leq m \leqmin(3,N)\).

  • CUBIC Cubic resizing innermost 2 dimensions of N-D, N >= 2.

shape The output shape. Must have the same number of dimensions as the input.

scales List of resize scales. Must have the same number of dimensions as the input.

coordinate_transformation Controls how to map the resized coordinate back to the original coordinate, defaults to ASYMMETRIC.

  • ALIGN_CORNERS In this mode: \(x_{original}=x_{resized} \cdot \frac{length_{original}-1}{length_{resized}-1}\).

  • ASYMMETRIC In this mode: \(x_{original} = x_{resized} \cdot \frac{length_{original}}{length_{resized}}\).

  • HALF_PIXEL In this mode: \(x_{original} = (x_{resized} + 0.5) \cdot \frac{length_{original}}{length_{resized}} -0.5\).

selector_for_single_pixel The coordinate selector when resize to single pixel output, defaults to FORMULA.

  • FORMULA Use formula to map the original index.

  • UPPER Select the upper left pixel.

nearest_rounding The rounding mode for nearest neighbor resize, defaults to FLOOR.

  • HALF_UP Round half up.

  • HALF_DOWN Round half down.

  • FLOOR Round to floor.

  • CEIL Round to ceil.

cubic_coeff The cubic coefficient to use when cubic resize is used.

Inputs

input: tensor of type T1.

Outputs

output: tensor of type T1.

Data Types

T1: int8, float16, float32.

Shape Information

input is a tensor with a shape of \([a_0,...,a_n]\).

output is a tensor with a shape of \([b_0,...,b_n]\), where:

  • When shape is set, \(b_i = shape_i\).

  • When scales is set, \(b_i = \lfloor a_i \cdot scales_i \rfloor\).

DLA Restrictions

Supported combination of parameters are:

when resize_mode is NEAREST:

coordinate_transformation

selector_for_single_pixel

nearest_rounding

ASYMMETRIC

FORMULA

FLOOR

HALF_PIXEL

FORMULA

HALF_DOWN

HALF_PIXEL

FORMULA

HALF_UP

when resize_mode is LINEAR:

coordinate_transformation

selector_for_single_pixel

HALF_PIXEL

FORMULA

HALF_PIXEL

UPPER

Examples

Linear Resize
input = network.add_input("input", dtype=trt.float32, shape=(1, 1, 3, 3))
layer = network.add_resize(input)
layer.resize_mode = trt.ResizeMode.LINEAR
layer.shape = (1, 1, 5, 5)
layer.coordinate_transformation = trt.ResizeCoordinateTransformation.ALIGN_CORNERS
network.mark_output(layer.get_output(0))

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

outputs[layer.get_output(0).name] = layer.get_output(0).shape
expected[layer.get_output(0).name] = np.array(
    [
        [
            [0, 0.5, 1, 1.5, 2],
            [1.5, 2, 2.5, 3, 3.5],
            [3, 3.5, 4, 4.5, 5],
            [4.5, 5, 5.5, 6, 6.5],
            [6, 6.5, 7, 7.5, 8],
        ]
    ]
)

Nearest Resize, Shape Is Calculated By Using Scales
input = network.add_input("input", dtype=trt.float32, shape=(1, 1, 3, 3))
layer = network.add_resize(input)
layer.resize_mode = trt.ResizeMode.NEAREST
layer.scales = (1, 1, 2, 2)
layer.coordinate_transformation = trt.ResizeCoordinateTransformation.ALIGN_CORNERS
network.mark_output(layer.get_output(0))

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

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, 1.0, 1.0, 2.0],
                [0.0, 0.0, 0.0, 1.0, 1.0, 2.0],
                [0.0, 0.0, 0.0, 1.0, 1.0, 2.0],
                [3.0, 3.0, 3.0, 4.0, 4.0, 5.0],
                [3.0, 3.0, 3.0, 4.0, 4.0, 5.0],
                [6.0, 6.0, 6.0, 7.0, 7.0, 8.0],
            ]
        ]
    ]
)

C++ API

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

Python API

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