简述LeNet5模型
LeNet5网络模型于1989年发表,是为了识别手写字体和计算机打印字符而设计,该模型在手写体识别领域非常成功,曾被广泛应用于美国银行支票手写体识别,该模型是第一个成熟的卷积神经网络模型。
LeNet5网络模型架构
LeNet5网络模型共有7层架构(不包含输入),分为:卷积层→Pooling层→卷积层→Pooling层→全连接层→全连接层→输出层(softmax层)
代码如下:
from keras.utils import to_categorical
from keras import models
from keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout
from keras.optimizers import Adam
from keras.datasets import mnist
import matplotlib.pyplot as plt
import numpy as np
############################################################### 1、load and deal
(train_image, train_labels), (test_image, test_labels) = mnist.load_data()
print(train_image.shape, test_image.shape)
print(train_labels.shape, test_labels.shape)
# 处理数据集
train_image = train_image.reshape(-1, train_image.shape[1], train_image.shape[2], 1)
test_image = test_image.reshape(-1, test_image.shape[1], test_image.shape[2], 1)
train_labels = to_categorical(train_labels) # one_hot变成向量,即一维数组
test_labels = to_categorical(test_labels)
# 归一化
train_image = train_image / 255
test_image = test_image / 255
############################################################### 2、build Neural Network
# 搭建神经网络模型---LeNet_5(3层--0,1,2)
def LeNet_5():
model = models.Sequential()
model.add(Conv2D(filters=32, kernel_size=(3, 3), padding="same", activation="relu", input_shape=[28, 28, 1]))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(filters=64, kernel_size=(5, 5), activation="relu")) # 卷积核=64
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(120, activation="relu"))
model.add(Dropout(0.5))
model.add(Dense(84, activation="relu"))
model.add(Dropout(0.5))
model.add(Dense(10, activation="softmax"))
return model
lenet5 = LeNet_5()
lenet5.summary() # 显示模型结构
############################################################### 3、build model and train
# 编译模型
Learning_Rate = 0.001
EPOCHS = 20
lenet5.compile(optimizer=Adam(lr=Learning_Rate), loss="categorical_crossentropy", metrics=["accuracy"])
# 训练
H = lenet5.fit(train_image, train_labels, validation_data=(test_image, test_labels), epochs=EPOCHS, batch_size=128,
verbose=2)
############################################################### 4、predict and evaluate
# 预测
y_pre = lenet5.predict(test_image[:10])
print("预测值: ", y_pre)
# 评估
test_loss, test_acc = lenet5.evaluate(test_image, test_labels)
print("test_loss:", test_loss, "test_accuracy:", test_acc)
############################################################### 5、draw
# 绘制图像比较结果
N = np.arange(0, EPOCHS) # 迭代数
plt.figure() # 新建画布
plt.plot(N, H.history["loss"], label="train_loss")
plt.plot(N, H.history["val_loss"], label="test_loss")
plt.plot(N, H.history["accuracy"], label="train_acc")
plt.plot(N, H.history["val_accuracy"], label="test_acc")
plt.title("Training loss and accuracy")
plt.xlabel("EPOCH")
plt.ylabel("loss/accuracy")
plt.legend()
plt.savefig(fname="mnist.png", figsize=[224, 224])
plt.show()
结果: