tensorflow conv2d的padding解釋以及參數解釋


1、padding的方式:

說明:

1、摘錄自http://stackoverflow.com/questions/37674306/what-is-the-difference-between-same-and-valid-padding-in-tf-nn-max-pool-of-t

2、不同的padding方式,VALID是采用丟棄的方式,比如上述的input_width=13,只允許滑動2次,多余的元素全部丟掉

3、SAME的方式,采用的是補全的方式,對於上述的情況,允許滑動3次,但是需要補3個元素,左奇右偶,在左邊補一個0,右邊補2個0

4、For the SAME padding, the output height and width are computed as:

 

  • out_height = ceil(float(in_height) / float(strides[1]))

    out_width = ceil(float(in_width) / float(strides[2]))

  • For the VALID padding, the output height and width are computed as:

    out_height = ceil(float(in_height - filter_height + 1) / float(strides[1]))

    out_width = ceil(float(in_width - filter_width + 1) / float(strides[2]))

2、conv2d的參數

 

1、strides[0] = strides[3] = 1

3、conv2d的參數解釋:

 

tf.nn.conv2d(input, filter, strides, padding, use_cudnn_on_gpu=None, name=None)

除去name參數用以指定該操作的name,與方法有關的一共五個參數

 

 

第一個參數input:指需要做卷積的輸入圖像,它要求是一個Tensor,具有[batch, in_height, in_width, in_channels]這樣的shape,具體含義是[訓練時一個batch的圖片數量, 圖片高度, 圖片寬度, 圖像通道數],注意這是一個4維的Tensor,要求類型為float32和float64其中之一

第二個參數filter:相當於CNN中的卷積核,它要求是一個Tensor,具有[filter_height, filter_width, in_channels, out_channels]這樣的shape,具體含義是[卷積核的高度,卷積核的寬度,圖像通道數,卷積核個數],要求類型與參數input相同,filter的通道數要求與input的in_channels一致,有一個地方需要注意,第三維in_channels,就是參數input的第四維

第三個參數strides:卷積時在圖像每一維的步長,這是一個一維的向量,長度4,strides[0]=strides[3]=1

第四個參數padding:string類型的量,只能是"SAME","VALID"其中之一,這個值決定了不同的卷積方式(后面會介紹)

第五個參數:use_cudnn_on_gpu:bool類型,是否使用cudnn加速,默認為true

結果返回一個Tensor,這個輸出,就是我們常說的feature map

4、conv2d的例子:

那么TensorFlow的卷積具體是怎樣實現的呢,用一些例子去解釋它:

import tensorflow as tf
#case 2
input = tf.Variable(tf.random_normal([1,3,3,5]))
filter = tf.Variable(tf.random_normal([1,1,5,1]))
op = tf.nn.conv2d(input, filter, strides=[1, 1, 1, 1], padding='VALID')

with tf.Session() as sess:
sess.run(tf.initialize_all_variables())
res = (sess.run(op))
print (res.shape)

 

import tensorflow as tf

input = tf.Variable(tf.random_normal([1,5,5,5]))
filter = tf.Variable(tf.random_normal([3,3,5,1]))
op = tf.nn.conv2d(input, filter, strides=[1, 1, 1, 1], padding='VALID')

with tf.Session() as sess:
sess.run(tf.initialize_all_variables())
res = (sess.run(op))
print (res.shape)

 

 

說明:

 

1、使用VALID方式,feature map的尺寸為
out_height = ceil(float(in_height - filter_height + 1) / float(strides[1]))=(5-3+1)/1 = 3

out_width = ceil(float(in_width - filter_width + 1) / float(strides[2])) = (5-3+1)/1 = 3

所以,feature map的尺寸為3*3

2、filter的參數個數為3*3*5*1,也即對於輸入的每個通道數都對應於一個3*3的濾波器,然后共5個通道數,conv2d的過程就是對5個輸入進行點擊然后求和,得到一張feature map。如果要得到3張feature map,那么應該使用的參數為3*3*5*3個參數.

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM