CNN卷积神经网络手写数字识别系统
实现了一个基于卷积神经网络(CNN)的智能手写数字识别系统,通过Tkinter框架构建了一个简单的图形用户界面(GUI)。用户可以在画布上手绘数字,系统会实时预测并显示识别结果。应用程序加载了一个已经训练好的MNIST数据集模型,通过Canvas组件允许用户自由绘制数字,并通过按钮触发数字识别。识别过程使用了Keras深度学习框架和TensorFlow,且界面中提供了清空画布和调整画笔大小的功能,使得用户体验更加友好。
基于卷积神经网络(CNN)的手写数字识别系统的实现代码,使用Python和深度学习框架TensorFlow/Keras完成。
代码实现
# 导入必要的库
import tensorflow as tf
from tensorflow.keras import layers, models
from tensorflow.keras.datasets import mnist
from tensorflow.keras.utils import to_categorical
import matplotlib.pyplot as plt
# 加载MNIST数据集
(x_train, y_train), (x_test, y_test) = mnist.load_data()
# 数据预处理
# 将图像数据归一化到 [0, 1] 范围,并调整形状以适应CNN输入
x_train = x_train.reshape((x_train.shape[0], 28, 28, 1)).astype('float32') / 255
x_test = x_test.reshape((x_test.shape[0], 28, 28, 1)).astype('float32') / 255
# 将标签转换为独热编码
y_train = to_categorical(y_train)
y_test = to_categorical(y_test)
# 构建CNN模型
model = models.Sequential()
# 第一层卷积层 + 最大池化层
model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)))
model.add(layers.MaxPooling2D((2, 2)))
# 第二层卷积层 + 最大池化层
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
# 第三层卷积层
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
# 展平层
model.add(layers.Flatten())
# 全连接层
model.add(layers.Dense(64, activation='relu'))
# 输出层
model.add(layers.Dense(10, activation='softmax'))
# 编译模型
model.compile(optimizer='adam',
loss='categorical_crossentropy',
metrics=['accuracy'])
# 打印模型结构
model.summary()
# 训练模型
history = model.fit(x_train, y_train, epochs=5, batch_size=64, validation_split=0.1)
# 评估模型
test_loss, test_acc = model.evaluate(x_test, y_test)
print(f"测试集准确率: {test_acc:.4f}")
# 可视化训练过程
plt.plot(history.history['accuracy'], label='训练准确率')
plt.plot(history.history['val_accuracy'], label='验证准确率')
plt.title('训练与验证准确率')
plt.xlabel('Epoch')
plt.ylabel('Accuracy')
plt.legend()
plt.show()
# 预测示例
predictions = model.predict(x_test)
example_index = 0
print(f"预测值: {predictions[example_index].argmax()}")
print(f"真实值: {y_test[example_index].argmax()}")
# 显示预测的图片
plt.imshow(x_test[example_index].reshape(28, 28), cmap='gray')
plt.title(f"预测值: {predictions[example_index].argmax()}")
plt.show()
—
代码说明
-
数据加载与预处理:
- 使用
mnist.load_data()
加载 MNIST 数据集。 - 将图像数据归一化到
[0, 1]
范围,并将形状调整为(28, 28, 1)
,以适应 CNN 输入格式。 - 标签通过
to_categorical
转换为独热编码。
- 使用
-
模型构建:
- 使用了三层卷积层(Conv2D),每层后接最大池化层(MaxPooling2D)。
- 卷积层提取特征,池化层降低维度。
- 展平层(Flatten)将多维数据展平为一维向量。
- 全连接层(Dense)进行分类,输出层使用
softmax
激活函数输出概率分布。
-
模型编译与训练:
- 使用
adam
优化器和categorical_crossentropy
损失函数。 - 训练过程中设置
epochs=5
和batch_size=64
。 - 验证集比例为
10%
。
- 使用
-
模型评估:
- 在测试集上评估模型性能,打印测试集准确率。
-
可视化与预测:
- 绘制训练和验证准确率曲线。
- 对测试集中的第一个样本进行预测,并显示其图像和预测结果。
—
运行环境
- Python >= 3.6
- TensorFlow >= 2.0
- Matplotlib >= 3.0
可以通过以下命令安装依赖:
pip install tensorflow matplotlib
运行结果
- 模型训练完成后,会输出训练和验证的准确率。
- 测试集上的准确率通常可以达到 98%-99%。
- 可视化训练过程的准确率曲线,以及预测结果的示例图片。