Mnist手写数字识别 Tensorflow
任务目标
了解mnist数据集
搭建和测试模型
编辑环境
操作系统:Win10
python版本:3.6
集成开发环境:pycharm
tensorflow版本:1.*
了解mnist数据集
mnist数据集:mnist数据集下载地址
??MNIST 数据集来自美国国家标准与技术研究所, National Institute of Standards and Technology (NIST). 训练集 (training set) 由来自 250 个不同人手写的数字构成, 其中 50% 是高中学生, 50% 来自人口普查局 (the Census Bureau) 的工作人员. 测试集(test set) 也是同样比例的手写数字数据.
??图片是以字节的形式进行存储, 我们需要把它们读取到 NumPy array 中, 以便训练和测试算法。
读取mnist数据集
mnist = input_data.read_data_sets("mnist_data", one_hot=True)
搭建和测试模型
代码
def getMnistModel(savemodel,is_train):
"""
:param savemodel: 模型保存路径
:param is_train: true为训练,false为测试模型
:return:None
"""
mnist = input_data.read_data_sets("mnist_data", one_hot=True)
with tf.variable_scope("data"):
x = tf.placeholder(tf.float32,shape=[None,784]) # 784=28*28*1 宽长为28,单通道图片
y_true = tf.placeholder(tf.int32,shape=[None,10]) # 10个类别
with tf.variable_scope("conv1"):
w_conv1 = tf.Variable(tf.random_normal([10,10,1,32])) # 10*10的卷积核 1个通道的输入图像 32个不同的卷积核,得到32个特征图
b_conv1 = tf.Variable(tf.constant(0.0,shape=[32]))
x_reshape = tf.reshape(x,[-1,28,28,1]) # n张 28*28 的单通道图片
conv1 = tf.nn.relu(tf.nn.conv2d(x_reshape,w_conv1,strides=[1,1,1,1],padding="SAME")+b_conv1) #[1, 1, 1, 1] 中间2个1,卷积每次滑动的步长 padding=‘SAME‘ 边缘自动补充
pool1 = tf.nn.max_pool(conv1,ksize=[1,2,2,1],strides=[1,2,2,1],padding="SAME") # 池化窗口为[1,2,2,1] 中间2个2,池化窗口每次滑动的步长 padding="SAME" 考虑边界,如果不够用 用0填充
with tf.variable_scope("conv2"):
w_conv2 = tf.Variable(tf.random_normal([10,10,32,64]))
b_conv2 = tf.Variable(tf.constant(0.0,shape=[64]))