Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ValueError: Unknown layer:name when loading a keras model

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?

like image 799
Allan_ZA Avatar asked Jan 21 '19 08:01

Allan_ZA


2 Answers

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})
like image 98
Hagbard Avatar answered Sep 28 '22 19:09

Hagbard


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

like image 39
niloofar rzi Avatar answered Sep 28 '22 19:09

niloofar rzi