『计算机视觉』深度可分离卷积
一、通道和区域
标准的卷积过程可以看上图,一个2×2的卷积核在卷积时,对应图像区域中的所有通道均被同时考虑,问题在于,为什么一定要同时考虑图像区域和通道?我们为什么不能把通道和空间区域分开考虑?
深度可分离卷积提出了一种新的思路:对于不同的输入channel采取不同的卷积核进行卷积。
二、结合TensorFlow API介绍具体实现
顺便一提,tf的实现可以接收rate参数,即可以采用空洞卷积的方式进行操作。
1、depthwise_conv2d 分离卷积部分
我们定义一张4*4的双通道图片
import tensorflow as tf img1 = tf.constant(value=[[[[1],[2],[3],[4]], [[1],[2],[3],[4]], [[1],[2],[3],[4]], [[1],[2],[3],[4]]]],dtype=tf.float32) img2 = tf.constant(value=[[[[1],[1],[1],[1]], [[1],[1],[1],[1]], [[1],[1],[1],[1]], [[1],[1],[1],[1]]]],dtype=tf.float32) img = tf.concat(values=[img1,img2],axis=3) img
<tf.Tensor 'concat_1:0' shape=(1, 4, 4, 2) dtype=float32>
使用3*3的卷积核,输入channel为2,输出channel为2(卷积核数目为2),
filter1 = tf.constant(value=0, shape=[3,3,1,1],dtype=tf.float32) filter2 = tf.constant(value=1, shape=[3,3,1,1],dtype=tf.float32) filter3 = tf.constant(value=2, shape=[3,3,1,1],dtype=tf.float32) filter4 = tf.constant(value=3, shape=[3,3,1,1],dtype=tf.float32) filter_out1 = tf.concat(values=[filter1,filter2],axis=2) filter_out2 = tf.concat(values=[filter3,filter4],axis=2) filter = tf.concat(values=[filter_out1,filter_out2],axis=3) filter
<tf.Tensor 'concat_4:0' shape=(3, 3, 2, 2) dtype=float32>
同时执行卷积操作,和深度可分离卷积操作,
out_img_conv = tf.nn.conv2d(input=img, filter=filter, strides=[1,1,1,1], padding='VALID') out_img_depthwise = tf.nn.depthwise_conv2d(input=img, filter=filter, strides=[1,1,1,1], rate=[1,1], padding='VALID') with tf.Session() as sess: res1 = sess.run(out_img_conv) res2 = sess.run(out_img_depthwise) print(res1, '\n', res1.shape) print(res2, '\n', res2.shape)
[[[[ 9. 63.] [ 9. 81.]] [[ 9. 63.] [ 9. 81.]]]] (1, 2, 2, 2) # 《----------
[[[[ 0. 36. 9. 27.] [ 0. 54. 9. 27.]] [[ 0. 36. 9. 27.] [ 0. 54. 9. 27.]]]] (1, 2, 2, 4)# 《----------
对比输出shape,depthwise_conv2d输出的channel数目为in_channel * 卷积核数目,每一个卷积核对应通道都会对对应的channel进行一次卷积,所以输出通道数更多,
看到这里大家可能会误解深度可分离卷积的输出通道数大于普通卷积,其实这只是“分离”部分,后面还有组合的步骤,而普通卷积只不过直接完成了组合:通过对应点相加,将四个卷积中间结果合并为卷积核个数(这里是2)
2、合并特征
合并过程如下,可分离卷积中的合并过程变成可学习的了,使用一个1*1的普通卷积进行特征合并,
point_filter = tf.constant(value=1, shape=[1,1,4,4],dtype=tf.float32) out_img_s = tf.nn.conv2d(input=out_img_depthwise, filter=point_filter, strides=[1,1,1,1], padding='VALID') with tf.Session() as sess: res3 = sess.run(out_img_s) print(res3, '\n', res3.shape)
[[[[ 72. 72. 72. 72.] [ 90. 90. 90. 90.]] [[ 72. 72. 72. 72.] [ 90. 90. 90. 90.]]]] (1, 2, 2, 4)
3、separable_conv2d 一步完成
out_img_se = tf.nn.separable_conv2d(input=img, depthwise_filter=filter, pointwise_filter=point_filter, strides=[1,1,1,1], rate=[1,1], padding='VALID') with tf.Session() as sess: print(sess.run(out_img_se))
[[[[ 72. 72. 72. 72.] [ 90. 90. 90. 90.]] [[ 72. 72. 72. 72.] [ 90. 90. 90. 90.]]]] (1, 2, 2, 4)
三、优势
参数量降低
假设输入通道数为3,要求输出通道数为256,两种做法:
1.直接接一个3×3×256的卷积核,参数量为:3×3×3×256 = 6,912
2.DW操作,分两步完成,参数量为:3×3×3 + 3×1×1×256 = 795
通道区域分离
深度可分离卷积将以往普通卷积操作同时考虑通道和区域改变(卷积先只考虑区域,然后再考虑通道),实现了通道和区域的分离。
- 深度卷积网络在计算机视觉中的应用研究综述
- 【计算机视觉】深度相机(一)--TOF总结
- 计算机视觉与深度学习公司
- 深度学习与计算机视觉(11)_基于deep learning的快速图像检索系统
- 资料网址大全 (数学,深度学习,机器学习,计算机视觉,人脸识别,图像处理等)
- 为什么深度学习几乎成了计算机视觉研究的标配?
- 斯坦福CS231n—深度学习与计算机视觉----学习笔记 课时5
- 一文看懂深度学习与计算机视觉(上)
- 深度学习与计算机视觉系列,细说卷积神经网络
- 10 大深度学习架构:计算机视觉优秀从业者必备(附代码实现)
- [计算机视觉][神经网络与深度学习]R-FCN、SSD、YOLO2、faster-rcnn和labelImg实验笔记
- 计算机视觉---2.1---卷积与模板操作
- 10大深度学习架构:计算机视觉优秀从业者必备(附代码实现)
- 计算机视觉和深度学习领域引用次数最多的论文
- 【期刊&&会议】机器学习&&深度学习 + 计算机视觉(To be continued..)
- 为什么深度学习几乎成了计算机视觉研究的标配?
- 机器学习、深度学习、计算机视觉、自然语言处理及应用案例——干货分享(持续更新……)
- 机器学习、深度学习、计算机视觉、自然语言处理及应用案例——干货分享
- 机器学习/深度学习在计算机视觉中的实际应用案例
- 斯坦福CS231n—深度学习与计算机视觉----学习笔记 课时26&&27