1. 程式人生 > >【TensorFlow】tf.nn.conv2d是怎樣實現卷積的?有1*1(1×1)卷積介紹

【TensorFlow】tf.nn.conv2d是怎樣實現卷積的?有1*1(1×1)卷積介紹

除去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相同,有一個地方需要注意,第三維in_channels,就是引數input的第四維

第三個引數strides:卷積時在影象每一維的步長,這是一個一維的向量,長度4

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

第五個引數:use_cudnn_on_gpu:bool型別,是否使用cudnn加速,預設為true

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

那麼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,這就相當於每一個畫素點,卷積核都與該畫素點的每一個通道做點積

  1. input = tf.Variable(tf.random_normal([1,3,3,
    5]))  
  2. filter = tf.Variable(tf.random_normal([1,1,5,1]))  
  3. op = tf.nn.conv2d(input, filter, strides=[1111], padding='VALID')  
3.把卷積核擴大,現在用3×3的卷積核做卷積,最後的輸出是一個值,相當於情況2的feature map所有畫素點的值求和
  1. input = tf.Variable(tf.random_normal([1,3,3,5]))  
  2. filter = tf.Variable(tf.random_normal([3,3,5,1]))  
  3. op = tf.nn.conv2d(input, filter, strides=[1111], padding='VALID')  
4.使用更大的圖片將情況2的圖片擴大到5×5,仍然是3×3的卷積核,令步長為1,輸出3×3的feature map
  1. input = tf.Variable(tf.random_normal([1,5,5,5]))  
  2. filter = tf.Variable(tf.random_normal([3,3,5,1]))  
  3. op = tf.nn.conv2d(input, filter, strides=[1111], padding='VALID')  
注意我們可以把這種情況看成情況2和情況3的中間狀態,卷積核以步長1滑動遍歷全圖,以下x表示的位置,表示卷積核停留的位置,每停留一個,輸出feature map的一個畫素
  1. .....  
  2. .xxx.  
  3. .xxx.  
  4. .xxx.  
  5. .....  

5.上面我們一直令引數padding的值為‘VALID’,當其為‘SAME’時,表示卷積核可以停留在影象邊緣,如下,輸出5×5的feature map

  1. input = tf.Variable(tf.random_normal([1,5,5,5]))  
  2. filter = tf.Variable(tf.random_normal([3,3,5,1]))  
  3. op = tf.nn.conv2d(input, filter, strides=[1111], padding='SAME')  
  1. xxxxx  
  2. xxxxx  
  3. xxxxx  
  4. xxxxx  
  5. xxxxx  
6.如果卷積核有多個
  1. input = tf.Variable(tf.random_normal([1,5,5,5]))  
  2. filter = tf.Variable(tf.random_normal([3,3,5,7]))  
  3. op = tf.nn.conv2d(input, filter, strides=[1111], padding='SAME')  
此時輸出7張5×5的feature map

7.步長不為1的情況,文件裡說了對於圖片,因為只有兩維,通常strides取[1,stride,stride,1]

  1. input = tf.Variable(tf.random_normal([1,5,5,5]))  
  2. filter = tf.Variable(tf.random_normal([3,3,5,7]))  
  3. op = tf.nn.conv2d(input, filter, strides=[1221], padding='SAME')  
此時,輸出7張3×3的feature map
  1. x.x.x  
  2. .....  
  3. x.x.x  
  4. .....  
  5. x.x.x  
8.如果batch值不為1,同時輸入10張圖
  1. input = tf.Variable(tf.random_normal([10,5,5,5]))  
  2. filter = tf.Variable(tf.random_normal([3,3,5,7]))  
  3. op = tf.nn.conv2d(input, filter, strides=[1221], padding='SAME')  
每張圖,都有7張3×3的feature map,輸出的shape就是[10,3,3,7]

