CNN中的卷積理解和實例


卷積操作是使用一個二維卷積核在在批處理的圖片中進行掃描,具體的操作是在每一張圖片上采用合適的窗口大小在圖片的每一個通道上進行掃描。

權衡因素:在不同的通道和不同的卷積核之間進行權衡

在tensorflow中的函數為例:

  • conv2d: 任意的卷積核,能同時在不同的通道上面進行卷積操作。
  卷積核的卷積過程是按照 strides 參數來確定的,比如 strides = [1, 1, 1, 1] 表示卷積核對每個像素點進行卷積,即在二維屏幕上面,兩個軸方向的步長都是1。 strides = [1, 2, 2, 1] 表示卷積核對每隔一個像素點進行卷積,即在二維屏幕上面,兩個軸方向的步長都是2
  卷積操作的空間含義定義如下:如果輸入數據是一個四維的  input ,數據維度是  [batch, in_height, in_width, ...],卷積核也是一個四維的卷積核,數據維度是  [filter_height, filter_width, ...]
  函數:tf.nn.conv2d(input, filter, strides, padding, use_cudnn_on_gpu=None, name=None)
  這個函數的作用是對一個四維的輸入數據  input 和四維的卷積核  filter 進行操作,然后對輸入數據進行一個二維的卷積操作,最后得到卷積之后的結果。
  給定的輸入張量的維度是  [batch, in_height, in_width, in_channels] ,卷積核張量的維度是  [filter_height, filter_width, in_channels, out_channels]
  注意,必須有  strides[0] = strides[3] = 1。在大部分處理過程中,卷積核的水平移動步數和垂直移動步數是相同的,即  strides = [1, stride, stride, 1]
 
實例代碼:
1 input_data = tf.Variable(np.random.rand(10, 6, 6, 3), dtype= np.float32)
2 filter_data = tf.Variable(np.random.rand(2, 2, 3, 1), dtype= np.float32)
3 y = tf.nn.conv2d(input_data, filter_data, strides =[1,1,1,1], padding='VALID')
4 with tf.Session() as sess:
5     init = tf.initialize_all_variables()
6     sess.run(init)
7     a = sess.run(y)
8     print (a)
9     print (tf.shape(a))
輸出:padding='VALID'
維度是(10,5,5,1),計算方法:6-2+1=5

[ 2.3715086 ]
[ 3.50508738]
[ 3.82352686]
[ 3.2169013 ]
[ 2.59157968]]]]

。。。

Tensor("Shape_14:0", shape=(4,), dtype=int32)
 
1 input_data = tf.Variable(np.random.rand(10, 6, 6, 3), dtype= np.float32)
2 filter_data = tf.Variable(np.random.rand(2, 2, 3, 1), dtype= np.float32)
3 y = tf.nn.conv2d(input_data, filter_data, strides =[1,1,1,1], padding='SAME')
4 with tf.Session() as sess:
5     init = tf.initialize_all_variables()
6     sess.run(init)
7     a = sess.run(y)
8     print (a)
9     print (tf.shape(a))

輸出:padding='SAME'

維度是(10,6,6,1)

[ 1.61058581]
[ 1.08910465]
[ 1.18494463]
[ 1.89793181]
[ 1.41800678]
[ 0.32431859]]]]

。。。

Tensor("Shape_15:0", shape=(4,), dtype=int32)

 
摘自:http://www.jianshu.com/p/e3a79eac554f


免責聲明!

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



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