手写数字识别 ----在已经训练好的数据上根据28*28的图片获取识别概率(基于Tensorflow,Python)
通过:
手写数字识别 ----卷积神经网络模型官方案例详解(基于Tensorflow,Python)
手写数字识别 ----Softmax回归模型官方案例详解(基于Tensorflow,Python)
运行程序后得的四个文件,再通过手写的
图片判断识别概率
代码:
import numpy as np
import tensorflow as tf
from flask import Flask, jsonify, render_template, request
import numpy as np
from PIL import Image
from mnist import model
import matplotlib.pyplot as plt
from matplotlib.image import imread # tf.placeholder(dtype, shape=None, name=None)
# 此函数可以理解为形参,用于定义过程,在执行的时候再赋具体的
# dtype:数据类型。常用的是tf.float32,tf.float64等数值类型
# shape:数据形状。默认是None,就是一维值,也可以是多维,比如[2,3], [None, 3]表示列是3,行不定
# name:名称。
# 返回:Tensor 类型 x = tf.placeholder("float", [None, 784]) '''用于运行TensorFlow操作的类。 '''
# session可能拥有的资源,如:tf.Variable,tf.QueueBase和tf.ReaderBase。
# 不再需要时释放这些资源是非常重要的。
# 为此,请在session中调用tf.Session.close方法,或使用session作为上下文管理器
sess = tf.Session() # 保存和恢复都需要实例化一个 tf.train.Saver。
# saver = tf.train.Saver()
# 在训练循环中,定期调用 saver.save() 方法,向文件夹中写入包含了当前模型中所有可训练变量的 checkpoint 文件。
# saver.save(sess, FLAGS.train_dir, global_step=step)
# 之后,就可以使用 saver.restore() 方法,重载模型的参数,继续训练或用于测试数据。
# saver.restore(sess, FLAGS.train_dir) # restore trained data
with tf.variable_scope("regression"):
y1, variables = model.regression(x)
saver = tf.train.Saver(variables)
saver.restore(sess, "mnist/data/regression.ckpt") # tf.get_variable(<name>, <shape>, <initializer>) 创建或返回给定名称的变量
# tf.variable_scope(<scope_name>) 管理传给get_variable()的变量名称的作用域
with tf.variable_scope("convolutional"):
keep_prob = tf.placeholder("float")
y2, variables = model.convolutional(x, keep_prob)
saver = tf.train.Saver(variables)
saver.restore(sess, "mnist/data/convolutional.ckpt") def regression(input):
# print('-------------------regression')
# print('y2:' + str(y1))
# print(input)
return sess.run(y1, feed_dict={x: input}).flatten().tolist() # run(
# fetches,
# feed_dict=None,
# options=None,
# run_metadata=None
# )
def convolutional(input):
# print('-------------------convolutional')
# print('y2:' + str(y2))
# print( input)
return sess.run(y2, feed_dict={x: input, keep_prob: 1.0}).flatten().tolist() # im = Image.open(r'C:\Users\admin\Desktop\无标题.png')
# im2 = np.array(im)
# print(im2) # img = imread(r'C:\Users\admin\Desktop\无标题.png') # 读入图像(设定合适的路径!)
# plt.imshow(img)
# plt.arr
# plt.show() # 读取图片
im = Image.open(r'C:\Users\admin\Desktop\2.png')
# 显示图片
# im.show()
im = im.convert("L")
# im.show()
data = im.getdata()
data = np.matrix(data) # print data
# 变换成512*512
data = np.reshape(data, (784, 1))
# new_im = Image.fromarray(data)
# # 显示图片
# new_im.show()
input = ((255 - np.array(data, dtype=np.uint8)) / 255.0).reshape(1, 784) # # print(input)
output1 = regression(input)
output2 = convolutional(input)
print(output1)
print(output2)
手写数字识别
运行后输出数据:其序号对应值为识别的数字,值为概率,有科学计数法显示数据。
[0.002712834160774946, 0.37007448077201843, 0.38919582962989807, 0.04636502265930176, 2.2569240172742866e-05, 0.12520278990268707, 0.04699072241783142, 0.0002446999424137175, 0.01896093040704727, 0.00023008222342468798](Softmax回归模型)
[0.0004617558151949197, 0.02070416323840618, 0.9636037349700928, 0.00868076179176569, 6.441913137678057e-05, 0.003921648487448692, 0.0009535282733850181, 0.0006638980703428388, 0.0006735732895322144, 0.0002723101933952421](卷积神经网络模型)
0.38919582962989807
0.9636037349700928
成功识别图片数字为2
相关代码链接:https://download.csdn.net/download/qq_35554617/10883571
手写数字识别 ----在已经训练好的数据上根据28*28的图片获取识别概率(基于Tensorflow,Python)的更多相关文章
- 使用TensorFlow的卷积神经网络识别手写数字(2)-训练篇
import numpy as np import tensorflow as tf import matplotlib import matplotlib.pyplot as plt import ...
- TensorFlow实战之Softmax Regression识别手写数字
关于本文说明,本人原博客地址位于http://blog.csdn.net/qq_37608890,本文来自笔者于2018年02月21日 23:10:04所撰写内容(http://blog.c ...
- C-01 手写数字识别
目录 手写数字识别应用程序 一.导入模块 二.图像转向量 三.训练并测试模型 四.模型转应用程序 4.1 展示图片 4.2 处理图片 4.3 预测图片 更新.更全的<机器学习>的更新网站, ...
- KNN 算法-实战篇-如何识别手写数字
公号:码农充电站pro 主页:https://codeshellme.github.io 上篇文章介绍了KNN 算法的原理,今天来介绍如何使用KNN 算法识别手写数字? 1,手写数字数据集 手写数字数 ...
- 【百度飞桨】手写数字识别模型部署Paddle Inference
从完成一个简单的『手写数字识别任务』开始,快速了解飞桨框架 API 的使用方法. 模型开发 『手写数字识别』是深度学习里的 Hello World 任务,用于对 0 ~ 9 的十类数字进行分类,即输入 ...
- 实现手写数字识别(数据集50000张图片)比较3种算法神经网络、灰度平均值、SVM各自的准确率—Jason niu
对手写数据集50000张图片实现阿拉伯数字0~9识别,并且对结果进行分析准确率, 手写数字数据集下载:http://yann.lecun.com/exdb/mnist/ 首先,利用图片本身的属性,图片 ...
- 学习OpenCV——SVM 手写数字检测
转自http://blog.csdn.net/firefight/article/details/6452188 是MNIST手写数字图片库:http://code.google.com/p/supp ...
- 07 训练Tensorflow识别手写数字
打开Python Shell,输入以下代码: import tensorflow as tf from tensorflow.examples.tutorials.mnist import input ...
- TensorFlow------单层(全连接层)实现手写数字识别训练及测试实例
TensorFlow之单层(全连接层)实现手写数字识别训练及测试实例: import tensorflow as tf from tensorflow.examples.tutorials.mnist ...
随机推荐
- Tomcat系列(9)——Tomcat 6方面调优(内存,线程,IO,压缩,缓存,集群)
核心部分 内存 线程 IO 压缩 缓存 集群 一.JVM内存优化 Tomcat内存优化,包括内存大小,垃圾回收策略. Windows 下的catalina.bat,Linux 下的catalina.s ...
- 部署kubernetes-dashboard
1.生成访问证书 | awk '{print $2}' | base64 -d >> kubecfg.crt | awk '{print $2}' | base64 -d >> ...
- SQL Server 游标的使用示例
Ø 简介 本文主要记录 MSSQL 中的游标使用示例,在有必要时方便借鉴查阅.游标一般定义在某段功能性的 SQL 语句中,或者存储过程中.之所以选择用它,是因为有时候无法使用简单的 SQL 语句满足 ...
- Java(19)JDBC
一.使用jdbc的步骤 a.引入数据库厂商提供的驱动程序(引入jar包) b.记载驱动程序 Clss.forName("驱动程序类") c.获得连接 Connection con ...
- 路径规划算法之Bellman-Ford算法
最近由于工作需要一直在研究Bellman-Ford算法,这也是我第一次用C++编写代码. 1.Bellman-Ford算法总结 (1)Bellman-Ford算法计算从源点(起始点)到任意一点的最短路 ...
- 背景上实现阴影——linear-gradient
/*从元素顶部有条阴影,两种方式,第二种更好,能控制阴影的宽度*/background-image: linear-gradient(0deg, rgba(226, 226, 226, 0) 97%, ...
- find 命令局部小结之 xtime
大家在使用find命令的时候往往会使用它的 -name 或者 -xtime,在这里就说下他的xtime. find / -mtime +7 .find / -mtime -7.find / -mti ...
- AttributeError: 'module' object has no attribute 'enableTrace'
Traceback (most recent call last): File "Long-lived-connection.py", line 29, in <module ...
- PostMan如何做Post请求测试
首先要下载 一个Postman的软件,我这里没有下载地址,据说要翻 墙 下面是使用postman模拟post请求的步骤 我这里请求的API地址和请求的参数都是乱填写的,使用的时候请自行替换你们需要的A ...
- Typescript04---模块、命名空间
在Typescript1.5 中,内部模块称作命名空间,外部模块成为模块 一.什么是模块? 模块就是一个或一组功能模块. 模块在其自身的作用域里执行,而不是在全局作用域里.意味着,模块中的变量.函数. ...