1. 使用卷积神经网络(CNN)来构建模型训练
import numpy as np
from keras import Sequential
from keras.api.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout, BatchNormalization
from keras.src.legacy.preprocessing.image import ImageDataGenerator
from sklearn.metrics import f1_score
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import OneHotEncoder
# 加载数据
X = np.load("Data/dataset.npy", allow_pickle=True)
y = np.load("Data/class.npy", allow_pickle=True)
# 数据预处理:归一化并重新调整形状为 (样本数, 28, 28, 1) 用于 CNN
X = X.astype('float32') / 255.0 # 归一化
X = X.reshape(-1, 28, 28, 1) # 调整形状
# One-hot 编码
onehot = OneHotEncoder(sparse_output=False)
y = onehot.fit_transform(y.reshape(-1, 1))
# 划分训练集和测试集
x_train, x_test, y_train, y_test = train_test_split(X, y, random_state=14)
# 创建数据增强生成器
datagen = ImageDataGenerator(
rotation_range=10,
width_shift_range=0.1,
height_shift_range=0.1,
shear_range=0.1,
zoom_range=0.1,
horizontal_flip=False,
fill_mode='nearest'
)
# 构建 CNN 模型
model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3), activation='relu', input_shape=(28, 28, 1)))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(64, kernel_size=(3, 3), activation='relu'))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5)) # 添加 Dropout 层以防止过拟合
model.add(Dense(10, activation='softmax')) # 输出层
# 编译模型
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
# 训练模型时使用数据增强
model.fit(datagen.flow(x_train, y_train, batch_size=32),
epochs=100,
validation_data=(x_test, y_test),
verbose=1)
# 评估模型
predictions = model.predict(x_test)
predicted_classes = np.argmax(predictions, axis=1)
y_test_classes = np.argmax(y_test, axis=1)
# 计算 F-score
print("F-score: {0:.2f}".format(f1_score(y_test_classes, predicted_classes, average='micro')))
# 保存模型
model.save("my_model02.keras")
2. 调用训练的模型进行测试
import cv2
import matplotlib.pyplot as plt
import numpy as np
from keras.api.models import load_model
# 加载模型
model = load_model("my_model02.keras")
# 加载手写数字图像
original_img = cv2.imread("Data/handwritten_digit.png", cv2.IMREAD_GRAYSCALE)
# 处理图像用于预测
img = cv2.resize(original_img, (28, 28)) # 调整为28x28大小
img = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY_INV)[1] # 二值化
img = img.astype('float32') / 255 # 归一化
img = img.reshape(1, 28, 28, 1) # 调整形状为 (1, 28, 28, 1)
# 进行预测
predictions = model.predict(img)
predicted_class = np.argmax(predictions, axis=1)
# 可视化预测结果
plt.figure(figsize=(6, 6))
# 显示原图
plt.imshow(original_img, cmap='gray', aspect='equal') # 使用原始图像
plt.title(f'Predicted: {predicted_class[0]}', fontsize=14)
plt.axis('off')
plt.tight_layout()
plt.show()