tensorflow 卷积神经网络 LeNet-5模型 MNIST手写体数字识别

本文主要是介绍tensorflow 卷积神经网络 LeNet-5模型 MNIST手写体数字识别,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

____tz_zs小练习


案例来源《TensorFlow实战Google深度学习框架》

cnn/mnist_inference.py

# -*- coding: utf-8 -*-
"""
@author: tz_zs卷积神经网络 mnist_inference.py
"""import tensorflow as tf# 定义神经网络结构相关的参数
INPUT_NODE = 784
OUTPUT_NODE = 10IMAGE_SIZE = 28
NUM_CHANNELS = 1
NUM_LABELS = 10# 第一层卷积层的尺寸和深度
CONV1_DEEP = 32
CONV1_SIZE = 5# 第二层卷积层的尺寸和深度
CONV2_DEEP = 64
CONV2_SIZE = 5# 全连接层的节点个数
FC_SIZE = 512def inference(input_tensor, train, regularizer):with tf.variable_scope('layer1-conv1'):conv1_weights = tf.get_variable("weight", [CONV1_SIZE, CONV1_SIZE, NUM_CHANNELS, CONV1_DEEP],initializer=tf.truncated_normal_initializer(stddev=0.1))conv1_biases = tf.get_variable("bias", [CONV1_DEEP], initializer=tf.constant_initializer(0.0))# print(conv1_weights)  # <tf.Variable 'layer1-conv1/weight:0' shape=(5, 5, 1, 32) dtype=float32_ref># print(conv1_biases)  # <tf.Variable 'layer1-conv1/bias:0' shape=(32,) dtype=float32_ref>conv1 = tf.nn.conv2d(input_tensor, conv1_weights, strides=[1, 1, 1, 1], padding='SAME')relu1 = tf.nn.relu(tf.nn.bias_add(conv1, conv1_biases))# print(conv1)  # Tensor("layer1-conv1/Conv2D:0", shape=(100, 28, 28, 32), dtype=float32)# print(relu1)  # Tensor("layer1-conv1/Relu:0", shape=(100, 28, 28, 32), dtype=float32)with tf.name_scope('layer2-pool1'):pool1 = tf.nn.max_pool(relu1, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')# print(pool1)  # Tensor("layer2-pool1/MaxPool:0", shape=(100, 14, 14, 32), dtype=float32)with tf.variable_scope('layer3-conv2'):conv2_weights = tf.get_variable("weight", [CONV2_SIZE, CONV2_SIZE, CONV1_DEEP, CONV2_DEEP],initializer=tf.truncated_normal_initializer(stddev=0.1))conv2_biases = tf.get_variable("bias", [CONV2_DEEP], initializer=tf.constant_initializer(0.0))conv2 = tf.nn.conv2d(pool1, conv2_weights, strides=[1, 1, 1, 1], padding='SAME')relu2 = tf.nn.relu(tf.nn.bias_add(conv2, conv2_biases))with tf.name_scope('layer4-pool2'):pool2 = tf.nn.max_pool(relu2, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')pool_shape = pool2.get_shape().as_list()# print(pool_shape)# [100, 7, 7, 64]-------[5000, 7, 7, 64]nodes = pool_shape[1] * pool_shape[2] * pool_shape[3]reshaped = tf.reshape(pool2, [pool_shape[0], nodes])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))fc1_biases = tf.get_variable("bias", [FC_SIZE], initializer=tf.constant_initializer(0.1))fc1 = tf.nn.relu(tf.matmul(reshaped, fc1_weights) + fc1_biases)if train:fc1 = tf.nn.dropout(fc1, 0.5)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))fc2_biases = tf.get_variable("bias", [NUM_LABELS], initializer=tf.constant_initializer(0.1))logit = tf.matmul(fc1, fc2_weights) + fc2_biasesreturn logit


cnn/mnist_train.py

