【CNN】——Depthwise卷积与Pointwise卷积

转载自:https://yinguobing.com/separable-convolution/#fn2
请读原文

卷积神经网络在图像处理中的地位已然毋庸置疑。卷积运算具备强大的特征提取能力、相比全连接又消耗更少的参数,应用在图像这样的二维结构数据中有着先天优势。然而受限于目前移动端设备硬件条件,显著降低神经网络的运算量依旧是网络结构优化的目标之一。本文所述的Separable Convolution就是降低卷积运算参数量的一种典型方法。

常规卷积运算

假设输入层为一个大小为64×64像素、三通道彩色图片。经过一个包含4个Filter的卷积层,最终输出4个Feature Map,且尺寸与输入层相同。整个过程可以用下图来概括。

在这里插入图片描述
此时,卷积层共4个Filter,每个Filter包含了3个Kernel,每个Kernel的大小为3×3。因此卷积层的参数数量可以用如下公式来计算:

Separable Convolution

Separable Convolution在Google的Xception[1]以及MobileNet[2]论文中均有描述。它的核心思想是将一个完整的卷积运算分解为两步进行,分别为Depthwise Convolution与Pointwise Convolution。

Depthwise Convolution

同样是上述例子,一个大小为64×64像素、三通道彩色图片首先经过第一次卷积运算,不同之处在于此次的卷积完全是在二维平面内进行,且Filter的数量与上一层的Depth相同。所以一个三通道的图像经过运算后生成了3个Feature map,如下图所示。
在这里插入图片描述
其中一个Filter只包含一个大小为3×3的Kernel,卷积部分的参数个数计算如下:

N_depthwise = 3 × 3 × 3 = 27

Depthwise Convolution完成后的Feature map数量与输入层的depth相同,但是这种运算对输入层的每个channel独立进行卷积运算后就结束了,没有有效的利用不同map在相同空间位置上的信息。因此需要增加另外一步操作来将这些map进行组合生成新的Feature map,即接下来的Pointwise Convolution。

Pointwise Convolution

Pointwise Convolution的运算与常规卷积运算非常相似,不同之处在于卷积核的尺寸为 1×1×M,M为上一层的depth。所以这里的卷积运算会将上一步的map在深度方向上进行加权组合,生成新的Feature map。有几个Filter就有几个Feature map。如下图所示。
在这里插入图片描述
由于采用的是1×1卷积的方式,此步中卷积涉及到的参数个数可以计算为:

N_pointwise = 1 × 1 × 3 × 4 = 12
经过Pointwise Convolution之后,同样输出了4张Feature map,与常规卷积的输出维度相同。

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
深度卷积深度可分离卷积的代码实现会有所不同。以下是它们的代码实现示例: 深度卷积的代码实现: ```python import tensorflow as tf # 输入张量 input_tensor = tf.placeholder(tf.float32, shape=[None, height, width, channels]) # 卷积核 kernel = tf.Variable(tf.truncated_normal([kernel_size, kernel_size, input_channels, output_channels], stddev=0.1)) # 深度卷积 conv_output = tf.nn.conv2d(input_tensor, kernel, strides=[1, stride, stride, 1], padding='SAME') ``` 深度可分离卷积的代码实现: ```python import tensorflow as tf # 输入张量 input_tensor = tf.placeholder(tf.float32, shape=[None, height, width, channels]) # 深度可分离卷积 depthwise_output = tf.nn.depthwise_conv2d(input_tensor, depthwise_filter, strides=[1, stride, stride, 1], padding='SAME') pointwise_output = tf.nn.conv2d(depthwise_output, pointwise_filter, strides=[1, 1, 1, 1], padding='SAME') ``` 在以上代码示例中,`input_tensor`表示输入张量,`kernel`表示卷积核,`conv_output`表示深度卷积的输出。对于深度可分离卷积,`depthwise_output`表示Depthwise Convolution的输出,`pointwise_output`表示Pointwise Convolution的输出。这里的代码只是示例,实际实现可能会有所不同,具体的实现方式会根据使用的深度学习框架而有所差异。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [深度可分离卷积](https://blog.csdn.net/zfjBIT/article/details/127521956)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [深度可分离卷积(计算过程及代码实现)](https://blog.csdn.net/m0_45267220/article/details/130291855)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值