[MNIST数据集]输入图像的预处理
因为MNIST数据是28*28的黑底白字图像,而且输入时要将其拉直,也就是可以看成1*784的二维张量(张量的值在0~1之间),所以我们要对图片进行预处理操作,是图片能被网络识别。
以下是代码部分
import tensorflow as tf
import numpy as np
from PIL import Image
import backward as bw
import forward as fw def restore(testPicArr):
with tf.Graph().as_default() as g:
x = tf.placeholder(tf.float32, [None, fw.INPUT_NODES])
y_ = tf.placeholder(tf.float32, [None, fw.OUTPUT_NODES])
y = fw.get_y(x, None)
preValue = tf.arg_max(y, 1) ema = tf.train.ExponentialMovingAverage(bw.MOVING_ARVERAGE_DECAY)
ema_restore = ema.variables_to_restore()
saver = tf.train.Saver(ema_restore) with tf.Session() as sess:
tf.logging.set_verbosity(tf.logging.WARN)#降低警告等级
ckpt = tf.train.get_checkpoint_state("./model/")
if ckpt and ckpt.model_checkpoint_path:
saver.restore(sess, ckpt.model_checkpoint_path) preValue = sess.run(preValue, feed_dict = {x: testPicArr})
return preValue
else:
print("NO!!!")
return -1 def pre_pic(picName):
img = Image.open(picName)
reIm = img.resize((28, 28), Image.ANTIALIAS)
im_arr = np.array(reIm.convert('L'))#变为灰度图
threshold = 50#阈值,将图片二值化操作
for i in range(28):
for j in range(28):
im_arr[i][j] = 255 - im_arr[i][j]#进行反色处理
if(im_arr[i][j] < threshold):
im_arr[i][j] = 0
else: im_arr[i][j] = 255 nm_arr = im_arr.reshape([1,784])
nm_arr = nm_arr.astype(np.float32)#类型转换
img_ready = np.multiply(nm_arr, 1.0/255.0)#把值变为0~1之间的数值 return img_ready def app():
testNum = input("Input the number of test pictutre:")
for i in range(int(testNum)):
testPic = input("the path of test picture:")
testPicArr = pre_pic(testPic)
preValue = restore(testPicArr)
print("The prediction number is :" , preValue) def main():
app() if __name__ == '__main__':
main()
[MNIST数据集]输入图像的预处理的更多相关文章
- SGD与Adam识别MNIST数据集
几种常见的优化函数比较:https://blog.csdn.net/w113691/article/details/82631097 ''' 基于Adam识别MNIST数据集 ''' import t ...
- 一个简单的TensorFlow可视化MNIST数据集识别程序
下面是TensorFlow可视化MNIST数据集识别程序,可视化内容是,TensorFlow计算图,表(loss, 直方图, 标准差(stddev)) # -*- coding: utf-8 -*- ...
- 基于MNIST数据集使用TensorFlow训练一个没有隐含层的浅层神经网络
基础 在参考①中我们详细介绍了没有隐含层的神经网络结构,该神经网络只有输入层和输出层,并且输入层和输出层是通过全连接方式进行连接的.具体结构如下: 我们用此网络结构基于MNIST数据集(参考②)进行训 ...
- MNIST数据集入门
简单的训练MNIST数据集 (0-9的数字图片) 详细地址(包括下载地址):http://www.tensorfly.cn/tfdoc/tutorials/mnist_beginners.html # ...
- keras实现mnist数据集手写数字识别
一. Tensorflow环境的安装 这里我们只讲CPU版本,使用 Anaconda 进行安装 a.首先我们要安装 Anaconda 链接:https://pan.baidu.com/s/1AxdGi ...
- 【TensorFlow/简单网络】MNIST数据集-softmax、全连接神经网络,卷积神经网络模型
初学tensorflow,参考了以下几篇博客: soft模型 tensorflow构建全连接神经网络 tensorflow构建卷积神经网络 tensorflow构建卷积神经网络 tensorflow构 ...
- Theano mnist数据集格式
首先链接一篇大牛的Theano文档翻译:http://www.cnblogs.com/xueliangliu/archive/2013/04/03/2997437.html 里面有mnist.pkl. ...
- 【转载】用Scikit-Learn构建K-近邻算法,分类MNIST数据集
原帖地址:https://www.jiqizhixin.com/articles/2018-04-03-5 K 近邻算法,简称 K-NN.在如今深度学习盛行的时代,这个经典的机器学习算法经常被轻视.本 ...
- 卷积神经网络CNN识别MNIST数据集
这次我们将建立一个卷积神经网络,它可以把MNIST手写字符的识别准确率提升到99%,读者可能需要一些卷积神经网络的基础知识才能更好的理解本节的内容. 程序的开头是导入TensorFlow: impor ...
随机推荐
- 用SERVLET进行用户名和密码验证
一.界面展示 二.登录成功显示 三.代码 html <!DOCTYPE html> <html> <head> <meta charset="UTF ...
- mycat 使用
介绍 支持SQL92标准 支持MySQL.Oracle.DB2.SQL Server.PostgreSQL等DB的常见SQL语法 遵守Mysql原生协议,跨语言,跨平台,跨数据库的通用中间件代理. 基 ...
- vue动态添加对象属性,视图不渲染
发现数据确实改变了.但是视图没有渲染.原因是赋值的问题,应该这样动态增加属性 vm.$set(vm.template.titleAttachInfoDetail,newKey,newVal) vm 当 ...
- 纯css美化下拉框、复选框以及单选框样式并用jquery获取到其被选中的val
具体样式如图所示: 注:获取val值时记得要先引入jquery库奥. 1.下拉框 css部分 #cargo_type_id{ font-size: 13px; border: solid 1px #b ...
- 代码,python1
def main(): try: number1,number2=eval(input("please enter two number")) result=number1/num ...
- Concurrent下的线程安全集合
1.ArrayBlockingQueue ArrayBlockingQueue是由数组支持的线程安全的有界阻塞队列,此队列按 FIFO(先进先出)原则对元素进行排序.这是一个典型的“有界缓存区”,固定 ...
- Accoridion折叠面板
详细操作见代码: <!doctype html> <html> <head> <meta charset="UTF-8"> < ...
- shiro登录认证
新建maven文件create a simple project shiro.ini文件 [users] root=123456 log4j.properties log4j.rootLogger=I ...
- day 14 - 2 生成器练习
相关练习 1.处理文件,用户指定要查找的文件和内容,将文件中包含要查找内容的每一行都输出到屏幕 #比较 low 的方法 def check_file(filename,aim): with open( ...
- C++设计模式——模板方法模式
模板方法模式 在GOF的<设计模式:可复用面向对象软件的基础>一书中对模板方法模式是这样说的:定义一个操作中的算法骨架,而将一些步骤延迟到子类中.TemplateMethod使得子类可以不 ...