padding的規則
· padding=‘VALID’時,輸出的寬度和高度的計算公式(下圖gif為例)
輸出寬度:output_width = (in_width-filter_width+1)/strides_width =(5-3+1)/2=1.5【向上取整=2】
輸出高度:output_height = (in_height-filter_height+1)/strides_height =(5-3+1)/2=1.5【向上取整=2】
輸出的形狀[1,2,2,1]

import tensorflow as tf image = [0,1.0,1,2,2,0,1,1,0,0,1,1,0,1,0,1,0,1,1,1,0,2,0,1,0] input = tf.Variable(tf.constant(image,shape=[1,5,5,1])) ##1通道輸入 fil1 = [-1.0,0,1,-2,0,2,-1,0,1] filter = tf.Variable(tf.constant(fil1,shape=[3,3,1,1])) ##1個卷積核對應1個featuremap輸出 op = tf.nn.conv2d(input,filter,strides=[1,2,2,1],padding='VALID') ##步長2,VALID不補0操作 init = tf.global_variables_initializer() with tf.Session() as sess: sess.run(init) # print('input:\n', sess.run(input)) # print('filter:\n', sess.run(filter)) print('op:\n',sess.run(op)) ##輸出結果 ''' [[[[ 2.] [-1.]] [[-1.] [ 0.]]]] '''
如果strides=[1,3,3,1]的情況又是如何呢?
輸出寬度:output_width = (in_width-filter_width+1)/strides_width =(5-3+1)/3=1
輸出高度:output_height = (in_height-filter_height+1)/strides_height =(5-3+1)/3=1
輸出的形狀[1,1,1,1],因此輸出的結果只有一個

import tensorflow as tf image = [0,1.0,1,2,2,0,1,1,0,0,1,1,0,1,0,1,0,1,1,1,0,2,0,1,0] input = tf.Variable(tf.constant(image,shape=[1,5,5,1])) ##1通道輸入 fil1 = [-1.0,0,1,-2,0,2,-1,0,1] filter = tf.Variable(tf.constant(fil1,shape=[3,3,1,1])) ##1個卷積核對應1個featuremap輸出 op = tf.nn.conv2d(input,filter,strides=[1,3,3,1],padding='VALID') ##步長2,VALID不補0操作 init = tf.global_variables_initializer() with tf.Session() as sess: sess.run(init) # print('input:\n', sess.run(input)) # print('filter:\n', sess.run(filter)) print('op:\n',sess.run(op)) ##輸出結果 ''' op: [[[[ 2.]]]] '''
padding=‘SAME’時,輸出的寬度和高度的計算公式
輸出寬度:output_width = in_width/strides_width=5/2=2.5【向上取整3】
輸出高度:output_height = in_height/strides_height=5/2=2.5【向上取整3】
則輸出的形狀:[1,3,3,1]
那么padding補0的規則又是如何的呢?【先確定輸出形狀,再計算補多少0】
pad_width = max((out_width-1)*strides_width+filter_width-in_width,0)=max((3-1)*2+3-5,0)=2
pad_height = max((out_height-1)*strides_height+filter_height-in_height,0)=max((3-1)*2+3-5,0)=2
pad_top = pad_height/2=1
pad_bottom = pad_height-pad_top=1
pad_left = pad_width/2=1
pad_right = pad_width-pad_left=1

import tensorflow as tf image = [0,1.0,1,2,2,0,1,1,0,0,1,1,0,1,0,1,0,1,1,1,0,2,0,1,0] input = tf.Variable(tf.constant(image,shape=[1,5,5,1])) ##1通道輸入 fil1 = [-1.0,0,1,-2,0,2,-1,0,1] filter = tf.Variable(tf.constant(fil1,shape=[3,3,1,1])) ##1個卷積核對應1個featuremap輸出 op = tf.nn.conv2d(input,filter,strides=[1,2,2,1],padding='SAME') ##步長2,VALID不補0操作 init = tf.global_variables_initializer() with tf.Session() as sess: sess.run(init) # print('input:\n', sess.run(input)) # print('filter:\n', sess.run(filter)) print('op:\n',sess.run(op)) ##輸出結果 ''' op: [[[[ 3.] [ 1.] [-4.]] [[ 3.] [ 0.] [-3.]] [[ 4.] [-1.] [-3.]]]] '''
如果步長為3呢?補0的規則又如何?
輸出寬度:output_width = in_width/strides_width=5/3=2
輸出高度:output_height = in_height/strides_height=5/3=2
則輸出的形狀:[1,2,2,1]
那么padding補0的規則又是如何的呢?【先確定輸出形狀,再計算補多少0】
pad_width = max((out_width-1)*strides_width+filter_width-in_width,0)=max((2-1)*3+3-5,0)=1
pad_height = max((out_height-1)*strides_height+filter_height-in_height,0)=max((2-1)*3+3-5,0)=1
pad_top = pad_height/2=0【向下取整】
pad_bottom = pad_height-pad_top=1
pad_left = pad_width/2=0【向下取整】
pad_right = pad_width-pad_left=1

import tensorflow as tf print(3/2) image = [0,1.0,1,2,2,0,1,1,0,0,1,1,0,1,0,1,0,1,1,1,0,2,0,1,0] input = tf.Variable(tf.constant(image,shape=[1,5,5,1])) ##1通道輸入 fil1 = [-1.0,0,1,-2,0,2,-1,0,1] filter = tf.Variable(tf.constant(fil1,shape=[3,3,1,1])) ##1個卷積核對應1個featuremap輸出 op = tf.nn.conv2d(input,filter,strides=[1,3,3,1],padding='SAME') ##步長2,VALID不補0操作 init = tf.global_variables_initializer() with tf.Session() as sess: sess.run(init) # print('input:\n', sess.run(input)) # print('filter:\n', sess.run(filter)) print('op:\n',sess.run(op)) ##輸出結果 ''' op: [[[[ 2.] [-3.]] [[ 0.] [-3.]]]] '''