Tensorflow tf.nn.depthwise_conv2d如何实现深度卷积的


Posted in Python onApril 20, 2020

实验环境:tensorflow版本1.2.0,python2.7

介绍

depthwise_conv2d来源于深度可分离卷积:

结果返回一个Tensor,shape为[batch, out_height, out_width, in_channels * channel_multiplier],注意这里输出通道变成了in_channels * channel_multiplier

实验

为了形象的展示depthwise_conv2d,我们必须要建立自定义的输入图像和卷积核

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)
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)

建立好了img和filter,就可以做卷积了

out_img = tf.nn.conv2d(input=img, filter=filter, strides=[1,1,1,1], padding='VALID')

好了,用一张图来详细展示这个过程

Tensorflow tf.nn.depthwise_conv2d如何实现深度卷积的 

Tensorflow tf.nn.depthwise_conv2d如何实现深度卷积的

这是普通的卷积过程,我们再来看深度卷积。

out_img = tf.nn.depthwise_conv2d(input=img, filter=filter, strides=[1,1,1,1], rate=[1,1], padding='VALID')

Tensorflow tf.nn.depthwise_conv2d如何实现深度卷积的 

Tensorflow tf.nn.depthwise_conv2d如何实现深度卷积的

现在我们可以形象的解释一下depthwise_conv2d卷积了。看普通的卷积,我们对卷积核每一个out_channel的两个通道分别和输入的两个通道做卷积相加,得到feature map的一个channel,而depthwise_conv2d卷积,我们对每一个对应的in_channel,分别卷积生成两个out_channel,所以获得的feature map的通道数量可以用in_channel* channel_multiplier来表达,这个channel_multiplier,就可以理解为卷积核的第四维。

代码清单

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)
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)

out_img = tf.nn.depthwise_conv2d(input=img, filter=filter, strides=[1,1,1,1], rate=[1,1], padding='VALID')

输出:

rate=1, VALID mode result:
[[[[ 0. 36. 9. 27.]
[ 0. 54. 9. 27.]]

[[ 0. 36. 9. 27.]
[ 0. 54. 9. 27.]]]]

到此这篇关于Tensorflow tf.nn.depthwise_conv2d如何实现深度卷积的的文章就介绍到这了,更多相关Tensorflow tf.nn.depthwise_conv2d深度卷积内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Python 相关文章推荐
pyside写ui界面入门示例
Jan 22 Python
爬山算法简介和Python实现实例
Apr 26 Python
Python处理文本文件中控制字符的方法
Feb 07 Python
Python实现 多进程导入CSV数据到 MySQL
Feb 26 Python
python OpenCV学习笔记直方图反向投影的实现
Feb 07 Python
Scrapy使用的基本流程与实例讲解
Oct 21 Python
python 发送和接收ActiveMQ消息的实例
Jan 30 Python
Python读取VOC中的xml目标框实例
Mar 10 Python
如何在python中判断变量的类型
Jul 29 Python
Python实现数字的格式化输出
Aug 01 Python
PyTorch预训练Bert模型的示例
Nov 17 Python
python生成随机数、随机字符、随机字符串
Apr 06 Python
解决python脚本中error: unrecognized arguments: True错误
Apr 20 #Python
python argparse传入布尔参数false不生效的解决
Apr 20 #Python
parser.add_argument中的action使用
Apr 20 #Python
Python ArgumentParse的subparser用法说明
Apr 20 #Python
python列表的逆序遍历实现
Apr 20 #Python
python sitk.show()与imageJ结合使用常见的问题
Apr 20 #Python
使用Python对Dicom文件进行读取与写入的实现
Apr 20 #Python
You might like
文件系统基本操作类
2006/11/23 PHP
在服务端进行目录建立、删除,文件上传、删除的过程的php代码
2008/09/10 PHP
php微信高级接口调用方法(自定义菜单接口、客服接口、二维码)
2016/11/28 PHP
Laravel框架模板继承操作示例
2018/06/11 PHP
简单三步,搞掂内存泄漏
2007/03/10 Javascript
利用json获取字符出现次数的代码
2012/03/22 Javascript
JS数组的遍历方式for循环与for...in
2014/07/31 Javascript
js格式化时间小结
2014/11/03 Javascript
node.js中的buffer.toString方法使用说明
2014/12/14 Javascript
nodejs实现遍历文件夹并统计文件大小
2015/05/28 NodeJs
jQuery实现的类似淘宝网站搜索框样式代码分享
2015/08/24 Javascript
jquery及js实现动态加载js文件的方法
2016/01/21 Javascript
js实现的鼠标滚轮滚动切换页面效果(类似360默认页面滚动切换效果)
2016/01/27 Javascript
js防阻塞加载的实现方法
2016/09/09 Javascript
详解Vue改变数组中对象的属性不重新渲染View的解决方案
2018/09/21 Javascript
Python中apply函数的用法实例教程
2014/07/31 Python
Python3连接MySQL(pymysql)模拟转账实现代码
2016/05/24 Python
python正则表达式re之compile函数解析
2017/10/25 Python
Python中extend和append的区别讲解
2019/01/24 Python
Python 如何批量更新已安装的库
2020/05/26 Python
解决Pycharm 中遇到Unresolved reference 'sklearn'的问题
2020/07/13 Python
python 使用递归的方式实现语义图片分割功能
2020/07/16 Python
用python对excel查重
2020/12/07 Python
tensorflow与numpy的版本兼容性问题的解决
2021/01/08 Python
Python实现淘宝秒杀功能的示例代码
2021/01/19 Python
详解前端HTML5几种存储方式的总结
2016/12/27 HTML / CSS
自考自我鉴定范文
2013/10/30 职场文书
应届毕业生自荐信例文
2014/02/26 职场文书
公司副总经理任命书
2014/06/05 职场文书
新学期开学标语
2014/06/30 职场文书
毕业生工作求职信
2014/06/30 职场文书
学校组织向国旗敬礼活动方案(中小学适用)
2014/09/27 职场文书
硕士毕业答辩开场白
2015/05/27 职场文书
农村婚礼司仪主持词
2015/06/29 职场文书
运动会100米广播稿
2015/08/19 职场文书
mysql timestamp比较查询遇到的坑及解决
2021/11/27 MySQL