EfficientNetB0 kernel crashes as training begins

I’m trying to use EfficientNetB0 on Keras. The Keras model instantiates correctly, finds the training images and sets up the first epoch, but then the Python kernel dies and re-initializes.

It isn’t just EfficientNetB0 – other architectures that have worked fine now produce the same behavior. What’s different is that I’ve had to install keras-nightly in order to get the keras-efficientnets package. I reinstalled Keras 2.7.0 and Tensorflow 2.7.0 without uninstalling the other Keras versions, so what I have now is:

pip list | findstr "keras"
keras                         2.7.0
keras-efficientnets           0.1.7
keras-nightly                 2.9.0.dev2022011508

pip list | findstr "tensorflow"
tensorflow                    2.7.0
tensorflow-estimator          2.7.0
tensorflow-gpu                2.7.0
tensorflow-io-gcs-filesystem  0.23.1

My training code, which has worked fine for various Keras model architectures, is below.

> def train(self, trainpath, validatepath, l_rate=.001, epochs=50, save_every_epoch=True, patience=50, batch_size=16):
>         os.makedirs(trainpath, exist_ok=True)
>         model = self.model
>         nb_train_samples = sum(len(files) for _, _, files in os.walk(trainpath))
>         nb_validation_samples = sum(len(files) for _, _, files in os.walk(validatepath))
>         if save_every_epoch:            
>             mc = ModelCheckpoint(filepath=self.codepath+"saved-ResNet50-model-"+str(self.img_size)+"-{epoch:02d}-{accuracy:.2f}-{loss:.2f}.h5", \
>                     monitor='accuracy', verbose=1, save_best_only=False, mode='min')
>             callbacks_list = [mc]
>         else:
>             es = EarlyStopping(monitor='accuracy', mode='min', verbose=1, patience=patience) 
>             mc = ModelCheckpoint(filepath=self.codepath+"ResNet50-model"+self.name+".h5", \
>                     monitor='accuracy', verbose=1, save_best_only=True)
>             callbacks_list = [es, mc]
>         model.compile(loss='binary_crossentropy',
>                       optimizer=Adam(lr=l_rate),
>                       metrics=['accuracy'])
>         train_datagen = ImageDataGenerator(rescale=1. / 255,
>                             horizontal_flip=True, vertical_flip=True,
>                             brightness_range=[0.8,1.2])
>         test_datagen = ImageDataGenerator(rescale=1. / 255)
>         train_generator = train_datagen.flow_from_directory(
>                 trainpath,
>                 target_size=(self.img_size, self.img_size),
>                 class_mode='binary')
>         validation_generator = test_datagen.flow_from_directory(
>                 validatepath,
>                 target_size=(self.img_size, self.img_size),
>                 batch_size=batch_size,
>                 class_mode='binary')
>         print("Training "+self.name+" with tiles in "+trainpath)
>         model.fit_generator(
>                 train_generator,
>                 epochs=epochs,
>                 callbacks=callbacks_list,
>                 validation_data=validation_generator)
>         print(self.name+" trained successfully.\n")
>         return

Why you need to use keras-efficientnets?

Can you use EfficientNet B0 to B7 ?

Using Keras 2.7.0, the latest version available on Anaconda, without keras-efficientnets produces a ModuleNotFound error when trying to load any EfficientNet model. If you try help(tf.keras.applications), none of the EfficientNet models are there.

Try to setup a fresh installation in a virtual env:

Thanks, I’ll give it a whirl. I’ll try setting up a new Anaconda environment first.

Did not work, unfortunately. The new install eliminated some deprecation errors, but the kernel still died. I tried re-installing tensorflow through Anaconda, and it installs v. 2.7.0, and it now works without killing the kernel – but not on EfficientNet models because it can’t find efficientnet:

ImportError: cannot import name ‘efficientnet’ from ‘tensorflow.keras.applications’ (unknown location)

The available models are listed as:

densenet (package)
inception_resnet_v2 (package)
inception_v3 (package)
mobilenet (package)
mobilenet_v2 (package)
nasnet (package)
resnet50 (package)
vgg16 (package)
vgg19 (package)
xception (package)

Please try with a fresh venv and pip with the official guide.

As anconda is not officially supported directly by Tensorflow/Google you could try later if It works with pip+env.

Thanks again, Bhack. It’s actually easier for me simply to move this project to Google Colabs rather than work locally outside Anaconda, and it EfficientNetB0 does work fine on Colabs.

1 Like

At the end you could try with conda-forge: