Created
July 20, 2019 09:05
-
-
Save R97416032/4771266b34c87448b08d0564420906d1 to your computer and use it in GitHub Desktop.
手写识别
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from tensorflow.examples.tutorials.mnist import input_data | |
from PIL import Image | |
import numpy as np | |
#路径要写全 | |
MNIST_data_folder="D:\MNIST\mnist" | |
mnist=input_data.read_data_sets(MNIST_data_folder,one_hot=True) | |
print(mnist.train.images.shape, mnist.train.labels.shape) | |
print(mnist.test.images.shape, mnist.test.labels.shape) | |
print(mnist.validation.images.shape, mnist.validation.labels.shape) | |
import tensorflow as tf | |
# 定义两个placeholder | |
x = tf.placeholder(tf.float32, [None, 784]) | |
y = tf.placeholder(tf.float32, [None, 10]) | |
x1 = tf.placeholder(tf.float32, [None, 784]) | |
keep_prob = tf.placeholder("float") | |
# 创建一个简单的神经网络 | |
W = tf.Variable(tf.zeros([784, 10])) | |
b = tf.Variable(tf.zeros([10])) | |
prediction = tf.nn.softmax(tf.matmul(x, W) + b) | |
# 二次代价函数 | |
loss = tf.reduce_mean(tf.square(y - prediction)) | |
# 使用梯度下降法 | |
train_step = tf.train.GradientDescentOptimizer(0.2).minimize(loss) | |
# 结果存放在一个布尔型列表中 | |
correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(prediction, 1)) # argmax返回一维张量中最大的值所在的位置 | |
# 求准确率 | |
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) | |
# 创建 summary node | |
# w, b 画直方图 | |
# loss, accuracy画标量图 | |
tf.summary.histogram('weight', W) | |
tf.summary.histogram('bias', b) | |
tf.summary.scalar('loss', loss) | |
tf.summary.scalar('accuracy', accuracy) | |
merged_summary_op = tf.summary.merge_all() | |
# 每个批次的大小 | |
batch_size = 100 | |
# 计算一共有多少个批次 | |
n_batch = mnist.train.num_examples // batch_size | |
print(n_batch) | |
logs_path = 'C:\\Users\\R\\Desktop\\' | |
saver = tf.train.Saver() #定义saver | |
CKPT_DIR = 'C:\\Users\\R\\Desktop\\' | |
ckpt = tf.train.get_checkpoint_state(CKPT_DIR) | |
with tf.Session() as sess: | |
init = tf.global_variables_initializer() | |
sess.run(init) | |
# saver.restore(sess, "d:/MNISTSAVE/model.ckpt") | |
ckpt = tf.train.get_checkpoint_state(CKPT_DIR) | |
if ckpt and ckpt.model_checkpoint_path: | |
saver.restore(sess, ckpt.model_checkpoint_path) | |
summary_writer = tf.summary.FileWriter(logs_path, graph=tf.get_default_graph()) | |
for epoch in range(0): | |
for batch in range(n_batch): | |
batch_xs, batch_ys = mnist.train.next_batch(batch_size) | |
sess.run(train_step, feed_dict={x: batch_xs, y: batch_ys}) | |
acc = sess.run(accuracy, feed_dict={x: mnist.test.images, y: mnist.test.labels}) | |
print("Iter " + str(epoch) + ",Testing Accuracy " + str(acc)) | |
if epoch % 100 == 0: | |
saver.save(sess, 'C:\\Users\\R\\Desktop\\' + 'model', global_step=epoch) | |
# saver.save(sess, 'd:/MNISTSAVE/model.ckpt') # 模型储存位置 | |
# summary_writer.add_summary(acc, epoch * n_batch + batch) | |
image_path="C:\\Users\\R\\Desktop\\3_59.png" | |
img = Image.open(image_path).convert('L') | |
flatten_img = np.reshape(img, 784) | |
x1 = np.array([1 - flatten_img]) | |
output = sess.run(prediction, feed_dict={x: x1, keep_prob:1.0}) | |
# out = sess.run(y, feed_dict={x:x1}) | |
print(image_path) | |
print(' -> Predict digit', np.argmax(output)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment