【问题标题】:TypeError when loading in Keras model在 Keras 模型中加载时出现 TypeError
【发布时间】:2019-03-26 03:26:09
【问题描述】:

我使用以下代码使用 keras 的 MobileNet 架构定义了一个模型:

model = MobileNet(input_shape=(size, size, 1), alpha=1.0, weights=None, classes=NCATS)
model.compile(optimizer=Adam(lr=0.002), loss='categorical_crossentropy',
              metrics=[categorical_crossentropy, categorical_accuracy, top_3_accuracy])

训练后,我保存了模型使用

model.save(MOD1_PATH)

我现在正在尝试使用

加载模型
model = load_model(MOD1_PATH)

但我在尝试加载时收到以下类型错误:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-26-2166dee60e7c> in <module>
----> 1 load_model(MOD1_PATH)

/opt/conda/lib/python3.6/site-packages/Keras-2.2.4-py3.6.egg/keras/engine/saving.py in load_model(filepath, custom_objects, compile)
    418     f = H5Dict(filepath, 'r')
    419     try:
--> 420         model = _deserialize_model(f, custom_objects, compile)
    421     finally:
    422         if opened_new_file:

/opt/conda/lib/python3.6/site-packages/Keras-2.2.4-py3.6.egg/keras/engine/saving.py in _deserialize_model(f, custom_objects, compile)
    224         raise ValueError('No model found in config.')
    225     model_config = json.loads(model_config.decode('utf-8'))
--> 226     model = model_from_config(model_config, custom_objects=custom_objects)
    227     model_weights_group = f['model_weights']
    228 

/opt/conda/lib/python3.6/site-packages/Keras-2.2.4-py3.6.egg/keras/engine/saving.py in model_from_config(config, custom_objects)
    457                         '`Sequential.from_config(config)`?')
    458     from ..layers import deserialize
--> 459     return deserialize(config, custom_objects=custom_objects)
    460 
    461 

/opt/conda/lib/python3.6/site-packages/Keras-2.2.4-py3.6.egg/keras/layers/__init__.py in deserialize(config, custom_objects)
     53                                     module_objects=globs,
     54                                     custom_objects=custom_objects,
---> 55                                     printable_module_name='layer')

/opt/conda/lib/python3.6/site-packages/Keras-2.2.4-py3.6.egg/keras/utils/generic_utils.py in deserialize_keras_object(identifier, module_objects, custom_objects, printable_module_name)
    143                     config['config'],
    144                     custom_objects=dict(list(_GLOBAL_CUSTOM_OBJECTS.items()) +
--> 145                                         list(custom_objects.items())))
    146             with CustomObjectScope(custom_objects):
    147                 return cls.from_config(config['config'])

/opt/conda/lib/python3.6/site-packages/Keras-2.2.4-py3.6.egg/keras/engine/network.py in from_config(cls, config, custom_objects)
   1020         # First, we create all layers and enqueue nodes to be processed
   1021         for layer_data in config['layers']:
-> 1022             process_layer(layer_data)
   1023         # Then we process nodes in order of layer depth.
   1024         # Nodes that cannot yet be processed (if the inbound node

/opt/conda/lib/python3.6/site-packages/Keras-2.2.4-py3.6.egg/keras/engine/network.py in process_layer(layer_data)
   1006 
   1007             layer = deserialize_layer(layer_data,
-> 1008                                       custom_objects=custom_objects)
   1009             created_layers[layer_name] = layer
   1010 

/opt/conda/lib/python3.6/site-packages/Keras-2.2.4-py3.6.egg/keras/layers/__init__.py in deserialize(config, custom_objects)
     53                                     module_objects=globs,
     54                                     custom_objects=custom_objects,
---> 55                                     printable_module_name='layer')

/opt/conda/lib/python3.6/site-packages/Keras-2.2.4-py3.6.egg/keras/utils/generic_utils.py in deserialize_keras_object(identifier, module_objects, custom_objects, printable_module_name)
    145                                         list(custom_objects.items())))
    146             with CustomObjectScope(custom_objects):
--> 147                 return cls.from_config(config['config'])
    148         else:
    149             # Then `cls` may be a function returning a class.

/opt/conda/lib/python3.6/site-packages/Keras-2.2.4-py3.6.egg/keras/engine/base_layer.py in from_config(cls, config)
   1107             A layer instance.
   1108         """
-> 1109         return cls(**config)
   1110 
   1111     def count_params(self):

/opt/conda/lib/python3.6/site-packages/Keras-2.2.4-py3.6.egg/keras/layers/advanced_activations.py in __init__(self, max_value, negative_slope, threshold, **kwargs)
    290                  threshold=0., **kwargs):
    291         super(ReLU, self).__init__(**kwargs)
--> 292         if max_value is not None and max_value < 0.:
    293             raise ValueError('max_value of ReLU layer '
    294                              'cannot be negative value: %s' % str(max_value))

TypeError: '<' not supported between instances of 'dict' and 'float'

以前有没有人在加载经过训练的模型时遇到过这种问题?任何帮助,将不胜感激。谢谢。

【问题讨论】:

  • 我尝试使用不同版本的 keras 加载模型,还尝试使用 tf.keras 加载它,但它们似乎都没有工作。如果有帮助,该模型是使用 keras 2.2.4 保存的。

标签: python keras typeerror


【解决方案1】:

我遇到了同样的问题:

张量流 1.11.0,

Keras 2.1.6-tf

我在 Google Colaboratory 中创建了一个笔记本,结果是一样的。 谷歌合作配置:

张量流 1.12.0-rc1,

Keras 2.1.6-tf

然后我尝试load_model

张量流 1.9.0,

Keras 2.1.6-tf

一切都开始起作用了。 可能这是 Tensorflow

新版本的错误

【讨论】:

    猜你喜欢
    • 2020-09-12
    • 1970-01-01
    • 2021-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-29
    • 2018-11-07
    • 1970-01-01
    相关资源
    最近更新 更多