深度学习基于卷积神经网络的人脸面部表情识别项目源码
时间: 2023-07-26 18:24:59 浏览: 166
以下是一个使用基于卷积神经网络的深度学习方法实现人脸面部表情识别的项目源码(Python):
```python
import os
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 Conv2D, MaxPooling2D, Dense, Flatten, Dropout
# 数据集路径
base_dir = '/path/to/dataset'
# 训练集、验证集、测试集路径
train_dir = os.path.join(base_dir, 'train')
validation_dir = os.path.join(base_dir, 'validation')
test_dir = os.path.join(base_dir, 'test')
# 图像大小
img_size = 48
# 数据增强
train_datagen = ImageDataGenerator(rescale=1./255,
rotation_range=30,
width_shift_range=0.2,
height_shift_range=0.2,
zoom_range=0.2,
horizontal_flip=True,
fill_mode='nearest')
validation_datagen = ImageDataGenerator(rescale=1./255)
# 生成训练集、验证集、测试集数据
train_generator = train_datagen.flow_from_directory(train_dir,
target_size=(img_size, img_size),
batch_size=32,
color_mode='grayscale',
class_mode='categorical')
validation_generator = validation_datagen.flow_from_directory(validation_dir,
target_size=(img_size, img_size),
batch_size=32,
color_mode='grayscale',
class_mode='categorical')
test_generator = validation_datagen.flow_from_directory(test_dir,
target_size=(img_size, img_size),
batch_size=32,
color_mode='grayscale',
class_mode='categorical')
# 构建模型
model = Sequential()
model.add(Conv2D(32, (3, 3), activation='relu', input_shape=(img_size, img_size, 1)))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D((2, 2)))
model.add(Dropout(0.25))
model.add(Conv2D(128, (3, 3), activation='relu'))
model.add(MaxPooling2D((2, 2)))
model.add(Conv2D(128, (3, 3), activation='relu'))
model.add(MaxPooling2D((2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(512, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(7, activation='softmax'))
# 编译模型
model.compile(loss='categorical_crossentropy',
optimizer='adam',
metrics=['accuracy'])
# 训练模型
history = model.fit(train_generator,
steps_per_epoch=train_generator.n // train_generator.batch_size,
epochs=50,
validation_data=validation_generator,
validation_steps=validation_generator.n // validation_generator.batch_size)
# 评估模型
test_loss, test_acc = model.evaluate(test_generator, verbose=2)
# 保存模型
model.save('emotion_detection_model.h5')
# 可视化训练过程
acc = history.history['accuracy']
val_acc = history.history['val_accuracy']
loss = history.history['loss']
val_loss = history.history['val_loss']
epochs = range(1, len(acc) + 1)
plt.plot(epochs, acc, 'bo', label='Training accuracy')
plt.plot(epochs, val_acc, 'b', label='Validation accuracy')
plt.title('Training and validation accuracy')
plt.legend()
plt.figure()
plt.plot(epochs, loss, 'bo', label='Training loss')
plt.plot(epochs, val_loss, 'b', label='Validation loss')
plt.title('Training and validation loss')
plt.legend()
plt.show()
```
这个项目使用了Keras框架,通过构建一个基于卷积神经网络的模型来实现人脸面部表情识别。在代码中,我们使用了ImageDataGenerator对数据进行增强,从而提高模型的泛化能力。另外,我们还使用了训练集、验证集、测试集的方式来评估模型的性能。最终,我们将训练好的模型保存为'h5'格式的文件,并使用matplotlib可视化了训练过程中的准确率和损失函数的变化。
阅读全文