CNN 卷积神经网络,我认为是深度学习里面最有意思,最excited的一部分。CNN和神经网络不一样的是,它增加了两个新的操作,一个是卷积,一个是池化。这给了CNN很强大的学习能力,在很多深度学习的场景下面,尤其是图像方面,取得了很好的效果。
先从卷积和池化说起
-
卷积
举个例子:
卷积运算
![](https://img.haomeiwen.com/i2730963/a565ac00ed910e02.gif)
在tensorflow里面,有两种方式的卷积,一种是“same”,一种是“想不到了”。
+ tensorflow 代码实现卷积
- 池化
池化常见的方式有两种最大池化和平均池化。
带有 2×2 和过滤器的且步幅为 2 的最大池化的例子
![](https://img.haomeiwen.com/i2730963/dfb6931ecbe9e206.png)
为什么要使用卷积?
下面看一下CNN是怎么分辨输入的图像是x还是o,如果逐个像素点对比,那肯定是不行的。因为如果这个x发生一点微小的旋转,那么像素点的排列会发生一个很大的改变。比如,下图。
![](https://img.haomeiwen.com/i2730963/c7693ba2c23bc496.png)
但是微小的旋转,并不会影响图片的局部性 。(可以理解为这些特征依然存在,只不过是换了地方)如下图所示。通过卷积窗口的移动,依然可以有效的捕捉这些特征。
![](https://img.haomeiwen.com/i2730963/a517a6ab9210b05b.png)
小结一下
- 对于图片的分类问题,CNN有效的解决。它不在意具体每个点的像素,而是通过一种叫卷积的手段,去**提取图片的特征。
- 对于CNN而言,第一步就是提取特征,卷积就是提取猜测特征的神奇手段。而我们不需要指定特征,任凭它自己去猜测。(通过大量的训练来让机器自己去学习)
一个简单的cnn网络
一个单步卷积的过程 :
![](https://img.haomeiwen.com/i2730963/617c400609f5dc1a.png)
一个demo,cnn识别手写字符集
"""
卷积神经网络结构:
两个卷积层,两个全连接层
输入 [sample * 28 * 28 * 1 ] (灰度图)
[ 28 * 28 *1 ] --> (32个卷积核,每个大小5*5*1,sample方式卷积) --> [ 28 * 28 * 32] --> (池化 2*2 ,步长2)--> [14 *14 *32]
[ 14 * 14 *32] --> (64个卷积核,每个大小 5 * 5 * 32,sample方式卷积) --> [14 * 14 *64] --> (池化 2*2 ,步长2)--> [7 * 7 *64]
[ 7 * 7 * 64] --> reshape 成列向量 --> (7 * 7 * 64)
[sample * (7*7*64)] 全连接层1 weights:[7*7*64 , 1024] --> [sample * 1024]
[sample * 1024] 全连接层2 weights:[1024,10] --> [sample *10]
输出:10个分类
"""
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
# number 1 to 10 data
mnist = input_data.read_data_sets('MNIST_data', one_hot=True)
def compute_accuracy(v_xs, v_ys):
global prediction
y_pre = sess.run(prediction, feed_dict={xs: v_xs, keep_prob: 1})
correct_prediction = tf.equal(tf.argmax(y_pre,1), tf.argmax(v_ys,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
result = sess.run(accuracy, feed_dict={xs: v_xs, ys: v_ys, keep_prob: 1})
return result
def weight_variable(shape):
initial = tf.truncated_normal(shape, stddev=0.1)
return tf.Variable(initial)
def bias_variable(shape):
initial = tf.constant(0.1, shape=shape)
return tf.Variable(initial)
def conv2d(x, W):
# stride [1, x_movement, y_movement, 1]
# Must have strides[0] = strides[3] = 1
return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')
def max_pool_2x2(x):
# stride [1, x_movement, y_movement, 1]
return tf.nn.max_pool(x, ksize=[1,2,2,1], strides=[1,2,2,1], padding='SAME')
# define placeholder for inputs to network
xs = tf.placeholder(tf.float32, [None, 784])/255. # 28x28
ys = tf.placeholder(tf.float32, [None, 10])
keep_prob = tf.placeholder(tf.float32)
#把xs的形状变成[-1,28,28,1],-1代表先不考虑输入的图片例子多少这个维度,后面的1是channel的数量
#因为我们输入的图片是黑白的,因此channel是1,例如如果是RGB图像,那么channel就是3。
x_image = tf.reshape(xs, [-1, 28, 28, 1])
# print(x_image.shape) # [n_samples, 28,28,1]
#建立卷积层
## 本层我们的卷积核patch的大小是5x5,因为黑白图片channel是1所以输入是1,输出是32个featuremap ??? 32 是什么意思?
W_conv1 = weight_variable([5,5, 1,32]) # patch 5x5, in size 1, out size 32
b_conv1 = bias_variable([32])
#卷积运算
h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1) # output size 28x28x32
h_pool1 = max_pool_2x2(h_conv1) # output size 14x14x32
W_conv2 = weight_variable([5,5, 32, 64]) # patch 5x5, in size 32, out size 64 ??? 64 是什么意思?
b_conv2 = bias_variable([64])
h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2) # output size 14x14x64
h_pool2 = max_pool_2x2(h_conv2) # output size 7x7x64
#全连接层
W_fc1 = weight_variable([7*7*64, 1024])
b_fc1 = bias_variable([1024])
# [n_samples, 7, 7, 64] ->> [n_samples, 7*7*64]
h_pool2_flat = tf.reshape(h_pool2, [-1, 7*7*64])
h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)
h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)
## fc2 layer ##
W_fc2 = weight_variable([1024, 10])
b_fc2 = bias_variable([10])
prediction = tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2)
# the error between prediction and real data
cross_entropy = tf.reduce_mean(-tf.reduce_sum(ys * tf.log(prediction),reduction_indices=[1])) # loss
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
sess = tf.Session()
if int((tf.__version__).split('.')[1]) < 12 and int((tf.__version__).split('.')[0]) < 1:
init = tf.initialize_all_variables()
else:
init = tf.global_variables_initializer()
sess.run(init)
# 原来是1000 为了快一点 改成了1000
for i in range(500):
batch_xs, batch_ys = mnist.train.next_batch(100)
sess.run(train_step, feed_dict={xs: batch_xs, ys: batch_ys, keep_prob: 0.5})
if i % 50 == 0:
print(compute_accuracy(mnist.test.images[:1000], mnist.test.labels[:1000]))
网友评论