ValueError: Unknown layer:name when loading a keras model

 ̄綄美尐妖づ 提交于 2020-08-06 12:31:35

问题


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?


回答1:


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




回答2:


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})


来源:https://stackoverflow.com/questions/54286368/valueerror-unknown-layername-when-loading-a-keras-model

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!