图像处理之卷积神经网络手写体识别

from tensorflow.examples.tutorials.mnist import input_data mnist = input_data.read_data_sets ( ‘MNIST_data/’, one_hot=True) 这两行代码课以自动下载 mnist数据集。这个例子是将tf 中examples 目录下 MINST tutorial 下载到当前工作目录,但是如果你tf 没有tutorial 就会提示 No module…这时可以自行下载tutorial 放在你所在环境的lib 下 tensorflowexamples,打开后就知道怎么放置下载的tutorials了。也可以用keras 自动下载数据集 import tensorflow as tf tf.version mint=tf.keras.datasets.mnist (x_,y_),(x_1,y_1)=mint.load_data() import matplotlib.pyplot as plt plt.imshow(x_[0], cmap=“binary”) plt.show() 这代码没有装keras也能运行成功,数据集下载到C盘你的用户名下.keras下的datasets. 不过下载的扩展名是npz,复制到工作目录无法读入,可以查看https://zhuanlan.zhihu.com/p/163203298 最终我还是把下载tuorial 放在examples比较方便。https://download..net/download/buaaweibin/40844865 这时可以运行以下代码了 from tensorflow.examples.tutorials.mnist import input_data mnist = input_data.read_data_sets(“MNIST_data”, one_hot=True) print ( ’ 输入数据:’, mnist.train.images) print ( ’ 输入数据打shape :’, mnist.train.images.shape) import pylab im = mnist.train.images[1] im = im.reshape(-1 ,28) pylab.imshow(im) pylab.show() 下一步要构建神经网络了, https://github.com/zhuangchen-nlp/mnist-tf 这里边两个py 一个是传统的,一个是卷积神经网络。

因为大部分tf的例子是1.1时代的,所以前面还要有个兼容性设置。 import tensorflow.compat.v1 as tf tf.disable_v2_behavior()

这些设置好后,采用什么程序都不会有问题了 这里有个比较全面的程序,还涉及到最后的手写识别过程,可以参考https://www.cnblogs.com/XDU-Lakers/p/10526748.html 基于keras的 https://zhuanlan.zhihu.com/p/158907160

经验分享 程序员 微信小程序 职场和发展