计算卷积后尺寸

这里单独把计算卷积之后的维度的公式拿出来,方便查看
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
3.tensorflow里面的tf.pad函数
虽然在卷积过程中有same选项可选,但是tensorflow里面还有一个可以用于填充的函数tf.pad(),具体用法如下:

tf.pad(tensor, paddings,mode='CONSTANT',name=None)

padings : 是一个张量,代表每一维填充多少行/列
mode : “CONSTANT” ,”REFLECT”,”SYMMETRIC”
“CONSTANT” 填充0, “REFLECT”是映射填充,上下(1维)填充顺序和paddings是相反的,左右(零维)顺序补齐, “SYMMETRIC”是对称填充,上下(1维)填充顺序是和paddings相同的,左右(零维)对称补齐
4.在这里补充一下,关于tensorflow中的max_pool()中的padding的含义,之前一直不能理解,现在理解了
(1)先给出代码

import tensorflow as tf
import numpy as np 

x = tf.placeholder(tf.float32, shape=(1, 500, 500, 3))
#print(type(image))
y1 = tf.nn.max_pool(x, [1, 3, 3, 1], [1, 3, 3, 1], padding = 'VALID')
y2 = 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())
    b = sess.run(y1, feed_dict={x: a})
    c = sess.run(y2, feed_dict={x: a})
    print(b.shape)  # Will succeed.  
    print(c.shape)  # Will succeed.  

(2)结果

(1, 166, 166, 3) #valid
(1, 167, 167, 3) #same

从结果可以看出:
对于padding=’VALID’, 166 = math.floor(l500/3)
对于padding=’SAME’, 167 =math.ceil( 500/3)

猜你喜欢

转载自blog.csdn.net/xuan_zizizi/article/details/78589022