8

I have trained a CNN and saved it accordingly:

model = Sequential()
model.add(Flatten(input_shape=train_data.shape[1:]))
model.add(Dense(256, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(1, activation='sigmoid'))

model.compile(optimizer='rmsprop',
              loss='binary_crossentropy', metrics=['accuracy'])

model.fit(train_data, train_labels,
          epochs=epochs,
          batch_size=batch_size,
          validation_data=(validation_data, validation_labels))
model.save('full_model.h5')

I now try load the model in another python script using the command:

model = tf.keras.models.load_model('full_model.h5')

and receive the following error:

    Traceback (most recent call last):
  File "/media/spt/Data/tensorflow_server/get_model.py", line 12, in <module>
    model = tf.keras.models.load_model('full_model.h5')
  File "/home/spt/.conda/envs/dev_env/lib/python3.6/site-packages/tensorflow/python/keras/engine/saving.py", line 229, in load_model
    model = model_from_config(model_config, custom_objects=custom_objects)
  File "/home/spt/.conda/envs/dev_env/lib/python3.6/site-packages/tensorflow/python/keras/engine/saving.py", line 306, in model_from_config
    return deserialize(config, custom_objects=custom_objects)
  File "/home/spt/.conda/envs/dev_env/lib/python3.6/site-packages/tensorflow/python/keras/layers/serialization.py", line 64, in deserialize
    printable_module_name='layer')
  File "/home/spt/.conda/envs/dev_env/lib/python3.6/site-packages/tensorflow/python/keras/utils/generic_utils.py", line 173, in deserialize_keras_object
    list(custom_objects.items())))
  File "/home/spt/.conda/envs/dev_env/lib/python3.6/site-packages/tensorflow/python/keras/engine/sequential.py", line 286, in from_config
    layer = layer_module.deserialize(conf, custom_objects=custom_objects)
  File "/home/spt/.conda/envs/dev_env/lib/python3.6/site-packages/tensorflow/python/keras/layers/serialization.py", line 64, in deserialize
    printable_module_name='layer')
  File "/home/spt/.conda/envs/dev_env/lib/python3.6/site-packages/tensorflow/python/keras/utils/generic_utils.py", line 193, in deserialize_keras_object
    function_name)
ValueError: Unknown layer:name

I came across more than one site describing the same/similar issue, e.g. stack overflow, github. Typically the issue is an outdated version of Keras. But in my case, all Keras related packages are up to date (output of conda list for all keras related packages):

keras-applications        1.0.6                    py36_0
keras-base                2.2.4                    py36_0
keras-gpu                 2.2.4                         0
keras-preprocessing       1.0.5                    py36_0

Can anyone suggest how I can fix/troubleshoot this issue?

Dominique
  • 8,687
  • 9
  • 28
  • 67
Allan_ZA
  • 81
  • 1
  • 4
  • 1
    Which versions of `tensorflow` and `tf.keras` do you use (`tf.VERSION`, `tf.keras.__version__`)? Is this model defined via `keras` or `tf.keras` and why do you use a `tf.keras` uploading a model, not a `keras`? – Mikhail Stepanov Jan 21 '19 at 09:05
  • 2
    Try using `load_model` from `keras.models` – Chris Jan 21 '19 at 09:35
  • I am new to TF and Keras and was not aware that keras and tf.keras were different. After reading your comments I updated my TF and now it works. Thanks a lot! I am using tf.keras since I am trying to use TF Serving – Allan_ZA Jan 21 '19 at 09:49
  • For anyone still struggling with issues similar to this one, see @M. Viaz's answer (and my comment) to https://stackoverflow.com/questions/53183865/unknown-initializer-glorotuniform-when-loading-keras-model – KamKam Jun 04 '19 at 11:19
  • I so wish someone would answer that – idoda Dec 22 '19 at 16:03

2 Answers2

1

i had the same problem, and It had been solved when I updated my Tensorflow and Keras version

1

If you are using a custom layer, you can load a keras model with such a layer as follows:

model = keras.models.load_model(model_path, custom_objects={'MyCustomLayer': InstanceOfMyCustomLayer})
Hagbard
  • 2,139
  • 2
  • 16
  • 42