您当前的位置: 首页 >  网络

Better Bench

暂无认证

  • 1浏览

    0关注

    695博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

【北京大学】11 TensorFlow1.x的卷积神经网络模型Lenet5实现

Better Bench 发布时间:2020-12-21 20:50:38 ,浏览量:1

目录
  • 1 Lenet5模型简介
  • 2 TensorFlow实现
    • mnist_lenet5_forward.py文件
    • mnist_lenet5_backward.py文件
    • mnist_lenet5_test.py文件
  • 相关笔记

1 Lenet5模型简介

在这里插入图片描述

2 TensorFlow实现 mnist_lenet5_forward.py文件
import tensorflow as tf
# 每张图片分辨率为28*28
IMAGE_SIZE = 28
# Mnist数据集为灰度图,故输入图片通道数NUM_CHANNELS取值为1
NUM_CHANNELS = 1
# 第一层卷积核大小为5
CONV1_SIZE = 5
# 卷积核个数为32
CONV1_KERNEL_NUM = 32
# 第二层卷积核大小为5
CONV2_SIZE = 5
# 卷积核个数为64
CONV2_KERNEL_NUM = 64
# 全连接层第一层为 512 个神经元
FC_SIZE = 512
# 全连接层第二层为 10 个神经元
OUTPUT_NODE = 10
# 权重w计算
def get_weight(shape, regularizer):
    w = tf.Variable(tf.truncated_normal(shape, stddev=0.1))
    if regularizer != None: tf.add_to_collection('losses', tf.contrib.layers.l2_regularizer(regularizer)(w))
    return w
# 偏置b计算
def get_bias(shape):
    b = tf.Variable(tf.zeros(shape))
    return b
# 卷积层计算
def conv2d(x, w):
    return tf.nn.conv2d(x, w, strides=[1, 1, 1, 1], padding='SAME')
# 最大池化层计算
def max_pool_2x2(x):
    return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
def forward(x, train, regularizer):
    # 实现第一层卷积
    conv1_w = get_weight([CONV1_SIZE, CONV1_SIZE, NUM_CHANNELS, CONV1_KERNEL_NUM], regularizer)
    conv1_b = get_bias([CONV1_KERNEL_NUM])
    conv1 = conv2d(x, conv1_w)
    # 非线性激活
    relu1 = tf.nn.relu(tf.nn.bias_add(conv1, conv1_b))
    # 最大池化
    pool1 = max_pool_2x2(relu1)
    # 实现第二层卷积
    conv2_w = get_weight([CONV2_SIZE, CONV2_SIZE, CONV1_KERNEL_NUM, CONV2_KERNEL_NUM], regularizer)
    conv2_b = get_bias([CONV2_KERNEL_NUM])
    conv2 = conv2d(pool1, conv2_w)
    relu2 = tf.nn.relu(tf.nn.bias_add(conv2, conv2_b))
    pool2 = max_pool_2x2(relu2)
    # 获取一个张量的维度
    pool_shape = pool2.get_shape().as_list()
    # pool_shape[1] 为长 pool_shape[2] 为宽 pool_shape[3]为高
    nodes = pool_shape[1] * pool_shape[2] * pool_shape[3]
    # 得到矩阵被拉长后的长度,pool_shape[0]为batch值
    reshaped = tf.reshape(pool2, [pool_shape[0], nodes])
    # 实现第三层全连接层
    fc1_w = get_weight([nodes, FC_SIZE], regularizer)
    fc1_b = get_bias([FC_SIZE])
    fc1 = tf.nn.relu(tf.matmul(reshaped, fc1_w) + fc1_b)
    # 如果是训练阶段,则对该层输出使用dropout
    if train: fc1 = tf.nn.dropout(fc1, 0.5)
    # 实现第四层全连接层
    fc2_w = get_weight([FC_SIZE, OUTPUT_NODE], regularizer)
    fc2_b = get_bias([OUTPUT_NODE])
    y = tf.matmul(fc1, fc2_w) + fc2_b
    return y 
