tf入門-tf.nn.conv2d是怎樣實現卷積的?

轉自:https://blog.csdn.net/mao_xiao_feng/article/details/78004522python

實驗環境:tensorflow版本1.2.0,python2.7dom


介紹

慣例先展現函數:python2.7

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

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

  • 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相同,有一個地方須要注意,第三維in_channels,就是參數input的第四維spa

  • strides:卷積時在圖像每一維的步長,這是一個一維的向量,長度4.net

  • padding: 
    string類型的量,只能是」SAME」,」VALID」其中之一,這個值決定了不一樣的卷積方式(後面會介紹)code

  • use_cudnn_on_gpu: 
    bool類型,是否使用cudnn加速,默認爲trueorm

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


實驗

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

1.考慮一種最簡單的狀況,如今有一張3×3單通道的圖像(對應的shape:[1,3,3,1]),用一個1×1的卷積核(對應的shape:[1,1,1,1])去作卷積,最後會獲得一張3×3的feature map

2.增長圖片的通道數,使用一張3×3五通道的圖像(對應的shape:[1,3,3,5]),用一個1×1的卷積核(對應的shape:[1,1,1,1])去作卷積,仍然是一張3×3的feature map,這就至關於每個像素點,卷積核都與該像素點的每個通道作點積

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')
  • 1
  • 2
  • 3
  • 4

3.把卷積核擴大,如今用3×3的卷積核作卷積,最後的輸出是一個值,至關於狀況2的feature map全部像素點的值求和

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

4.使用更大的圖片將狀況2的圖片擴大到5×5,仍然是3×3的卷積核,令步長爲1,輸出3×3的feature map

..... .xxx. .xxx. .xxx. .....
  • 1
  • 2
  • 3
  • 4
  • 5

5.上面咱們一直令參數padding的值爲‘VALID’,當其爲‘SAME’時,表示卷積核能夠停留在圖像邊緣,以下,輸出5×5的feature map

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='SAME')
  • 1
  • 2
  • 3
  • 4
xxxxx
xxxxx
xxxxx
xxxxx
xxxxx
  • 1
  • 2
  • 3
  • 4
  • 5

6.若是卷積核有多個

input = tf.Variable(tf.random_normal([1,5,5,5])) filter = tf.Variable(tf.random_normal([3,3,5,7])) op = tf.nn.conv2d(input, filter, strides=[1, 1, 1, 1], padding='SAME')
  • 1
  • 2
  • 3
  • 4

此時輸出7張5×5的feature map

7.步長不爲1的狀況,文檔裏說了對於圖片,由於只有兩維,一般strides取[1,stride,stride,1]

input = tf.Variable(tf.random_normal([1,5,5,5])) filter = tf.Variable(tf.random_normal([3,3,5,7])) op = tf.nn.conv2d(input, filter, strides=[1, 2, 2, 1], padding='SAME')
  • 1
  • 2
  • 3
  • 4

此時,輸出7張3×3的feature map

x.x.x
..... x.x.x ..... x.x.x
  • 1
  • 2
  • 3
  • 4
  • 5

8.若是batch值不爲1,同時輸入10張圖

input = tf.Variable(tf.random_normal([10,5,5,5])) filter = tf.Variable(tf.random_normal([3,3,5,7])) op = tf.nn.conv2d(input, filter, strides=[1, 2, 2, 1], padding='SAME')
  • 1
  • 2
  • 3
  • 4

每張圖,都有7張3×3的feature map,輸出的shape就是[10,3,3,7]

相關文章
相關標籤/搜索