首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Keras未能加载SavedModel: TypeError‘模块’对象不可调用

Keras未能加载SavedModel: TypeError‘模块’对象不可调用
EN

Stack Overflow用户
提问于 2020-12-02 09:43:52
回答 2查看 2.5K关注 0票数 2

我使用使用MobileNet 2的TensorFlow对象检测API训练了一个SSD v2网络,然后将经过训练的模型转换为SavedModel。现在,我需要将SavedModel转换为FrozenGraph,以便使模型与外部库(如OpenCV )兼容。我使用这个例子进行转换,甚至无法加载Keras模型。

代码语言:javascript
复制
from keras.models import load_model
model = load_model("training/model/saved_model")

调用load_model()会产生一个异常:

代码语言:javascript
复制
Exception has occurred: TypeError
'module' object is not callable

Full traceback:
Traceback (most recent call last):
  File "path\to\tensorflow\python\util\dispatch.py", line 201, in wrapper
    return target(*args, **kwargs)
TypeError: 'str' object is not callable

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "path\to\saved_model_to_frozen_graph.py", line 17, in <module>
    model = load_model("saved_model")
  File "path\to\tensorflow\python\keras\saving\save.py", line 187, in load_model
    return saved_model_load.load(filepath, compile, options)
  File "path\to\tensorflow\python\keras\saving\saved_model\load.py", line 121, in load
    path, options=options, loader_cls=KerasObjectLoader)
  File "path\to\tensorflow\python\saved_model\load.py", line 633, in load_internal
    ckpt_options)
  File "path\to\tensorflow\python\keras\saving\saved_model\load.py", line 194, in __init__
    super(KerasObjectLoader, self).__init__(*args, **kwargs)
  File "path\to\tensorflow\python\saved_model\load.py", line 130, in __init__
    self._load_all()
  File "path\to\tensorflow\python\keras\saving\saved_model\load.py", line 221, in _load_all
    self._finalize_objects()
  File "path\to\tensorflow\python\keras\saving\saved_model\load.py", line 530, in _finalize_objects
    self._reconstruct_all_models()
  File "path\to\tensorflow\python\keras\saving\saved_model\load.py", line 548, in _reconstruct_all_models
    self._reconstruct_model(model_id, model, layers)
  File "path\to\tensorflow\python\keras\saving\saved_model\load.py", line 589, in _reconstruct_model
    config, created_layers={layer.name: layer for layer in layers})
  File "path\to\tensorflow\python\keras\engine\functional.py", line 1215, in reconstruct_from_config
    process_node(layer, node_data)
  File "path\to\tensorflow\python\keras\engine\functional.py", line 1163, in process_node
    output_tensors = layer(input_tensors, **kwargs)
  File "path\to\tensorflow\python\keras\engine\base_layer.py", line 926, in __call__
    input_list)
  File "path\to\tensorflow\python\keras\engine\base_layer.py", line 1117, in _functional_construction_call
    outputs = call_fn(cast_inputs, *args, **kwargs)
  File "path\to\tensorflow\python\keras\layers\core.py", line 903, in call
    result = self.function(inputs, **kwargs)
  File "path\to\tensorflow\python\util\dispatch.py", line 205, in wrapper
    result = dispatch(wrapper, args, kwargs)
TypeError: 'module' object is not callable

type(layer) = <class 'tensorflow.python.keras.layers.core.Lambda'>

有人知道怎么解决吗?

其他信息:

Windows 10、Python3.7、TensorFlow 2.3.1、Keras2.4.3

代码语言:javascript
复制
# Train the network
python model_main_tf2.py --model_dir="training" --pipeline_config_path="training/pipeline.config"
# Export the network
python exporter_main_v2.py --input_type=image_tensor --pipeline_config_path="training/pipeline.config" --trained_checkpoint_dir="training" --output_directory="training/model"

文件夹结构:

代码语言:javascript
复制
training/
-> label_map.pbtxt
-> pipeline.config
-> model
 . -> checkpoint
 . -> saved_model
 .  . -> assets
 .  . -> variables
 .  . -> saved_model.pb
-> checkpoint
-> ckpt.x.data-00000-of-00001
-> ckpt.x.index
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-01-04 11:09:47

我找到了一种不用Keras来转换模型的方法,而Keras是为我工作的。https://github.com/opencv/opencv/issues/16879

代码语言:javascript
复制
import tensorflow as tf
from tensorflow.python.framework.convert_to_constants import convert_variables_to_constants_v2

loaded = tf.saved_model.load('training/model/saved_model')
infer = loaded.signatures['serving_default']

f = tf.function(infer).get_concrete_function(input_tensor=tf.TensorSpec(shape=[1, None, None, 3], dtype=tf.uint8))
f2 = convert_variables_to_constants_v2(f)
graph_def = f2.graph.as_graph_def()

# Export frozen graph
with tf.io.gfile.GFile('frozen_graph.pb', 'wb') as f:
   f.write(graph_def.SerializeToString())
票数 3
EN

Stack Overflow用户

发布于 2021-04-08 14:26:07

原因可能是模型中的lambda层。

Keras中有一些问题可能会在加载带有lambda层的模型时引发TypeError(str is not callable or module is not callable)

为了解决这个问题,不要用model.save保存模型。相反,保存模型权重并使用代码重新创建模型体系结构,然后加载权重。

例子:

代码语言:javascript
复制
model.save_weights('model_weights.h5')

代码语言:javascript
复制
ModelCheckPoint(file,save_weights_only=True)#this will call save_weights

创建模型之后,然后:

代码语言:javascript
复制
model.load_weights('model_weights.h5')

注意:将模型体系结构保存为JSON文件不能解决错误

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65105594

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档