已有注释,不解释,今天算是真正理解卷积层和池化层的意义和作用了。比MLP识别率要高。
一,代码
# 导入所需模块
import numpy as np
import pandas as pd
from keras.utils import np_utils
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPooling2D
np.random.seed(10)
# 查看单项数据
def plot_image(image):
fig = plt.gcf()
fig.set_size_inches(2, 2)
plt.imshow(image, cmap='binary')
plt.show()
# 查看多项数据
def plot_images_labels_prediction(images, labels, prediction, idx, num=10):
fig = plt.gcf()
fig.set_size_inches(12, 14)
if num > 25: num = 25
for i in range(0, num):
ax = plt.subplot(5, 5, 1+i)
ax.imshow(images[idx], cmap='binary')
title = "label=" + str(labels[idx])
if len(prediction) > 0:
title += ", predict=" + str(prediction[idx])
ax.set_title(title, fontsize=10)
ax.set_xticks([]); ax.set_yticks([])
idx += 1
plt.show()
# 读取MNIST数据
(x_Train, y_Train), \
(x_Test, y_Test) = mnist.load_data()
# 将features转换为4维矩阵
x_Train4D = x_Train.reshape(x_Train.shape[0], 28, 28, 1).astype('float32')
x_Test4D = x_Test.reshape(x_Test.shape[0], 28, 28, 1).astype('float32')
# 将features标准化
x_Train4D_normalize = x_Train4D / 255
x_Test4D_normalize = x_Test4D / 255
# label以One-Hot Encoding进行转换
y_Train_OneHot = np_utils.to_categorical(y_Train)
y_Test_OneHot = np_utils.to_categorical(y_Test)
# 建立Sequential模型
model = Sequential()
# 建立卷积层1
model.add(Conv2D(filters=16,
kernel_size=(5, 5),
padding='same',
input_shape=(28, 28, 1),
activation='relu'))
# 建立池化层1
model.add(MaxPooling2D(pool_size=(2, 2)))
# 建立卷积层2
model.add(Conv2D(filters=36,
kernel_size=(5, 5),
padding='same',
activation='relu'))
# 建立池化层2
model.add(MaxPooling2D(pool_size=(2, 2)))
# 加入Dropout功能避免过度拟合
model.add(Dropout(0.5))
# 建立平坦层
model.add(Flatten())
# 建立隐藏层
model.add(Dense(128, activation='relu'))
# 加入Dropout功能避免过度拟合
model.add(Dropout(0.5))
# 建立输出层
model.add(Dense(10, activation='softmax'))
# 查看模型摘要
print(model.summary())
# 定义训练方式
model.compile(loss='categorical_crossentropy',
optimizer='adam', metrics=['accuracy'])
# 开始训练
train_history = model.fit(x=x_Train4D_normalize,
y=y_Train_OneHot,
validation_split=0.2,
epochs=5,
batch_size=300,
verbose=2)
# 显示训练过程
def show_train_history(train_history, train, validation):
plt.plot(train_history.history[train])
plt.plot(train_history.history[validation])
plt.title('Train History')
plt.ylabel(train)
plt.xlabel('Epoch')
plt.legend(['train', 'validation'], loc='upper left')
plt.show()
# 画出准确率执行结果
show_train_history(train_history, 'acc', 'val_acc')
# 画出误差执行结果
show_train_history(train_history, 'loss', 'val_loss')
# 评估模型准确率
scores = model.evaluate(x_Test4D_normalize, y_Test_OneHot)
print('accuracy=', scores[1])
# 执行预测
prediction = model.predict_classes(x_Test4D_normalize)
# 显示10项预测结果
plot_images_labels_prediction(x_test_image, y_test_label, prediction, idx=340)
# 混淆矩阵显示
pd.crosstab(y_test, prediction, rownames=['label'], colnames=['predict'])
df = pd.DataFrame({'label': y_test, 'predict': prediction})
二,输出
<pre style="box-sizing: border-box; overflow: auto; font-family: monospace; font-size: inherit; display: block; padding: 1px 0px; margin: 0px; line-height: inherit; color: black; word-break: break-all; overflow-wrap: break-word; background-color: transparent; border: 0px; border-radius: 0px; white-space: pre-wrap; vertical-align: baseline;">_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d_8 (Conv2D) (None, 28, 28, 16) 416
_________________________________________________________________
max_pooling2d_7 (MaxPooling2 (None, 14, 14, 16) 0
_________________________________________________________________
conv2d_9 (Conv2D) (None, 14, 14, 36) 14436
_________________________________________________________________
max_pooling2d_8 (MaxPooling2 (None, 7, 7, 36) 0
_________________________________________________________________
dropout_11 (Dropout) (None, 7, 7, 36) 0
_________________________________________________________________
flatten_4 (Flatten) (None, 1764) 0
_________________________________________________________________
dense_30 (Dense) (None, 128) 225920
_________________________________________________________________
dropout_12 (Dropout) (None, 128) 0
_________________________________________________________________
dense_31 (Dense) (None, 10) 1290
=================================================================
Total params: 242,062
Trainable params: 242,062
Non-trainable params: 0
_________________________________________________________________
None
Train on 48000 samples, validate on 12000 samples
Epoch 1/5
- 60s - loss: 0.5297 - acc: 0.8328 - val_loss: 0.1023 - val_acc: 0.9702
Epoch 2/5
- 63s - loss: 0.1619 - acc: 0.9516 - val_loss: 0.0665 - val_acc: 0.9804
Epoch 3/5
- 62s - loss: 0.1213 - acc: 0.9630 - val_loss: 0.0513 - val_acc: 0.9844
Epoch 4/5
- 64s - loss: 0.0997 - acc: 0.9693 - val_loss: 0.0452 - val_acc: 0.9864
Epoch 5/5
- 61s - loss: 0.0855 - acc: 0.9738 - val_loss: 0.0395 - val_acc: 0.9885
</pre>
![image.png](https://upload-images.jianshu.io/upload_images/23118846-14ca0908256c0f84.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![image.png](https://upload-images.jianshu.io/upload_images/23118846-daf223a539ba7192.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
<pre style="box-sizing: border-box; overflow: auto; font-family: monospace; font-size: inherit; display: block; padding: 1px 0px; margin: 0px; line-height: inherit; color: black; word-break: break-all; overflow-wrap: break-word; background-color: transparent; border: 0px; border-radius: 0px; white-space: pre-wrap; vertical-align: baseline;">10000/10000 [==============================] - 5s 501us/step
accuracy= 0.9883</pre>