正确使用带有fit_generator的流(接收零精度或警告)

时间:2018-10-01 07:52:55

标签: python keras

我正在创建一个模型,并想在生成器中使用数据增强。

因此,我正在使用此功能:

def data_augmentation(X_train, y_train, X_val, y_val, batch_size, seed):

    data_gen_args = dict(featurewise_center=True,
                         featurewise_std_normalization=True,
                         rotation_range=90,
                         width_shift_range=0.1,
                         height_shift_range=0.1,
                         zoom_range=0.2)


    image_datagen = ImageDataGenerator(**data_gen_args)
    mask_datagen = ImageDataGenerator(**data_gen_args)

    #image_datagen.fit(X_train, augment=True, seed=seed)
    #mask_datagen.fit(y_train, augment=True, seed=seed)

    X_train_aug = image_datagen.flow(X_train, batch_size=batch_size, shuffle=True, seed=seed)
    y_train_aug = mask_datagen.flow(y_train, batch_size=batch_size, shuffle=True, seed=seed)


    image_datagen_val = ImageDataGenerator()
    mask_datagen_val = ImageDataGenerator()

    #image_datagen_val.fit(X_val, augment=False, seed=seed)
    #mask_datagen_val.fit(y_val, augment=False, seed=seed)

    X_val_aug = image_datagen_val.flow(X_val, batch_size=batch_size, shuffle=True, seed=seed)
    y_val_aug = mask_datagen_val.flow(y_val, batch_size=batch_size, shuffle=True, seed=seed)


    train_generator = zip(X_train_aug, y_train_aug)
    val_generator = zip(X_val_aug, y_val_aug)

    return train_generator, val_generator

如果我取消对进行拟合的线的注释并运行模型,则我将始终采用零精度,并且大多数情况下为负非常小的损失。

如果我将其保留下来,则表示损失和准确性属于正常值,但我会收到警告:

This ImageDataGenerator specifies feature_center but it hasn't fit on any training data.Fit it first by calling...

我正在打电话给fit_generator:

history = model.fit_generator(train_generator,
                              validation_data=val_generator,
                              steps_per_epoch=steps_per_epoch,
                              validation_steps=val_steps,
                              epochs = 50,
                              callbacks=[early_stopping, model_checkpoint, reduce_lr])

该模型还可以,因为无需使用data_augmentation即可正常工作。

0 个答案:

没有答案