# -*- coding: utf-8 -*-
"""
@author: tz_zs卷积神经网络 mnist_train.py
"""
import tensorflow as tf
import os
from tensorflow.examples.tutorials.mnist import input_data
import numpy as np
# 加载函数
import cnn.mnist_inference as mnist_inference# 配置神经网络参数
BATCH_SIZE = 100
LEARNING_RATE_BASE = 0.1
LEARNING_RATE_DECAY = 0.99
REGULARIZATION_RATE = 0.0001
TRAINING_STEPS = 30000
MOVING_AVERAGE_DECAY = 0.99# 模型保存路径和文件名
MODEL_SAVE_PATH = "/path/to/model/cnn/"
MODEL_NAME = "model.ckpt"def train(mnist):# 定义输入输出的placeholder# x = tf.placeholder(tf.float32, [None, mnist_inference.INPUT_NODE], name='x-input')x = tf.placeholder(tf.float32, [BATCH_SIZE,mnist_inference.IMAGE_SIZE,mnist_inference.IMAGE_SIZE,mnist_inference.NUM_CHANNELS],name='x-input')y_ = tf.placeholder(tf.float32, [BATCH_SIZE, mnist_inference.OUTPUT_NODE], name='y-input')# 定义正则化regularizer = tf.contrib.layers.l2_regularizer(REGULARIZATION_RATE)# 使用前向传播y = mnist_inference.inference(x, True, regularizer)global_step = tf.Variable(0, trainable=False)# 滑动平均variable_averages = tf.train.ExponentialMovingAverage(MOVING_AVERAGE_DECAY, global_step)variable_averages_op = variable_averages.apply(tf.trainable_variables())# print(tf.trainable_variables())# [<tf.Variable 'layer1-conv1/weight:0' shape=(5, 5, 1, 32) dtype=float32_ref>,# <tf.Variable 'layer1-conv1/bias:0' shape=(32,) dtype=float32_ref>,# <tf.Variable 'layer3-conv2/weight:0' shape=(5, 5, 32, 64) dtype=float32_ref>,# <tf.Variable 'layer3-conv2/bias:0' shape=(64,) dtype=float32_ref>,# <tf.Variable 'layer5-fc1/weight:0' shape=(3136, 512) dtype=float32_ref>,# <tf.Variable 'layer5-fc1/bias:0' shape=(512,) dtype=float32_ref>,# <tf.Variable 'layer6-fc2/weight:0' shape=(512, 10) dtype=float32_ref>,# <tf.Variable 'layer6-fc2/bias:0' shape=(10,) dtype=float32_ref>]# 损失函数cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(labels=tf.argmax(y_, 1), logits=y)cross_entropy_mean = tf.reduce_mean(cross_entropy)loss = cross_entropy_mean + tf.add_n(tf.get_collection('losses'))# print(tf.get_collection('losses'))# #[<tf.Tensor 'layer5-fc1/l2_regularizer:0' shape=() dtype=float32>,# <tf.Tensor 'layer6-fc2/l2_regularizer:0' shape=() dtype=float32>]# 学习率learning_rate = tf.train.exponential_decay(LEARNING_RATE_BASE,global_step,mnist.train.num_examples / BATCH_SIZE,LEARNING_RATE_DECAY)# 优化算法train_step = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss, global_step=global_step)with tf.control_dependencies([train_step, variable_averages_op]):train_op = tf.no_op(name="train")# 持久化saver = tf.train.Saver()config = tf.ConfigProto()config.gpu_options.allow_growth = Truewith tf.Session(config=config) as sess:tf.global_variables_initializer().run()for i in range(TRAINING_STEPS):xs, ys = mnist.train.next_batch(BATCH_SIZE)# 调整为四维矩阵reshaped_xs = np.reshape(xs, [BATCH_SIZE,mnist_inference.IMAGE_SIZE,mnist_inference.IMAGE_SIZE,mnist_inference.NUM_CHANNELS])# 运行_, loss_valuue, step = sess.run([train_op, loss, global_step], feed_dict={x: reshaped_xs, y_: ys})# 每1000轮保存一次模型if i % 1000 == 0:print("After %d training step(s), loss on training batch is %g." % (step, loss_valuue))saver.save(sess, os.path.join(MODEL_SAVE_PATH, MODEL_NAME), global_step=global_step)def main(argv=None):mnist = input_data.read_data_sets("/tmp/data", one_hot=True)train(mnist)if __name__ == '__main__':tf.app.run()'''
运行结果:
After 1 training step(s), loss on training batch is 6.73231.
After 1001 training step(s), loss on training batch is 0.730202.
After 2001 training step(s), loss on training batch is 0.644094.
After 3001 training step(s), loss on training batch is 0.640496.
After 4001 training step(s), loss on training batch is 0.634515.
After 5001 training step(s), loss on training batch is 0.64231.
After 6001 training step(s), loss on training batch is 0.581734.
After 7001 training step(s), loss on training batch is 0.590254.
After 8001 training step(s), loss on training batch is 0.546791.
After 9001 training step(s), loss on training batch is 0.553352.
After 10001 training step(s), loss on training batch is 0.526924.
After 11001 training step(s), loss on training batch is 0.516263.
After 12001 training step(s), loss on training batch is 0.510524.
After 13001 training step(s), loss on training batch is 0.530617.
After 14001 training step(s), loss on training batch is 0.500552.
After 15001 training step(s), loss on training batch is 0.49316.
After 16001 training step(s), loss on training batch is 0.478148.
After 17001 training step(s), loss on training batch is 0.470733.
After 18001 training step(s), loss on training batch is 0.471833.
After 19001 training step(s), loss on training batch is 0.456701.
After 20001 training step(s), loss on training batch is 0.451218.
After 21001 training step(s), loss on training batch is 0.446669.
After 22001 training step(s), loss on training batch is 0.440087.
After 23001 training step(s), loss on training batch is 0.43465.
After 24001 training step(s), loss on training batch is 0.428076.
After 25001 training step(s), loss on training batch is 0.42475.
After 26001 training step(s), loss on training batch is 0.416584.
After 27001 training step(s), loss on training batch is 0.428798.
After 28001 training step(s), loss on training batch is 0.406561.
After 29001 training step(s), loss on training batch is 0.404045.
'''

cnn/mnist_eval.py

# -*- coding: utf-8 -*-
"""
@author: tz_zs卷积神经网络 测试程序 mnist_eval.py
"""
import time
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_dataimport cnn.mnist_inference
import cnn.mnist_trainimport numpy as np# 每十秒加载一次最新的模型,并在测试数据上测试最新模型的准确率
EVAL_INTERVAL_SECS = 10def evaluate(mnist):with tf.Graph().as_default() as g:# x = tf.placeholder(tf.float32, [None, mnist_inference.INPUT_NODE], name="x-input")x = tf.placeholder(tf.float32, [mnist.validation.num_examples,cnn.mnist_inference.IMAGE_SIZE,cnn.mnist_inference.IMAGE_SIZE,cnn.mnist_inference.NUM_CHANNELS],name='x-input')y_ = tf.placeholder(tf.float32, [mnist.validation.num_examples, cnn.mnist_inference.OUTPUT_NODE],name="y-input")# 数据输入调整为四维矩阵reshaped_xs = np.reshape(mnist.validation.images,[mnist.validation.num_examples,cnn.mnist_inference.IMAGE_SIZE,cnn.mnist_inference.IMAGE_SIZE,cnn.mnist_inference.NUM_CHANNELS])validate_feed = {x: reshaped_xs, y_: mnist.validation.labels}# 测试(测试时不用计算正则化损失)y = cnn.mnist_inference.inference(x, False, None)# 计算准确率correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))# 加载模型variable_averages = tf.train.ExponentialMovingAverage(cnn.mnist_train.MOVING_AVERAGE_DECAY)variables_to_restore = variable_averages.variables_to_restore()saver = tf.train.Saver(variables_to_restore)#print(variables_to_restore)# {'layer3-conv2/bias/ExponentialMovingAverage': <tf.Variable 'layer3-conv2/bias:0' shape=(64,) dtype=float32_ref>,# 'layer1-conv1/bias/ExponentialMovingAverage': <tf.Variable 'layer1-conv1/bias:0' shape=(32,) dtype=float32_ref>,# 'layer6-fc2/bias/ExponentialMovingAverage': <tf.Variable 'layer6-fc2/bias:0' shape=(10,) dtype=float32_ref>,# 'layer3-conv2/weight/ExponentialMovingAverage': <tf.Variable 'layer3-conv2/weight:0' shape=(5, 5, 32, 64) dtype=float32_ref>,# 'layer6-fc2/weight/ExponentialMovingAverage': <tf.Variable 'layer6-fc2/weight:0' shape=(512, 10) dtype=float32_ref>,# 'layer1-conv1/weight/ExponentialMovingAverage': <tf.Variable 'layer1-conv1/weight:0' shape=(5, 5, 1, 32) dtype=float32_ref>,# 'layer5-fc1/bias/ExponentialMovingAverage': <tf.Variable 'layer5-fc1/bias:0' shape=(512,) dtype=float32_ref>,# 'layer5-fc1/weight/ExponentialMovingAverage': <tf.Variable 'layer5-fc1/weight:0' shape=(3136, 512) dtype=float32_ref>}config = tf.ConfigProto()config.gpu_options.allow_growth = Truewhile True:with tf.Session(config=config) as sess:# 找到文件名ckpt = tf.train.get_checkpoint_state(cnn.mnist_train.MODEL_SAVE_PATH)# print(ckpt)# model_checkpoint_path: "/path/to/model/cnn/model.ckpt-4001"# all_model_checkpoint_paths: "/path/to/model/cnn/model.ckpt-1"# all_model_checkpoint_paths: "/path/to/model/cnn/model.ckpt-1001"# all_model_checkpoint_paths: "/path/to/model/cnn/model.ckpt-2001"# all_model_checkpoint_paths: "/path/to/model/cnn/model.ckpt-3001"# all_model_checkpoint_paths: "/path/to/model/cnn/model.ckpt-4001"if ckpt and ckpt.model_checkpoint_path:# 加载模型saver.restore(sess, ckpt.model_checkpoint_path)# 通过文件名获得模型保存时迭代的轮数global_step = ckpt.model_checkpoint_path.split('/')[-1].split('-')[-1]# 运算出数据accuracy_score = sess.run(accuracy, feed_dict=validate_feed)print("After %s training stpe(s), validation accuracy = %g" % (global_step, accuracy_score))else:print("No checkpoint file found")returntime.sleep(EVAL_INTERVAL_SECS)def main(argv=None):mnist = input_data.read_data_sets("/tmp/data", one_hot=True)evaluate(mnist)if __name__ == '__main__':tf.app.run()'''
After 1 training stpe(s), validation accuracy = 0.047
After 1001 training stpe(s), validation accuracy = 0.9848
After 2001 training stpe(s), validation accuracy = 0.9894
After 3001 training stpe(s), validation accuracy = 0.9904
...
...
After 27001 training stpe(s), validation accuracy = 0.9932
After 28001 training stpe(s), validation accuracy = 0.9926
After 29001 training stpe(s), validation accuracy = 0.993
'''


这篇关于tensorflow 卷积神经网络 LeNet-5模型 MNIST手写体数字识别的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/954356

相关文章

Java使用Spire.Barcode for Java实现条形码生成与识别

《Java使用Spire.BarcodeforJava实现条形码生成与识别》在现代商业和技术领域,条形码无处不在,本教程将引导您深入了解如何在您的Java项目中利用Spire.Barcodefor... 目录1. Spire.Barcode for Java 简介与环境配置2. 使用 Spire.Barco

Java领域模型示例详解

《Java领域模型示例详解》本文介绍了Java领域模型(POJO/Entity/VO/DTO/BO)的定义、用途和区别,强调了它们在不同场景下的角色和使用场景,文章还通过一个流程示例展示了各模型如何协... 目录Java领域模型(POJO / Entity / VO/ DTO / BO)一、为什么需要领域模

深入理解Redis线程模型的原理及使用

《深入理解Redis线程模型的原理及使用》Redis的线程模型整体还是多线程的,只是后台执行指令的核心线程是单线程的,整个线程模型可以理解为还是以单线程为主,基于这种单线程为主的线程模型,不同客户端的... 目录1 Redis是单线程www.chinasem.cn还是多线程2 Redis如何保证指令原子性2.

Linux五种IO模型的使用解读

《Linux五种IO模型的使用解读》文章系统解析了Linux的五种IO模型(阻塞、非阻塞、IO复用、信号驱动、异步),重点区分同步与异步IO的本质差异,强调同步由用户发起,异步由内核触发,通过对比各模... 目录1.IO模型简介2.五种IO模型2.1 IO模型分析方法2.2 阻塞IO2.3 非阻塞IO2.4

如何正确识别一台POE交换机的好坏? 选购可靠的POE交换机注意事项

《如何正确识别一台POE交换机的好坏?选购可靠的POE交换机注意事项》POE技术已经历多年发展,广泛应用于安防监控和无线覆盖等领域,需求量大,但质量参差不齐,市场上POE交换机的品牌繁多,如何正确识... 目录生产标识1. 必须包含的信息2. 劣质设备的常见问题供电标准1. 正规的 POE 标准2. 劣质设

基于Python实现数字限制在指定范围内的五种方式

《基于Python实现数字限制在指定范围内的五种方式》在编程中,数字范围限制是常见需求,无论是游戏开发中的角色属性值、金融计算中的利率调整,还是传感器数据处理中的异常值过滤,都需要将数字控制在合理范围... 目录引言一、基础条件判断法二、数学运算巧解法三、装饰器模式法四、自定义类封装法五、NumPy数组处理

Python中Tensorflow无法调用GPU问题的解决方法

《Python中Tensorflow无法调用GPU问题的解决方法》文章详解如何解决TensorFlow在Windows无法识别GPU的问题,需降级至2.10版本,安装匹配CUDA11.2和cuDNN... 当用以下代码查看GPU数量时,gpuspython返回的是一个空列表,说明tensorflow没有找到

Python中图片与PDF识别文本(OCR)的全面指南

《Python中图片与PDF识别文本(OCR)的全面指南》在数据爆炸时代,80%的企业数据以非结构化形式存在,其中PDF和图像是最主要的载体,本文将深入探索Python中OCR技术如何将这些数字纸张转... 目录一、OCR技术核心原理二、python图像识别四大工具库1. Pytesseract - 经典O

Python基于微信OCR引擎实现高效图片文字识别

《Python基于微信OCR引擎实现高效图片文字识别》这篇文章主要为大家详细介绍了一款基于微信OCR引擎的图片文字识别桌面应用开发全过程,可以实现从图片拖拽识别到文字提取,感兴趣的小伙伴可以跟随小编一... 目录一、项目概述1.1 开发背景1.2 技术选型1.3 核心优势二、功能详解2.1 核心功能模块2.

Python验证码识别方式(使用pytesseract库)

《Python验证码识别方式(使用pytesseract库)》:本文主要介绍Python验证码识别方式(使用pytesseract库),具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全... 目录1、安装Tesseract-OCR2、在python中使用3、本地图片识别4、结合playwrigh