您的位置:首页 > 其它

TensorFlow实现mnist数字识别——CNN LeNet-5模型

2018-02-23 10:15 926 查看
一、概述LeNet-5模型 
    构成:卷积层--池化层--卷积层--池化层--全连接层--全连接层

    1.1 卷积层

        使用n*n的滤波器来进行图像信息的采集        如下展示了一个3×3的卷积核在5×5的图像上做卷积的过程。每个卷积都代表了一种特征提取方式,就像一个筛子,将图像中符合条件(激活值越大越符合条件)的部分筛选出来。

        局部感受野其实是一个滤波的过程,也是一个对图像降维的过程。        维度减小,深度加深
        1.2池化层        池化是对特征向量的下采样,进一步降低了特征的维度,减少了计算量,并且使训练不容易出现过拟合现象。一般有两种池化方式,一种是平均池化(mean-pooling),一种是最大值池化(max-pooling)。池化也是一种滤波。    1.3 全连接  full connect              w * x + b = y 
二、构建CNN层    2.1 定义各个节点的相关参数
input_node  = 784   #输入层的节点数  相当于图片的像素28*28
output_node = 10    #输出层的节点数  相当于分类器的类别的个数
IMAGE_SIZE = 28
IMAGE_CHANNELS = 1
NUM_LABELS = 10
#第一层卷积尺寸和深度
CONV1_DEEP = 32
CONV1_SIZE = 5
#第二层卷积尺寸和深度
CONV2_DEEP = 64
CONV2_SIZE = 5
#全连接层的节点个数
FC_SIZE = 512


2.2 构建CNN网络#================================== 第一层 卷积层 ==============================//
with tf.variable_scope('layer1_cov1'):
cov1_weights = tf.get_variable("weight",[CONV1_SIZE,
CONV1_SIZE,
IMAGE_CHANNELS,
CONV1_DEEP], #过滤器的深度
initializer=tf.truncated_normal_initializer(stddev = 0.1))

cov1_bias = tf.get_variable("bias",[CONV1_DEEP],initializer = tf.constant_initializer(0.0))

cov1 = tf.nn.conv2d(input_tensor, #代表第几张图片
cov1_weights, #权重
strides=[1,1,1,1], #前后两位必须为1 中间代表步长step 长宽每次步进1
padding='SAME') #采用0来填充图片

#添加偏置项 + 计算结果正则化
relu1 = tf.nn.relu(tf.nn.bias_add(cov1,cov1_bias))
#================================== 第二层 池化层 ===============================//
#这一层的输入为上一层的输出  即28*28*32   池化层选取的过滤器边长为2  使用全0填充
#移动的步长也为2   因此 输出为14*14*32
with tf.name_scope("layer2-pool1"):
pool1 = tf.nn.max_pool(
relu1,
ksize=[1,2,2,1],    #过滤器的尺寸  首尾必须为1  中间代表过滤器的尺寸 2*2
strides=[1,2,2,1],  #后两位必须为1   中间代表步长step 长宽每次步进2
padding='SAME'      #采用0来填充图片
)
#================================== 第三层  卷积层 ==============================//
#这一层的输入为上一层的输出  即14*14*32  batch为5*5  使用全0填充
#移动的步长也为1   因此 输出为14*14*64
with tf.variable_scope('layer3_cov2'):
cov2_weights = tf.get_variable("weight",[CONV2_SIZE,
CONV2_SIZE,
CONV1_DEEP,
CONV2_DEEP],  #过滤器的深度
initializer=tf.truncated_normal_initializer(stddev = 0.1))

cov2_bias    = tf.get_variable("bias",[CONV2_DEEP],initializer = tf.constant_initializer(0.0))

cov2 = tf.nn.conv2d(pool1,              #代表第几张图片
cov2_weights,       #权重
strides=[1,1,1,1],  #前后两位必须为1   中间代表步长step 长宽每次步进1
padding='SAME')     #采用0来填充图片

#添加偏置项 + 计算结果正则化
relu2  = tf.nn.relu(tf.nn.bias_add(cov2,cov2_bias))
#================================== 第四层 池化层 ===============================//

#这一层的输入为上一层的输出  即14*14*64   池化层选取的过滤器边长为2  使用全0填充
#移动的步长也为2   因此 输出为7*7*64
with tf.name_scope("layer4-pool2"):
pool2 = tf.nn.max_pool(
relu2,
ksize=[1,2,2,1],    #过滤器的尺寸  首尾必须为1  中间代表过滤器的尺寸 2*2
strides=[1,2,2,1],  #后两位必须为1   中间代表步长step 长宽每次步进2
padding='SAME'      #采用0来填充图片
)
#==================== 将第四层的输出转化为第五层全连接的输入格式 ======================//

#7*7*64的矩阵拉成一维向量  3136
pool_shape = pool2.get_shape().as_list()
nodes = pool_shape[1] * pool_shape[2] * pool_shape[3]
#将第四层输出变成一个batch的向量
reshaped = tf.reshape(pool2,[pool_shape[0],nodes])
#================================== 第五层 全连接1 ===============================//

#将3136的一维数组经过全连接之后输出512
# 输入reshaped  为 1*3136
# w为3136*512
# b为1*512
# 输出fc1为 1*512

with tf.variable_scope('layer5_fc1'):
fc1_weights = tf.get_variable("weight",[nodes,
FC_SIZE],  # 全连接尺寸
initializer=tf.truncated_normal_initializer(stddev = 0.1))

#加入正则化
if regularizer!=None:
tf.add_to_collection('losses',regularizer(fc1_weights)) #将当前变量的正则化损失加入losses集合

fc1_biasws =  tf.get_variable("bias",[FC_SIZE],initializer = tf.constant_initializer(0.1))
fc1  = tf.nn.relu(tf.matmul(reshaped,fc1_weights)+fc1_biasws)
if train:
fc1 = tf.nn.dropout(fc1,0.5)  #丢弃掉部分点  防止过拟合
#================================== 第六层 全连接2 ===============================//

#将512的一维数组经过全连接之后输出10
# fc1  为 1*512
# w为512*10
# b为1*10
# 输出fc1为 1*10

with tf.variable_scope('layer6_fc2'):
fc2_weights = tf.get_variable("weight",[FC_SIZE,
NUM_LABELS],  # 全连接尺寸
initializer=tf.truncated_normal_initializer(stddev = 0.1))

#加入正则化
if regularizer!=None:
tf.add_to_collection('losses',regularizer(fc2_weights)) #将当前变量的正则化损失加入losses集合
fc2_biasws =  tf.get_variable("bias",[NUM_LABELS],initializer = tf.constant_initializer(0.1))
logit  = tf.matmul(fc1,fc2_weights)+fc2_biasws
三、定义训练过程
四、定义测试过程
        同http://blog.csdn.net/yunge812/article/details/79347905
下载地址:
        http://download.csdn.net/download/yunge812/10255801
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