【Python】keras神經網路識別mnist

Dsp Tian發表於2018-09-12

上次用Matlab寫過一個識別Mnist的神經網路,地址在:https://www.cnblogs.com/tiandsp/p/9042908.html

這次又用Keras做了一個差不多的,畢竟,現在最流行的專案都是Python做的,我也跟一下潮流:)

資料是從本地解析好的影象和標籤載入的。

神經網路有兩個隱含層,都有512個節點。

import numpy as np
from keras.preprocessing import image
from keras.models import Sequential
from keras.layers.core import Dense, Dropout, Activation


# 從資料夾影象與標籤檔案載入資料
def create_x(filenum, file_dir):
    train_x = []
    for i in range(filenum):
        img = image.load_img(file_dir + str(i) + ".bmp", target_size=(28, 28))
        img = img.convert('L')
        x = image.img_to_array(img)
        train_x.append(x)
    train_x = np.array(train_x)
    train_x = train_x.astype('float32')
    train_x /= 255
    return train_x


def create_y(classes, filename):
    train_y = []
    file = open(filename, "r")
    for line in file.readlines():
        tmp = []
        for j in range(classes):
            if j == int(line):
                tmp.append(1)
            else:
                tmp.append(0)
        train_y.append(tmp)
    file.close()
    train_y = np.array(train_y).astype('float32')
    return train_y


classes = 10
X_train = create_x(55000, './train/')
X_test = create_x(10000, './test/')

X_train = X_train.reshape(X_train.shape[0], 784)
X_test = X_test.reshape(X_test.shape[0], 784)

Y_train = create_y(classes, 'train.txt')
Y_test = create_y(classes, 'test.txt')

# 從網路下載的資料集直接解析資料
'''
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
X_train, Y_train = mnist.train.images, mnist.train.labels
X_test, Y_test = mnist.test.images, mnist.test.labels
X_train = X_train.astype('float32')
X_test = X_test.astype('float32')
X_train = X_train.reshape(55000, 784)
X_test = X_test.reshape(10000, 784)
'''
model = Sequential()

model.add(Dense(512, input_shape=(784,)))
model.add(Activation('relu'))
model.add(Dropout(0.4))

model.add(Dense(512))
model.add(Activation('relu'))
model.add(Dropout(0.4))

model.add(Dense(10))
model.add(Activation('softmax'))
model.summary()

model.compile(loss='categorical_crossentropy', optimizer='rmsprop', metrics=['accuracy'])
history = model.fit(X_train, Y_train, batch_size=500, epochs=20, verbose=1, validation_data=(X_test, Y_test))
score = model.evaluate(X_test, Y_test, verbose=0)

test_result = model.predict(X_test)
result = np.argmax(test_result, axis = 1)

print(result)
print('Test score:', score[0])
print('Test accuracy:', score[1])

最終在測試集上識別率在98%左右。

相關測試資料可以在這裡下載到。

相關文章