DL之CNN:利用卷积神经网络算法(2→2,基于Keras的API-Functional)利用MNIST(手写数字图片识别)数据集实现多分类预测
目录
输出结果
设计思路
核心代码
下边两张图对应查看,可知,数字0有965个是被准确识别到!
1.10.0
Size of:
- Training-set: 55000
- Validation-set: 5000
- Test-set: 10000
Epoch 1/1
128/55000 [..............................] - ETA: 14:24 - loss: 2.3439 - acc: 0.0938
256/55000 [..............................] - ETA: 14:05 - loss: 2.2695 - acc: 0.1016
384/55000 [..............................] - ETA: 13:20 - loss: 2.2176 - acc: 0.1302
512/55000 [..............................] - ETA: 13:30 - loss: 2.1608 - acc: 0.2109
640/55000 [..............................] - ETA: 13:29 - loss: 2.0849 - acc: 0.2500
768/55000 [..............................] - ETA: 13:23 - loss: 2.0309 - acc: 0.2734
896/55000 [..............................] - ETA: 13:30 - loss: 1.9793 - acc: 0.2946
1024/55000 [..............................] - ETA: 13:23 - loss: 1.9105 - acc: 0.3369
1152/55000 [..............................] - ETA: 13:22 - loss: 1.8257 - acc: 0.3776
……
53760/55000 [============================>.] - ETA: 18s - loss: 0.2106 - acc: 0.9329
53888/55000 [============================>.] - ETA: 16s - loss: 0.2103 - acc: 0.9330
54016/55000 [============================>.] - ETA: 14s - loss: 0.2100 - acc: 0.9331
54144/55000 [============================>.] - ETA: 13s - loss: 0.2096 - acc: 0.9333
54272/55000 [============================>.] - ETA: 11s - loss: 0.2092 - acc: 0.9334
54400/55000 [============================>.] - ETA: 9s - loss: 0.2089 - acc: 0.9335
54528/55000 [============================>.] - ETA: 7s - loss: 0.2086 - acc: 0.9336
54656/55000 [============================>.] - ETA: 5s - loss: 0.2082 - acc: 0.9337
54784/55000 [============================>.] - ETA: 3s - loss: 0.2083 - acc: 0.9337
54912/55000 [============================>.] - ETA: 1s - loss: 0.2082 - acc: 0.9337
55000/55000 [==============================] - 837s 15ms/step - loss: 0.2080 - acc: 0.9338
32/10000 [..............................] - ETA: 21s
160/10000 [..............................] - ETA: 8s
288/10000 [..............................] - ETA: 6s
448/10000 [>.............................] - ETA: 5s
576/10000 [>.............................] - ETA: 5s
736/10000 [=>............................] - ETA: 4s
864/10000 [=>............................] - ETA: 4s
1024/10000 [==>...........................] - ETA: 4s
1152/10000 [==>...........................] - ETA: 4s
1312/10000 [==>...........................] - ETA: 4s
1440/10000 [===>..........................] - ETA: 4s
1600/10000 [===>..........................] - ETA: 3s
1728/10000 [====>.........................] - ETA: 3s
……
3008/10000 [========>.....................] - ETA: 3s
3168/10000 [========>.....................] - ETA: 3s
3296/10000 [========>.....................] - ETA: 3s
3456/10000 [=========>....................] - ETA: 2s
……
5248/10000 [==============>...............] - ETA: 2s
5376/10000 [===============>..............] - ETA: 2s
5536/10000 [===============>..............] - ETA: 2s
5664/10000 [===============>..............] - ETA: 1s
5792/10000 [================>.............] - ETA: 1s
……
7360/10000 [=====================>........] - ETA: 1s
7488/10000 [=====================>........] - ETA: 1s
7648/10000 [=====================>........] - ETA: 1s
7776/10000 [======================>.......] - ETA: 1s
7936/10000 [======================>.......] - ETA: 0s
8064/10000 [=======================>......] - ETA: 0s
8224/10000 [=======================>......] - ETA: 0s
……
9760/10000 [============================>.] - ETA: 0s
9920/10000 [============================>.] - ETA: 0s
10000/10000 [==============================] - 4s 449us/step
loss 0.05686537345089018
acc 0.982
acc: 98.20%
[[ 965 0 4 0 0 0 4 1 2 4]
[ 0 1128 3 0 0 0 0 1 3 0]
[ 0 0 1028 0 0 0 0 1 3 0]
[ 0 0 10 991 0 2 0 2 3 2]
[ 0 0 3 0 967 0 1 1 1 9]
[ 2 0 1 7 1 863 5 1 4 8]
[ 2 3 0 0 3 2 946 0 2 0]
[ 0 1 17 1 1 0 0 987 2 19]
[ 2 0 9 2 0 1 0 1 955 4]
[ 1 4 3 2 8 0 0 0 1 990]]
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_1 (InputLayer) (None, 784) 0
_________________________________________________________________
reshape (Reshape) (None, 28, 28, 1) 0
_________________________________________________________________
layer_conv1 (Conv2D) (None, 28, 28, 16) 416
_________________________________________________________________
max_pooling2d (MaxPooling2D) (None, 14, 14, 16) 0
_________________________________________________________________
layer_conv2 (Conv2D) (None, 14, 14, 36) 14436
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 7, 7, 36) 0
_________________________________________________________________
flatten (Flatten) (None, 1764) 0
_________________________________________________________________
dense (Dense) (None, 128) 225920
_________________________________________________________________
dense_1 (Dense) (None, 10) 1290
=================================================================
Total params: 242,062
Trainable params: 242,062
Non-trainable params: 0
_________________________________________________________________
(5, 5, 1, 16)
(1, 28, 28, 16)
后期更新……
path_model = 'Functional_model.keras'
from tensorflow.python.keras.models import load_model
model2_1 = load_model(path_model)
model_weights_path = 'Functional_model_weights.keras'
model2_1.save_weights(model_weights_path )
model2_1.load_weights(model_weights_path, by_name=True )
model2_1.load_weights(model_weights_path)
result = model.evaluate(x=data.x_test,
y=data.y_test)
for name, value in zip(model.metrics_names, result):
print(name, value)
print("{0}: {1:.2%}".format(model.metrics_names[1], result[1]))
y_pred = model.predict(x=data.x_test)
cls_pred = np.argmax(y_pred, axis=1)
plot_example_errors(cls_pred)
plot_confusion_matrix(cls_pred)
images = data.x_test[0:9]
cls_true = data.y_test_cls[0:9]
y_pred = model.predict(x=images)
cls_pred = np.argmax(y_pred, axis=1)
title = 'MNIST(Sequential Model): plot predicted example, resl VS predict'
plot_images(title, images=images,
cls_true=cls_true,
cls_pred=cls_pred)