即使在退出和批量归一化之后,CNN仍然处于过度拟合状态

时间:2018-06-13 10:06:23

标签: python keras conv-neural-network

我试图通过finetuning预训练的model(vggface)来训练我的模型。我的模型有12个类,1774个训练图像和313个验证图像,每个类有大约150个图像。 我的模型过度拟合所以我添加了dropout和FC层,并进行了批量标准化,看看它是如何进行的。但是,模型仍然适用:

let name;
beforeEach(function() {

        browser.get('http://localhost:8080/#/'); 

        //name
        ame = element(by.css("*[id='field_nombre']"));
}   

以下是时代:

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)
xx = Dense(1024, activation = 'softmax')(x)
x2 = Dropout(0.5)(xx)
y = Dense(1024, activation = 'softmax')(x2)
yy = BatchNormalization()(y)
y1 = Dropout(0.5)(yy)
x3 = Dense(12, activation='softmax', name='classifier')(y1)

custom_vgg_model = Model(vggface.input, x3)


# Create the model
model = models.Sequential()

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

model.summary()
model = load_model('facenet_resnet_lr3_SGD_relu_1024.h5')

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 = 32
val_batchsize = 32

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=100,
      validation_data=validation_generator,
      validation_steps=validation_generator.samples/validation_generator.batch_size,
      verbose=1)

# Save the model
model.save('facenet_resnet_lr3_SGD_relu_1024_1.h5')

1 个答案:

答案 0 :(得分:0)

CNN深度网络需要庞大的数据进行培训。您有一个小数据集,模型无法从这个小数据集中推广。你有两个选择

  1. 缩小网络规模
  2. 增加数据集的数量
  3. 在回答评论后编辑:

    该模型存在一些问题。您不会将softmax用于隐藏图层 如果您想克服过度拟合的问题,您将冻结训练过的图层并仅训练新添加的图层。如果模型仍然过度使用,您可以删除已添加的某些图层或降低其单位数。