使用TensorFlow的卷积神经网络识别手写数字(3)-识别篇
from PIL import Image
import numpy as np
import tensorflow as tf
import time bShowAccuracy = True # 加载手写图片
def loadHandWritingImage(strFilePath):
im = Image.open(strFilePath, 'r')
ndarrayImg = np.array(im.convert("L"), dtype='float') return ndarrayImg # 最大最小值归一化
def normalizeImage(ndarrayImg, maxVal = 255, minVal = 0):
ndarrayImg = (ndarrayImg - minVal) / (maxVal - minVal)
return ndarrayImg # 1)构造自己的手写图片集合,用加载的已训练好的模型识别
print('构造待识别数据...') # 待识别的手写图片,文件名是0...39
fileList = range(0, 39+1) ndarrayImgs = np.zeros((len(fileList), 784)) # x行784列 for index in range(len(fileList)): # 加载图片
ndarrayImg = loadHandWritingImage('28-pixel-numbers/' + str(index) + '.png') # 归一化
normalizeImage(ndarrayImg) # 转为1x784的数组
ndarrayImg = ndarrayImg.reshape((1, 784)) # 加入到测试集中
ndarrayImgs[index] = ndarrayImg ##import sys
##sys.exit() # 构建测试样本的实际值集合,用于计算正确率 # 真实结果,用于测试准确度。40行10列
ndarrayLabels = np.eye(10, k=0, dtype='float')
ndarrayLabels = np.vstack((ndarrayLabels, ndarrayLabels))
ndarrayLabels = np.vstack((ndarrayLabels, ndarrayLabels)) # 2)下面开始CNN相关 print('定义Tensor...') #定义变量和计算公式 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 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) x = tf.placeholder(tf.float32, shape=[None, 784])
y_ = tf.placeholder(tf.float32, shape=[None, 10]) W_conv1 = weight_variable([5, 5, 1, 32])
b_conv1 = bias_variable([32]) x_image = tf.reshape(x, [-1, 28, 28, 1]) h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1)
h_pool1 = max_pool_2x2(h_conv1) W_conv2 = weight_variable([5, 5, 32, 64])
b_conv2 = bias_variable([64]) h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2)
h_pool2 = max_pool_2x2(h_conv2) W_fc1 = weight_variable([7 * 7 * 64, 1024])
b_fc1 = bias_variable([1024]) 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) keep_prob = tf.placeholder(tf.float32)
h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob) W_fc2 = weight_variable([1024, 10])
b_fc2 = bias_variable([10]) y_conv = tf.matmul(h_fc1_drop, W_fc2) + b_fc2 correct_prediction = tf.equal(tf.argmax(y_conv, 1), tf.argmax(y_, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) with tf.Session() as sess:
sess.run(tf.global_variables_initializer()) # 3)创建saver对象并加载模型
print('加载已训练好的CNN模型...')
saver = tf.train.Saver()
saver.restore(sess, "saved_model/cnn_handwrite_number.ckpt") # 测试耗时
print('进行预测:') start = time.time() # 4)执行预测
output = sess.run(y_conv, feed_dict={x: ndarrayImgs, keep_prob:1.0}) end = time.time() print('预测数字为:\n', output.argmax(axis=1)) # axis:0表示按列,1表示按行
print('实际数字为:\n', ndarrayLabels.argmax(axis=1)) if(bShowAccuracy):
accu = accuracy.eval(feed_dict={x: ndarrayImgs, y_: ndarrayLabels, keep_prob: 1.0})
print('识别HateMath苍劲有力的手写数据%d个, 准确率为 %.2f%%, 每个耗时%.5f秒' %
(len(ndarrayImgs), accu*100, (end-start)/len(ndarrayImgs))) # todo
# 图像分割的准确度
使用TensorFlow的卷积神经网络识别手写数字(3)-识别篇的更多相关文章
- 用Keras搭建神经网络 简单模版(三)—— CNN 卷积神经网络(手写数字图片识别)
# -*- coding: utf-8 -*- import numpy as np np.random.seed(1337) #for reproducibility再现性 from keras.d ...
- TensorFlow卷积神经网络实现手写数字识别以及可视化
边学习边笔记 https://www.cnblogs.com/felixwang2/p/9190602.html # https://www.cnblogs.com/felixwang2/p/9190 ...
- 卷积神经网络CNN 手写数字识别
1. 知识点准备 在了解 CNN 网络神经之前有两个概念要理解,第一是二维图像上卷积的概念,第二是 pooling 的概念. a. 卷积 关于卷积的概念和细节可以参考这里,卷积运算有两个非常重要特性, ...
- 第二节,TensorFlow 使用前馈神经网络实现手写数字识别
一 感知器 感知器学习笔记:https://blog.csdn.net/liyuanbhu/article/details/51622695 感知器(Perceptron)是二分类的线性分类模型,其输 ...
- 基于卷积神经网络的手写数字识别分类(Tensorflow)
import numpy as np import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_dat ...
- TensorFlow(十):卷积神经网络实现手写数字识别以及可视化
上代码: import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data mnist = inpu ...
- 莫烦pytorch学习笔记(八)——卷积神经网络(手写数字识别实现)
莫烦视频网址 这个代码实现了预测和可视化 import os # third-party library import torch import torch.nn as nn import torch ...
- 用Keras搭建神经网络 简单模版(四)—— RNN Classifier 循环神经网络(手写数字图片识别)
# -*- coding: utf-8 -*- import numpy as np np.random.seed(1337) from keras.datasets import mnist fro ...
- PyTorch基础——使用卷积神经网络识别手写数字
一.介绍 实验内容 内容包括用 PyTorch 来实现一个卷积神经网络,从而实现手写数字识别任务. 除此之外,还对卷积神经网络的卷积核.特征图等进行了分析,引出了过滤器的概念,并简单示了卷积神经网络的 ...
- TensorFlow实战之Softmax Regression识别手写数字
关于本文说明,本人原博客地址位于http://blog.csdn.net/qq_37608890,本文来自笔者于2018年02月21日 23:10:04所撰写内容(http://blog.c ...
随机推荐
- Atcoder AGC016 E Poor Turkeys
比赛的时候口胡这道题口胡了一年,看完题解被教做人 题意:有n只火鸡,m个猎人按序来杀火鸡,从自己预先选的两只中杀一只,问有多少火鸡对可以同时存活 考虑对于每一只火鸡i,按时间逆序维护一个最小的集合Si ...
- eclipse plugin
快速查看目录 org.sf.easyexplore_1.0.4.jar mongo DB net.jumperz.app.MMonjaDB_1.0.16.jar jasper report jaspe ...
- 关于企业邮箱无法提醒解决办法(未安装邮件客户端可添加至网易邮箱大师/qq邮箱等)
关于企业邮箱无法提醒解决办法: 一.使用现有的邮箱客户端,以下以网易的邮箱大师为例mail.exe 点击客户端左边的添加邮箱账号,在出现的对话框中输入账号和密码后,点击登陆按钮后,等待添加完成即可,邮 ...
- (转)Module ngx_http_fastcgi_module
Example ConfigurationDirectives fastcgi_bind fastcgi_buffer_size fastcgi_buffering f ...
- 实现Sublime Text3中vue文件高亮显示的最有效的方法
今天第一次使用Sublime Text3软件,在实现vue文件高亮显示的过程中一直报错,经过了半天时间的不停尝试终于找到了最有效的一种解决方法!错误提示如下: 刚开始尝试了很多方法都不行,只要打开in ...
- 省厅报件7.0 读取mdb 生成xml 文件
using System;using System.Collections.Generic;using System.Data;using System.Data.OleDb;using System ...
- JSP jsp内置对象
jsp(java server pages):java服务器端的页面 JSP的执行过程 1.浏览器输入一个jsp页面 2.tomcat会接受*.jsp请求,将该请求发送到org.apache.ja ...
- AngularJS(五):表单及输入验证
本文也同步发表在我的公众号“我的天空” 表单基础 表单是HTML中很重要的一个部分,基本上我们的信息录入都依靠表单,接下来我们学习如何在AngularJS中使用表单,首先看以下示例代码: <bo ...
- Protocol Buffer学习教程之开篇概述(一)
1. Protocol Buffer是什么 Protocol Buffer是google旗下的产品,用于序列化与反序列化数据结构,但是比xml更小.更快.更简单,而且能跨语言.跨平台.你可以把你的数据 ...
- CodeForces 48C D - The Race (Fraction,数学)
每个加油的站可以确定一个alpha的上下界,比如,第i次加油站a[i],前面加了i次油,a[i]*10≤ alpha*i <(a[i]+1)*10. 取最大的下界,取最小的上界,看看两者之间的满 ...