我已经使用来自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:
---------------------------------------------------------------------------
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'
有人在加载经过训练的模型之前遇到过这种问题吗?任何帮助,将不胜感激。谢谢。
答案 0 :(得分:0)
我在以下方面有同样的问题
Tensorflow 1.11.0 ,
Keras 2.1.6-tf
我在Google合作实验室创建了一个笔记本,结果是一样的。 Google相关配置:
Tensorflow 1.12.0-rc1 ,
Keras 2.1.6-tf
然后我尝试使用以下方法尝试load_model
:
Tensorflow 1.9.0 ,
Keras 2.1.6-tf
,一切开始起作用。 可能是新版本 Tensorflow
的错误