gpt4 book ai didi

python - 添加批量归一化后训练预训练模型 keras_vggface 会产生非常高的损失

转载 作者:行者123 更新时间:2023-11-30 09:17:39 25 4
gpt4 key购买 nike

我正在尝试使用预训练的 Keras VGGFace 来训练我的模型在包含 1774 个训练图像和 313 个验证图像(由 12 个类别组成)的数据集(所有面孔)上。

最近在我的代码脚本中添加了批量标准化和丢弃,因为它过度拟合。 (我的训练 acc 约为 99,val acc 约为 80)。这是我的代码:

train_data_path = 'dataset_cfps/train'
validation_data_path = 'dataset_cfps/validation'

#Parametres
img_width, img_height = 224, 224

vggface = VGGFace(model='resnet50', include_top=False, input_shape=(img_width, img_height, 3))

last_layer = vggface.get_layer('avg_pool').output
x = Flatten(name='flatten')(last_layer)
x1 = Dense(12, activation='sigmoid', name='classifier')(x)
x2 = BatchNormalization()(x1)
x3 = Dropout(0.5)(x2)
custom_vgg_model = Model(vggface.input, x3)

# Create the model
model = models.Sequential()

# Add the convolutional base model
model.add(custom_vgg_model)

当我尝试训练模型时,损失刚刚超过 10,这是不应该发生的。 Batch Normalization 和 dropout 层是否添加在正确的位置?我尝试采用他们的 github 存储库中的代码。

最佳答案

在代码中的 sigmoid 分类层之后添加批量归一化和 dropout。 Dropout 会导致密集层输出的一些 sigmoid 激活或概率不被考虑,这就是为什么在尝试找到具有最大概率的类时可能会出现错误的分类预测。

请尝试按如下方式修改您的代码,以包含两个密集层。第一个隐藏密集层的尺寸应约为输入和输出尺寸之和的一半。最终的密集层可以是使用 sigmoid 激活来预测输出类概率的层。正则化层可以添加在两层之间。

train_data_path = 'dataset_cfps/train'
validation_data_path = 'dataset_cfps/validation'

#Parametres
img_width, img_height = 224, 224

vggface = VGGFace(model='resnet50', include_top=False, input_shape=(img_width, img_height, 3))

last_layer = vggface.get_layer('avg_pool').output
x = Flatten(name='flatten')(last_layer)
x1 = Dense(1024, activation='relu')(x)
x2 = BatchNormalization()(x1)
x3 = Dropout(0.5)(x2)
x4 = Dense(12, activation='sigmoid', name='classifier')(x3)
custom_vgg_model = Model(vggface.input, x4)

# Create the model
model = models.Sequential()

# Add the convolutional base model
model.add(custom_vgg_model)

关于python - 添加批量归一化后训练预训练模型 keras_vggface 会产生非常高的损失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50801878/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com