调整大小

将输入张量调整大小为输出张量。输出形状可以直接使用 shape 设置,或者通过 scales 参数计算。

属性

resize_mode 控制使用的调整大小类型,默认为 NEAREST

  • NEAREST 最近邻调整大小,作用于秩为 N 的输入张量最内层的 m 个维度,其中 \(0\leq m \leqmin(3,N)\)

  • LINEAR 线性调整大小,作用于秩为 N 的输入张量最内层的 m 个维度,其中 \(0 \leq m \leqmin(3,N)\)

  • CUBIC 立方调整大小,作用于 N 维张量最内层的 2 个维度,N >= 2。

shape 输出形状。元素数量必须与 input 中的维度数量相同。

scales 调整大小比例列表。必须具有与输入相同的维度数量。

coordinate_transformation 控制如何将调整大小后的坐标映射回原始坐标,默认为 ASYMMETRIC

  • ALIGN_CORNERS 在此模式下:\(x_{original}=x_{resized} \cdot \frac{length_{original}-1}{length_{resized}-1}\)

  • ASYMMETRIC 在此模式下:\(x_{original} = x_{resized} \cdot \frac{length_{original}}{length_{resized}}\)

  • HALF_PIXEL 在此模式下:\(x_{original} = (x_{resized} + 0.5) \cdot \frac{length_{original}}{length_{resized}} -0.5\)

selector_for_single_pixel 当调整大小为单像素输出时,坐标选择器,默认为 FORMULA

  • FORMULA 使用公式映射原始索引。

  • UPPER 选择左上角像素。

nearest_rounding 最近邻调整大小的舍入模式,默认为 FLOOR

  • HALF_UP 四舍五入。

  • HALF_DOWN 四舍五入(向下)。

  • FLOOR 向下取整。

  • CEIL 向上取整。

cubic_coeff 使用立方调整大小时的立方系数。

输入

input0:类型为 T1 的张量。

input1:可选的类型为 Int32Int64 的张量,带有 shape

输出

output:类型为 T1 的张量。

数据类型

T1int8, float16, float32

形状信息

input0 是一个形状为 \([a_0,...,a_n]\) 的张量。

input1 的形状为 \([n]\)

output 是一个形状为 \([b_0,...,b_n]\) 的张量,其中

  • 当设置 shape 时,\(b_i = shape_i\)

  • 当设置 scales 时,\(b_i = \lfloor a_i \cdot scales_i \rfloor\)

体积限制

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

DLA 支持

支持 DLA FP16 和 DLA INT8。

支持的参数组合是

resize_modeNEAREST

coordinate_transformation

selector_for_single_pixel

nearest_rounding

ASYMMETRIC

FORMULA

FLOOR

HALF_PIXEL

FORMULA

HALF_DOWN

HALF_PIXEL

FORMULA

HALF_UP

resize_modeLINEAR

coordinate_transformation

selector_for_single_pixel

HALF_PIXEL

FORMULA

HALF_PIXEL

UPPER

示例

线性调整大小
input = network.add_input("input", dtype=trt.float32, shape=(1, 1, 3, 3))
layer = network.add_resize(input)
layer.resize_mode = trt.InterpolationMode.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],
        ]
    ]
)

最近邻调整大小,形状通过使用比例计算
input = network.add_input("input", dtype=trt.float32, shape=(1, 1, 3, 3))
layer = network.add_resize(input)
layer.resize_mode = trt.InterpolationMode.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

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

Python API

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