天天看點

tensorflow 處理mnist手寫體資料集(訓練+預測代碼)

一,tensorflow提供了自動下載下傳mnist資料集的接口,若下載下傳不了,請嘗試翻牆後再試,或者從其他網站下載下傳。

二,訓練代碼:

import tensorflow as tf
from  tensorflow.examples.tutorials.mnist import input_data
import numpy as np
import os

"""------------------加載資料---------------------"""
# 載入資料
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True) #自動下載下傳mnist到MNIST_data/目錄下
trX, trY, teX, teY = mnist.train.images, mnist.train.labels, mnist.test.images, mnist.test.labels
# 改變資料格式,為了能夠輸入卷積層
trX = trX.reshape(-1, 28, 28, 1)  # -1表示自适應,與batchsize相等,1表示單通道
teX = teX.reshape(-1, 28, 28, 1)

"""------------------構模組化型---------------------"""
# 定義輸入輸出的資料容器
X = tf.placeholder("float", [None, 28, 28, 1])
Y = tf.placeholder("float", [None, 10])


# 定義和初始化權重、dropout參數
def init_weights(shape):
    return tf.Variable(tf.random_normal(shape, stddev=0.01))


w1 = init_weights([3, 3, 1, 32])        # 3X3的卷積核,獲得32個特征
w2 = init_weights([3, 3, 32, 64])       # 3X3的卷積核,獲得64個特征
w3 = init_weights([3, 3, 64, 128])      # 3X3的卷積核,獲得128個特征
w4 = init_weights([128 * 4 * 4, 625])   # 從卷積層到全連層
w_o = init_weights([625, 10])           # 從全連層到輸出層,輸出層的10表示是十分類問題,因為mnist是數字,識别0~9十個數字

p_keep_conv = tf.placeholder("float")
p_keep_hidden = tf.placeholder("float")

