Kaggle神經網絡實戰:CNN實現手寫數字辨識

簡要介紹

  • 本文是基於Kaggle入門項目Digit Recognizer的處理方案,在MINST數據集上訓練可以識別手寫數字的模型。項目鏈接
  • 代碼來自項目Kernels,使用tensorflow實現CNN網絡,完整圖文及代碼請參照Kernel原文

代碼實現

庫的導入和常量定義

import numpy as np
import pandas as pd

%matplotlib inline
import matplotlib.pyplot as plt
import matplotlib.cm as cm

import tensorflow as tf

LEARNING_RATE = 1e-4   #學習率
TRAINING_ITERATIONS = 2500  #訓練次數

DROPOUT = 0.5   #dropout防止過擬合
BATCH_SIZE = 50     #每個訓練批次大小

VALIDATION_SIZE = 2000

IMAGE_TO_DISPLAY = 10   #顯示第10張圖像

數據預處理

通過CSV文件導入數據,數據格式爲42000X785的矩陣,每行表示一條記錄,一共42000張圖片,每張圖片採用28X28像素表示,第一列爲延伸

data = pd.read_csv('../input/train.csv')

圖片處理

讀入的數據以DataFrame格式保存,其中第一列爲標籤(stretched array),對其去掉第一列並去索引

images = data.iloc[:,1:].values

images = images.astype(np.float)    #轉換爲浮點型

images = np.multiply(images,1.0/255.0)  #壓縮灰度值

初始化圖片相關屬性變量

image_size = images.shape[1] #圖片大小   

image_width = image_height = np.ceil(np.sqrt(image_size)).astype(np.uint8)
#圖片寬度和高度

定義圖像可視化函數

def display(img):

    #(784) => (28X28)
    one_image = img.reshape(image_width,image_height)

    #不顯示座標軸
    plt.axis('off')
    #arg2:默認黑白圖像
    plt.imshow(one_image,cmap=cm.binary)

#函數測試
display(images[IMAGE_TO_DISPLAY])

獲得了4200X784的圖像數組

標籤處理

這裏對原代碼做了修改

取data的第一列即標籤,展平,統計標籤種類數

labels_flat = data.iloc[:,0].values

labels_count = np.unique(labels_flat).shape[0]
#np.unique(array)保留參數數組中不同的值,返回兩個值
#1:不同的值組成的數組  2:這些值首次出現位置組成的數組

編寫函數,將標籤轉用獨熱碼(one-hot)表示

# 0 => [1 0 0 0 0 0 0 0 0 0]
# 1 => [0 1 0 0 0 0 0 0 0 0]
# ...
# 9 => [0 0 0 0 0 0 0 0 0 1]

def dense_to_one_hot(labels_dense, num_classes):
    #arg1:標籤數組   arg2:去重標籤數組
    num_labels = labels_dense.shape[0]
    #統計標籤數量
    index_offset = np.arange(num_labels) * num_classes
    #確定大小
    labels_one_hot = np.zeros((num_labels, num_classes))
    #初始化0矩陣(42000X10)
    labels_one_hot.flat[index_offset + labels_dense.ravel()] = 1
    #完成賦值
    return labels_one_hot

labels = dense_to_one_hot(labels_flat, labels_count)
labels = labels.astype(np.uint8)

獲得了42000X10的標籤數組

劃分訓練集與測試集

對圖像和標籤採用最簡單的方法劃分訓練集與測試集
前2000份爲測試集,後40000份爲訓練集

validation_images = images[:VALIDATION_SIZE]
validation_labels = labels[:VALIDATION_SIZE]

train_images = images[VALIDATION_SIZE:]
train_labels = labels[VALIDATION_SIZE:]

網絡架構

激勵函數使用ReLU,因爲其易於訓練,初始化爲極小的正數避免神經元死亡的現象發生。

變量定義

權重和偏置變量

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')
    #採用邊緣補0的方法做卷積,步長爲1

池化層

def max_pool_2x2(x):
    return tf.nn.max_pool(x,ksize=[1,2,2,1],strides=[1,2,2,1],padding='SAME')
    #2X2劃分大小,步長爲2

網絡的輸入和輸出

x = tf.placeholder('float',shape=[None,imagesize])
#784維向量
y = tf.placeholder('float',shape=[None,labels_count])
#10維向量

具體實現

網絡採用2組卷積-池化層
第一組卷積層使用32個5X5的filter提取特徵,池化層採用2X2的分割
28X28X1 => 28X28X32 =>14X14X32

#第一組卷積-池化層

W_conv1 = weight_variable([5, 5, 1, 32])
#[width,height,channels,features]
b_conv1 = bias_variable([32])

