1 搭建卷积神经网络

1.0 网络结构

图1.0 卷积网络结构

1.2 网络分析

序号 网络层 描述
1 卷积层 一张原始图像(28, 28, 1),batch=1,经过卷积处理,得到图像特征(28, 28, 32)
2 下采样 即池化层,最大池化后图像特征(14, 14, 32)
3 卷积层 将池化特征(14, 14, 32)卷积处理后,得到图像特征(14, 14, 64)
4 下采样 最大池化,得到图像特征(7, 7, 64)
5 全连接层 将上一层即池化层的图像特征经过矩阵内积计算,拉成一个向量(7764=3136),特征为(1, 3136)
6 全连接层 继续矩阵计算,得到特征为(1, 512)
7 全连接 高斯矩阵计算,得到特征(1, 10)

2 网络结构

2.1 网络结构可视化

图2.1 网络结构

2.2 网络结构-源

def conv2d(input_tensor, ksize, strides, pad, name_w, name_b):weights = tf.get_variable(name=name_w, shape=ksize, dtype=tf.float32, initializer=tf.truncated_normal_initializer(stddev=0.1))biases = tf.get_variable(name=name_b, shape=[ksize[-1]], dtype=tf.float32, initializer=tf.constant_initializer(0.1))conv = tf.nn.conv2d(input_tensor, weights, strides=strides, padding=pad)conv = tf.nn.relu(conv + biases)return convdef max_pooling(input_tensor, ksize, strides, pad):max_pool = tf.nn.max_pool(input_tensor, ksize=ksize, strides=strides, padding=pad)return max_pooldef fullc(input_tensor, wsize, name_w, name_b):weights = tf.get_variable(name=name_w, shape=wsize, dtype=tf.float32, initializer=tf.truncated_normal_initializer(stddev=0.1))biases = tf.get_variable(name=name_b, shape=[wsize[-1]], dtype=tf.float32, initializer=tf.constant_initializer(0.1))fullc = tf.matmul(input_tensor, weights) + biasesreturn fullc
def inference(inputs, keep_prob):with tf.name_scope("conv_1"):conv_1 = conv2d(inputs, [5, 5, 1, 32], [1, 1, 1, 1], "SAME", "cw_1", "cb_1")with tf.name_scope("max_pool_1"):pooling_1 = max_pooling(conv_1, [1, 2, 2, 1], [1, 2, 2, 1], "SAME")with tf.name_scope("conv_2"):conv_2 = conv2d(pooling_1, [5, 5, 32, 64], [1, 1, 1, 1], "SAME", "cw_2", "cb_2")with tf.name_scope("max_pool_2"):pooling_2 = max_pooling(conv_2, [1, 2, 2, 1], [1, 2, 2, 1], "SAME")feature_shape = pooling_2.get_shape()flatten_1 = feature_shape[1].value * feature_shape[2].value * feature_shape[3].valuefeature_reshape = tf.reshape(pooling_2, [-1, flatten_1])with tf.name_scope("fc_1"):fc_1 = fullc(feature_reshape, [flatten_1, 512], "fw_1", "fb_1")fc_1 = tf.nn.dropout(fc_1, keep_prob)with tf.name_scope("fc_2"):fc_2 = fullc(fc_1, [512, 10], "fw_2", "fb_2")return fc_2

3 训练及测试

3.1 载入数据

mnist = input_data.read_data_sets("./mnist_data", one_hot=True)
img_inputs, img_labels = mnist.train.next_batch(BATCH_SIZE)
img_inputs = np.reshape(img_inputs, (BATCH_SIZE, 28, 28, 1))

3.2 训练及保存模型

def train_new(mnist):  inputs = tf.placeholder(tf.float32, [None, 28, 28, 1], name="img-inputs")labels = tf.placeholder(tf.float32, [None, 10], name="label-outputs")prediction = inference(inputs, 0.5)loss = loss_cal(prediction, labels)accuracy = evaluation(prediction, labels)summary_op = tf.summary.merge(tf.get_collection(tf.GraphKeys.SUMMARIES))train_step = tf.train.GradientDescentOptimizer(LEARNING_RATE).minimize(loss)saver = tf.train.Saver()with tf.Session() as sess:init_op = tf.global_variables_initializer()sess.run(init_op)summary_writer = tf.summary.FileWriter(LOG_DIR, sess.graph)for i in range(TRAINING_STEPS):img_inputs, img_labels = mnist.train.next_batch(BATCH_SIZE)img_inputs = np.reshape(img_inputs, (BATCH_SIZE, 28, 28, 1))_, loss_value, acc, summary = sess.run([train_step, loss, accuracy, summary_op], feed_dict={inputs: img_inputs, labels: img_labels})# _, loss_value, step, acc = sess.run([train_op, loss, global_step, accuracy], feed_dict={inputs: img_inputs, labels: img_labels})if i % 10 == 0:print("After {} training steps, loss is {}, accuracy: {}".format(i, loss_value, acc))# print("After {} training steps, loss is {}, accuracy: {}".format(step, loss_value, acc)saver.save(sess, os.path.join(MODEL_PATH, MODEL_NAME))summary_writer.add_summary(summary, i)

