我正在构建一个面部情绪识别系统,可以对快乐、悲伤、愤怒、惊讶等情绪进行分类。我使用 TensorFlow/Keras 训练了一个卷积神经网络模型,目前它的准确率达到了 50 左右%。然而,我相信微调超参数可能会进一步提高准确性。
现在,我有兴趣优化模型的超参数以实现更好的准确性。我听说过使用遗传算法进行超参数优化,但我不确定如何继续。有人可以指导我如何应用遗传算法来微调模型的超参数吗?具体来说,我如何修改我的代码以纳入遗传算法以进行超参数优化?
这是我的代码摘要:
import tensorflow as tf
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras import models,layers
# Data Augmentation
augmentor = ImageDataGenerator(
rescale=1.0/255,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True
)
# Loading data and resizing images to 48x48 pixels
augmented_trained_data = augmentor.flow_from_directory(
"Facial Recognition Dataset/Train",
target_size=(48, 48),
batch_size=32,
color_mode="grayscale",
class_mode="categorical"
)
augmented_validation_data = augmentor.flow_from_directory(
"Facial Recognition Dataset/Validation",
target_size=(48, 48),
batch_size=32,
color_mode="grayscale",
class_mode="categorical"
)
augmented_testing_data = augmentor.flow_from_directory(
"Facial Recognition Dataset/Test",
target_size=(48, 48),
batch_size=32,
color_mode="grayscale",
class_mode="categorical"
)
# Model Definition
model = models.Sequential([
layers.Conv2D(32, (2, 2), activation="relu", input_shape=(48, 48, 1)),
layers.MaxPool2D((2, 2)),
layers.Conv2D(64, (2, 2), activation="relu"),
layers.MaxPool2D((2, 2)),
layers.Conv2D(128, (2, 2), activation="relu"),
layers.MaxPool2D((2, 2)),
layers.Flatten(),
layers.Dense(128, activation="relu"),
layers.Dropout(0.25),
layers.Dense(6, activation="softmax")
])
# Model Compilation
model.compile(
optimizer='adam',
loss=tf.keras.losses.CategoricalCrossentropy(from_logits=False),
metrics=["accuracy"]
)
# Model Training
model.fit(
augmented_trained_data,
validation_data=augmented_validation_data,
epochs=10
)
# Model Evaluation
test_loss, test_accuracy = model.evaluate(augmented_testing_data)
print(f"Test Accuracy: {test_accuracy * 100:.2f}%")'''
也许您需要尝试另一种像这样的情绪识别系统https://luxand.cloud/face-recognition-blog/tutorial-facial-emotion-recognition-with-python
我用过它,训练起来更容易