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 相关文章推荐
python写xml文件的操作实例
Oct 05 Python
Python中条件判断语句的简单使用方法
Aug 21 Python
Python开发之快速搭建自动回复微信公众号功能
Apr 22 Python
Python 两个列表的差集、并集和交集实现代码
Sep 21 Python
Ubuntu安装Jupyter Notebook教程
Oct 18 Python
使用python获取csv文本的某行或某列数据的实例
Apr 03 Python
浅谈django三种缓存模式的使用及注意点
Sep 30 Python
Django+JS 实现点击头像即可更改头像的方法示例
Dec 26 Python
对pyqt5中QTabWidget的相关操作详解
Jun 21 Python
python安装scipy的方法步骤
Jun 26 Python
实例代码讲解Python 线程池
Aug 24 Python
如何Tkinter模块编写Python图形界面
Oct 14 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
注册页面之前先验证用户名是否存在的php代码
2012/07/14 PHP
PHP轻量级数据库操作类Medoo增加、删除、修改、查询例子
2014/07/04 PHP
php构造方法中析构方法在继承中的表现
2016/04/12 PHP
详解php curl带有csrf-token验证模拟提交方法
2018/04/18 PHP
PHP简单实现解析xml为数组的方法
2018/05/02 PHP
php/JS实现的生成随机密码(验证码)功能示例
2019/06/06 PHP
JavaScript 判断判断某个对象是Object还是一个Array
2010/01/28 Javascript
js opener的使用详解
2014/01/11 Javascript
jQuery插件fullPage.js实现全屏滚动效果
2016/12/02 Javascript
DWR3 访问WEB元素的两种方法实例详解
2017/01/03 Javascript
Bootstrap 响应式实用工具实例详解
2017/03/29 Javascript
如何在 JavaScript 中更好地利用数组
2018/09/27 Javascript
Vue中nprogress页面加载进度条的方法实现
2020/11/13 Javascript
Python安装第三方库及常见问题处理方法汇总
2016/09/13 Python
对Python中内置异常层次结构详解
2018/10/18 Python
在Pandas中给多层索引降级的方法
2018/11/16 Python
用python 实现在不确定行数情况下多行输入方法
2019/01/28 Python
python实现祝福弹窗效果
2019/04/07 Python
python买卖股票的最佳时机(基于贪心/蛮力算法)
2019/07/05 Python
python  文件的基本操作 菜中菜功能的实例代码
2019/07/17 Python
Python 脚本实现淘宝准点秒杀功能
2019/11/13 Python
Python编译成.so文件进行加密后调用的实现
2019/12/23 Python
django 解决自定义序列化返回处理数据为null的问题
2020/05/20 Python
python3.4中清屏的处理方法
2020/07/06 Python
一款纯css3实现的鼠标悬停动画按钮
2014/12/29 HTML / CSS
html5利用canvas绘画二级树形结构图的示例
2017/09/27 HTML / CSS
HTML5新增form控件和表单属性实例代码详解
2019/05/15 HTML / CSS
自我评价的写作规则
2014/01/06 职场文书
挂牌仪式策划方案
2014/05/18 职场文书
村道德模范事迹材料
2014/08/28 职场文书
2014年质量管理工作总结
2014/12/01 职场文书
专家推荐信范文
2015/03/26 职场文书
学校勤俭节约倡议书
2015/04/29 职场文书
Python Flask请求扩展与中间件相关知识总结
2021/06/11 Python
windows11怎么查看wifi密码? win11查看wifi密码的技巧
2021/11/21 数码科技
解决Vmware虚拟机安装centos8报错“Section %Packages Does Not End With %End. Pane Is Dead”
2022/06/01 Servers