【问题标题】:Error when Loading a .pb Tensorflow Model加载 .pb Tensorflow 模型时出错
【发布时间】:2021-01-13 09:04:16
【问题描述】:

我在 Windows 10 机器上创建了一个 tensorflow 模型并使用以下方法保存它:

model.save('myfolder')

在文件夹myfolder 我得到:

- saved_model.pb
- Variables folder
- Assets folder

现在我正在尝试在 Linux 机器中加载模型,所以我将文件夹路径作为模型路径传递给它:

model = tf.keras.models.load_model('myfolder')

它实际上检测到了模型,但抛出了这个巨大的错误:

2020-09-27 18:43:57.980054: W tensorflow/core/graph/graph_constructor.cc:772] Node 'cond/while' has 13 outputs but the _output_shapes attribute specifies shapes for 44 outputs. Output shapes may be inaccurate.
2020-09-27 18:44:02.211495: W tensorflow/core/graph/graph_constructor.cc:772] Node 'cond/while' has 13 outputs but the _output_shapes attribute specifies shapes for 44 outputs. Output shapes may be inaccurate.
2020-09-27 18:44:02.376792: W tensorflow/core/graph/graph_constructor.cc:772] Node 'cond' has 5 outputs but the _output_shapes attribute specifies shapes for 45 outputs. Output shapes may be inaccurate.
2020-09-27 18:44:16.361762: W tensorflow/core/graph/graph_constructor.cc:772] Node 'cond' has 5 outputs but the _output_shapes attribute specifies shapes for 45 outputs. Output shapes may be inaccurate.
2020-09-27 18:44:18.184022: W tensorflow/core/graph/graph_constructor.cc:772] Node 'cond/while' has 13 outputs but the _output_shapes attribute specifies shapes for 44 outputs. Output shapes may be inaccurate.
2020-09-27 18:44:18.331177: W tensorflow/core/graph/graph_constructor.cc:772] Node 'cond' has 5 outputs but the _output_shapes attribute specifies shapes for 45 outputs. Output shapes may be inaccurate.
2020-09-27 18:44:19.087747: W tensorflow/core/graph/graph_constructor.cc:772] Node 'cond/while' has 13 outputs but the _output_shapes attribute specifies shapes for 44 outputs. Output shapes may be inaccurate.
2020-09-27 18:44:22.933245: W tensorflow/core/graph/graph_constructor.cc:772] Node 'cond/while' has 13 outputs but the _output_shapes attribute specifies shapes for 44 outputs. Output shapes may be inaccurate.
2020-09-27 18:44:23.074951: W tensorflow/core/graph/graph_constructor.cc:772] Node 'cond' has 5 outputs but the _output_shapes attribute specifies shapes for 45 outputs. Output shapes may be inaccurate.
2020-09-27 18:44:28.451176: W tensorflow/core/graph/graph_constructor.cc:772] Node 'cond/while' has 13 outputs but the _output_shapes attribute specifies shapes for 44 outputs. Output shapes may be inaccurate.
2020-09-27 18:44:28.597107: W tensorflow/core/graph/graph_constructor.cc:772] Node 'cond' has 5 outputs but the _output_shapes attribute specifies shapes for 45 outputs. Output shapes may be inaccurate.
2020-09-27 18:44:33.579605: W tensorflow/core/graph/graph_constructor.cc:772] Node 'cond/while' has 13 outputs but the _output_shapes attribute specifies shapes for 44 outputs. Output shapes may be inaccurate.
2020-09-27 18:44:33.742273: W tensorflow/core/graph/graph_constructor.cc:772] Node 'cond' has 5 outputs but the _output_shapes attribute specifies shapes for 45 outputs. Output shapes may be inaccurate.
2020-09-27 18:44:51.043732: W tensorflow/core/graph/graph_constructor.cc:772] Node 'cond/while' has 13 outputs but the _output_shapes attribute specifies shapes for 44 outputs. Output shapes may be inaccurate.
2020-09-27 18:44:57.151346: W tensorflow/core/graph/graph_constructor.cc:772] Node 'cond' has 5 outputs but the _output_shapes attribute specifies shapes for 45 outputs. Output shapes may be inaccurate.
2020-09-27 18:45:00.168905: W tensorflow/core/graph/graph_constructor.cc:772] Node 'cond/while' has 13 outputs but the _output_shapes attribute specifies shapes for 44 outputs. Output shapes may be inaccurate.
2020-09-27 18:45:00.312153: W tensorflow/core/graph/graph_constructor.cc:772] Node 'cond' has 5 outputs but the _output_shapes attribute specifies shapes for 45 outputs. Output shapes may be inaccurate.
2020-09-27 18:45:01.051383: W tensorflow/core/graph/graph_constructor.cc:772] Node 'cond/while' has 13 outputs but the _output_shapes attribute specifies shapes for 44 outputs. Output shapes may be inaccurate.
2020-09-27 18:45:01.190471: W tensorflow/core/graph/graph_constructor.cc:772] Node 'cond' has 5 outputs but the _output_shapes attribute specifies shapes for 45 outputs. Output shapes may be inaccurate.
2020-09-27 18:45:01.395016: W tensorflow/core/graph/graph_constructor.cc:772] Node 'cond/while' has 13 outputs but the _output_shapes attribute specifies shapes for 44 outputs. Output shapes may be inaccurate.
2020-09-27 18:45:01.542457: W tensorflow/core/graph/graph_constructor.cc:772] Node 'cond' has 5 outputs but the _output_shapes attribute specifies shapes for 45 outputs. Output shapes may be inaccurate.
2020-09-27 18:45:05.023475: W tensorflow/core/graph/graph_constructor.cc:772] Node 'cond/while' has 13 outputs but the _output_shapes attribute specifies shapes for 44 outputs. Output shapes may be inaccurate.
2020-09-27 18:45:05.235829: W tensorflow/core/graph/graph_constructor.cc:772] Node 'cond' has 5 outputs but the _output_shapes attribute specifies shapes for 45 outputs. Output shapes may be inaccurate.
2020-09-27 18:45:05.568045: W tensorflow/core/graph/graph_constructor.cc:772] Node 'cond' has 5 outputs but the _output_shapes attribute specifies shapes for 45 outputs. Output shapes may be inaccurate.
2020-09-27 18:45:06.300144: W tensorflow/core/graph/graph_constructor.cc:772] Node 'cond/while' has 13 outputs but the _output_shapes attribute specifies shapes for 44 outputs. Output shapes may be inaccurate.
2020-09-27 18:45:06.439828: W tensorflow/core/graph/graph_constructor.cc:772] Node 'cond' has 5 outputs but the _output_shapes attribute specifies shapes for 45 outputs. Output shapes may be inaccurate.
2020-09-27 18:45:11.585829: W tensorflow/core/graph/graph_constructor.cc:772] Node 'cond/while' has 13 outputs but the _output_shapes attribute specifies shapes for 44 outputs. Output shapes may be inaccurate.
2020-09-27 18:45:11.729289: W tensorflow/core/graph/graph_constructor.cc:772] Node 'cond' has 5 outputs but the _output_shapes attribute specifies shapes for 45 outputs. Output shapes may be inaccurate.
Traceback (most recent call last):
  File "main.py", line 95, in <module>
    model = tf.keras.models.load_model('model')
  File "/home/.local/lib/python3.7/site-packages/tensorflow_core/python/keras/saving/save.py", line 150, in load_model
    return saved_model_load.load(filepath, compile)
  File "/home/.local/lib/python3.7/site-packages/tensorflow_core/python/keras/saving/saved_model/load.py", line 89, in load
    model = tf_load.load_internal(path, loader_cls=KerasObjectLoader)
  File "/home/.local/lib/python3.7/site-packages/tensorflow_core/python/saved_model/load.py", line 541, in load_internal
    export_dir)
  File "/home/.local/lib/python3.7/site-packages/tensorflow_core/python/keras/saving/saved_model/load.py", line 118, in __init__
    super(KerasObjectLoader, self).__init__(*args, **kwargs)
  File "/home/.local/lib/python3.7/site-packages/tensorflow_core/python/saved_model/load.py", line 121, in __init__
    self._load_all()
  File "/home/.local/lib/python3.7/site-packages/tensorflow_core/python/saved_model/load.py", line 239, in _load_all
    node, setter = self._recreate(proto)
  File "/home/.local/lib/python3.7/site-packages/tensorflow_core/python/saved_model/load.py", line 322, in _recreate
    return factory[kind]()
  File "/home/.local/lib/python3.7/site-packages/tensorflow_core/python/saved_model/load.py", line 309, in <lambda>
    "user_object": lambda: self._recreate_user_object(proto.user_object),
  File "/home/.local/lib/python3.7/site-packages/tensorflow_core/python/saved_model/load.py", line 328, in _recreate_user_object
    return self._recreate_base_user_object(proto)
  File "/home/.local/lib/python3.7/site-packages/tensorflow_core/python/keras/saving/saved_model/load.py", line 216, in _recreate_base_user_object
    return revived_cls._init_from_metadata(metadata)  # pylint: disable=protected-access
  File "/home/.local/lib/python3.7/site-packages/tensorflow_core/python/keras/saving/saved_model/load.py", line 297, in _init_from_metadata
    revived_obj = cls(**init_args)
  File "/home/.local/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/input_layer.py", line 86, in __init__
    batch_size = batch_input_shape[0]
KeyError: 0

我已经在 GitHub 的 Tensorflow 问题中进行了搜索,但我还没有发现任何有趣的东西。我看到SO answer 建议使用已加载模型的重量创建一个新模型,但我什至无法执行此操作,因为加载时出现错误。

【问题讨论】:

    标签: python tensorflow keras tf.keras


    【解决方案1】:

    我刚刚解决了同样的问题。

    检查您用于训练和保存模型的 Tensorflow 版本以及您用于加载模型的版本。它们必须相同,或者至少是compatible versions

    【讨论】:

    • 我将 'compile=False' 作为参数添加到 ''tf.keras.models.load_model()'' 并且似乎正在工作,我仍在试图理解为什么会发生这种情况(我是使用相同版本的 TF 训练和预测)
    • @DRRorresRuiz 我有同样的问题....我还在保存和加载环境中检查了 tensorflow 版本。它们是相同的版本。
    猜你喜欢
    • 2020-05-23
    • 1970-01-01
    • 1970-01-01
    • 2022-06-21
    • 2019-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-06
    相关资源
    最近更新 更多