image = tf.reshape(x, [-1,image_width , image_height,1]
# (40000,784) => (40000,28,28,1)

h_conv1 = tf.nn.relu(conv2d(image, W_conv1) + b_conv1)
#print (h_conv1.get_shape()) # => (40000, 28, 28, 32)
h_pool1 = max_pool_2x2(h_conv1)
#print (h_pool1.get_shape()) # => (40000, 14, 14, 32)

第二組卷積層使用64個5X5的filter提取特徵,池化層採用2X2的分割
14X14X32 => 14X14X64 =>7X7X64

# second convolutional layer
W_conv2 = weight_variable([5, 5, 32, 64])
b_conv2 = bias_variable([64])

h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2)
#print (h_conv2.get_shape()) # => (40000, 14,14, 64)
h_pool2 = max_pool_2x2(h_conv2)
#print (h_pool2.get_shape()) # => (40000, 7, 7, 64)

全連接層部分
對第二級池化層的輸出做展平操作,與隱層神經元連接

W_fc1 = weight_variable([7 * 7 * 64, 1024])
b_fc1 = bias_variable([1024])
#1024個隱層神經元

# (40000, 7, 7, 64) => (40000, 3136)
h_pool2_flat = tf.reshape(h_pool2, [-1, 7*7*64])

h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)
#print (h_fc1.get_shape()) # => (40000, 1024)

dropout防止過擬合

# dropout
keep_prob = tf.placeholder('float')
h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)

隱層神經元連接輸出層神經元,這裏的激活函數使用softmax函數,輸出網絡判定最合適的分類

# 輸出層
W_fc2 = weight_variable([1024, labels_count])
b_fc2 = bias_variable([labels_count])

y = tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2)

訓練與測試

  • 損失函數採用交叉熵(cross-entropy)
  • 梯度下降優化法選用ADAM
# 損失函數
cross_entropy = -tf.reduce_sum(y_*tf.log(y))

# 優化函數
train_step = tf.train.AdamOptimizer(LEARNING_RATE).minimize(cross_entropy)

# 準確度評估函數
correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))

accuracy = tf.reduce_mean(tf.cast(correct_prediction, 'float'))

predict = tf.argmax(y,1)

設置訓練參數

epochs_completed = 0
index_in_epoch = 0
num_examples = train_images.shape[0]

# 按小批次(batch)處理數據
def next_batch(batch_size):

    global train_images
    global train_labels
    global index_in_epoch
    global epochs_completed

    start = index_in_epoch
    index_in_epoch += batch_size

    # when all trainig data have been already used, it is reorder randomly    
    if index_in_epoch > num_examples:
        # finished epoch
        epochs_completed += 1
        # shuffle the data
        perm = np.arange(num_examples)
        np.random.shuffle(perm)
        train_images = train_images[perm]
        train_labels = train_labels[perm]
        # start next epoch
        start = 0
        index_in_epoch = batch_size
        assert batch_size <= num_examples
    end = index_in_epoch
    return train_images[start:end], train_labels[start:end]

# start TensorFlow session
init = tf.initialize_all_variables()
sess = tf.InteractiveSession()

sess.run(init)

# visualisation variables
train_accuracies = []
validation_accuracies = []
x_range = []

display_step=1

在訓練過程中追蹤準確率

for i in range(TRAINING_ITERATIONS):

    #get new batch
    batch_xs, batch_ys = next_batch(BATCH_SIZE)        

    # check progress on every 1st,2nd,...,10th,20th,...,100th... step
    if i%display_step == 0 or (i+1) == TRAINING_ITERATIONS:

        train_accuracy = accuracy.eval(feed_dict={x:batch_xs, 
                                                  y_: batch_ys, 
                                                  keep_prob: 1.0})       
        if(VALIDATION_SIZE):
            validation_accuracy = accuracy.eval(feed_dict={ x: validation_images[0:BATCH_SIZE], 
                                                            y_: validation_labels[0:BATCH_SIZE], 
                                                            keep_prob: 1.0})                                  
            print('training_accuracy / validation_accuracy => %.2f / %.2f for step %d'%(train_accuracy, validation_accuracy, i))

            validation_accuracies.append(validation_accuracy)

        else:
             print('training_accuracy => %.4f for step %d'%(train_accuracy, i))
        train_accuracies.append(train_accuracy)
        x_range.append(i)

        # increase display_step
        if i%(display_step*10) == 0 and i:
            display_step *= 10
    # train on batch
    sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys, keep_prob: DROPOUT})

在測試集上計算誤差,同時對兩個準確度做可視化

# check final accuracy on validation set  
if(VALIDATION_SIZE):
    validation_accuracy = accuracy.eval(feed_dict={x: validation_images, 
                                                   y_: validation_labels, 
                                                   keep_prob: 1.0})
    print('validation_accuracy => %.4f'%validation_accuracy)
    plt.plot(x_range, train_accuracies,'-b', label='Training')
    plt.plot(x_range, validation_accuracies,'-g', label='Validation')
    plt.legend(loc='lower right', frameon=False)
    plt.ylim(ymax = 1.1, ymin = 0.7)
    plt.ylabel('accuracy')
    plt.xlabel('step')
    plt.show()

可視化結果

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章