RE:从0开始的人表情识别


该客户已经通过了毕业答辩

数据集处理

FER2013的数据多少是有问题的,好心的微软帮我们重新标注了。
FERPlus
将下载好的fer2013.csv和重新标注的fer2013new.csv放在同一个目录下。

python3 generate_training_data.py -d ./ -fer ./fer2013.csv -ferplus ./fer2013new.csv

fer2013new将数据集分成了10种,分别是:
neutral、happiness、surprise、sadness、anger、disgust、fear、contempt、unknown、NF(NOT FACE)

有现成的Excel函数,就不重复造轮子了。直接

=MAX(C1:L1)
=MATCH(M1,C1:L1,0)

另存为一个csv,方便打标。
源数据是有十个集合的,我把第10,也就是NF集去掉了。

我参照了VGG,用3*3的小核卷积,示例代码如下:


# -- coding: utf-8 --
import numpy as np
import pandas as pd
import keras
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten, Activation, ZeroPadding2D, Conv2D, MaxPooling2D
from keras.optimizers import SGD
from keras import optimizers

df=pd.read_csv(r"./label_train.csv")
df['label']=df['label'].astype('str')
datagen=ImageDataGenerator(rescale=1./255)
train_generator=datagen.flow_from_dataframe(dataframe=df, directory="./FER2013Train", x_col="name", y_col="label", class_mode="categorical", target_size=(48,48), batch_size=32)
df2=pd.read_csv(r"./label_val.csv")
df2['label']=df2['label'].astype('str')
datagen2=ImageDataGenerator(rescale=1./255)
valid_generator=datagen2.flow_from_dataframe(dataframe=df2, directory="./FER2013Valid", x_col="name", y_col="label", class_mode="categorical", target_size=(48,48), batch_size=32)

model = Sequential()
model.add(ZeroPadding2D((1,1),input_shape=(48,48,3)))#卷积输入层,指定了输入图像的大小
model.add(Conv2D(64,(3,3), activation="relu"))  # 64个3x3的卷积核,生成644848的图像,激活函数为relu
model.add(ZeroPadding2D((1, 1)))
model.add(Conv2D(64,(3,3), activation="relu"))  # 再来一次卷积 生成644848
model.add(MaxPooling2D((2, 2), strides=(2, 2)))  # pooling操作,相当于变成642424
model.add(ZeroPadding2D((1, 1)))
model.add(Conv2D(128,(3,3), activation="relu"))
model.add(ZeroPadding2D((1, 1)))
model.add(Conv2D(128, (3,3), activation="relu"))
model.add(MaxPooling2D((2, 2), strides=(2, 2)))  # 1281212
model.add(ZeroPadding2D((1, 1)))
model.add(Conv2D(256, (3,3), activation="relu"))
model.add(ZeroPadding2D((1, 1)))
model.add(Conv2D(256, (3,3), activation="relu"))
model.add(ZeroPadding2D((1, 1)))
model.add(Conv2D(256, (3,3), activation="relu"))
model.add(MaxPooling2D((2, 2), strides=(2, 2)))  # 25666

model.add(Flatten())  # 压平上述向量,变成一维9216
model.add(Dense(1024, activation="relu"))  # 全连接层有1024个神经核,参数个数就是1024*9216
model.add(Dropout(0.5))  # 0.5的概率抛弃一些连接
model.add(Dense(512, activation="relu"))  # 再来一个全连接
model.add(Dropout(0.5))
model.add(Dense(128, activation="relu"))  # 再来一个全连接
model.add(Dropout(0.5))
model.add(Dense(9, activation='softmax'))

model.compile(optimizers.rmsprop(lr=0.0001),
loss="categorical_crossentropy", metrics=["accuracy"])
STEP_SIZE_TRAIN=train_generator.n//train_generator.batch_size
STEP_SIZE_VALID=valid_generator.n//valid_generator.batch_size
model.fit_generator(generator=train_generator,
                    steps_per_epoch=STEP_SIZE_TRAIN,
                    validation_data=valid_generator,
                    validation_steps=STEP_SIZE_VALID,
                    epochs=20)
model_save_path = "./model_file_path.h5"
# 保存模型
model.save(model_save_path)

模型summary如下:

然后用FER2013Test集测试:


# -- coding: utf-8 --
import matplotlib
import numpy
import numpy as np
import pandas as pd
import keras
import scipy
from keras.engine.saving import load_model
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten, Activation, ZeroPadding2D, Conv2D, MaxPooling2D
from keras.optimizers import SGD
from keras import optimizers
from matplotlib import image
from scipy import misc

df=pd.read_csv(r"./label_test.csv")
df['label']=df['label'].astype('str')
datagen=ImageDataGenerator(rescale=1./255)
test_generator=datagen.flow_from_dataframe(dataframe=df, directory="./FER2013Test", x_col="name", y_col="label", class_mode="categorical", target_size=(48,48), batch_size=32)

model = load_model("./model_file_path.h5")

predictions = model.evaluate(test_generator)
print(predictions)

结果如下,能有73%的准确率,对于单模型来说也算不错的结果了。

声明:@ギャズOfficial|版权所有,违者必究|如未注明,均为原创|本网站采用BY-NC-SA协议进行授权

转载:转载请注明原文链接 - RE:从0开始的人表情识别


我们终将知道,我们必须知道。