首页 > 解决方案 > TensorRT 中的 Depthwise_conv2d

问题描述

从 Tensorflow 转换为 TensorRT 时,Depthwise_con2d 存在问题。

实现如图所示。

在此处输入图像描述

TensorFlow 代码如下。

    def gauss_kernel(self, kernlen=21, nsig=3, channels=1):
        interval = (2*nsig+1.)/(kernlen)
        x = np.linspace(-nsig-interval/2., nsig+interval/2., kernlen+1)
        kern1d = np.diff(st.norm.cdf(x))
        kernel_raw = np.sqrt(np.outer(kern1d, kern1d))
        kernel = kernel_raw/kernel_raw.sum()
        out_filter = np.array(kernel, dtype = np.float32)
        out_filter = out_filter.reshape((kernlen, kernlen, 1, 1))
        out_filter = np.repeat(out_filter, channels, axis = 2)
        return out_filter

    def make_gauss_var(self, name, size, sigma, c_i):
        # with tf.device("/cpu:0"):
        kernel = self.gauss_kernel(size, sigma, c_i)
        var = tf.Variable(tf.convert_to_tensor(kernel), name=name)
        return var
    @layer
    def smoother(self, input, name='gaussian_heatMat'):
        # Get the number of channels in the input
        c_i = input.get_shape().as_list()[3]
        # Convolution for a given input and kernel
        convolve = lambda i, k: tf.nn.depthwise_conv2d(i, k, [1, 1, 1, 1], padding='SAME')
        with tf.variable_scope(name) as scope:
            kernel = self.make_gauss_var('gauss_weight', 25, 3.0, c_i)#filter_size=25,sigma=3.0
            output = convolve(input, kernel)
        return output

我在从 uff 解析到网络时出错

[TensorRT] ERROR: gaussian_heatMat/depthwise: kernel weights has count 11875 but 7796358 was expected
[TensorRT] ERROR: gaussian_heatMat/depthwise: count of 11875 weights in kernel, but kernel dimensions (25, 25) with 1459200 input channels, 19 output channels and 19 groups were specified.
[TensorRT] ERROR: UffParser: Parser error: MarkOutput_0: Order size is not matching the number dimensions of TensorRT

11875 = 25*25*19 所以这是内核。为什么预期为 7796358?也不要在其他错误下。

标签: tensorflowtensorrt

解决方案


推荐阅读