4 载入模型及预测

def load_model_only_with_params():g_params = tf.Graph()with g_params.as_default():inputs = tf.placeholder(tf.float32, [None, 28, 28, 1], name="img-inputs")labels = tf.placeholder(tf.float32, [None, 10], name="label-outputs")prediction = inference(inputs, 0.5)mnist = input_data.read_data_sets("./mnist_data", one_hot=True)img = mnist.test.images[0]img = np.reshape(img, (1, 28, 28, 1))img_label = mnist.test.labels[0]img_label = np.argmax(img_label)with tf.Session(graph=g_params) as sess:saver = tf.train.Saver()ckpt = tf.train.get_checkpoint_state("./conv_models")model_path = ckpt.model_checkpoint_pathsaver.restore(sess, model_path)pre = sess.run(prediction, feed_dict={inputs: img})pre_num = tf.argmax(pre, 1)pre_num = sess.run(pre_num)print("prediction: {}, real: {}".format(pre_num[0], img_label))
prediction: 7, real: 7

5 完整程序

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
import os
import numpy as npLEARNING_RATE_BASE = 0.8
LEARNING_RATE_DECAY = 0.99
REGULARAZTION_RATE = 0.0001
LEARNING_RATE = 0.0001
TRAINING_STEPS = 30000
MOVING_AVERAGE_DECAY = 0.99
MODEL_PATH = "./conv_models"
MODEL_NAME = "conv_model.ckpt"
LOG_DIR = "./logs"
BATCH_SIZE = 100
if not os.path.exists(MODEL_PATH):os.makedirs(MODEL_PATH)def conv2d(input_tensor, ksize, strides, pad, name_w, name_b):weights = tf.get_variable(name=name_w, shape=ksize, dtype=tf.float32, initializer=tf.truncated_normal_initializer(stddev=0.1))biases = tf.get_variable(name=name_b, shape=[ksize[-1]], dtype=tf.float32, initializer=tf.constant_initializer(0.1))conv = tf.nn.conv2d(input_tensor, weights, strides=strides, padding=pad)conv = tf.nn.relu(conv + biases)return convdef max_pooling(input_tensor, ksize, strides, pad):max_pool = tf.nn.max_pool(input_tensor, ksize=ksize, strides=strides, padding=pad)return max_pooldef fullc(input_tensor, wsize, name_w, name_b):weights = tf.get_variable(name=name_w, shape=wsize, dtype=tf.float32, initializer=tf.truncated_normal_initializer(stddev=0.1))biases = tf.get_variable(name=name_b, shape=[wsize[-1]], dtype=tf.float32, initializer=tf.constant_initializer(0.1))fullc = tf.matmul(input_tensor, weights) + biasesreturn fullcdef inference(inputs, keep_prob):with tf.name_scope("conv_1"):conv_1 = conv2d(inputs, [5, 5, 1, 32], [1, 1, 1, 1], "SAME", "cw_1", "cb_1")with tf.name_scope("max_pool_1"):pooling_1 = max_pooling(conv_1, [1, 2, 2, 1], [1, 2, 2, 1], "SAME")with tf.name_scope("conv_2"):conv_2 = conv2d(pooling_1, [5, 5, 32, 64], [1, 1, 1, 1], "SAME", "cw_2", "cb_2")with tf.name_scope("max_pool_2"):pooling_2 = max_pooling(conv_2, [1, 2, 2, 1], [1, 2, 2, 1], "SAME")feature_shape = pooling_2.get_shape()flatten_1 = feature_shape[1].value * feature_shape[2].value * feature_shape[3].valuefeature_reshape = tf.reshape(pooling_2, [-1, flatten_1])with tf.name_scope("fc_1"):fc_1 = fullc(feature_reshape, [flatten_1, 512], "fw_1", "fb_1")fc_1 = tf.nn.dropout(fc_1, keep_prob)with tf.name_scope("fc_2"):fc_2 = fullc(fc_1, [512, 10], "fw_2", "fb_2")return fc_2def loss_cal(prediction, labels):with tf.name_scope("loss"):cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=prediction, labels=tf.argmax(labels, 1))loss = tf.reduce_mean(cross_entropy)tf.summary.scalar("loss", loss)return lossdef evaluation(logits, labels):with tf.name_scope("accuracy"):correct_prediction = tf.equal(tf.argmax(logits, 1), tf.argmax(labels, 1))accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))tf.summary.scalar("accuracy", accuracy)return accuracydef train(mnist):inputs = tf.placeholder(tf.float32, [None, 28, 28, 1], name="img-inputs")labels = tf.placeholder(tf.float32, [None, 10], name="label-outputs")global_step = tf.Variable(0, trainable=False)variable_averages = tf.train.ExponentialMovingAverage(MOVING_AVERAGE_DECAY, global_step)variables_averages_op = variable_averages.apply(tf.trainable_variables())prediction = inference(inputs, 0.5)cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=prediction, labels=tf.argmax(labels, 1))loss = tf.reduce_mean(cross_entropy)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)# accuracy = evaluation(prediction, labels)with tf.control_dependencies([train_step, variables_averages_op]):train_op = tf.no_op(name="train")saver = tf.train.Saver()with tf.Session() as sess:init_op = tf.global_variables_initializer()sess.run(init_op)for i in range(TRAINING_STEPS):img_inputs, img_labels = mnist.train.next_batch(BATCH_SIZE)img_inputs = np.reshape(img_inputs, (BATCH_SIZE, 28, 28, 1))_, loss_value, step = sess.run([train_op, loss, global_step], feed_dict={inputs: img_inputs, labels: img_labels})# _, loss_value, step, acc = sess.run([train_op, loss, global_step, accuracy], feed_dict={inputs: img_inputs, labels: img_labels})if i % 10 == 0:print("After {} training steps, loss is {}".format(step, loss_value))# print("After {} training steps, loss is {}, accuracy: {}".format(step, loss_value, acc)saver.save(sess, os.path.join(MODEL_PATH, MODEL_NAME), global_step=global_step)def train_new(mnist):  inputs = tf.placeholder(tf.float32, [None, 28, 28, 1], name="img-inputs")labels = tf.placeholder(tf.float32, [None, 10], name="label-outputs")prediction = inference(inputs, 0.5)loss = loss_cal(prediction, labels)accuracy = evaluation(prediction, labels)summary_op = tf.summary.merge(tf.get_collection(tf.GraphKeys.SUMMARIES))train_step = tf.train.GradientDescentOptimizer(LEARNING_RATE).minimize(loss)saver = tf.train.Saver()with tf.Session() as sess:init_op = tf.global_variables_initializer()sess.run(init_op)summary_writer = tf.summary.FileWriter(LOG_DIR, sess.graph)for i in range(TRAINING_STEPS):img_inputs, img_labels = mnist.train.next_batch(BATCH_SIZE)img_inputs = np.reshape(img_inputs, (BATCH_SIZE, 28, 28, 1))_, loss_value, acc, summary = sess.run([train_step, loss, accuracy, summary_op], feed_dict={inputs: img_inputs, labels: img_labels})# _, loss_value, step, acc = sess.run([train_op, loss, global_step, accuracy], feed_dict={inputs: img_inputs, labels: img_labels})if i % 10 == 0:print("After {} training steps, loss is {}, accuracy: {}".format(i, loss_value, acc))# print("After {} training steps, loss is {}, accuracy: {}".format(step, loss_value, acc)saver.save(sess, os.path.join(MODEL_PATH, MODEL_NAME))summary_writer.add_summary(summary, i)def load_model_only_with_params():g_params = tf.Graph()with g_params.as_default():inputs = tf.placeholder(tf.float32, [None, 28, 28, 1], name="img-inputs")labels = tf.placeholder(tf.float32, [None, 10], name="label-outputs")prediction = inference(inputs, 0.5)mnist = input_data.read_data_sets("./mnist_data", one_hot=True)img = mnist.test.images[0]img = np.reshape(img, (1, 28, 28, 1))img_label = mnist.test.labels[0]img_label = np.argmax(img_label)with tf.Session(graph=g_params) as sess:saver = tf.train.Saver()ckpt = tf.train.get_checkpoint_state("./conv_models")model_path = ckpt.model_checkpoint_pathsaver.restore(sess, model_path)pre = sess.run(prediction, feed_dict={inputs: img})pre_num = tf.argmax(pre, 1)pre_num = sess.run(pre_num)print("prediction: {}, real: {}".format(pre_num[0], img_label))def main(argv=None):mnist = input_data.read_data_sets("./mnist_data", one_hot=True).# 训练train_new(mnist)# 载入训练模型# load_model_only_with_params()if __name__ == "__main__":tf.app.run()

