Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way to stop training a model in Keras after a certain accuracy has been achieved?

I know that it's easy to mention the number of epochs while training using fit_generator method. I have a lot of images to train and I can't use array to load them at once, because it shows MemoryError. I need to stop training after a certain validation accuracy, say 98%, has been reached. If the accuracy has not been achieved after the given number of epochs, the training will stop. Is there any way to do this in Keras? I am using Tensorflow backend.

Edit: I have seen EarlyStopping module in Keras, but it only keeps track of the change of a monitored quantity.

like image 888
Preetom Saha Arko Avatar asked May 02 '18 04:05

Preetom Saha Arko


1 Answers

You can take code for EarlyStopping from Keras.

class EarlyStoppingByAccuracy(Callback):
    def __init__(self, monitor='accuracy', value=0.98, verbose=0):
        super(Callback, self).__init__()
        self.monitor = monitor
        self.value = value
        self.verbose = verbose

    def on_epoch_end(self, epoch, logs={}):
        current = logs.get(self.monitor)
        if current is None:
            warnings.warn("Early stopping requires %s available!" % self.monitor, RuntimeWarning)

        if current >= self.value:
            if self.verbose > 0:
                print("Epoch %05d: early stopping THR" % epoch)
            self.model.stop_training = True

And the custom early stopping can be used like following

callbacks = [
    EarlyStoppingByAccuracy(monitor='accuracy', value=0.98, verbose=1),
    ModelCheckpoint(kfold_weights_path, monitor='val_loss', save_best_only=True, verbose=0),
]
model.fit(X_train.astype('float32'), Y_train, batch_size=batch_size, nb_epoch=nb_epoch,
      shuffle=True, verbose=1, validation_data=(X_valid, Y_valid),
      callbacks=callbacks)
like image 102
Md Johirul Islam Avatar answered Sep 21 '22 01:09

Md Johirul Islam