街景字符编码识别-Task3—字符识别模型
1、加载库
import numpy as np
import tensorflow as tf
from tensorflow import keras
import pandas as pd
from pylab import rcParams
import matplotlib.pyplot as plt
from matplotlib import rc
import matplotlib.image as mpimg
import matplotlib.patches as patches
from pandas.plotting import register_matplotlib_converters
from sklearn.model_selection import train_test_split
import urllib
import os
import csv
import cv2
import time
from PIL import Image
import pprint
2、参数设置
train_label = './train.json'
val_label = './val.json'
width = 224
height = 112
channel = 3
batch_size = 64
3、加载并查看数据集
json_train = pd.read_json(train_label)
json_valid = pd.read_json(val_label)
json_train.head()
json_train = json_train.T
json_valid = json_valid.T
json_train.head()
json_train = json_train.reset_index(drop=False)
json_valid = json_valid.reset_index(drop=False)
json_train.head()
4、添加分类标签
train_label_fill_in_x = json_train['label'].map(lambda x: x[:4] + (4 - len(x))*[10])
valid_label_fill_in_x = json_valid['label'].map(lambda x: x[:4] + (4 - len(x))*[10])
json_train['label_train'] = train_label_fill_in_x
json_valid['label_valid'] = valid_label_fill_in_x
json_train.head()
def sep_dig_labels(df, label):
d0=[]
d1=[]
d2=[]
d3=[]
extract_labels = df[label]
for each in extract_labels:
d0.append(each[0])
d1.append(each[1])
d2.append(each[2])
d3.append(each[3])
df['d0'] = d0
df['d1'] = d1
df['d2'] = d2
df['d3'] = d3
sep_dig_labels(json_train, 'label_train')
sep_dig_labels(json_valid, 'label_valid')
json_train.head()
json_valid.head()
5、构建数据集
train_folder = './train/'
valid_folder = './val/'
trainGen = keras.preprocessing.image.ImageDataGenerator(rescale=1./255,
rotation_range=40,
width_shift_range=0.2,
height_shift_range=0.2,
shear_range=0.2,
zoom_range=0.2,
fill_mode='nearest')
validGen=keras.preprocessing.image.ImageDataGenerator(rescale=1./255)
train_generator = trainGen.flow_from_dataframe(json_train, #dataframe
#directory=train_folder, #根目录(当前路径)
directory='./train', #根目录(当前路径)
x_col='index',
y_col=['d0','d1','d2','d3'],
target_size=(width, height),
batch_size=batch_size,
seed=3,
shuffle=True,
class_mode='multi_output',)
valid_generator = validGen.flow_from_dataframe(json_valid,
directory='./val',
x_col='index',
y_col=['d0','d1','d2','d3'],
target_size=(width, height),
batch_size=batch_size,
seed=3,
shuffle=False,
class_mode='multi_output',)
6、定义模型
6.1、自定义模型
input_img = keras.layers.Input(shape=(width, height, channel), name='img')
# # ----------------------
x = keras.layers.Conv2D(filters=64,kernel_size=3,padding='same',activation='relu')(input_img)
# x = keras.layers.BatchNormalization()(x)
x = keras.layers.MaxPooling2D(pool_size=(2,2))(x)
# x = keras.layers.Dropout(0.2)(x)
# # ----------------------
x = keras.layers.Conv2D(filters=64,kernel_size=3,activation='relu')(x)
# x = keras.layers.BatchNormalization()(x)
x = keras.layers.MaxPooling2D(pool_size=(2,2))(x)
# x = keras.layers.Dropout(0.2)(x)
# # ----------------------
x = keras.layers.Conv2D(filters=128,kernel_size=3,activation='relu')(x)
# x = keras.layers.BatchNormalization()(x)
x = keras.layers.MaxPooling2D(pool_size=(2,2))(x)
# x = keras.layers.Dropout(0.2)(x)
# # ----------------------
x = keras.layers.Conv2D(filters=128,kernel_size=3,activation='relu')(x)
# x = keras.layers.BatchNormalization()(x)
x = keras.layers.MaxPooling2D(pool_size=(2,2))(x)
# x = keras.layers.Dropout(0.2)(x)
# # ----------------------
x = keras.layers.Conv2D(filters=256,kernel_size=3,activation='relu')(x)
# x = keras.layers.BatchNormalization()(x)
x = keras.layers.MaxPooling2D(pool_size=(2,2))(x)
# x = keras.layers.Dropout(0.2)(x)
# # ----------------------
x = keras.layers.Flatten()(x)
dig0 = keras.layers.Dense(11, activation='softmax', name='d0')(x)
dig1 = keras.layers.Dense(11, activation='softmax', name='d1')(x)
dig2 = keras.layers.Dense(11, activation='softmax', name='d2')(x)
dig3 = keras.layers.Dense(11, activation='softmax', name='d3')(x)
model = keras.models.Model(input_img, [dig0,dig1,dig2,dig3])
model.summary()
losses = {
"d0": 'sparse_categorical_crossentropy',
"d1": 'sparse_categorical_crossentropy',
"d2": 'sparse_categorical_crossentropy',
"d3": 'sparse_categorical_crossentropy',
}
model.compile(optimizer='adam',
loss=losses,
metrics=['accuracy'])
logdir = os.path.join('hourse_num')#'./hourse_num'
if not os.path.exists(logdir):
os.mkdir(logdir)
output_model_file = os.path.join(logdir,
"first_try.h5")
callbacks=[
keras.callbacks.TensorBoard(logdir),
keras.callbacks.ModelCheckpoint(output_model_file,
save_best_only = True,
save_weights_only = False),
keras.callbacks.EarlyStopping(patience=5,min_delta=1e-3)
]
train_num = train_generator.samples
valid_num = valid_generator.samples
print(train_num)
print(valid_num)
epochs=10
history = model.fit_generator(train_generator,
steps_per_epoch=train_num//batch_size,
validation_data=valid_generator,
validation_steps=valid_num//batch_size,
epochs=epochs,
callbacks=callbacks)
6.2、VGG16模型
1、使用tensorflow2.0自带的VGG16迁移学习
2、epochs = 10
3、batch_size = 32
4、使用验证集验证模型
5、使用tensorflow回调函数callbacks实现:保存模型(h5格式)、提前终止模型、tensorboard显示
from tensorflow.keras.applications import VGG16
input_img = keras.layers.Input(shape=(width, height, channel), name='img')
conv_base = VGG16(weights='imagenet',
include_top=False,
input_shape=(width, height, channel))(input_img)
conv_base.trainable = False
x = keras.layers.Flatten()(conv_base)
dig0 = keras.layers.Dense(11, activation='softmax', name='d0')(x)
dig1 = keras.layers.Dense(11, activation='softmax', name='d1')(x)
dig2 = keras.layers.Dense(11, activation='softmax', name='d2')(x)
dig3 = keras.layers.Dense(11, activation='softmax', name='d3')(x)
#conv_base.trainable = False
model = keras.models.Model(input_img, [dig0,dig1,dig2,dig3])
model.summary()
losses = {
"d0": 'sparse_categorical_crossentropy',
"d1": 'sparse_categorical_crossentropy',
"d2": 'sparse_categorical_crossentropy',
"d3": 'sparse_categorical_crossentropy',
}
model.compile(optimizer='adam',
loss=losses,
metrics=['accuracy'])
logdir = os.path.join('hourse_num')#'./hourse_num'
if not os.path.exists(logdir):
os.mkdir(logdir)
output_model_file = os.path.join(logdir,
"first_try.h5")
callbacks=[
keras.callbacks.TensorBoard(logdir),
keras.callbacks.ModelCheckpoint(output_model_file,
save_best_only = True,
save_weights_only = False),
keras.callbacks.EarlyStopping(patience=5,min_delta=1e-3)
]
train_num = train_generator.samples
valid_num = valid_generator.samples
print(train_num)
print(valid_num)
epochs=10
history = model.fit_generator(train_generator,
steps_per_epoch=train_num//batch_size,
validation_data=valid_generator,
validation_steps=valid_num//batch_size,
epochs=epochs,
callbacks=callbacks)
7、预测并生成提交文件