9

I tried to use google colab resources to save my CNN model weights and I get this error. I tried googling it but nothing helps.

'Sequential' object has no attribute '_in_multi_worker_mode'

My code:

checkpoint_path = "training_1/cp.ckpt"
checkpoint_dir = os.path.dirname(checkpoint_path)
cp_callback = tf.keras.callbacks.ModelCheckpoint(checkpoint_path, save_weights_only=True, verbose=1)


cnn_model = Sequential()
cnn_model.add(Conv2D(filters = 64, kernel_size = (3,3), activation = "relu", input_shape = Input_shape ))
cnn_model.add(Conv2D(filters = 64, kernel_size = (3,3), activation = "relu"))
cnn_model.add(MaxPooling2D(2,2))
cnn_model.add(Dropout(0.4))

cnn_model = Sequential()
cnn_model.add(Conv2D(filters = 128, kernel_size = (3,3), activation = "relu"))
cnn_model.add(Conv2D(filters = 128, kernel_size = (3,3), activation = "relu"))
cnn_model.add(MaxPooling2D(2,2))
cnn_model.add(Dropout(0.3))


cnn_model.add(Flatten())

cnn_model.add(Dense(units = 512, activation = "relu"))
cnn_model.add(Dense(units = 512, activation = "relu"))

cnn_model.add(Dense(units = 10, activation = "softmax"))

history = cnn_model.fit(X_train, y_train, batch_size = 32,epochs = 1, 
shuffle = True, callbacks = [cp_callback])

Stack trace:

AttributeError                            Traceback (most recent call last)
<ipython-input-19-35c1db9636b7> in <module>()
----> 1 history = cnn_model.fit(X_train, y_train, batch_size = 32,epochs = 1, shuffle = True, callbacks = [cp_callback])

4 frames
/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/keras/callbacks.py in on_train_begin(self, logs)
    903   def on_train_begin(self, logs=None):
    904     # pylint: disable=protected-access
--> 905     if self.model._in_multi_worker_mode():
    906       # MultiWorkerTrainingState is used to manage the training state needed
    907       # for preemption-recovery of a worker in multi-worker training.

AttributeError: 'Sequential' object has no attribute '_in_multi_worker_mode'
theduck
  • 2,589
  • 13
  • 17
  • 23
Adem Kakhadze
  • 111
  • 1
  • 2
  • Welcome to stackoverflow. Please see how to create a [Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example). – o-90 Oct 31 '19 at 22:38

4 Answers4

17

I've recently faced the same issue

instead of,

from tensorflow.keras.callbacks import ModelCheckpoint

use,

from keras.callbacks import ModelCheckpoint
3

Check your tensorflow version. You actually only need to synchronize it. check if all your import uses

from keras import ...

or

from tensorflow.keras import ...

use only one of the above for your keras imports. using different (both) at the same time can cause collision by the libraries.

2

Instead of

tf.keras.callbacks.ModelCheckpoint

in your model building process, you can use

from keras.callbacks import ModelCheckpoint

in order to import ModelCheckpoint, and then just use ModelCheckpoint in the later code.

Asocia
  • 5,935
  • 2
  • 21
  • 46
0

Please check if your version of tensorflow matches the latest one.In my case the error was solved when is updated it to 2.1.0.