2023年11月9日,基于tensorflow框架对5000张验证码进行识别操作。其中4500张作为训练集,250张作为验证集,250张作为预测集。
问题1:训练集的正确率可以达到99%,但是验证集最多只能到68%。而且68%作为4个字符的识别正确率太低。对于验证集的4个字符全部识别正确,正确率只有12%。
import os
import cv2
import numpy as np
import random
import matplotlib.pyplot as plt
from Image_processing import trans_string
from tensorflow.keras import layers, models
# 常量定义
IMAGE_HEIGHT, IMAGE_WIDTH, IMAGE_CHANNELS = 40, 120, 3
# 字符映射
letter_to_number = {char: i for i, char in enumerate("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")}
def load_images_and_labels(folder_path):
image_files = [f for f in os.listdir(folder_path) if f.endswith('.jpg')]
random.shuffle(image_files)
file_paths = [os.path.join(folder_path, f) for f in image_files]
images = [cv2.imread(file_path) / 255 for file_path in file_paths]
labels = [trans_string(file.split('_')[0]) for file in image_files]
return np.array(images), np.array(labels)
# 加载训练和测试数据
x_train, y_train = load_images_and_labels(r'D:\test\im\certificate\dataset1\train1')
x_test, y_test = load_images_and_labels(r'D:\test\im\certificate\dataset1\val1')
# 构建模型
model = models.Sequential([
layers.Conv2D(32, (3, 3), activation='relu', input_shape=(IMAGE_HEIGHT, IMAGE_WIDTH, IMAGE_CHANNELS)),
layers.MaxPooling2D((2, 2)),
layers.Conv2D(32, (3, 3), activation='relu'),
layers.MaxPooling2D((2, 2)),
layers.Conv2D(32, (2, 2), activation='relu'),
layers.MaxPooling2D((2, 2)),
layers.Flatten(),
layers.Dense(1280, activation='relu', kernel_regularizer=models.regularizers.l2(0.005)),
layers.Dense(4 * 62),
layers.Reshape((4, 62)),
layers.Softmax()
])
# 编译模型
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
# 模型训练
history = model.fit(x_train, y_train, batch_size=100, epochs=20)
# 模型评估
test_loss, test_acc = model.evaluate(x_test, y_test)
print("Test accuracy:", test_acc)
# 保存模型
model.save('my_model')
下一步将对模型进行修改,提高预测集的正确率。