数据集下载
# 读取文件
from keras.preprocessing.image import ImageDataGenerator
train_gen = ImageDataGenerator(rescale=1/255.0, horizontal_flip=True, rotation_range=30, vertical_flip=True, featurewise_center=True, width_shift_range=12, height_shift_range=12)
valid_gen = ImageDataGenerator(rescale=1/255.0, horizontal_flip=True, rotation_range=30, vertical_flip=True, featurewise_center=True, width_shift_range=12, height_shift_range=12)
test_gen = ImageDataGenerator(rescale= 1/ 255.0)
train_img_gen = train_gen.flow_from_directory('./tf_car_license_dataset/train_images/training-set/', target_size=(32, 32), batch_size=32, color_mode='grayscale')
valid_img_gen = train_gen.flow_from_directory('./tf_car_license_dataset/train_images/validation-set/', target_size=(32, 32), batch_size=32, color_mode='grayscale')
- 这里
# 构建神经网络
model = keras.Sequential([
keras.layers.Conv2D(32, (3, 3), 1, padding='same', activation="relu", input_shape = (32, 32, 1)),
keras.layers.MaxPool2D(2, 2),
keras.layers.Conv2D(64, (3, 3), 1, activation='relu'),
keras.layers.MaxPool2D(2, 2),
keras.layers.Flatten(),
keras.layers.Dense(128, activation='relu'),
keras.layers.Dense(40, activation='softmax')
])
model.compile(optimizer='adam', loss = keras.losses.categorical_crossentropy, metrics=['acc'])
model.summary()
history = model.fit(train_img_gen, validation_data = valid_img_gen, epochs=30, batch_size=128)
import matplotlib.pyplot as plt
losses = history.history['loss']
val_losses = history.history['val_loss']
acc = history.history['acc']
val_acc = history.history['val_acc']
plt.figure(figsize=(12, 6))
plt.subplot(121)
plt.xlabel('fit_nums')
plt.ylabel('losses')
plt.plot(losses,'b-')
plt.plot(val_losses,'b--')
plt.subplot(122)
plt.xlabel('fit_nums')
plt.ylabel('acc')
plt.plot(acc, 'b-', label='acc')
plt.plot(val_acc, 'b--', label = 'val_acc')
plt.legend()
plt.show()
history.history
可以看到, 训练的效果在不断增强,不过准确率不是很好,还有过拟合的风险,后面优化下。
运用PIL的Image
from PIL import Image
img = Image.open('./tf_car_license_dataset/test_images/1.bmp')
img = img.resize((32, 32))
# 这里变成输入的格式(count, size,size, 层数)
img_arr = np.array(img).reshape((1, 32, 32, 1))
注意: 因为我们训练时输入的是灰度图像,没有三通道的,所以之前的图片需要特殊处理成灰度图在进行输入
model.predict(img_arr)
可以看到,这里输出了,one-hot的编码数组,接下来只需要取最大值就可以了
template = ['0','1','2','3','4','5','6','7','8','9',
'A','B','C','D','E','F','G','H','J','K','L','M','N','P','Q','R','S','T','U','V','W','X','Y','Z',
'京','闽','粤','苏','沪','浙']
template[np.argmax(model.predict(img_arr))]
‘闽’
def test_img(path):
img = Image.open(path).convert("L")
img = img.resize((32, 32))
img_arr = np.array(img).reshape((1, 32, 32, 1))
template = ['0','1','2','3','4','5','6','7','8','9',
'A','B','C','D','E','F','G','H','J','K','L','M','N','P','Q','R','S','T','U','V','W','X','Y','Z',
'京','闽','粤','苏','沪','浙']
return template[np.argmax(model.predict(img_arr))]
# return model.predict(img_arr)
这里直接返回预测的字符
import os
out = ""
for path in os.listdir('./split/'):
out += test_img('./split/' + path)
print(out)
接下来我将用模板匹配的方式进行效果预测,看看会不会有更好的结果
import tensorflow.keras as keras
import numpy as np
# 读取文件
from keras.preprocessing.image import ImageDataGenerator
train_gen = ImageDataGenerator(rescale=1/255.0, horizontal_flip=True, rotation_range=30, vertical_flip=True, featurewise_center=True, width_shift_range=12, height_shift_range=12)
valid_gen = ImageDataGenerator(rescale=1/255.0, horizontal_flip=True, rotation_range=30, vertical_flip=True, featurewise_center=True, width_shift_range=12, height_shift_range=12)
test_gen = ImageDataGenerator(rescale= 1/ 255.0)
train_img_gen = train_gen.flow_from_directory('./tf_car_license_dataset/train_images/training-set/', target_size=(32, 32), batch_size=32, color_mode='grayscale')
valid_img_gen = train_gen.flow_from_directory('./tf_car_license_dataset/train_images/validation-set/', target_size=(32, 32), batch_size=32, color_mode='grayscale')
# 构建神经网络
model = keras.Sequential([
keras.layers.Conv2D(32, (3, 3), 1, padding='same', activation="relu", input_shape = (32, 32, 1)),
keras.layers.MaxPool2D(2, 2),
keras.layers.Conv2D(64, (3, 3), 1, activation='relu'),
keras.layers.MaxPool2D(2, 2),
keras.layers.Flatten(),
keras.layers.Dense(128, activation='relu'),
keras.layers.Dense(40, activation='softmax')
])
model.compile(optimizer='adam', loss = keras.losses.categorical_crossentropy, metrics=['acc'])
model.summary()
history = model.fit(train_img_gen, validation_data = valid_img_gen, epochs=30, batch_size=128)
import matplotlib.pyplot as plt
losses = history.history['loss']
val_losses = history.history['val_loss']
acc = history.history['acc']
val_acc = history.history['val_acc']
plt.figure(figsize=(12, 6))
plt.subplot(121)
plt.xlabel('fit_nums')
plt.ylabel('losses')
plt.plot(losses,'b-')
plt.plot(val_losses,'b--')
plt.subplot(122)
plt.xlabel('fit_nums')
plt.ylabel('acc')
plt.plot(acc, 'b-', label='acc')
plt.plot(val_acc, 'b--', label = 'val_acc')
plt.legend()
plt.show()
history.history
def test_img(path):
img = Image.open(path).convert("L")
img = img.resize((32, 32))
img_arr = np.array(img).reshape((1, 32, 32, 1))
template = ['0','1','2','3','4','5','6','7','8','9',
'A','B','C','D','E','F','G','H','J','K','L','M','N','P','Q','R','S','T','U','V','W','X','Y','Z',
'京','闽','粤','苏','沪','浙']
return template[np.argmax(model.predict(img_arr))]
# return model.predict(img_arr)
import os
out = ""
for path in os.listdir('./split/'):
out += test_img('./split/' + path)
print(out)