700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > MNIST数据集实现手写数字识别(基于tensorflow)

MNIST数据集实现手写数字识别(基于tensorflow)

时间:2019-05-09 08:29:34

相关推荐

MNIST数据集实现手写数字识别(基于tensorflow)

------------先看看别人的博客---------------------

Tensorflow 实现 MNIST 手写数字识别 用这个的代码跑通了

使用Tensorflow和MNIST识别自己手写的数字

-------------我觉得要学习一些tf的函数知识才能搞得动-------------

1. tf.placeholder 与 tf.Variable

TensorFlow 辨异 —— tf.placeholder 与 tf.Variable

TensorFlow 学习积累(1):variable 和 placeholder

2. tf.zeros()

tf.zeros()的使用

3. tf.matmul函数——matrixmultiply 矩阵乘法

TensorFlow:tf.matmul函数

4. reduce_sum()函数和reduce_mean()函数

TensorFlow基础1:reduce_sum()函数和reduce_mean()函数

tf.reduce_mean()中第二个参数应该是要去掉的维度,tensor是按照先行后列的方式存储的

5. input_data模块

走进tensorflow第十步——手写数字识别中的input_data模块

-----------回过头来看看用的什么模型-------------

使用tensorflow构建一个单层的手写数字识别上面跑通的代码属于这种,单层感知机

tensorflow实现CNN识别手写数字

My Machine Learn(四):mnist数字识别神经网络的优化(c++版本)使用BP神经网络,能搞清楚为什么网络输入是784元素

mnist数据集的每一张图片的分辨率是28x28, 也就是说输入层会有784个数据节点,就像一个有784元素的数组

【注】也就是MNIST数据集中的每张图片有784个像素,如果用Mat类表示,就是一个784维数组

-------------如何调用训练好的模型--------------------

机器学习Tensorflow基于MNIST数据集识别自己的手写数字(读取和测试自己的模型)

使用Tensorflow和MNIST识别自己手写的数字 这个介绍了一些细节

得到一个手写数字的图片之后。下面我们就要对它进行预处理,缩小它的大小为28*28像素,并转变为灰度图,进行二值化处理。我使用的是Opencv对图像进行处理,也可以使用MATLAB等进行预处理。

完成预处理程序后,我们得到了这样的图片:

这就是28*28的二值化后的图片,这样的格式和我们MNIST数据集中的图片格式相同。只有这样,我们才能将图片输入到网络中进行识别。

【注】调用保存的网络用到 saver.restore()函数。大概是新建一个相同的网络模型,用saver.restore()函数将训练完保存的每一层参数导入我们新建的这个网络。

评论又说:

测试的时候不必再定义一次模型,直接采用训练完毕所保存的即可^_^

没弄清楚

TensorFlow手把手入门之 — TensorFlow保存还原模型的正确方式,Saver的save和restore方法,亲测可用

---------------------用下面这个代码把调用跑通了---------------------------------------

机器学习Tensorflow基于MNIST数据集识别自己的手写数字(读取和测试自己的模型)

其中图像预处理还是用的OpenCV(C++) 使用Tensorflow和MNIST识别自己手写的数字

训练代码

from tensorflow.examples.tutorials.mnist import input_dataimport tensorflow as tfmnist = input_data.read_data_sets("D:\\MNIST_DATABASE", one_hot=True) #MNIST数据集所在路径x = tf.placeholder(tf.float32, [None, 784])y_ = tf.placeholder(tf.float32, [None, 10])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"))saver = tf.train.Saver() #定义saverwith tf.Session() as sess:sess.run(tf.global_variables_initializer())for i in range(2000):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.save(sess, 'D:\\Python_Test\\SAVE\\model.ckpt') #模型储存位置print('test accuracy %g' % accuracy.eval(feed_dict={x: mnist.test.images, y_: mnist.test.labels, keep_prob: 1.0}))

调用代码

from PIL import Image, ImageFilterimport tensorflow as tfimport matplotlib.pyplot as pltdef imageprepare(): im = Image.open('D:\\Image\\temp6.png') #读取的图片所在路径,注意是28*28像素plt.imshow(im) #显示需要识别的图片plt.show()im = im.convert('L')tv = list(im.getdata()) tva = [(255-x)*1.0/255.0 for x in tv] return tvaresult=imageprepare()x = tf.placeholder(tf.float32, [None, 784])y_ = tf.placeholder(tf.float32, [None, 10])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"))saver = tf.train.Saver()with tf.Session() as sess:sess.run(tf.global_variables_initializer())saver.restore(sess, "D:\\SAVE\\model.ckpt") #使用模型,参数和之前的代码保持一致prediction=tf.argmax(y_conv,1)predint=prediction.eval(feed_dict={x: [result],keep_prob: 1.0}, session=sess)print('识别结果:')print(predint[0])

运行测试代码有时候会报错,网络参数在ckpt里找不到啊之类的,退出python环境重新打开又好了。

搞不懂啊,同样的程序,有时候加载模型没问题,有时候又加载不出来。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。