首页 > 解决方案 > 我是否获得了 tf.nn.depthwise_conv2d() 的正确输出?

问题描述

我正在尝试创建自己的深度可分离卷积,并且我想针对 tensorflow 实现对其进行测试,但我不知道我是否让它正常工作。这是我的代码

首先,我创建图像和深度过滤器。图像是 2 个通道,每个通道是 3x3。过滤器也是 2 个通道,3x3。两个过滤器上的过滤器具有相同的值,而图像通道的值略有不同。

depth_filter = np.array([   [[0,-1,0],[-1,5,-1],[0,-1,0]],
                                [[0,-1,0],[-1,5,-1],[0,-1,0]]], 
                            np.float32)
image = np.array([  [[102,109,124],[99,98,98],[103,103,98]],
                        [[101,108,123],[98,97,97],[102,102,97]]], 
                        np.float32)

然后我设置了一些变量来让我自己轻松一点。

C = image.shape[0]
H = image.shape[1]
W = image.shape[2]
R_H = depth_filter.shape[1]
R_W = depth_filter.shape[2]

然后我重塑我的矩阵,以便它们可以用于 tensorflow 的实现。

kernel = tf.reshape(depth_filter, [R_H,R_W,C,1], name='kernel')
imageconv = tf.reshape(image, [1,H,W,C], name='imageconv')
dout = tf.nn.depthwise_conv2d(imageconv, kernel, [1,1,1,1], padding='SAME')

问题是我得到了这个输出

[[[-325.    0.]
  [-407.  406.]
  [-206.  406.]]

 [[ 327. -208.]
  [  85.   76.]
  [-199.  212.]]

 [[ 393. -204.]
  [ 438. -424.]
  [   0. -326.]]]

我期望得到 2 个 3x3 矩阵,其中内核上的通道 1 将过滤通道 1 图像,内核上的通道 2 将过滤通道 2 图像。当每个通道只有 1 个通道时,我会得到 1 个 3x3 图像。我是不了解深度卷积的工作原理还是我做错了什么。

标签: pythontensorflowmachine-learningconv-neural-network

解决方案


推荐阅读