gpt4 book ai didi

python - CNN with keras,精度保持不变,没有提高

转载 作者:太空宇宙 更新时间:2023-11-04 04:40:12 24 4
gpt4 key购买 nike

我正在尝试通过 finetuning 训练我的模型预训练 model (vggface)。我的模型有 12 个类,有 1774 个训练图像和 313 个验证图像,每个类有大约 150 个图像。到目前为止,我已经能够使用 keras 中的以下脚本实现大约 80% 的最大准确度:

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)
out = Dense(12, activation='softmax', name='classifier')(x)
custom_vgg_model = Model(vggface.input, out)


# Create the model
model = models.Sequential()

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

train_datagen = ImageDataGenerator(
rescale=1./255,
rotation_range=20,
width_shift_range=0.2,
height_shift_range=0.2,
horizontal_flip=True,
fill_mode='nearest')

validation_datagen = ImageDataGenerator(rescale=1./255)

# Change the batchsize according to your system RAM
train_batchsize = 16
val_batchsize = 16

train_generator = train_datagen.flow_from_directory(
train_data_path,
target_size=(img_width, img_height),
batch_size=train_batchsize,
class_mode='categorical')

validation_generator = validation_datagen.flow_from_directory(
validation_data_path,
target_size=(img_width, img_height),
batch_size=val_batchsize,
class_mode='categorical',
shuffle=True)

# Compile the model
model.compile(loss='categorical_crossentropy',
optimizer=optimizers.SGD(lr=1e-3),
metrics=['acc'])
# Train the model
history = model.fit_generator(
train_generator,
steps_per_epoch=train_generator.samples/train_generator.batch_size ,
epochs=50,
validation_data=validation_generator,
validation_steps=validation_generator.samples/validation_generator.batch_size,
verbose=1)

到目前为止我已经尝试过:

  • 将 epochs 增加到 100。
  • 通过改变学习率。
  • 通过将优化器更改为 rmsprop。但这给出了更糟糕的结果。
  • 有人建议我添加更多带有 dropout 和 batch normalization 的 FC 层。我这样做了,验证准确率仍然在 79% 左右。

这是我的零钱:

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

#vgg_model = VGGFace(include_top=False, input_shape=(224, 224, 3))

last_layer = vggface.get_layer('avg_pool').output
x = Flatten(name='flatten')(last_layer)
xx = Dense(256, activation = 'relu')(x)
x1 = BatchNormalization()(xx)
x2 = Dropout(0.3)(xx)

y = Dense(256, activation = 'relu')(x2)
yy = BatchNormalization()(y)
y1 = Dropout(0.3)(y)

z = Dense(256, activation = 'relu')(y1)
zz = BatchNormalization()(z)
z1 = Dropout(0.6)(zz)

x3 = Dense(12, activation='softmax', name='classifier')(z1)

custom_vgg_model = Model(vggface.input, x3)

我现在按照 SymbolixAU 的建议将我的激活设为 softmax here . val acc 现在仍然是 81%,而 training acc 接近 99%

我做错了什么?

最佳答案

小心你的连接。在前两个 block 中,BatchNormalization 没有连接到 dropout。改变两个第一个dropout的输入。

xx = Dense(256, activation = 'relu')(x)
x1 = BatchNormalization()(xx)
x2 = Dropout(0.3)(x1)

y = Dense(256, activation = 'relu')(x2)
yy = BatchNormalization()(y)
y1 = Dropout(0.3)(yy)

您提供的值暗示您的网络过度拟合。批量归一化或添加更多 dropout 可能会有所帮助。但是,鉴于图像数量较少,您真的应该尝试图像增强来增加训练图像的数量。

关于python - CNN with keras,精度保持不变,没有提高,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50831870/

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