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
原文地址:https://www.cnblogs.com/demo-deng/p/7509347.html