基本信息
源码名称:基于python实现的手写数字识别
源码大小:32.97M
文件格式:.zip
开发语言:Python
更新时间:2019-12-17
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
│ 8.png
│ checkpoint
│ demo01_ce.py
│ demo02_tfdemo.py
│ demo03_mnist.py
│ demo04_mnist_test.py
│ mnist_model.ckpt.data-00000-of-00001
│ mnist_model.ckpt.index
│ mnist_model.ckpt.meta
│ 神经网络分类模型_初始.png
│ 神经网络分类模型_结果.png
│
└─MNIST_data
t10k-images-idx3-ubyte.gz
t10k-images.idx3-ubyte
t10k-labels-idx1-ubyte.gz
t10k-labels.idx1-ubyte
train-images-idx3-ubyte.gz
train-images.idx3-ubyte
train-labels-idx1-ubyte.gz
train-labels.idx1-ubyte
import tensorflow as tf import cv2 as cv import numpy as np #生成权重 def weight_variable(shape): initial = tf.random_normal(shape, stddev=0.1) return tf.Variable(initial) #生成b 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') x = tf.placeholder("float", shape=[None, 784]) y_ = tf.placeholder("float", shape=[None, 10]) x_image = tf.reshape(x, [-1,28,28,1]) W_conv1 = weight_variable([5, 5, 1, 32]) b_conv1 = bias_variable([32]) 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) h_pool2_flat = tf.reshape(h_pool2, [-1, 7*7*64]) W_fc1 = weight_variable([7 * 7 * 64, 1024]) b_fc1 = bias_variable([1024]) h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) b_fc1) W_fc2 = weight_variable([1024, 10]) b_fc2 = bias_variable([10]) y_conv=tf.nn.softmax(tf.matmul(h_fc1, W_fc2) b_fc2) saver = tf.train.Saver() with tf.Session() as sess: # 加载模型参数 saver.restore(sess, 'mnist_model.ckpt') print('Variables restored.') # 读入测试图片 original = cv.imread('8.png') # 图片转灰度 gray = cv.cvtColor(original, cv.COLOR_BGR2GRAY) # 把灰度小于等于10的=0,大于10的=1(纯 黑色像素 值为 0, 纯 白色像素值为 1) gray[gray<=10] = 0 gray[gray>10] = 1 # 修改灰度图片的像素改为28x28, 并且转为二维数组 gray = cv.resize(gray, (28, 28)).reshape(1, 28*28) # 喂数据(把gray代入方程获取测试结果) pred_y = sess.run(y_conv, feed_dict={x:gray}) # 取预测结果的最大值的索引就是真实预测结果 print(pred_y.argmax()) # from tensorflow.examples.tutorials.mnist import input_data # import tensorflow as tf # mnist = input_data.read_data_sets('MNIST_data', one_hot=True) # batch = mnist.test.next_batch(5000) # test_x = batch[0] # test_y = batch[1] # pred_y = sess.run(y_conv, feed_dict={x:test_x}) # for pred, label in zip( # np.argmax(pred_y, axis=1), # np.argmax(test_y, axis=1)): # print(pred, '<-', label) # # print((np.argmax(pred_y, axis=1) == np.argmax(test_y, axis=1)).sum() / 50)