[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 ...
随机推荐
- FWT快速沃尔什变换学习笔记
FWT快速沃尔什变换学习笔记 1.FWT用来干啥啊 回忆一下多项式的卷积\(C_k=\sum_{i+j=k}A_i*B_j\) 我们可以用\(FFT\)来做. 甚至在一些特殊情况下,我们\(C_k=\ ...
- tomcat在windows及linux环境下安装
下载tomcat 下载地址: https://tomcat.apache.org/download-90.cgi 7,8,9的版本都可以下,这里下载最新版本 注意:Binary是编译好的,可以直接使用 ...
- kafka 发送确认参数acks的几种模式
1. acks=0 意味着生产者能够通过网络吧消息发送出去,那么就认为消息已成功写入Kafka 一定会丢失一些数据 2. acks=1 意味着首领在疏导消息并把它写到分区数据问津是会返回确认或者错误响 ...
- go 闭包程序解读
package main import "fmt" // 下面这函数就是闭包函数(就是返回一个匿名函数的地址的函数,其中要1.定义一个需要保护的局部变量i 2.一个匿名的函数) , ...
- 1.4分布式-通讯协议TCP/IP
服务器和浏览器的通讯依靠http协议,今天就来分析一下http协议的具体内容以及https的加密过程.除了这些协议,为了增加服务器和浏览器交互的可拓展性,也出现了rest风格的请求方式,方便调用接口. ...
- js值类型转换(boolean/String/number),js运算符,if条件,循环结构,函数,三种弹出框
js值类型转换 number | string | boolean boolean类型转换 num = 0; var b1 = Boolean(num); console.log(b1) 转化为数字类 ...
- Consequence of Point-by-Point Bounds
设 $X$ 是完备距离空间, $\scrF$ 是 $X$ 上的实连续函数族且具有性质: 对于每一 $x\in X$, 存在常数 $M_x>0$, 使得对于每一 $F\in\scrF$, $$\b ...
- 纯css美化下拉框、复选框以及单选框样式并用jquery获取到其被选中的val
具体样式如图所示: 注:获取val值时记得要先引入jquery库奥. 1.下拉框 css部分 #cargo_type_id{ font-size: 13px; border: solid 1px #b ...
- RT-SA-2019-007 Code Execution via Insecure Shell Functiongetopt_simple
Advisory: Code Execution via Insecure Shell Function getopt_simple RedTeam Pentesting discovered tha ...
- Contest2156 - 2019-3-7 高一noip基础知识点 测试2 题解版
传送门 预计得分:100+70+100+50=320 实际得分100+63+77+30=270 Ctrl_C+Ctrl_V时不要粘贴翻译的,直接粘原文, In a single line of the ...