在卷积神经网络设计网型的时候,需要对卷积核和池化层进行参数设置。有三个重要的参数,首先是卷积核的大小,其次是设置步长(padding)的大小,最后是是否采用padding。这几个因素直接影响了卷积、池化后的特征图的大小,对于网络形状的设计非常重要的参数。本博客将针对这三个参数进行解释,并且利用tensorflow进行结论的实验。
一、卷积核
卷积核这个东西比较好理解,就是卷积滤波器的大小,当然,卷积核也可能是三维的。卷积的过程如如图2所示,其中的卷积核是3*3的卷积。三维卷积的形状如图1。
图1 三维卷积
二、步长(stride)
所谓的步长就是卷积核在原始图像上每一次移动的步数,如图2所示该卷积操作的步长为1,步长用stride来表示。
图2 卷积操作的过程
三、填充(padding)
当我们用一个卷积核去和一个影像进行卷积操作的时候,我们可以发现,不同位置的像素利用率是不同的,例如位于图像中心的像素,它参与了多次的卷积运算,对整个卷积过程的贡献大。但是想图像四个角的像素只参与了以此的卷积运算,贡献就被忽略了。另外,由于卷积操作,使得原始图像的大小不断地减小。为了避免上述的两个问题,我们采用填充的方式,就是在原始图像的四周再添加一圈像素值,这样就能够保证每一个原始图像的像素都参与相同贡献的卷积操作。
从图中可以看出,蓝色的原始影像进行了一个像素的padding操作,这样就使得位于角点的像素也能参与更多的卷积过程。
四、卷积后特征图大小的计算。参考博客卷积后图像大小计算
1.卷积后尺寸计算
out_height=(in_height+2pad-filter_height)/strides[1]+1
out_width=(in_width+2pad-filter_width)/strides[2] +1
2.tensorflow中卷积参数same和valid运算之后的维度计算
(1)same
out_height=ceil(float(in_height))/float(strides[1])
out_width=ceil(float(in_width))/float(strides[2])
(2)valid
out_height=ceil(float(in_height-filter_height+1))/float(strides[1])
out_width=ceil(float(in_width-filter_width+1))/float(strides[2])
(3)参数
padding: SAME和VALID两种形式
filter: [5,5,1,32]表示5*5的卷积核,1个channel,32个卷积核。
strides: [1,4,4,1]表示横向和竖向的步长都为4
五、实验
利用tensorflow进行实际的实验操作。实验的源代码如下:
-
# -*- coding: utf-8 -*-
-
import tensorflow
as tf
-
import numpy
as np
-
"""
-
Created on Tue Jul 17 10:03:21 2018
-
-
@author: C.H.
-
"""
-
tf.reset_default_graph()
#这一句话非常重要,如果没有这句话,就会出现重复定义变量的错误
-
x = tf.placeholder(tf.float32, shape=(
1,
500,
500,
3))
-
#分别设置3*3,5*5,7*7三种大小的卷积核
-
weights1 = tf.get_variable(
'weights1',shape=[
3,
3,
3,
16],dtype=tf.float32,initializer=tf.truncated_normal_initializer(stddev=
0.1, dtype=tf.float32))
-
weights2 = tf.get_variable(
'weights2',shape=[
5,
5,
3,
16],dtype=tf.float32,initializer=tf.truncated_normal_initializer(stddev=
0.1, dtype=tf.float32))
-
weights3 = tf.get_variable(
'weights3',shape=[
7,
7,
3,
16],dtype=tf.float32,initializer=tf.truncated_normal_initializer(stddev=
0.1, dtype=tf.float32))
-
#第一组实验采用步长为1,填充采用SAME,然后采用三种不同大小的卷积核来进行实验,讨论卷积核对卷积后图像大小的影响。第一组实验为其他实验的对照组
-
conv1 = tf.nn.conv2d(x, weights1, strides=[
1,
1,
1,
1], padding=
'SAME')
-
conv2 = tf.nn.conv2d(x, weights2, strides=[
1,
1,
1,
1], padding=
'SAME')
-
conv3 = tf.nn.conv2d(x, weights3, strides=[
1,
1,
1,
1], padding=
'SAME')
-
#第二组实验,控制卷积核的大小为3*3,分别采用1,2,3三种步长,padding方式采用SAME,讨论步长对卷积后图像大小的影响。
-
conv4 = tf.nn.conv2d(x, weights1, strides=[
1,
1,
1,
1], padding=
'SAME')
-
conv5 = tf.nn.conv2d(x, weights1, strides=[
1,
2,
2,
1], padding=
'SAME')
-
conv6 = tf.nn.conv2d(x, weights1, strides=[
1,
3,
3,
1], padding=
'SAME')
-
#第三组实验,与第一组实验对照,选择和第一组实验相同的卷积核大小和步长,采用padding的填充方式进行测试。讨论不同padding方式对卷积后图像的影响
-
conv7 = tf.nn.conv2d(x, weights1, strides=[
1,
1,
1,
1], padding=
'VALID')
-
conv8 = tf.nn.conv2d(x, weights2, strides=[
1,
1,
1,
1], padding=
'VALID')
-
conv9 = tf.nn.conv2d(x, weights3, strides=[
1,
1,
1,
1], padding=
'VALID')
-
#池化过程的'VALID','SAME'参数的对照。讨论不同参数设置对最大池化过程后图像大小的影响
-
pool1 = tf.nn.max_pool(x, [
1,
3,
3,
1], [
1,
3,
3,
1], padding =
'VALID')
-
pool2 = tf.nn.max_pool(x, [
1,
3,
3,
1], [
1,
3,
3,
1], padding =
'SAME')
-
-
with tf.Session()
as sess:
-
a = np.full((
1,
500,
500,
3),
2)
-
sess.run(tf.global_variables_initializer())
-
conv1= sess.run(conv1,feed_dict={x: a})
-
conv2= sess.run(conv2,feed_dict={x: a})
-
conv3= sess.run(conv3,feed_dict={x: a})
-
conv4= sess.run(conv4,feed_dict={x: a})
-
conv5= sess.run(conv5,feed_dict={x: a})
-
conv6= sess.run(conv6,feed_dict={x: a})
-
conv7= sess.run(conv7,feed_dict={x: a})
-
conv8= sess.run(conv8,feed_dict={x: a})
-
conv9= sess.run(conv9,feed_dict={x: a})
-
pool1= sess.run(pool1,feed_dict={x: a})
-
pool2= sess.run(pool2,feed_dict={x: a})
-
print(conv1.shape)
-
print(conv2.shape)
-
print(conv3.shape)
-
print(conv4.shape)
-
print(conv5.shape)
-
print(conv6.shape)
-
print(conv7.shape)
-
print(conv8.shape)
-
print(conv9.shape)
-
print(pool1.shape)
-
print(pool2.shape)
实验的结果如下:
-
runfile(
'E:/Study/研究生文档/深度学习资料/深度学习程序/computeshapeafterconv.py', wdir=
'E:/Study/研究生文档/深度学习资料/深度学习程序')
-
(
1,
500,
500,
16)
#out=ceil(float(in))/float(strides)
-
(
1,
500,
500,
16)
#out=ceil(float(in))/float(strides)
-
(
1,
500,
500,
16)
#out=ceil(float(in))/float(strides)
-
(
1,
500,
500,
16)
#out=ceil(float(in))/float(strides)
-
(
1,
250,
250,
16)
#out=ceil(float(in))/float(strides)
-
(
1,
167,
167,
16)
#out=ceil(float(in))/float(strides)
-
(
1,
498,
498,
16)
#out=ceil(float(in-filter+1))/float(strides)
-
(
1,
496,
496,
16)
#out=ceil(float(in-filter+1))/float(strides)
-
(
1,
494,
494,
16)
#out=ceil(float(in-filter+1))/float(strides)
-
(
1,
166,
166,
3)
#out=floor(float(in))/float(strides)
-
(
1,
167,
167,
3)
#out=ceil(float(in))/float(strides)
另外,在程序中出现了两个需要注意的语句。
tf.reset_default_graph()#这一句话非常重要,如果没有这句话,就会出现重复定义变量的错误
这句话如果没有加在程序的最开始,那么如果你想进行多次程序的运行和调试的时候,程序就会报错。
-
ValueError: Variable weights1 already exists, disallowed. Did you mean to set reuse=
True
or reuse=tf.AUTO_REUSE
in VarScope? Originally defined at:
-
-
File
"D:\Anaconda3-5.0.1-Windows-x86_64\RESULT\envs\tensorflow\lib\site-packages\tensorflow\python\framework\ops.py", line
1625,
in __init__
-
self._traceback = self._graph._extract_stack()
# pylint: disable=protected-access
-
File
"D:\Anaconda3-5.0.1-Windows-x86_64\RESULT\envs\tensorflow\lib\site-packages\tensorflow\python\framework\ops.py", line
3160,
in create_op
-
op_def=op_def)
-
File
"D:\Anaconda3-5.0.1-Windows-x86_64\RESULT\envs\tensorflow\lib\site-packages\tensorflow\python\framework\op_def_library.py", line
787,
in _apply_op_helper
-
op_def=op_def)
错误的原因是第一次运行程序的时候,计算图中已经存在所有定义的变量,如果再运行程序,就会重复定义变量。加上重置计算图那个语句就搞定了。
第二个需要注意的地方
conv1= sess.run(conv1,feed_dict={x: a})
在这里将numpy变量a,feed给变量x的时候,每次只能feed一个变量。不能出现下面的写法。
conv1,conv2= sess.run(conv1,conv2,feed_dict={x: a})
如果这样的话就会出现以下错误:
TypeError: run() got multiple values for argument 'feed_dict'
总结,这篇博客讨论了卷积运算操作时候的三个重要的参数,卷积核大小,步长大小和填充大小。分别进行了对照试验,验证了总结的经验公式。这种基础操作为网络设计和控制输出tensor大小做了必要的准备。
<li class="tool-item tool-active is-like "><a href="javascript:;"><svg class="icon" aria-hidden="true"> <use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#csdnc-thumbsup"></use> </svg><span class="name">点赞</span> <span class="count">9</span> </a></li> <li class="tool-item tool-active is-collection "><a href="javascript:;" data-report-click="{"mod":"popu_824"}"><svg class="icon" aria-hidden="true"> <use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#icon-csdnc-Collection-G"></use> </svg><span class="name">收藏</span></a></li> <li class="tool-item tool-active is-share"><a href="javascript:;" data-report-click="{"mod":"1582594662_002"}"><svg class="icon" aria-hidden="true"> <use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#icon-csdnc-fenxiang"></use> </svg>分享</a></li> <!--打赏开始--> <!--打赏结束--> <li class="tool-item tool-more"> <a> <svg t="1575545411852" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5717" xmlns:xlink="http://www.w3.org/1999/xlink" width="200" height="200"><defs><style type="text/css"></style></defs><path d="M179.176 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5718"></path><path d="M509.684 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5719"></path><path d="M846.175 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5720"></path></svg> </a> <ul class="more-box"> <li class="item"><a class="article-report">文章举报</a></li> </ul> </li> </ul> </div> </div> <div class="person-messagebox"> <div class="left-message"><a href="https://blog.csdn.net/qq_32466233"> <img src="https://profile.csdnimg.cn/1/0/E/3_qq_32466233" class="avatar_pic" username="qq_32466233"> <img src="https://g.csdnimg.cn/static/user-reg-year/2x/4.png" class="user-years"> </a></div> <div class="middle-message"> <div class="title"><span class="tit"><a href="https://blog.csdn.net/qq_32466233" data-report-click="{"mod":"popu_379"}" target="_blank">梦的漂流瓶</a></span> </div> <div class="text"><span>发布了20 篇原创文章</span> · <span>获赞 13</span> · <span>访问量 1万+</span></div> </div> <div class="right-message"> <a href="https://im.csdn.net/im/main.html?userName=qq_32466233" target="_blank" class="btn btn-sm btn-red-hollow bt-button personal-letter">私信 </a> <a class="btn btn-sm bt-button personal-watch" data-report-click="{"mod":"popu_379"}">关注</a> </div> </div> </div> </article>