# -*- coding: utf-8 -*-
"""
Created on Sat Nov 25 22:08:35 2017
@author: Administrator
"""
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets('MNIST_data', one_hot=True)
import tensorflow as tf
import numpy
import os
from PIL import Image
from skimage import img_as_int
from skimage import img_as_ubyte
import cv2
'''
导入图像
'''
im = Image.open("C:/Users/Administrator/Desktop/ml_test/my_test_data/my_test0_6.jpg")
#im = Image.open("3.jpg")
#转换灰度图
im_L =numpy.mat(im.convert("L"))
im_dL=255-img_as_ubyte(im_L)
im_dL=numpy.float32(im_dL/255)
data_to_test= numpy.mat(im_dL[0,:])
for i in range(1,28):
data_to_test=numpy.hstack ((data_to_test,numpy.mat(im_dL[i,:])))
#the_beging_data=tf.convert_to_tensor(data_to_test)
#data_to_test=numpy.mat(mnist.test.images[0,:])
my_y_=numpy.array([[1,0,0,0,0,0,0,0,0,0]])
sess = tf.InteractiveSession()
x = tf.placeholder("float", shape=[None, 784])
y_ = tf.placeholder("float", shape=[None, 10])
W = tf.Variable(tf.zeros([784,10]))
b = tf.Variable(tf.zeros([10]))
#sess.run(tf.initialize_all_variables())
#y = tf.nn.softmax(tf.matmul(x,W) + b)
'''
权重初始化
'''
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)
'''
卷积和池化
'''
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')
#第一层卷积
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("float")
h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)
W_fc2 = weight_variable([1024, 10])
b_fc2 = bias_variable([10])
'''
构建模型,交叉熵
'''
y_conv=tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2)
cross_entropy = -tf.reduce_sum(y_*tf.log(y_conv))
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
correct_prediction = tf.equal(tf.argmax(y_conv,1), tf.argmax(y_,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
'''
初始化开始训练
'''
sess.run(tf.initialize_all_variables())
"""
for i in range(20000):
batch = mnist.train.next_batch(50)
if i%100 == 0:
train_accuracy = accuracy.eval(feed_dict={
x:batch[0], y_: batch[1], keep_prob: 1.0})
print ("step %d, training accuracy %g"%(i, train_accuracy))
train_step.run(feed_dict={x: batch[0], y_: batch[1], keep_prob: 0.5})
"""
saver = tf.train.Saver({'W_conv1':W_conv1,'b_conv1':b_conv1,'W_conv2':W_conv2,'b_conv2':b_conv2,'W_fc1':W_fc1,'b_fc1':b_fc1,'W_fc2':W_fc2,'b_fc2':b_fc2})
sess = tf.InteractiveSession()
saver.restore(sess,'C:/Users/Administrator/Desktop/ml_test/Train_result/Result')
'''
打印结果
''''''
print ("test accuracy %g"%accuracy.eval(feed_dict={
x: data_to_test, y_: my_y_, keep_prob: 1.0}))
print ("test accuracy %r"%y_conv.eval(feed_dict={
x: data_to_test, y_: my_y_, keep_prob: 1.0}))
'''
'''
MNIST测试数据
print("The MNIST test data")
print ("test accuracy %g"%accuracy.eval(feed_dict={
x: mnist.test.images, y_: mnist.test.labels, keep_prob: 1.0}))
'''
M=0
N=0
for root,dirs,files in os.walk('C:/Users/Administrator/Desktop/ml_test/my_test_newdata'):
for filespath in files:
M=M+1
im = Image.open("C:/Users/Administrator/Desktop/ml_test/my_test_newdata/"+filespath)
#转换灰度图
im_L =numpy.mat(im.convert("L"))
im_dL=img_as_ubyte(im_L)
im_dL=numpy.float32(im_dL/255.0)
#data_to_test=cv2.resize(im_dL,(784,1))
data_to_test= numpy.mat(im_dL[0,:])
for i in range(1,28):
data_to_test=numpy.hstack ((data_to_test,numpy.mat(im_dL[i,:])))
the_output= numpy.mat(y_conv.eval(feed_dict={x: data_to_test, y_: my_y_, keep_prob: 1.0}))
the_out=numpy.max(the_output)
for i in range(0,10):
if the_out == the_output[0,i]:
print("%s The digital is %d" %(filespath ,i))
if i == 1:
N=N+1
print(N/M)
'''
file_name='C:/Users/Administrator/Desktop/ml_test/NNT_binT/9/9_491.jpg'#导入自己的图片地址
im = Image.open(file_name).convert('L')
tv = list(im.getdata()) #get pixel values
tva = [ (255-x)*1.0/255.0 for x in tv]
#print(tva)
data_to_test=numpy.mat(tva)
the_output= numpy.mat(y_conv.eval(feed_dict={x: data_to_test, y_: my_y_, keep_prob: 1.0}))
the_out=numpy.max(the_output)
for i in range(0,10):
if the_out == the_output[0,i]:
print("The digital is %d"%i)
'''
'''
Num_AllFile=0
Num_RigFile=0
for root,dirs,files in os.walk('C:/Users/Administrator/Desktop/ml_test/NNT_binT/0'):
for filespath in files:
#print(filespath)
Num_AllFile=Num_AllFile+1
im = Image.open("C:/Users/Administrator/Desktop/ml_test/NNT_binT/0/"+filespath)
#转换灰度图
im_L =numpy.mat(im.convert("L"))
im_dL=img_as_ubyte(im_L)
im_dL=numpy.float32(im_dL/255.0)
data_to_test= numpy.mat(im_dL[0,:])
for i in range(1,28):
data_to_test=numpy.hstack ((data_to_test,numpy.mat(im_dL[i,:])))
the_output= numpy.mat(y_conv.eval(feed_dict={x: data_to_test, y_: my_y_, keep_prob: 1.0}))
the_out=numpy.max(the_output)
for i in range(0,10):
if the_out == the_output[0,i]:
#print("The digital is %d"%i)
if i == 0:
Num_RigFile=Num_RigFile+1
print("The accuracy of digital 9 is %f"%(Num_RigFile/Num_AllFile))
'''
'''
MNIST测试数据
'''
'''
for The_num in range(0,100):
the_out=numpy.max(mnist.test.labels[The_num])
for i in range(0,10):
if the_out == mnist.test.labels[The_num,i]:
print(i)
In_data=numpy.mat(mnist.test.images[The_num,:])
In_data=In_data*255
Input_data= numpy.mat(In_data[0,0:28])
for i in range(1,28):
Input_data=numpy.vstack((Input_data,numpy.mat(In_data[0,28*i:(28*(i+1))])))
im_dL=numpy.float32(Input_data/255.0)
data_to_test= numpy.mat(im_dL[0,:])
for i in range(1,28):
data_to_test=numpy.hstack ((data_to_test,numpy.mat(im_dL[i,:])))
the_output= numpy.mat(y_conv.eval(feed_dict={x: data_to_test, y_: my_y_, keep_prob: 1.0}))
the_out=numpy.max(the_output)
for i in range(0,10):
if the_out == the_output[0,i]:
print("The digital is %d"%i)
'''
'''
MNIST测试数据
print("The MNIST test data")
print ("test accuracy %g"%accuracy.eval(feed_dict={
x: mnist.test.images, y_: mnist.test.labels, keep_prob: 1.0}))
'''
"""
'''
保存结果
'''
saver = tf.train.Saver({'W_conv1':W_conv1,'b_conv1':b_conv1,'W_conv2':W_conv2,'b_conv2':b_conv2,'W_fc1':W_fc1,'b_fc1':b_fc1,'W_fc2':W_fc2,'b_fc2':b_fc2})
saver.save(sess,'C:/Users/Administrator/Desktop/ml_test/Train_result/Result')
"""
'''
读取结果
'''
'''
saver.restore(sess,'C:/Users/Administrator/Desktop/ml_test/Train_result/Result')
'''
print
没有合适的资源?快使用搜索试试~ 我知道了~
TensorFlow_CNN_MNIST
共180个文件
jpg:165个
gz:4个
py:4个
1星 需积分: 13 16 下载量 86 浏览量
2017-12-29
08:43:33
上传
评论
收藏 34.87MB RAR 举报
温馨提示
TensorFlow与CNN做的MNIST手写数字识别,并存储训练结果,预测时对自己写的数字进行识别。包括CNN训练,结果保存,图片预处理,图片预测
资源推荐
资源详情
资源评论
收起资源包目录
TensorFlow_CNN_MNIST (180个子文件)
checkpoint 173B
Result.data-00000-of-00001 12.49MB
train-images-idx3-ubyte.gz 9.45MB
t10k-images-idx3-ubyte.gz 1.57MB
train-labels-idx1-ubyte.gz 28KB
t10k-labels-idx1-ubyte.gz 4KB
Result.index 350B
my_test6_6.jpg 18KB
my_test4_4.jpg 18KB
my_test6_0.jpg 18KB
my_test9_8.jpg 18KB
my_test4_6.jpg 18KB
my_test3_0.jpg 18KB
my_test4_1.jpg 18KB
my_test6_2.jpg 18KB
my_test0_6.jpg 18KB
my_test4_5.jpg 18KB
my_test8_0.jpg 18KB
my_test6_4.jpg 18KB
my_test9_6.jpg 18KB
my_test6_5.jpg 18KB
my_test2_0.jpg 18KB
my_test6_1.jpg 18KB
my_test9_9.jpg 18KB
my_test5_0.jpg 18KB
my_test4_7.jpg 18KB
my_test9_4.jpg 18KB
my_test9_5.jpg 18KB
my_test7_0.jpg 18KB
my_test6_3.jpg 18KB
my_test9_2.jpg 18KB
my_test4_0.jpg 18KB
my_test4_3.jpg 18KB
my_test4_2.jpg 18KB
my_test9_7.jpg 18KB
my_test9_3.jpg 18KB
my_test9_1.jpg 18KB
my_test9_0.jpg 17KB
my_test9_10.jpg 17KB
my_test1_0.jpg 17KB
my_test1_2.jpg 17KB
my_test1_4.jpg 17KB
my_test1_1.jpg 17KB
my_test1_3.jpg 17KB
my_test1_5.jpg 17KB
my_test1_6.jpg 17KB
my_test9_17.jpg 8KB
my_test9_16.jpg 8KB
my_test8_6.jpg 1KB
my_test9_13.jpg 1KB
my_test8_5.jpg 1KB
my_test8_3.jpg 1KB
my_test8_4.jpg 1KB
my_test0_0.jpg 1KB
my_test2_4.jpg 1KB
my_test8_1.jpg 1KB
my_test5_6.jpg 1KB
my_test3_5.jpg 1KB
my_test9_14.jpg 1KB
my_test5_4.jpg 1KB
my_test3_6.jpg 1KB
my_test8_2.jpg 1KB
my_test0_4.jpg 1KB
my_test3_3.jpg 1KB
my_test0_1.jpg 1KB
my_test0_5.jpg 1KB
my_test0_3.jpg 1KB
my_test5_1.jpg 1KB
my_test2_5.jpg 1KB
my_test2_1.jpg 1KB
my_test3_1.jpg 1KB
my_test5_5.jpg 1KB
my_test2_2.jpg 1KB
my_test2_3.jpg 1KB
my_test3_4.jpg 1KB
my_test3_2.jpg 1KB
my_test2_6.jpg 1KB
my_test7_3.jpg 1KB
my_test0_2.jpg 1KB
my_test5_2.jpg 1KB
my_test5_3.jpg 1KB
my_test7_2.jpg 1022B
my_test7_5.jpg 998B
v2-23c007758a828c1e57e1cf3e483fc931_hd.jpg 985B
my_test7_6.jpg 967B
my_test9_12.jpg 962B
my_test3_4.jpg 961B
my_test7_1.jpg 957B
my_test4_0.jpg 957B
my_test5_2.jpg 955B
my_test0_1.jpg 954B
my_test7_4.jpg 949B
my_test5_6.jpg 943B
my_test9_15.jpg 942B
my_test0_3.jpg 927B
my_test3_2.jpg 927B
my_test3_1.jpg 927B
my_test3_5.jpg 925B
my_test3_3.jpg 922B
my_test3_6.jpg 921B
共 180 条
- 1
- 2
资源评论
- z_r_j2018-04-24就是一个minist示例代码
普通网友
- 粉丝: 0
- 资源: 11
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- Windows 常见运行运行库32+64
- 基于3KW光伏并网单相逆变器设计(TMS320F28035控制板+显示板+STM32F103功率板)硬件(原理图+PCB)工程
- 正点原子HAL库 STM32F4 外部中断(学习自用附源码)
- delphi rzcombobox DropDownList 灰色背景改为白色
- sap sd.docsap sd.doc
- torch-1.10.2-cp38-cp38-win-amd64.whl
- 菜单栏实现增加数据,修改数据,查询数据,删除数据
- 全国省市区三级联动json文件,带code
- C8_全局&局部&static.zip
- Unity和安卓交互插件Unity调Android Native Goodies PRO
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功