最後,把程式總結一下:

  1. import tensorflow as tf  
  2. #case 2
  3. input = tf.Variable(tf.random_normal([1,3,3,5]))  
  4. filter = tf.Variable(tf.random_normal([1,1,5,1]))  
  5. op2 = tf.nn.conv2d(input, filter, strides=[1111], padding='VALID')  
  6. #case 3
  7. input = tf.Variable(tf.random_normal([1,3,3,5]))  
  8. filter = tf.Variable(tf.random_normal([3,3,5,1]))  
  9. op3 = tf.nn.conv2d(input, filter, strides=[1111], padding='VALID')  
  10. #case 4
  11. input = tf.Variable(tf.random_normal([1,5,5,5]))  
  12. filter = tf.Variable(tf.random_normal([3,3,5,1]))  
  13. op4 = tf.nn.conv2d(input, filter, strides=[1111], padding='VALID')  
  14. #case 5
  15. input = tf.Variable(tf.random_normal([1,5,5,5]))  
  16. filter = tf.Variable(tf.random_normal([3,3,5,1]))  
  17. op5 = tf.nn.conv2d(input, filter, strides=[1111], padding='SAME')  
  18. #case 6
  19. input = tf.Variable(tf.random_normal([1,5,5,5]))  
  20. filter = tf.Variable(tf.random_normal([3,3,5,7]))  
  21. op6 = tf.nn.conv2d(input, filter, strides=[1

    相關推薦

    TensorFlowtf.nn.conv2d是怎樣實現的?

    文章出處:http://blog.csdn.net/mao_xiao_feng/article/details/53444333 tf.nn.conv2d是TensorFlow裡面實現卷積的函式,參考文件對它的介紹並不是很詳細,實際上這是搭建卷積神經網路比較

    TensorFlowtf.nn.conv2d是怎樣實現的?1*11×1介紹

    除去name引數用以指定該操作的name,與方法有關的一共五個引數: 第一個引數input:指需要做卷積的輸入影象,它要求是一個Tensor,具有[batch, in_height, in_width, in_channels]這樣的shape,具體含義是[訓練時一個batch的圖片數量, 圖片

    TensorFlowtf.nn.max_pool實現池化操作

    版權宣告:本文為博主原創文章,轉載請註明出處。    https://blog.csdn.net/mao_xiao_feng/article/details/53453926 max pooling是CNN當中的最大值池化操作,其實用法和卷積很類似 有些地方可以從卷積去參考

    TensorFlowtf.nn.conv2d_transpose是怎樣實現反捲的?

    三個月沒更新了啊,回來更一發~~ csdn上主要講一些coding過程中遇到的函式,問題,解決方案。偏實踐 另外,如果你想看一些理論方面的東西,歡迎加我的知乎知乎主頁 csdn私信幾乎不看,有問題交流可以發郵箱:[email protected]或者知乎私

    TensorFlowtf.nn.softmax_cross_entropy_with_logits的用法

    white 交叉 none padding tomat ros true const cross 在計算loss的時候,最常見的一句話就是 tf.nn.softmax_cross_entropy_with_logits ,那麽它到底是怎麽做的呢? 首先明確一點,loss是代

    Tensorflowtf.nn.separable_conv2d

    【Tensorflow】tf.nn.separable_conv2d實現深度可分卷積   出處:https://blog.csdn.net/mao_xiao_feng/article/details/78002811 實驗環境:tensorflow版本1.2.0,python2

    Tensorflowtf.nn.dropout函式

    tf.nn.dropout(x, keep_prob, noise_shape=None, seed=None, name=None) 此函式是為了防止在訓練中過擬合的操作,將訓練輸出按一定規則進行變

    Tensorflowtf.nn.atrous_conv2d如何實現空洞

    value:  指需要做卷積的輸入影象,要求是一個4維Tensor,具有[batch, height, width, channels]這樣的shape,具體含義是[訓練時一個batch的圖片數量, 圖片高度, 圖片寬度, 影象通道數] filters: 

    tensorflow隨筆-tf.nn.conv2d運算(2)

    #!/usr/bin/env python2 # -*- coding: utf-8 -*- """ Created on Tue Oct 2 13:23:27 2018 @author: myh

    tensorflow隨筆-tf.nn.conv2d運算(3)

    #!/usr/bin/env python2 # -*- coding: utf-8 -*- """ Created on Tue Oct 2 13:23:27 2018 @author: myha

    tensorflow隨筆-tf.nn.conv2d運算(5)

    #!/usr/bin/env python2 # -*- coding: utf-8 -*- """ Created on Tue Oct 2 13:23:27 2018 @author: myha

    tensorflow隨筆-tf.nn.conv2d運算(1)

    #!/usr/bin/env python2 # -*- coding: utf-8 -*- """ Created on Tue Oct 2 13:23:27 2018 @author: myha

    tensorflow隨筆-tf.nn.conv2d運算(10)

    運動模糊 #!/usr/bin/env python2 # -*- coding: utf-8 -*- """ Created on Tue Oct 2 13:23:27 2018 @author:

    Tensorflowtf.nn.conv2d實現原理

    先解釋一下tf.nn.conv2d函式:tf.nn.conv2d(input, filter, strides, padding, use_cudnn_on_gpu=None, data_format=None, name=None):給定一個input的張量[batch,

    tensorFlowtf.reshape()報錯信息 - TypeError: Expected binary or unicode string

    bject port cas inpu dimen div nts sof expec 今天在使用tensoflow跑cifar10的CNN分類時候,download一個源碼,但是報錯 TypeError: Failed to convert object of type

    tensorflowtf.identity()

    常與tf.control_dependencies(self, control_inputs)配合使用,只有當這個上下文管理器中的是一個操作時,control_inputs才會執行。 x = tf.Variable(0.0) x_plus_1 = tf.assign_add(x, 1)

    tensorflowtf.get_variable()和tf.Variable()的區別

    1.tf.Variable() tf.Variable(initial_value, trainable=True, collections=None, validate_shape=True, name=None) ##initial_value為變數的初始值 tf.get

    Tensorflowtf.import_graph_def

    tf.import_graph_def tf.import_graph_def(     graph_def,     input_map=None,     return_elements=None,     name=None,     op_dict=None,  

    tensorflow隨筆-tf.nn.conv2d

    基於騰訊雲開發者實驗室 import tensorflow as tf a = tf.constant([1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,1,1,0,0,1,1,0

    Tensorflowtf.app.run() 與 tf.app.flags()

    tf.app.flags tf.app.flags.DEFINE_XXX()用來定義引數項: import tensorflow as tf tf.app.flags.DEFINE_float(