【Python機器學習及實踐】實戰篇:MNIST手寫體數字圖片識別

Python機器學習及實踐——實戰篇:MNIST手寫體數字圖片識別

作爲kaggle上入門級別比賽,手寫體數字識別是最基本的一個。這裏所用的數據爲MNIST提供的0-9的手寫體數字,kaggle上提供了csv格式的數據文件,可以直接讀取。這裏推薦使用readr包的read_csv函數讀取,能夠進一步提升文件讀取速度。

數據簡介:訓練集樣本個數爲42000,測試集樣本個數爲28000,第一列爲label爲標籤,第2到第785列爲黑白圖片每個像素點的灰度值(28*28).

模型搭建:採用多種基於skflow工具包的模型完成大規模手寫體數字圖片識別的任務。這些模型包括:線性迴歸器,全連接幷包含三個隱層的深度神經網絡(DNN)以及一個較爲複雜但是性能強大的卷積神經網絡(CNN)。

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
@File  : MNIST.py
@Author: Xinzhe.Pang
@Date  : 2019/7/28 0:27
@Desc  : 
"""
import pandas as pd

train = pd.read_csv('./train.csv')
# 查看訓練樣本信息
print(train.shape)

# 使用pandas從本地讀取的MNIST手寫體數字測試圖片集
test = pd.read_csv('./test.csv')
print(test.shape)

# 將訓練集中的數據特徵與對應標記分離
y_train = train['label']
X_train = train.drop('label', 1)

# 準備測試特徵
X_test = test

# 分別導入tensorflow和skflow
import tensorflow as tf
import skflow

# 使用skflow中已經封裝好的基於tensorflow搭建的線性分類器TensorflowFlowLinearClassifier進行學習預測
classifier = skflow.TensorFlowLinearClassifier(n_classes=10, batch_size=100, steps=1000, learning_rate=0.01)
classifier.fit(X_train, y_train)

linear_y_pred = classifier.predict(X_test)

linear_submission = pd.DataFrame({'ImageId': range(1, 28001), 'Label': linear_y_pred})
linear_submission.to_csv('./linear_submission.csv', index=False)

# 使用基於tensorflow搭建的全連接深度神經網絡TensorflowDNNClassifier進行學習預測
classifier = skflow.TensorFlowDNNClassifier(hidden_units=[200, 50, 10], n_classes=10, steps=5000, learning_rate=0.01,
                                            batch_size=50)
classifier.fit(X_train, y_train)

dnn_y_pred = classifier.predict(X_test)
dnn_submission = pd.DataFrame({'ImageId': range(1, 28001), 'Label': dnn_y_pred})
dnn_submission.to_csv('./dnn_submission.csv', index=False)

# 使用Tensorflow中的算子自行搭建更爲複雜的卷積神經網絡,並使用skflow的程序接口從事MNIST數據的學習與預測
def max_pool_2x2(tensor_in):
    return tf.nn.max_pool(tensor_in, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')


def conv_model(X, y):
    X = tf.reshape(X, [-1, 28, 28, 1])
    with tf.variable_scope('conv_layer1'):
        h_conv1 = skflow.ops.conv2d(X, n_filters=32, filter_shape=[5, 5], bias=True, activation=tf.nn.relu)
        h_pool1 = max_pool_2x2(h_conv1)
    with tf.variable_scope('conv_layer2'):
        h_conv2 = skflow.ops.conv2d(h_pool1, n_filters=64, filter_shape=[5, 5], bias=True, activation=tf.nn.relu)
        h_pool2 = max_pool_2x2(h_conv2)
        h_pool2_flat = tf.reshape(h_pool2, [-1, 7 * 7 * 64])
    h_fcl = skflow.ops.dnn(h_pool2_flat, [1024], activation=tf.nn.relu, keep_prob=0.5)
    return skflow.models.logistic_regression(h_fcl, y)


classifier = skflow.TensorFlowEstimator(model_fn=conv_model, n_classes=10, batch_size=100, steps=20000,
                                        learning_rate=0.001)
classifier.fit(X_train, y_train)

# 這裏不要直接將所有測試樣本交給模型進行預測,因爲Tensorflow會同時對所有測試樣本進行矩陣運算,一次對28000個測試圖片進行計算
# 會消耗大量的內存和計算資源,這裏採用的是逐批次地對樣本進行預測,最後拼接全部預測結果
conv_y_pred = []
import numpy as np

for i in np.arange(100, 28001, 100):
    conv_y_pred = np.append(conv_y_pred, classifier.predict(X_test[i - 100:i]))
conv_submission = pd.DataFrame({'ImageId': range(1, 28001), 'Label': np.int32(conv_y_pred)})
conv_submission.to_csv('./conv_submission.csv', index=False)

 

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