神经网络的训练需要大量的数据,数据的量决定了网络模型可以达到的高度,网络模型尽量地逼近这个高度。然而对于人脸表情的数据来说,都只存在少量的数据Extended Cohn-Kanada (CK+)的数据量是远远不够的,并且CK+多是比较夸张的数据。Kaggle Fer2013数据集也不过只有3万多数据量,而且有很多遮挡、角度等外界影响因素。既然收集数据要花费很大的人力物力,那么我们就用技术解决这个问题,为避免重复开发首先还是看看有没有写好的库。其中ImageDataGenerator的图片生成器就可完成这一目标。
ImageDataGenerator()是一个图片生成器,同时也可以在batch中对数据进行增强,扩充数据集大小(比如进行旋转,变形,归一化等),增强模型的泛化能力。
"""
Description: 训练人脸表情识别程序
"""
from keras.callbacks import CSVLogger, ModelCheckpoint, EarlyStopping
from keras.callbacks import ReduceLROnPlateau
from keras.preprocessing.image import ImageDataGenerator
from load_and_process import load_fer2013
from load_and_process import preprocess_input
from models.cnn import mini_XCEPTION
from sklearn.model_selection import train_test_split
# 参数
batch_size = 32
num_epochs = 10000
input_shape = (48, 48, 1)
validation_split = .2
verbose = 1
num_classes = 7
patience = 50
base_path = 'models/'
# 构建模型
model = mini_XCEPTION(input_shape, num_classes)
model.compile(optimizer='adam', # 优化器采用adam
loss='categorical_crossentropy', # 多分类的对数损失函数
metrics=['accuracy'])
model.summary()
# 定义回调函数 Callbacks 用于训练过程
log_file_path = base_path + '_emotion_training.log'
csv_logger = CSVLogger(log_file_path, append=False)
early_stop = EarlyStopping('val_loss', patience=patience)
reduce_lr = ReduceLROnPlateau('val_loss', factor=0.1,
patience=int(patience/4),
verbose=1)
# 模型位置及命名
trained_models_path = base_path + '_mini_XCEPTION'
model_names = trained_models_path + '.{epoch:02d}-{val_acc:.2f}.hdf5'
# 定义模型权重位置、命名等
model_checkpoint = ModelCheckpoint(model_names,
'val_loss', verbose=1,
save_best_only=True)
callbacks = [model_checkpoint, csv_logger, early_stop, reduce_lr]
# 载入数据集
faces, emotions = load_fer2013()
faces = preprocess_input(faces)
num_samples, num_classes = emotions.shape
# 划分训练、测试集
xtrain, xtest,ytrain,ytest = train_test_split(faces, emotions,test_size=0.2,shuffle=True)
# 图片产生器,在批量中对数据进行增强,扩充数据集大小
data_generator = ImageDataGenerator(
featurewise_center=False,
featurewise_std_normalization=False,
rotation_range=10,
width_shift_range=0.1,
height_shift_range=0.1,
zoom_range=.1,
horizontal_flip=True)
# 利用数据增强进行训练
model.fit_generator(data_generator.flow(xtrain, ytrain, batch_size),
steps_per_epoch=len(xtrain) / batch_size,
epochs=num_epochs,
verbose=1, callbacks=callbacks,
validation_data=(xtest,ytest))
————————————————
具体参考网址:https://blog.csdn.net/qq_32892383/article/details/91347164