import os
import tensorflow as tf
from tensorflow.keras.datasets import mnist
from tensorflow.keras.utils import to_categorical
import numpy as np
E:\Anaconda3\envs\dl\lib\site-packages\tensorflow\python\framework\dtypes.py:516: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
_np_qint8 = np.dtype([("qint8", np.int8, 1)])
E:\Anaconda3\envs\dl\lib\site-packages\tensorflow\python\framework\dtypes.py:517: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
_np_quint8 = np.dtype([("quint8", np.uint8, 1)])
E:\Anaconda3\envs\dl\lib\site-packages\tensorflow\python\framework\dtypes.py:518: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
_np_qint16 = np.dtype([("qint16", np.int16, 1)])
E:\Anaconda3\envs\dl\lib\site-packages\tensorflow\python\framework\dtypes.py:519: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
_np_quint16 = np.dtype([("quint16", np.uint16, 1)])
E:\Anaconda3\envs\dl\lib\site-packages\tensorflow\python\framework\dtypes.py:520: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
_np_qint32 = np.dtype([("qint32", np.int32, 1)])
E:\Anaconda3\envs\dl\lib\site-packages\tensorflow\python\framework\dtypes.py:525: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
np_resource = np.dtype([("resource", np.ubyte, 1)])
E:\Anaconda3\envs\dl\lib\site-packages\tensorboard\compat\tensorflow_stub\dtypes.py:541: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
_np_qint8 = np.dtype([("qint8", np.int8, 1)])
E:\Anaconda3\envs\dl\lib\site-packages\tensorboard\compat\tensorflow_stub\dtypes.py:542: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
_np_quint8 = np.dtype([("quint8", np.uint8, 1)])
E:\Anaconda3\envs\dl\lib\site-packages\tensorboard\compat\tensorflow_stub\dtypes.py:543: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
_np_qint16 = np.dtype([("qint16", np.int16, 1)])
E:\Anaconda3\envs\dl\lib\site-packages\tensorboard\compat\tensorflow_stub\dtypes.py:544: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
_np_quint16 = np.dtype([("quint16", np.uint16, 1)])
E:\Anaconda3\envs\dl\lib\site-packages\tensorboard\compat\tensorflow_stub\dtypes.py:545: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
_np_qint32 = np.dtype([("qint32", np.int32, 1)])
E:\Anaconda3\envs\dl\lib\site-packages\tensorboard\compat\tensorflow_stub\dtypes.py:550: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
np_resource = np.dtype([("resource", np.ubyte, 1)])
(X_tarin, y_train), (X_test, y_test) = mnist.load_data()
X_train4D = X_tarin.reshape(X_tarin.shape[0], 28, 28, 1).astype('float32')
X_test4D = X_test.reshape(X_test.shape[0], 28, 28, 1).astype('float32')
X_train4D_Normalize = X_train4D / 255 # 归一化
X_test4D_Normalize = X_test4D / 255
y_trainOnehot = to_categorical(y_train)
y_testOnehot = to_categorical(y_test)
# 导入相关包
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPool2D
# 建立模型
model = Sequential()
# 一层卷积
model.add(
Conv2D(
filters=16,
kernel_size=(5, 5),
padding='same', # 保证卷积核大小,不够补零
input_shape=(28, 28, 1),
activation='relu'))
# 池化层1
model.add(MaxPool2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
# 二层卷积
model.add(
Conv2D(filters=32, kernel_size=(5, 5), padding='same', activation='relu'))
# 池化层2
model.add(MaxPool2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(
Conv2D(filters=64, kernel_size=(5, 5), padding='same', activation='relu'))
model.add(
Conv2D(filters=128, kernel_size=(5, 5), padding='same', activation='relu'))
model.add(MaxPool2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten()) # 平坦层
model.add(Dense(128, activation='relu')) # 全连接层
model.add(Dropout(0.25))
model.add(Dense(10, activation='softmax')) # 激活函数
model.summary()
Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d (Conv2D) (None, 28, 28, 16) 416
_________________________________________________________________
max_pooling2d (MaxPooling2D) (None, 14, 14, 16) 0
_________________________________________________________________
dropout (Dropout) (None, 14, 14, 16) 0
_________________________________________________________________
conv2d_1 (Conv2D) (None, 14, 14, 32) 12832
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 7, 7, 32) 0
_________________________________________________________________
dropout_1 (Dropout) (None, 7, 7, 32) 0
_________________________________________________________________
conv2d_2 (Conv2D) (None, 7, 7, 64) 51264
_________________________________________________________________
conv2d_3 (Conv2D) (None, 7, 7, 128) 204928
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 (None, 3, 3, 128) 0
_________________________________________________________________
dropout_2 (Dropout) (None, 3, 3, 128) 0
_________________________________________________________________
flatten (Flatten) (None, 1152) 0
_________________________________________________________________
dense (Dense) (None, 128) 147584
_________________________________________________________________
dropout_3 (Dropout) (None, 128) 0
_________________________________________________________________
dense_1 (Dense) (None, 10) 1290
=================================================================
Total params: 418,314
Trainable params: 418,314
Non-trainable params: 0
_________________________________________________________________
# 训练模型
model.compile(loss='categorical_crossentropy',
optimizer='adam',
metrics=['accuracy'])
train_history = model.fit(x=X_train4D_Normalize,
y=y_trainOnehot,
validation_split=0.2,
batch_size=300,
epochs=10,
verbose=2)
WARNING:tensorflow:From E:\Anaconda3\envs\dl\lib\site-packages\tensorflow\python\ops\math_grad.py:1250: add_dispatch_support.<locals>.wrapper (from tensorflow.python.ops.array_ops) is deprecated and will be removed in a future version.
Instructions for updating:
Use tf.where in 2.0, which has the same broadcast rule as np.where
Train on 48000 samples, validate on 12000 samples
Epoch 1/10
48000/48000 - 81s - loss: 0.4865 - accuracy: 0.8395 - val_loss: 0.0870 - val_accuracy: 0.9719
Epoch 2/10
48000/48000 - 80s - loss: 0.1150 - accuracy: 0.9651 - val_loss: 0.0539 - val_accuracy: 0.9842
Epoch 3/10
48000/48000 - 92s - loss: 0.0841 - accuracy: 0.9746 - val_loss: 0.0442 - val_accuracy: 0.9857
Epoch 4/10
48000/48000 - 90s - loss: 0.0639 - accuracy: 0.9797 - val_loss: 0.0373 - val_accuracy: 0.9892
Epoch 5/10
48000/48000 - 89s - loss: 0.0532 - accuracy: 0.9835 - val_loss: 0.0341 - val_accuracy: 0.9889
Epoch 6/10
48000/48000 - 83s - loss: 0.0496 - accuracy: 0.9846 - val_loss: 0.0305 - val_accuracy: 0.9908
Epoch 7/10
48000/48000 - 85s - loss: 0.0415 - accuracy: 0.9869 - val_loss: 0.0298 - val_accuracy: 0.9912
Epoch 8/10
48000/48000 - 92s - loss: 0.0390 - accuracy: 0.9879 - val_loss: 0.0288 - val_accuracy: 0.9913
Epoch 9/10
48000/48000 - 85s - loss: 0.0322 - accuracy: 0.9901 - val_loss: 0.0265 - val_accuracy: 0.9925
Epoch 10/10
48000/48000 - 83s - loss: 0.0299 - accuracy: 0.9905 - val_loss: 0.0304 - val_accuracy: 0.9912
# 定义训练参数可视化
import matplotlib.pyplot as plt
%matplotlib inline
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, 'accuracy', 'val_accuracy')
# 损失率
show_train_history(train_history, 'loss', 'val_loss')
model.evaluate(X_test4D_Normalize, y_testOnehot)[1]
10000/10000 [==============================] - 4s 423us/sample - loss: 0.0252 - accuracy: 0.9915
0.9915
# 预测值
prediction = model.predict_classes(X_test4D_Normalize)
# 预测前十
prediction[:10]
array([7, 2, 1, 0, 4, 1, 4, 9, 5, 9], dtype=int64)
# 定义图片可视化
def plot_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, i + 1)
ax.imshow(images[idx], cmap='binary')
if len(prediction) > 0:
title = ',labels' + str(prediction[idx])
ax.set_title(title, fontsize=10)
ax.set_xticks([])
ax.set_yticks([])
idx += 1
plt.show()
plot_labels_prediction(X_test, y_test, prediction, idx=0)
# 交叉表查看预测数据与原数据对比
import pandas as pd
pd.crosstab(y_test, prediction, rownames=['lables'], colnames=['predict'])
predict | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
---|---|---|---|---|---|---|---|---|---|---|
lables | ||||||||||
0 | 977 | 0 | 0 | 0 | 0 | 0 | 1 | 1 | 1 | 0 |
1 | 0 | 1120 | 3 | 0 | 0 | 0 | 5 | 1 | 6 | 0 |
2 | 0 | 0 | 1029 | 1 | 0 | 0 | 0 | 1 | 1 | 0 |
3 | 0 | 0 | 0 | 1000 | 0 | 8 | 0 | 0 | 2 | 0 |
4 | 0 | 0 | 1 | 0 | 973 | 0 | 0 | 0 | 2 | 6 |
5 | 1 | 0 | 0 | 2 | 0 | 888 | 1 | 0 | 0 | 0 |
6 | 5 | 1 | 0 | 0 | 0 | 3 | 947 | 0 | 2 | 0 |
7 | 1 | 2 | 6 | 1 | 0 | 0 | 0 | 1016 | 1 | 1 |
8 | 0 | 0 | 1 | 0 | 0 | 1 | 0 | 0 | 971 | 1 |
9 | 0 | 0 | 1 | 0 | 3 | 4 | 0 | 1 | 6 | 994 |
由上图显示对角线为预测准确的次数