# MNIST手写数字识别的卷积神经网络版本 import tensorflow as tf import random import matplotlib.pyplot as plt from tensorflow.examples.tutorials.mnist import input_data import numpy as np tf.set_random_seed(777) #设置随机种子 # 1.获取数据集 mnist = input_data.read_data_sets("MNIST_data_bak", one_hot=True) # 参数 learning_rate = 0.001 # 学习率 training_epochs = 15 # 训练总周期 batch_size = 100 # 训练每批样本数 #2定义占位符 X = tf.placeholder(tf.float32, [None, 784]) X_img = tf.reshape(X, [-1, 28, 28, 1]) # 不确定样本数量,长,宽,通道 Y = tf.placeholder(tf.float32, [None, 10]) # 独热编码 # 3.第1层卷积,输入图片数据(?, 28, 28, 1) W1 = tf.Variable(tf.random_normal([3, 3, 1, 32])) #卷积核3x3,深度1,输出特征图32张 #tf.nn.conv2d 2d卷积(输入图像, 卷积核设置,步长, 0边填充 ) L1 = tf.nn.conv2d(X_img, W1, strides=[1, 1, 1, 1], padding='SAME') #卷积输出 (?, 28, 28, 32) L1 = tf.nn.relu(L1) #ksize2*2的池化盒 L1 = tf.nn.max_pool(L1, ksize=[1, 2, 2, 1],strides=[1, 2, 2, 1], padding='SAME') #池化输出 (?, 14, 14, 32) # 4.第2层卷积,输入图片数据(?, 14, 14, 32) W2 = tf.Variable(tf.random_normal([3, 3, 32, 64], stddev=0.01)) #卷积核3x3,输入通道32,输出通道64 L2 = tf.nn.conv2d(L1, W2, strides=[1, 1, 1, 1], padding='SAME') #卷积输出 (?, 14, 14, 64) L2 = tf.nn.relu(L2) L2 = tf.nn.max_pool(L2, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') #池化输出 (?, 7, 7, 64) #5.展开成全连接层 L2_flat = tf.reshape(L2, [-1, 7 * 7 * 64]) # 变成二维向量 (?, 3136) # 6.定义全连接参数 7x7x64 inputs -> 10 outputs 保证梯度下降趋势相同 W3 = tf.get_variable("W3", shape=[7 * 7 * 64, 10], initializer=tf.contrib.layers.xavier_initializer()) # W3 = tf.Variable(tf.random_normal([7 * 7 * 64, 10], stddev=0.01)) b = tf.Variable(tf.random_normal([10])) logits = tf.matmul(L2_flat, W3) + b #代价或损失函数 ''' 第一步是先对网络最后一层的输出做一个softmax 第二步是softmax的输出向量[Y1,Y2,Y3...]和样本的实际标签做一个交叉熵 ''' cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=Y)) optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost) # 优化器 # 7创建会话 sess = tf.Session() sess.run(tf.global_variables_initializer()) #全局变量初始化 # 8迭代训练 print('开始学习...') for epoch in range(training_epochs):#循环总次数 avg_cost = 0 total_batch = int(mnist.train.num_examples / batch_size) # 批次 550 for i in range(total_batch): #9从mnist中获取训练数据集,获取下100条数据,接收值 image,label batch_xs, batch_ys = mnist.train.next_batch(batch_size) feed_dict = {X: batch_xs, Y: batch_ys} c, _ = sess.run([cost, optimizer], feed_dict=feed_dict) #550次 c avg_cost += c / total_batch print('Epoch:', (epoch + 1), 'cost =', avg_cost) print('学习完成') # 10.测试模型检查准确率 correct_prediction = tf.equal(tf.argmax(logits, 1), tf.argmax(Y, 1)) accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) print('Accuracy:', sess.run(accuracy, feed_dict={X: mnist.test.images[:5000], Y: mnist.test.labels[:5000]})) # 在测试集中随机抽一个样本进行测试 r = random.randint(0, mnist.test.num_examples - 1) print("Label: ", sess.run(tf.argmax(mnist.test.labels[r:r + 1], 1))) print("Prediction: ", sess.run(tf.argmax(logits, 1), feed_dict={X: mnist.test.images[r:r + 1]})) # 手写生成一个24位彩色bmp图片(宽度28,高度28)进行测试 img = plt.imread('1.bmp') #img.shape=(28,28,3) gravity = np.array([1., 0., 0.]) greyimg = np.dot(255 - img, gravity)/255#greyimg.shape=(28,28) #255-img变背景颜色, np.dot取的某一通道 print("Prediction: ", sess.run(tf.argmax(logits, 1), feed_dict={X: greyimg.reshape([1, 784])})) plt.imshow(greyimg, cmap='Greys', interpolation='nearest') plt.show() ''' 开始学习... Epoch: 1 cost = 0.17211615171114167 Epoch: 2 cost = 0.053189885938032096 Epoch: 3 cost = 0.03904086626854473 Epoch: 4 cost = 0.030902175904166974 Epoch: 5 cost = 0.024272618824510244 Epoch: 6 cost = 0.020677360098872534 Epoch: 7 cost = 0.017665539223340933 Epoch: 8 cost = 0.014775432829125995 Epoch: 9 cost = 0.01196916008488518 Epoch: 10 cost = 0.01059760161803173 Epoch: 11 cost = 0.00843130687872789 Epoch: 12 cost = 0.0064097662605623025 Epoch: 13 cost = 0.0072243955252237054 Epoch: 14 cost = 0.005092451396864245 Epoch: 15 cost = 0.00760021077886367 学习完成 '''
手写体识别加卷积神经网络
最新推荐文章于 2023-03-11 21:37:57 发布