加载mnist数据集
1 | from keras.datasets import mnist |
(60000, 28, 28) <class 'numpy.ndarray'>
(60000,) <class 'numpy.ndarray'>
(10000, 28, 28) <class 'numpy.ndarray'>
(10000,) <class 'numpy.ndarray'>
数据处理:规范化
channels_last
对应的输入:(batch,height,width,channels)
channels_first
对应的输入:(batch,channels,height,width)
默认channels_last
,修改:~/.keras/keras.json
1 | from keras import backend as K |
(60000, 28, 28, 1) <class 'numpy.ndarray'>
(10000, 28, 28, 1) <class 'numpy.ndarray'>
1 | # 将数据转换为float32,为了进行归一化,不然/255得到全部是0 |
统计训练数据中个标签数量
1 | import numpy as np |
[0 1 2 3 4 5 6 7 8 9] [5923 6742 5958 6131 5842 5421 5918 6265 5851 5949]
1 | fig = plt.figure(figsize=(8, 5)) |
对标签进行one-hot编码
1 | from keras.utils import np_utils |
(60000, 10)
1 | print(y_train[0]) |
5
[0. 0. 0. 0. 0. 1. 0. 0. 0. 0.]
使用Keras sequential model 定义MNIST CNN网络
1 | from keras.models import Sequential |
查看 MNIST CNN 模型网络结构
1 | model.summary() |
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d_1 (Conv2D) (None, 26, 26, 32) 320
_________________________________________________________________
conv2d_2 (Conv2D) (None, 24, 24, 64) 18496
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 12, 12, 64) 0
_________________________________________________________________
dropout_1 (Dropout) (None, 12, 12, 64) 0
_________________________________________________________________
flatten_1 (Flatten) (None, 9216) 0
_________________________________________________________________
dense_1 (Dense) (None, 128) 1179776
_________________________________________________________________
dropout_2 (Dropout) (None, 128) 0
_________________________________________________________________
dense_2 (Dense) (None, 10) 1290
=================================================================
Total params: 1,199,882
Trainable params: 1,199,882
Non-trainable params: 0
_________________________________________________________________
1 | for layer in model.layers: |
[None, 26, 26, 32]
[None, 24, 24, 64]
[None, 12, 12, 64]
[None, 12, 12, 64]
[None, None]
[None, 128]
[None, 128]
[None, 10]
编译模型
1 | model.compile(loss='categorical_crossentropy',optimizer='adam', metrics=['accuracy']) |
训练模型,并将指标保存到history中
1 | history = model.fit(X_train, Y_train, batch_size=128, epochs=5,verbose=2, validation_data=(X_test, Y_test)) |
Train on 60000 samples, validate on 10000 samples
Epoch 1/5
- 131s - loss: 0.2330 - acc: 0.9290 - val_loss: 0.0540 - val_acc: 0.9817
Epoch 2/5
- 146s - loss: 0.0853 - acc: 0.9747 - val_loss: 0.0372 - val_acc: 0.9882
Epoch 3/5
- 136s - loss: 0.0605 - acc: 0.9812 - val_loss: 0.0315 - val_acc: 0.9898
Epoch 4/5
- 129s - loss: 0.0514 - acc: 0.9843 - val_loss: 0.0283 - val_acc: 0.9913
Epoch 5/5
- 130s - loss: 0.0416 - acc: 0.9873 - val_loss: 0.0272 - val_acc: 0.9911
可视化指标
1 | fig = plt.figure() |
保存模型
1 | import os |
Saved trained model at .\model\keras_mnist.h5
加载模型
1 | from keras.models import load_model |
统计模型在测试集上的分类结果
1 | loss_and_metrics = mnist_model.evaluate(X_test, Y_test, verbose=2) |
Test Loss: 0.027159390095694836
Test Accuracy: 99.11%
Classified correctly count: 9911
Classified incorrectly count: 89