Why is ValueError thrown by keras.models.model_from_config() in the Keras-to-Tensorflow exporting example?

别等时光非礼了梦想. 提交于 2020-12-13 04:20:37

问题


The Keras website has this article about exporting Keras models to core Tensorflow. However the step

new_model = model_from_config(config)

throws an error:

Traceback (most recent call last):
  File "/home/hal9000/tf_serving_experiments/sndbx.py", line 38, in <module>
    new_model = model_from_config(config)
  File "/home/hal9000/keras2env/local/lib/python2.7/site-packages/keras/models.py", line 304, in model_from_config
    return layer_module.deserialize(config, custom_objects=custom_objects)
  File "/home/hal9000/keras2env/local/lib/python2.7/site-packages/keras/layers/__init__.py", line 54, in deserialize
    printable_module_name='layer')
  File "/home/hal9000/keras2env/local/lib/python2.7/site-packages/keras/utils/generic_utils.py", line 122, in deserialize_keras_object
    raise ValueError('Improper config format: ' + str(config))
ValueError: Improper config format: {'layers': [{'class_name': 'InputLayer', 'config': {...

People have suggested that there's a problem using the model_from_config() method with Keras v1 models since the release of v2. However I have tried this with a range of models from different versions, including the built-in Keras ResNet50 and a simple single-layer MLP defined in that very script. All throw the same error.

It would appear that the keras.utils.generic_utils.deserialize_keras_object() method wants to find a key "class_name" or "config" in the config dictionary (see source). Upon inspection of the config dict that get_config() creates, there is no such entry; instead there are keys:

  • "input_layers"
  • "layers"
  • "name"
  • "output_layers"

I also opened an issue https://github.com/fchollet/keras/issues/7232 and created a Gist that you can run for yourself and see the error. https://gist.github.com/9thDimension/e1cdb2cd11f11309bfaf297b276f7456

  • Keras 2.0.6
  • Tensorflow 1.1.0

回答1:


For whatever reason the dictionry object that keras.models.Model.get_config() returns is not compatible with the keras.models.model_from_config() method to rehydrate models.

I replaced these with equivalent calls to keras.models.Model.model_to_json() and keras.models.model_from_json() and was able to proceed successfully.




回答2:


The inverse of keras.models.Model.get_config() seems to be keras.models.Model.from_config(), not keras.models.model_from_config() https://keras.io/models/about-keras-models/. Try that instead?



来源:https://stackoverflow.com/questions/45352589/why-is-valueerror-thrown-by-keras-models-model-from-config-in-the-keras-to-ten

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