Keras EarlyStopping is not recognized

Viewed 10836

I am using Early Stopping in my U-net model but it is raising error

File "main.py", line 18, in <module>
    earlystopper = EarlyStopping(monitor='val_loss', min_delta=0, patience=15, verbose=1, mode='auto', baseline=None, restore_best_weights=False)
NameError: name 'EarlyStopping' is not defined

However Keras callback function is working well

I cannot understand where the problem is? any help is acknowledged.

from model import *
from data import *

data_gen_args = dict(rotation_range=0.2,
                width_shift_range=0.05,
                height_shift_range=0.05,
                shear_range=0.05,
                zoom_range=0.05,
                horizontal_flip=True,
                fill_mode='nearest')
myGene = trainGenerator(2,'data/membrane/train','image','label',data_gen_args,save_to_dir = None)

model = unet()
model_checkpoint = ModelCheckpoint('unet_membrane.hdf5', monitor='loss',verbose=1, save_best_only=True)
earlystopper = EarlyStopping(monitor='val_loss', min_delta=0, patience=15, verbose=1, mode='auto', baseline=None, restore_best_weights=False)
model.fit_generator(myGene,steps_per_epoch=300,epochs=200,callbacks=[model_checkpoint, earlystopper])

testGene = testGenerator("data/membrane/test")
results = model.predict_generator(testGene,86,verbose=1)
saveResult("data/membrane/test",results)
1 Answers

You have to import classes before using them:

from keras.callbacks import ModelCheckpoint, EarlyStopping

Since you are using from something import *, it is possible that the imports from those modules are leaking into the current module, so it might error for some classes but not for others.

As a good practice, always import in the same file.

Related