Tensorflow预测自己手写的数字

用画图工具手写的数字:

在这里插入图片描述

预测结果: [0 1 2 3 4 5 5 7 3 7 3 3],把【6,8,9】给认错了,BMP格式的位图只有0和1两个特征,而使用JPEG格式能保留最精确的特征。打印一张官方的MNIST数据图的矩阵,可以看到特征是精确到两位数的。

代码实现:

import tensorflow as tf
import random
import matplotlib.pyplot as plt
from tensorflow.examples.tutorials.mnist import input_data
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt

mnist = input_data.read_data_sets("./mnist_data/",one_hot=True)
x = tf.placeholder("float",[None,784])

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")

#图片转矩阵(测试自己绘制的数字图片用)
def ImageToMatrix(filename):
    im = Image.open(filename)
    im = im.convert("L") 
    data = im.getdata()
    data = np.matrix(data,dtype='float')/-255.0
    new_data = np.reshape(data,28*28)
    return new_data+1

# 第一次卷积
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([3,3,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)

# Dopout,减少过拟合
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)

y_ = tf.placeholder("float",[None,10])
cross_entropy = -tf.reduce_sum(y_*tf.log(y_conv))
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)

original_label = tf.argmax(y_,1)
predict_label = tf.argmax(y_conv,1)
correct_prediction = tf.equal(original_label,predict_label)
accuracy = tf.reduce_mean(tf.cast(correct_prediction,"float"))
#存档两个。
saver = tf.train.Saver(max_to_keep=2)
max_acc = 0

is_train=False

# 不备注为训练,备注为预测精准度。
# is_train=True

with tf.Session() as sess:
	init = tf.global_variables_initializer()
	sess.run(init)
	#使用上次的存档点,训练时可减少时间成本
	model_file=tf.train.latest_checkpoint('./ckpt/')
	saver.restore(sess,model_file)
	if is_train:
		for i in range(5001):
			batch = mnist.train.next_batch(200)
			#每100次训练,计算输出一次准确度。
			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))
				#准确度更高时进行存档。
				if train_accuracy>=max_acc or i==5000:
					max_acc = train_accuracy
					saver.save(sess,'./ckpt/mnist.ckpt',global_step=i)
			#进行训练。
			train_step.run(feed_dict={x: batch[0], y_: batch[1], keep_prob: 0.5})	
	
	#结束训练时(或者is_train==False,使用存档),打印出预测整个测试集的准确度
	# print("test accuracy %g"%accuracy.eval(feed_dict={
	# 	x: mnist.test.images, y_: mnist.test.labels, keep_prob: 1.0}))

	#自己绘制的数字图片28*28

	data0 = ImageToMatrix('./image/0.bmp')
	data1 = ImageToMatrix('./image/1.bmp')
	data2 = ImageToMatrix('./image/2.bmp')
	data3 = ImageToMatrix('./image/3.bmp')
	data4 = ImageToMatrix('./image/4.bmp')
	data5 = ImageToMatrix('./image/5.bmp')
	data6 = ImageToMatrix('./image/6.bmp')
	data7 = ImageToMatrix('./image/7.bmp')
	data8 = ImageToMatrix('./image/8.bmp')
	data9 = ImageToMatrix('./image/9.bmp')

	data3a = ImageToMatrix('./image/3a.bmp')
	data3b = ImageToMatrix('./image/3b.bmp')
	
	#predict: [0 1 2 3 4 5 5 7 3 7 3 3],把【6,8,9】给认错了。。。或许是图片转矩阵的方法,灰度丢失的缘故。
	print("predict:",predict_label.eval(feed_dict={
					x:np.vstack((data0,data1,data2,data3,data4,data5,data6,data7,data8,data9,data3a,data3b)), keep_prob: 1.0}))
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值