卷积

在输入张量上计算卷积,并添加可选的偏置以生成输出张量。

属性

num_output_maps 卷积的输出映射数量。

pre_padding 前填充。默认值为 \((0, 0)\)

post_padding 后填充。默认值为 \((0, 0)\)

padding_mode 填充模式。填充模式可以是以下之一

\[\begin{split}\begin{gather} I = \text{输入图像的维度} \\ B = \text{前填充,在图像数据之前。对于反卷积,前填充在输出之前设置。} \\ A = \text{后填充,在图像数据之后。对于反卷积,后填充在输出之后设置。} \\ P = \text{输入和输出之间的差值} \\ S = \text{步长} \\ F = \text{滤波器} \\ O = \text{输出} \\ D = \text{膨胀。对于池化层,始终等于 1} \\ M = I + B + A\text{数据加上任何填充} \\ DK = 1 + D \cdot (F - 1) \\ \end{gather}\end{split}\]
  • EXPLICIT_ROUND_DOWN 使用显式填充,向下舍入输出大小。
    \(O = \lfloor\frac{M - DK}{S}\rfloor + 1\)
  • EXPLICIT_ROUND_UP 使用显式填充,向上舍入输出大小。
    \(O = \lceil\frac{M - DK}{S}\rceil + 1\)
  • SAME_UPPER 使用 SAME 填充,其中 \(\text{前填充} \leq \text{后填充}\)
    \(\begin{gather}O = \lceil\frac{I}{S}\rceil \\ P = \lfloor\frac{I-1}{S}\rfloor \cdot S + DK -I \\ B = \lfloor\frac{P}{2}\rfloor \\ A = P - B \end{gather}\)
  • SAME_LOWER 使用 SAME 填充,其中 \(\text{前填充} \geq \text{后填充}\)
    \(\begin{gather}O = \lceil\frac{I}{S}\rceil \\ P = \lfloor\frac{I-1}{S}\rfloor \cdot S + DK -I \\ A = \lfloor\frac{P}{2}\rfloor \\ B = P - A \end{gather}\)

num_groups 卷积的组数。

kernel 卷积的内核权重。

bias 卷积的偏置权重。

kernel_size_nd 卷积的多维内核大小。

stride_nd 卷积的多维步长。默认值为 \((1, \cdots, 1)\)

padding_nd 卷积的多维填充。默认值为 \((0,\cdots,0)\)

dilation_nd 卷积的多维膨胀。默认值为 \((1,\cdots,1)\)

输入

input: 类型为 T 的张量

输出

output: 类型为 T 的张量

数据类型

T: int8, float16, float32, bfloat16

形状信息

input 是形状为 \([A_0,\cdots,A_n]\) 的张量,\(n\geq3\)

W 是形状为 \([k, W_0,\cdots,W_m]\) 的张量,\(m\in(2,3)\)

bias 是形状为 \([k]\) 的张量

output 是形状为 \([A_0, m, B_{n-m},\cdots, B_n]\) 的张量,其中

\[ \begin{align}\begin{aligned}m: \text{num_output_maps}\\d: \text{dilation}\\k: \text{kernel_size}\\p: \text{padding}\\B_i=\lfloor(A_i \cdot p_{i-n+m} - t_{i-n+m})\rfloor + 1\\t_{i-n+m} = 1 + d_{i-n+m} \cdot (k_{i-n+m} - 1)\end{aligned}\end{align} \]

体积限制

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

体积限制

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

DLA 支持

DLA FP16 和 DLA INT8 支持 2D 卷积。

示例

2D 卷积
input_shape = [1, 1, 5, 5]
in1 = network.add_input("input1", dtype=trt.float32, shape=input_shape)
num_filter = 3
w = np.array(
    [
        [
            [0.3, -0.8, 1.0],
            [0.5, -0.5, 0.0],
            [0.4, -0.2, 0.9],
        ],
        [
            [0.4, -0.7, 0.8],
            [0.3, -0.2, 1.0],
            [0.3, 0.2, 0.3],
        ],
        [
            [0.1, -0.2, 0.3],
            [0.1, -0.2, 0.3],
            [0.1, -0.2, 0.9],
        ],
    ],
    np.float32,
)
layer = network.add_convolution_nd(in1, num_filter, kernel_shape=(3, 3), kernel=trt.Weights(w))
network.mark_output(layer.get_output(0))

inputs[in1.name] = np.array(
    [
        [
            [
                [-3.0, -2.0, -1.0, -2.0, -1.0],
                [10.0, -25.0, 0.0, -2.0, -1.0],
                [1.0, 2.0, -2.0, -2.0, -1.0],
                [10.0, -25.0, 0.0, -2.0, -1.0],
                [-3.0, -2.0, -1.0, -2.0, -1.0],
            ]
        ]
    ]
)

outputs[layer.get_output(0).name] = layer.get_output(0).shape
expected[layer.get_output(0).name] = np.array(
    [
        [
            [[15.4, -14.9, 0.0], [31.5, -19.3, 0.1], [12.5, -14.7, 0.1]],
            [[7.5, -11.6, -1.7], [17.4, -20.7, -1.3], [3.8, -10.3, -1.8]],
            [[3.7, -4.9, -0.6], [11.1, -7.4, -0.5], [4.3, -4.9, -0.6]],
        ]
    ]
)
3D 卷积
input_shape = [1, 1, 3, 3, 3]
in1 = network.add_input("input1", dtype=trt.float32, shape=input_shape)
num_filter = 1
w = np.array(
    [
        [
            [0.3],
        ]
    ],
    np.float32,
)
layer = network.add_convolution_nd(in1, num_filter, kernel_shape=(1, 1, 1), kernel=trt.Weights(w))
network.mark_output(layer.get_output(0))

inputs[in1.name] = np.array(
    [
        [
            [
                [
                    [0.3, -0.8, 1.0],
                    [0.5, -0.5, 0.0],
                    [0.4, -0.2, 0.9],
                ],
                [
                    [0.4, -0.7, 0.8],
                    [0.3, -0.2, 1.0],
                    [0.3, 0.2, 0.3],
                ],
                [
                    [0.1, -0.2, 0.3],
                    [0.1, -0.2, 0.3],
                    [0.1, -0.2, 0.9],
                ],
            ]
        ]
    ]
)

outputs[layer.get_output(0).name] = layer.get_output(0).shape
expected[layer.get_output(0).name] = np.array(
    [
        [
            [
                [[0.09, -0.24000001, 0.3], [0.15, -0.15, 0.0], [0.12, -0.06, 0.27]],
                [[0.12, -0.21000001, 0.24000001], [0.09, -0.06, 0.3], [0.09, 0.06, 0.09]],
                [[0.03, -0.06, 0.09], [0.03, -0.06, 0.09], [0.03, -0.06, 0.27]],
            ]
        ]
    ]
)

C++ API

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

Python API

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