mnist_lenet5_backward.py文件
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
import mnist_lenet5_forward
import os
import numpy as np
# batch的数量
BATCH_SIZE = 100
# 初始学习率
LEARNING_RATE_BASE = 0.005
# 学习率衰减率
LEARNING_RATE_DECAY = 0.99
# 正则化
REGULARIZER = 0.0001
# 最大迭代次数
STEPS = 50000
# 滑动平均衰减率
MOVING_AVERAGE_DECAY = 0.99
# 模型保存路径
MODEL_SAVE_PATH = "./model/"
# 模型名称
MODEL_NAME = "mnist_model"
def backward(mnist):
    # 卷积层输入为四阶张量
    # 第一阶表示每轮喂入的图片数量,第二阶和第三阶分别表示图片的行分辨率和列分辨率,第四阶表示通道数
    x = tf.placeholder(tf.float32, [
        BATCH_SIZE,
        mnist_lenet5_forward.IMAGE_SIZE,
        mnist_lenet5_forward.IMAGE_SIZE,
        mnist_lenet5_forward.NUM_CHANNELS])
    y_ = tf.placeholder(tf.float32, [None, mnist_lenet5_forward.OUTPUT_NODE])
    # 前向传播过程
    y = mnist_lenet5_forward.forward(x, True, REGULARIZER)
    # 声明一个全局计数器
    global_step = tf.Variable(0, trainable=False)
    # 对网络最后一层的输出y做softmax,求取输出属于某一类的概率
    ce = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=y, labels=tf.argmax(y_, 1))
    # 向量求均值
    cem = tf.reduce_mean(ce)
    # 正则化的损失值
    loss = cem + tf.add_n(tf.get_collection('losses'))
    # 指数衰减学习率
    learning_rate = tf.train.exponential_decay(
        LEARNING_RATE_BASE,
        global_step,
        mnist.train.num_examples / BATCH_SIZE,
        LEARNING_RATE_DECAY,
        staircase=True)
    # 梯度下降算法的优化器
    # train_step = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss, global_step=global_step)
    train_step = tf.train.MomentumOptimizer(learning_rate, 0.9).minimize(loss, global_step=global_step)
    # 采用滑动平均的方法更新参数
    ema = tf.train.ExponentialMovingAverage(MOVING_AVERAGE_DECAY, global_step)
    ema_op = ema.apply(tf.trainable_variables())
    # 将train_step和ema_op两个训练操作绑定到train_op上
    with tf.control_dependencies([train_step, ema_op]):
        train_op = tf.no_op(name='train')
    # 实例化一个保存和恢复变量的saver
    saver = tf.train.Saver()
    # 创建一个会话
    with tf.Session() as sess:
        init_op = tf.global_variables_initializer()
        sess.run(init_op)
        # 通过 checkpoint 文件定位到最新保存的模型,若文件存在,则加载最新的模型
        ckpt = tf.train.get_checkpoint_state(MODEL_SAVE_PATH)
        if ckpt and ckpt.model_checkpoint_path:
            saver.restore(sess, ckpt.model_checkpoint_path)
        for i in range(STEPS):
            # 读取一个batch数据,将输入数据xs转成与网络输入相同形状的矩阵
            xs, ys = mnist.train.next_batch(BATCH_SIZE)
            reshaped_xs = np.reshape(xs, (
                BATCH_SIZE,
                mnist_lenet5_forward.IMAGE_SIZE,
                mnist_lenet5_forward.IMAGE_SIZE,
                mnist_lenet5_forward.NUM_CHANNELS))
            # 读取一个batch数据,将输入数据xs转成与网络输入相同形状的矩阵
            _, loss_value, step = sess.run([train_op, loss, global_step], feed_dict={x: reshaped_xs, y_: ys})
            if i % 100 == 0:
                print("After %d training step(s), loss on training batch is %g." % (step, loss_value))
                saver.save(sess, os.path.join(MODEL_SAVE_PATH, MODEL_NAME), global_step=global_step)
def main():
    mnist = input_data.read_data_sets("./data/", one_hot=True)
    backward(mnist)
if __name__ == '__main__':
    main()
