CNN与keras,准确性保持不变,并没有改善

时间:2018-06-13 07:53:44

标签: python keras deep-learning conv-neural-network

我试图通过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)

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

  • 将纪元增加到100。
  • 通过改变学习率。
  • 将优化程序更改为rmsprop。但结果更糟糕。
  • 我建议添加更多带有丢失和批量规范化的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 here的建议将我的激活设为softmax。 val acc现在仍然是81%,而培训acc接近99%

我做错了什么?

1 个答案:

答案 0 :(得分:1)

小心你的联系。在前两个块中,BatchNormalization未连接到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)

您提供的值意味着您的网络过度拟合。批量标准化或添加更多丢失可能会有所帮助。但是,鉴于图像数量较少,您应该尝试使用图像增强来增加训练图像的数量。