6 训练结果

6.1 损失值

图6.1 损失值

6.2 准确度

图6.1 评估精度

(二)Tensorflow搭建卷积神经网络实现MNIST手写字体识别及预测相关推荐

  1. PyTorch入门一:卷积神经网络实现MNIST手写数字识别

    先给出几个入门PyTorch的好的资料: PyTorch官方教程(中文版):http://pytorch123.com <动手学深度学习>PyTorch版:https://github.c ...

  2. Tensorflow之 CNN卷积神经网络的MNIST手写数字识别

    点击"阅读原文"直接打开[北京站 | GPU CUDA 进阶课程]报名链接 作者,周乘,华中科技大学电子与信息工程系在读. 前言 tensorflow中文社区对官方文档进行了完整翻 ...

  3. 神经网络学习(三)比较详细 卷积神经网络原理、手写字体识别(卷积网络实现)

    之前写了一篇基于minist数据集(手写数字0-9)的全连接层神经网络,识别率(85%)并不高,这段时间学习了一些卷积神经网络的知识又实践了一把, 识别率(96%左右)确实上来了 ,下面把我的学习过程 ...

  4. 基于TensorFlow深度学习框架,运用python搭建LeNet-5卷积神经网络模型和mnist手写数字识别数据集,设计一个手写数字识别软件。

    本软件是基于TensorFlow深度学习框架,运用LeNet-5卷积神经网络模型和mnist手写数字识别数据集所设计的手写数字识别软件. 具体实现如下: 1.读入数据:运用TensorFlow深度学习 ...

  5. 用tensorflow搭建RNN(LSTM)进行MNIST 手写数字辨识

    用tensorflow搭建RNN(LSTM)进行MNIST 手写数字辨识 循环神经网络RNN相比传统的神经网络在处理序列化数据时更有优势,因为RNN能够将加入上(下)文信息进行考虑.一个简单的RNN如 ...

  6. PyTorch基础与简单应用:构建卷积神经网络实现MNIST手写数字分类

    文章目录 (一) 问题描述 (二) 设计简要描述 (三) 程序清单 (四) 结果分析 (五) 调试报告 (六) 实验小结 (七) 参考资料 (一) 问题描述 构建卷积神经网络实现MNIST手写数字分类 ...

  7. MNIST手写字体识别入门编译过程遇到的问题及解决

    MNIST手写字体识别入门编译过程遇到的问题及解决 以MNIST手写字体识别作为神经网络及各种网络模型的作为练手,将遇到的问题在这里记录与交流. 激活tensorflow环境后,运行spyder或者j ...

  8. matlab文字bp识别,MNIST手写字体识别(CNN+BP两种实现)-Matlab程序

    [实例简介] MNIST手写字 Matlab程序,包含BP和CNN程序.不依赖任何库,包含MNIST数据,BP网络可达到98.3%的识别率,CNN可达到99%的识别率.CNN比较耗时,关于CNN的程序 ...

  9. 【TensorFlow-windows】(四) CNN(卷积神经网络)进行手写数字识别(mnist)

    主要内容: 1.基于CNN的mnist手写数字识别(详细代码注释) 2.该实现中的函数总结 平台: 1.windows 10 64位 2.Anaconda3-4.2.0-Windows-x86_64. ...