mnist_lenet5_test.py文件
import time
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
import mnist_lenet5_forward
import mnist_lenet5_backward
import numpy as np
TEST_INTERVAL_SECS = 5
#创建一个默认图,在该图中执行以下操作
def test(mnist):
    with tf.Graph().as_default() as g: 
        x = tf.placeholder(tf.float32,[
            mnist.test.num_examples,
            mnist_lenet5_forward.IMAGE_SIZE,
            mnist_lenet5_forward.IMAGE_SIZE,
            mnist_lenet5_forward.NUM_CHANNELS]) 
        y_ = tf.placeholder(tf.float32, [None, mnist_lenet5_forward.OUTPUT_NODE])
        #训练好的网络,故不使用 dropout
        y = mnist_lenet5_forward.forward(x,False,None)
        ema = tf.train.ExponentialMovingAverage(mnist_lenet5_backward.MOVING_AVERAGE_DECAY)
        ema_restore = ema.variables_to_restore()
        saver = tf.train.Saver(ema_restore)
        #判断预测值和实际值是否相同
        correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1)) 
        ## 求平均得到准确率
        accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) 
        while True:
            with tf.Session() as sess:
                ckpt = tf.train.get_checkpoint_state(mnist_lenet5_backward.MODEL_SAVE_PATH)
                if ckpt and ckpt.model_checkpoint_path:
                    saver.restore(sess, ckpt.model_checkpoint_path) 
                    # 根据读入的模型名字切分出该模型是属于迭代了多少次保存的
                    global_step = ckpt.model_checkpoint_path.split('/')[-1].split('-')[-1] 
                    reshaped_x = np.reshape(mnist.test.images,(
                    mnist.test.num_examples,
                    mnist_lenet5_forward.IMAGE_SIZE,
                    mnist_lenet5_forward.IMAGE_SIZE,
                    mnist_lenet5_forward.NUM_CHANNELS))
                    #利用多线程提高图片和标签的批获取效率
                    coord = tf.train.Coordinator()#3
                    threads = tf.train.start_queue_runners(sess=sess, coord=coord)#4
                    accuracy_score = sess.run(accuracy, feed_dict={x:reshaped_x,y_:mnist.test.labels}) 
                    print("After %s training step(s), test accuracy = %g" % (global_step, accuracy_score))
                    #关闭线程协调器
                    coord.request_stop()#6
                    coord.join(threads)#7
                else:
                    print('No checkpoint file found')
                    return
            time.sleep(TEST_INTERVAL_SECS) 
def main():
    mnist = input_data.read_data_sets("./data/", one_hot=True)
    test(mnist)
if __name__ == '__main__':
    main()
相关笔记

以下所有源码以及更详细PDF笔记请在github下载 TensorFolwNotebook-from-Peking-University

  1. 【北京大学】1 TensorFlow1.x中Python基础知识
  2. 【北京大学】2 TensorFlow1.x的张量、计算图、会话
  3. 【北京大学】3 TensorFlow1.x的前向传播推导与实现
  4. 【北京大学】4 TensorFlow1.x的反向传播推导与实现
  5. 【北京大学】5 TensorFlow1.x的损失函数和交叉熵举例讲解及实现
  6. 【北京大学】6 TensorFlow1.x的学习率、滑动平均和正则化实例及实现
  7. 【北京大学】7 TensorFlow1.x的神经网络模块设计思想举例及实现
  8. 【北京大学】8 TensorFlow1.x的Mnist数据集实例实现
  9. 【北京大学】9 TensorFlow1.x的实现自定义Mnist数据集
  10. 【北京大学】10 TensorFlow1.x的卷积神经网络(CNN)相关基础知识
  11. 【北京大学】11 TensorFlow1.x的卷积神经网络模型Lenet5实现
  12. 【北京大学】12 TensorFlow1.x的卷积神经网络模型VGGNet实现
  13. 【北京大学】13 TensorFlow1.x的项目实战之手写英文体识别OCR技术
  14. 【北京大学】14 TensorFlow1.x的二值神经网络实现MNIST数据集手写数字识别
  15. 【北京大学】15 TensorFlow1.x的项目实战之人脸表情识别
  16. 【北京大学】16 TensorFlow1.x的项目实战之图像风格融合与快速迁移
关注
打赏
1665674626
查看更多评论
立即登录/注册

微信扫码登录

0.0451s