Search code examples
pythonmachine-learningkerascomputer-visiontraining-data

how to get history['val_accuracy'] from ImageDataGenerator


I'm using keras.preprocessing.image.ImageDataGenerator

When i fed it to model.fit like that

history = model.fit(
    train_data_gen,
    epochs=EPOCHS,
    steps_per_epoch=steps_per_epoch,
    validation_data=val_data_gen,
    validation_freq=validation_freq,
    callbacks=[EarlyStopping(monitor='val_accuracy', patience=2)]
)

it works fine, but there is no actual validation data, so my callback doesn't work, as well as plotting, since history.history['val_accuracy'] simply doesn't exist, i have only two items in this dict accuracy and loss

so my main question how to get it work like that

history = model.fit(
    x=train_data_gen,
    y=val_data_gen,
)

but w/o

ValueError: `y` argument is not supported when using python generator as input.

Solution

  • problem was in model.fit arguments

    validation_freq=validation_freq instead of validation_steps=validation_freq

    so after that everything works just fine, val_accuracy is available finally

    history = model.fit(
        x=train_data_gen,
        epochs=EPOCHS,
        steps_per_epoch=steps_per_epoch,
        validation_data=val_data_gen,
        validation_steps=validation_freq,
        callbacks=[
            EarlyStopping(monitor='val_accuracy', patience=2),
            ModelCheckpoint('models/m-{epoch:02d}-{val_accuracy:.4f}.h5')
        ],
    ).history