创建 IConvolutionLayer 之后记得设置步长和零填充

error as follows:

[06/17/2021-11:30:13] [E] [TRT] (Unnamed Layer* 88) [ElementWise]: elementwise inputs must have same dimensions or follow broadcast rules (input dimensions were [256,56,56] and [256,54,54]).

Cause:

Forgot to set stride and padding after IConvolutionLayer creation. The default stride(0,0) and padding(1,1) of IConvolutionLayer can be found in 'NvInfer.h':

    TRT_DEPRECATED virtual DimsHW getStride() const TRTNOEXCEPT = 0;

    //!
    //! \brief Set the padding of the convolution.
    //!
    //! The input will be zero-padded by this number of elements in the height and width directions.
    //! Padding is symmetric.
    //!
    //! Default: (0,0)

    TRT_DEPRECATED virtual DimsHW getPadding() const TRTNOEXCEPT = 0;

    //!
    //! \brief Set the number of groups for a convolution.
    //!
    //! The input tensor channels are  divided into \p nbGroups groups, and a convolution is executed for each group,
    //! using a filter per group. The results of the group convolutions are concatenated to form the output.
    //!
    //! \note When using groups in int8 mode, the size of the groups (i.e. the channel count divided by the group
    //! count) must be a multiple of 4 for both input and output.
    //!
    //! Default: 1

 

Solution: set stride and padding

    IConvolutionLayer* cmap_att = network->addConvolutionNd(*cmap_up_3->getOutput(0), 256, DimsHW{3,3}, weightMap["1.cmap_att.weight"], weightMap["1.cmap_att.bias"]);
    cmap_att->setStrideNd(DimsHW{1,1});
    cmap_att->setPaddingNd(DimsHW{1,1});

 

Others: how to show output dimension of each layer when defining Network in TensorRT using C++ API

    std::cout << "cmap_up_3_dimMAX:" << cmap_up_3->getOutput(0)->getDimensions().nbDims << std::endl; // 3
    std::cout << "cmap_up_3_dim0:" << cmap_up_3->getOutput(0)->getDimensions().d[0] << std::endl;
    std::cout << "cmap_up_3_dim1:" << cmap_up_3->getOutput(0)->getDimensions().d[1] << std::endl;
    std::cout << "cmap_up_3_dim2:" << cmap_up_3->getOutput(0)->getDimensions().d[2] << std::endl;

 

posted @ 2021-06-17 11:38  略略略——  阅读(211)  评论(0编辑  收藏  举报