# 定義模型
def create_model(X, w1, w2, w3, w4, w_o, p_keep_conv, p_keep_hidden):
    # 第一組卷積層和pooling層
    conv1 = tf.nn.conv2d(X, w1, strides=[1, 1, 1, 1], padding='SAME')
    conv1_out = tf.nn.relu(conv1)
    pool1 = tf.nn.max_pool(conv1_out, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
    pool1_out = tf.nn.dropout(pool1, p_keep_conv)

    # 第二組卷積層和pooling層
    conv2 = tf.nn.conv2d(pool1_out, w2, strides=[1, 1, 1, 1], padding='SAME')
    conv2_out = tf.nn.relu(conv2)
    pool2 = tf.nn.max_pool(conv2_out, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
    pool2_out = tf.nn.dropout(pool2, p_keep_conv)

    # 第三組卷積層和pooling層
    conv3 = tf.nn.conv2d(pool2_out, w3, strides=[1, 1, 1, 1], padding='SAME')
    conv3_out = tf.nn.relu(conv3)
    pool3 = tf.nn.max_pool(conv3_out, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
    pool3 = tf.reshape(pool3, [-1, w4.get_shape().as_list()[0]])  # 轉化成一維的向量
    pool3_out = tf.nn.dropout(pool3, p_keep_conv)

    # 全連層
    fully_layer = tf.matmul(pool3_out, w4)
    fully_layer_out = tf.nn.relu(fully_layer)
    fully_layer_out = tf.nn.dropout(fully_layer_out, p_keep_hidden)

    # 輸出層
    out = tf.matmul(fully_layer_out, w_o)
    return out


model = create_model(X, w1, w2, w3, w4, w_o, p_keep_conv, p_keep_hidden) #輸出預測機率清單

# 定義代價函數、訓練方法、預測操作
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=model, labels=Y)) #損失函數為交叉熵
train_op = tf.train.RMSPropOptimizer(0.001, 0.9).minimize(cost) #訓練操作,以最小化cost為目标
predict_op = tf.argmax(model, 1,name="predict")  #預測操作

# 定義一個saver
saver=tf.train.Saver()

# 定義模型存儲路徑
ckpt_dir="./ckpt_dir"
if not os.path.exists(ckpt_dir):
    os.makedirs(ckpt_dir)

"""------------------訓練模型---------------------"""
train_batch_size = 128  # 訓練集的mini_batch_size=128
test_batch_size = 256   # 測試集中調用的batch_size=256
epoches = 5  # 疊代周期
'''以上屬于計算圖,隻是單純地定義操作。而下面的表示輸入資料後,調用計算圖中定義的操作'''
with tf.Session() as sess:
    """-------訓練模型--------"""
    # 初始化所有變量
    tf.global_variables_initializer().run()

    # 訓練操作
    for i in range(epoches):
        train_batch = zip(range(0, len(trX), train_batch_size),
                          range(train_batch_size, len(trX) + 1, train_batch_size))
        for start, end in train_batch:
            #執行訓練操作
            sess.run(train_op, feed_dict={X: trX[start:end], Y: trY[start:end],
                                          p_keep_conv: 0.8, p_keep_hidden: 0.5})
        # 每個周期用測試集中随機抽出test_batch_size個圖檔進行測試
        test_indices = np.arange(len(teX))  # 傳回一個array[0,1...len(teX)]
        np.random.shuffle(test_indices)     # 打亂這個array
        test_indices = test_indices[0:test_batch_size]

        # 擷取測試集test_batch_size章圖檔的的預測結果
        predict_result = sess.run(predict_op, feed_dict={X: teX[test_indices],
                                                         p_keep_conv: 1.0,
                                                         p_keep_hidden: 1.0})
        # 擷取真實的标簽值
        true_labels = np.argmax(teY[test_indices], axis=1)
        # 計算準确率
        accuracy = np.mean(true_labels == predict_result)
        print("epoch", i, ":", accuracy)

        # 儲存模型
        saver.save(sess,ckpt_dir+"/model.ckpt",global_step=i)


if __name__ =='__main__':
    pass
           

運作結果:

tensorflow 處理mnist手寫體資料集(訓練+預測代碼)

并會生成模型:

tensorflow 處理mnist手寫體資料集(訓練+預測代碼)

每個epoch生成一個模型,每個模型分成3個檔案,五個epoch生成五個模型

三,預測代碼:

import tensorflow as tf
import numpy as np
import cv2

"""------------------構模組化型---------------------"""
# 定義輸入輸出的資料容器
X = tf.placeholder("float", [None, 28, 28, 1])
Y = tf.placeholder("float", [None, 10])


# 定義和初始化權重、dropout參數
def init_weights(shape):
    return tf.Variable(tf.random_normal(shape, stddev=0.01))


w1 = init_weights([3, 3, 1, 32])        # 3X3的卷積核,獲得32個特征
w2 = init_weights([3, 3, 32, 64])       # 3X3的卷積核,獲得64個特征
w3 = init_weights([3, 3, 64, 128])      # 3X3的卷積核,獲得128個特征
w4 = init_weights([128 * 4 * 4, 625])   # 從卷積層到全連層
w_o = init_weights([625, 10])           # 從全連層到輸出層

p_keep_conv = tf.placeholder("float")
p_keep_hidden = tf.placeholder("float")
#
# # 定義模型
def create_model(X, w1, w2, w3, w4, w_o, p_keep_conv, p_keep_hidden):
    # 第一組卷積層和pooling層
    conv1 = tf.nn.conv2d(X, w1, strides=[1, 1, 1, 1], padding='SAME')
    conv1_out = tf.nn.relu(conv1)
    pool1 = tf.nn.max_pool(conv1_out, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
    pool1_out = tf.nn.dropout(pool1, p_keep_conv)

    # 第二組卷積層和pooling層
    conv2 = tf.nn.conv2d(pool1_out, w2, strides=[1, 1, 1, 1], padding='SAME')
    conv2_out = tf.nn.relu(conv2)
    pool2 = tf.nn.max_pool(conv2_out, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
    pool2_out = tf.nn.dropout(pool2, p_keep_conv)

    # 第三組卷積層和pooling層
    conv3 = tf.nn.conv2d(pool2_out, w3, strides=[1, 1, 1, 1], padding='SAME')
    conv3_out = tf.nn.relu(conv3)
    pool3 = tf.nn.max_pool(conv3_out, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
    pool3 = tf.reshape(pool3, [-1, w4.get_shape().as_list()[0]])  # 轉化成一維的向量
    pool3_out = tf.nn.dropout(pool3, p_keep_conv)

    # 全連層
    fully_layer = tf.matmul(pool3_out, w4)
    fully_layer_out = tf.nn.relu(fully_layer)
    fully_layer_out = tf.nn.dropout(fully_layer_out, p_keep_hidden)

    # 輸出層
    out = tf.matmul(fully_layer_out, w_o)
    return out
'''上面是與訓練代碼一樣的模型代碼'''

model = create_model(X, w1, w2, w3, w4, w_o, p_keep_conv, p_keep_hidden)
predict_op = tf.argmax(model, 1,name="predict") #預測操作

# 定義一個saver
saver=tf.train.Saver()

with tf.Session() as sess:
    """-------訓練模型--------"""
    # 初始化所有變量
    tf.global_variables_initializer().run()
    """-----加載模型,用導入的圖檔進行測試--------"""
    # 載入圖檔
    src = cv2.imread('./7.png')
    cv2.imshow("待測圖檔", src)

    # 将圖檔轉化為28*28的灰階圖
    src = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY)
    print(src.shape)
    dst = cv2.resize(src, (28, 28), interpolation=cv2.INTER_CUBIC)
    # # 将灰階圖歸一化
    picture = np.zeros((28, 28))
    for i in range(0, 28):
        for j in range(0, 28):
            picture[i][j] = (255 - dst[i][j])/255
    picture = picture.reshape(1, 28, 28, 1) #由于網絡輸入格式是4維的,是以輸入的圖檔也要轉成(batchsize,h,w,c)四維

    # 載入模型
    saver.restore(sess,"./ckpt_dir/model.ckpt-4")
    # 進行預測
    predict_result = sess.run(predict_op, feed_dict={X: picture,
                                                    p_keep_conv: 1.0,
                                                    p_keep_hidden: 1.0})
    print("你導入的圖檔是:",predict_result[0])
    cv2.waitKey(0)
           

預測結果:

先用畫圖工具畫出一張32x32的手寫體:

tensorflow 處理mnist手寫體資料集(訓練+預測代碼)

再輸入到預測代碼中運作:

tensorflow 處理mnist手寫體資料集(訓練+預測代碼)

繼續閱讀