更新:
2018.11.9:
我把代码,模型,测试图片放上来了,可以下载了,记得自行配置相关库
下载地址:
https://download.youkuaiyun.com/download/qq_38269418/10774623
另外,有个网址很有意思,以前忘记放上来了,可以帮你很好理解神经网络的模型!
http://scs.ryerson.ca/~aharley/vis/conv/
2018.12.5:
上面那个网址好像现在需要挂VPN了,不然打不开
以下为原博:
废话不多说,先上效果图
整体来看,效果是非常不错的,模型的训练,参照官方代码mnist_deep.py,准确率是高达99.2%
那么,我是怎么实现的呢?
一.读懂卷积神经网络代码(至少得把程序跑通)
首先参照Tensorflow中文社区教程传送门:
http://www.tensorfly.cn/tfdoc/tutorials/mnist_pros.html
能在自己的环境中成功运行代码,具体代码的实现我就不在这里具体赘述了,因为关于代码的文章太多了,百度都能一大堆。博主是参照了Tensorflow中社区教程如图:
(注意一点:关于教程的print函数)
在博主用的Python3.6版本中,print已经成为了一个函数,而在Python2.7当中print不是一个函数,这里博主是需要加上括号。
所以根据自己所用的版本,更改这里,例如
print("Hello") python3.6
print "Hello" python2.7
如果你按照教程,将代码跑通之后,可以进行下一步了。那就是将模型保存,只需调用一个简单的函数,以下就是博主根据教程敲得完整代码:
from tensorflow.examples.tutorials.mnist import input_data
import tensorflow as tf
mnist = input_data.read_data_sets('F:/DEEPLEARN/Anaconda/Lib/site-packages/tensorflow/examples/tutorials/mnist/MNIST_data', 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