最新文章

  1. 原来女孩要的不是真爱,而是关爱...
  2. PHP算法之四大基础算法
  3. Codeforces 833B 题解(DP+线段树)
  4. 用法 the_【课堂】a、an、the的用法
  5. ActionBarDisplayOptions展示选项的菜单
  6. maven详解之坐标与依赖
  7. 获取系统信息2——linux中使用随机数
  8. 一文搞懂隐马尔可夫模型(HMM)
  9. Eclipse开启或取消快速导航栏(toggle breadcrumb)
  10. 将 instance 连接到 second_local_net - 每天5分钟玩转 OpenStack(85)
  11. .NetCore实践爬虫系统(一)解析网页内容
  12. 6.面向对象的三大特征
  13. matlab将函数展开成幂级数,解析函数展开成幂级数的方法分析.doc
  14. 在Ubuntu 10上使用D-Link DWA 130无线网卡
  15. 如何打造一个能自动回复的钉钉机器人
  16. 密探查询系统服务器码,车辆国几排放查询
  17. android自定义通知栏_推送图片
  18. keras的model保存和载入
  19. icon图标 地址栏 收藏夹显示 代码
  20. 开发者应用盈利最佳渠道-KeyMob移动广告聚合平台

热门文章

  1. php爬新浪股票,GitHub - asa1525/SinaFinance-Crawler: 爬取了深沪股票的资讯,包含标题、时间、来源、内容、关键词、链接和股票ID...
  2. panda中的apply()方法介绍
  3. req.getparameter()
  4. 【bzoj4883】[Lydsy2017年5月月赛]棋盘上的守卫 最小环套树森林
  5. 邻接表-数据结构(C语言)
  6. FileProvider使用
  7. WPF-样式问题-ListBox或ListView中子项全填充去除边线问题
  8. flutter wrap和chip
  9. python 创建一个txt文件
  10. Locksupport 与 Condition