当前位置:网站首页>tensorflow2.0 cnn(layerwise)
tensorflow2.0 cnn(layerwise)
2022-07-31 15:55:00 【Full stack programmer webmaster】
大家好,又见面了,我是你们的朋友全栈君.
实验环境:tensorflow版本1.2.0,python2.7
介绍
depthwise_conv2d
Derived from depthwise separable convolution:
Xception: Deep Learning with Depthwise Separable Convolutions
tf.nn.depthwise_conv2d(input,filter,strides,padding,rate=None,name=None,data_format=None)
除去name
参数用以指定该操作的name,data_format
指定数据格式,与方法有关的一共五个参数:
- input: 指需要做卷积的输入图像,要求是一个4维Tensor,具有
[batch, height, width, in_channels]
这样的shape,具体含义是[训练时一个batch的图片数量, 图片高度, 图片宽度, 图像通道数] - filter: 相当于CNN中的卷积核,要求是一个4维Tensor,具有
[filter_height, filter_width, in_channels, channel_multiplier]
这样的shape,具体含义是[卷积核的高度,卷积核的宽度,输入通道数,output convolution multiplier],The same goes for the third dimension herein_channels,就是参数value的第四维 - strides: The sliding step size of the convolution.
- padding: string类型的量,只能是”SAME”,”VALID”其中之一,This value determines how different edges are filled.
- rate: For a detailed explanation of this parameter, see 【Tensorflow】tf.nn.atrous_conv2d如何实现空洞卷积?
结果返回一个Tensor,shape为[batch, out_height, out_width, in_channels * channel_multiplier]
,Note that here the output channel becomesin_channels * channel_multiplier
实验
for image displaydepthwise_conv2d
,We have to create custom input images and convolution kernels
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,You can do convolution
out_img = tf.nn.conv2d(input=img, filter=filter, strides=[1,1,1,1], padding='VALID')
好了,Use a diagram to illustrate this process in detail
This is the normal convolution process,Let's look at depthwise convolution again.
out_img = tf.nn.depthwise_conv2d(input=img, filter=filter, strides=[1,1,1,1], rate=[1,1], padding='VALID')
Now we can explain it visuallydepthwise_conv2d
卷积了.See ordinary convolution,We apply convolution kernels to eachout_channel
The two channels of , respectively, are convolved and added to the two channels of the input,得到feature map的一个channel,而depthwise_conv2d
卷积,We correspond to each onein_channel
,Convolution to generate two respectivelyout_channel
,所以获得的feature mapnumber of channels availablein_channel* channel_multiplier
来表达,这个channel_multiplier
,It can be understood as the fourth dimension of the convolution kernel.
代码清单
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.]]]]
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/127979.html原文链接:https://javaforall.cn
边栏推荐
猜你喜欢
Why is the field of hacking almost filled with boys?
.NET 20周年专访 - 张善友:.NET 技术是如何赋能并改变世界的
Getting Started with TextBlock Control Basic Tools Usage, Get Started
工程水文学复习资料
6-22 Vulnerability exploit - postgresql database password cracking
tooltips使用教程(鼠标悬停时显示提示)
Premiere Pro 2022 for (pr 2022)v22.5.0
MySQL基础篇【单行函数】
[MySQL] Mysql paradigm and the role of foreign keys
定时器的类型
随机推荐
基于ABP实现DDD
T - sne + data visualization parts of the network parameters
软件实现AT命令操作过程
单细胞测序流程(单细胞rna测序)
第二届中国PWA开发者日
C语言-函数
MySQL基础篇【单行函数】
Doing things software development - the importance of law and understanding of reasonable conclusions
npm安装时卡在sill idealTree buildDeps,npm安装速度慢,npm安装卡在一个地方不动
Oracle dynamically registers non-1521 ports
Implementing DDD based on ABP
01 邂逅typescript,环境搭建
复制延迟案例(1)-最终一致性
Premiere Pro 2022 for (pr 2022)v22.5.0
leetcode303 Weekly Match Replay
工程水文学名词解释总结
Character pointer assignment [easy to understand]
LeetCode_733_图像渲染
对话庄表伟:开源第一课
mongo进入报错