【一起入坑AI】LeNet5模型+keras---实现手写数字识别[99.38%]

简述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()

结果:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小样x

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值