本文介绍了利用keras做mnist数据集的手写数字识别。
参考网址 http://www.cnblogs.com/lc1217/p/7132364.html
mnist数据集中的图片为28*28的单通道黑白图片。
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Apr 26 11:07:39 2019
@author: allenlz
"""
import numpy as np
from keras.datasets import mnist
from keras.models import Sequential, Model
from keras.layers import Convolution2D, Dense, Activation, Flatten, MaxPool2D
from keras.utils import np_utils
from keras.optimizers import Adam
# 1.load mnist data
(x_train, y_train), (x_test, y_test) = mnist.load_data()
# 2.reshape the 2-D to 1-D, to a 1-D vector
x_train = x_train.reshape(len(x_train), 28, 28, 1)
x_test = x_test.reshape(len(x_test), 28, 28, 1)
# 3.reshape type to float32
x_train = x_train.astype('float32')
y_test = y_test.astype('float32')
# 4.normalize data to [0, 1]
x_train = x_train/255
x_test = x_test/255
# 5.one-hot encoding
nb_class = 10
y_train = np_utils.to_categorical(y_train, nb_class)
y_test = np_utils.to_categorical(y_test, nb_class)
# 6.build the CNN model
model = Sequential()
# 1st cnn layer
model.add(Convolution2D(
filters=64, # 64 kernels
kernel_size=[5, 5], # kernel size = 5 * 5
padding='same', # padding
input_shape=(28,28,1),
kernel_initializer='he_normal',
name = 'cnn_l1'
))
model.add(Activation('relu'))
model.add(MaxPool2D(
pool_size=(2, 2), # pooling size
strides=(2, 2), # strides
padding='same',
))
# 2nd cnn layer
model.add(Convolution2D(
filters=32, # 64 kernels
kernel_size=[5, 5], # kernel size = 5 * 5
padding='same', # padding
name = 'cnn_l2',
kernel_initializer='he_normal'
))
model.add(Activation('relu'))
model.add(MaxPool2D(
pool_size=(2, 2), # pooling size
strides=(2, 2), # strides
padding='same', # padding
))
# fully connected layer
model.add(Flatten()) # flat
model.add(Dense(1024))
model.add(Activation('relu'))
model.add(Dense(10))
model.add(Activation('softmax'))
# output the model structure
model.summary()
# optimize way and learning rate
adam = Adam(lr=1e-4)
# define optimizer, loss and metrics
model.compile(optimizer=adam,
loss='categorical_crossentropy',
metrics=['accuracy'])
# train the model
nb_epoch = 10
batchsize = 64
model.fit(x = x_train, # original data
y = y_train, # labels of original data
epochs = nb_epoch,
batch_size = batchsize,
validation_data = (x_test, y_test))
# evaluate the trained model in training set
score = model.evaluate(x_train, y_train, verbose=0)
print('Train loss:', score[0])
print('Train accuracy:', score[1])
# evaluate the trained model in test set
score = model.evaluate(x_test, y_test, verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])
Layer (type) Output Shape Param #
=================================================================
cnn_l1 (Conv2D) (None, 28, 28, 64) 1664
_________________________________________________________________
activation_3 (Activation) (None, 28, 28, 64) 0
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 (None, 14, 14, 64) 0
_________________________________________________________________
cnn_l2 (Conv2D) (None, 14, 14, 32) 51232
_________________________________________________________________
activation_4 (Activation) (None, 14, 14, 32) 0
_________________________________________________________________
max_pooling2d_3 (MaxPooling2 (None, 7, 7, 32) 0
_________________________________________________________________
flatten_1 (Flatten) (None, 1568) 0
_________________________________________________________________
dense_1 (Dense) (None, 1024) 1606656
_________________________________________________________________
activation_5 (Activation) (None, 1024) 0
_________________________________________________________________
dense_2 (Dense) (None, 10) 10250
_________________________________________________________________
activation_6 (Activation) (None, 10) 0
=================================================================
Total params: 1,669,802
Trainable params: 1,669,802
Non-trainable params: 0
Train on 60000 samples, validate on 10000 samples
Epoch 1/10
60000/60000 [==============================] - 140s 2ms/step - loss: 0.2011 - acc: 0.9418 - val_loss: 0.0696 - val_acc: 0.9784
Epoch 2/10
60000/60000 [==============================] - 139s 2ms/step - loss: 0.0596 - acc: 0.9818 - val_loss: 0.0418 - val_acc: 0.9868
Epoch 3/10
60000/60000 [==============================] - 139s 2ms/step - loss: 0.0406 - acc: 0.9875 - val_loss: 0.0325 - val_acc: 0.9885
Epoch 4/10
60000/60000 [==============================] - 138s 2ms/step - loss: 0.0314 - acc: 0.9899 - val_loss: 0.0378 - val_acc: 0.9869
Epoch 5/10
60000/60000 [==============================] - 138s 2ms/step - loss: 0.0242 - acc: 0.9925 - val_loss: 0.0301 - val_acc: 0.9893
Epoch 6/10
60000/60000 [==============================] - 138s 2ms/step - loss: 0.0192 - acc: 0.9942 - val_loss: 0.0261 - val_acc: 0.9913
Epoch 7/10
60000/60000 [==============================] - 138s 2ms/step - loss: 0.0150 - acc: 0.9954 - val_loss: 0.0280 - val_acc: 0.9904
Epoch 8/10
60000/60000 [==============================] - 138s 2ms/step - loss: 0.0134 - acc: 0.9957 - val_loss: 0.0291 - val_acc: 0.9905
Epoch 9/10
60000/60000 [==============================] - 138s 2ms/step - loss: 0.0097 - acc: 0.9971 - val_loss: 0.0238 - val_acc: 0.9920
Epoch 10/10
60000/60000 [==============================] - 138s 2ms/step - loss: 0.0085 - acc: 0.9977 - val_loss: 0.0254 - val_acc: 0.9912
Train loss: 0.006155262773358845
Train accuracy: 0.9984666666666666
Test loss: 0.025420136153507338
Test accuracy: 0.9912