将 TF 2 保存的模型转换为冻结图 - 无属性 model.inputs[0]

时间:2020-12-22 16:32:00

标签: python tensorflow

我通过 TF 2.4(带有 CUDA 11.0、Python 3.7)创建/训练了一个模型 /models/research/object_detection 教程。没有错误,似乎可以正常运行 25000 步。一切看起来都很正常,Tensorboard 显示总损失 < 0.5。它根据教程生成了一个 saved_model.pb。我现在想转换为冻结图以进行推理。

它似乎加载良好(此代码在 Jupyter notebook 中运行):

!ls {model_path} -l
model = tf.compat.v2.saved_model.load(export_dir=model_path)
print (type(model))

输出:

total 13232
drwxr-xr-x 2 jay jay     4096 Dec 21 10:41 assets
-rw-r--r-- 1 jay jay 13538598 Dec 21 10:41 saved_model.pb
drwxr-xr-x 2 jay jay     4096 Dec 21 10:41 variables
<class 'tensorflow.python.saved_model.load.Loader._recreate_base_user_object.<locals>._UserObject'>

但是,当我开始转换它时,出现错误

full_model = tf.function(lambda x: model(x))
full_model = full_model.get_concrete_function(
    tf.TensorSpec(model.inputs[0].shape, model.inputs[0].dtype))

输出:

AttributeError                            Traceback (most recent call last)
<ipython-input-73-50e1947f8357> in <module>
      2 full_model = tf.function(lambda x: model(x))
      3 full_model = full_model.get_concrete_function(
----> 4     tf.TensorSpec(model.inputs[0].shape, model.inputs[0].dtype))

AttributeError: '_UserObject' object has no attribute 'inputs'

此外,模型 cli 似乎有效:

!saved_model_cli show --dir {model_path} --all

缩写输出:

2020-12-22 11:38:23.453843: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library libcudart.so.11.0

MetaGraphDef with tag-set: 'serve' contains the following SignatureDefs:

signature_def['__saved_model_init_op']:
  The given SavedModel SignatureDef contains the following input(s):
  The given SavedModel SignatureDef contains the following output(s):
    outputs['__saved_model_init_op'] tensor_info:
        dtype: DT_INVALID
        shape: unknown_rank
        name: NoOp
  Method name is: 

signature_def['serving_default']:
  The given SavedModel SignatureDef contains the following input(s):
    inputs['input_tensor'] tensor_info:
        dtype: DT_UINT8
        shape: (1, -1, -1, 3)
        name: serving_default_input_tensor:0

<content removed for brevity>

Defined Functions:
  Function Name: '__call__'
    Option #1
      Callable with:
        Argument #1
          input_tensor: TensorSpec(shape=(1, None, None, 3), dtype=tf.uint8, name='input_tensor')

是我的模型不好还是我在这里做错了什么?我应该使用 tf.keras 加载模型吗?

tf.keras.models.load_model(model_path, custom_objects=None, compile=True, options=None)

当我使用 tf.keras 时,我收到加载错误:

~/anaconda3/envs/tf24/lib/python3.7/site-packages/tensorflow/python/keras/saving/saved_model/load.py in infer_inputs_from_restored_call_function(fn)
    980     return tensor_spec.TensorSpec(defun.common_shape(x.shape, y.shape),
    981                                   x.dtype, x.name)
--> 982   spec = fn.concrete_functions[0].structured_input_signature[0][0]
    983   for concrete in fn.concrete_functions[1:]:
    984     spec2 = concrete.structured_input_signature[0][0]

IndexError: list index out of range

2 个答案:

答案 0 :(得分:0)

  1. 使用 tf.keras.models.load_model()
  2. 但是(截至 20201226),它似乎不起作用

https://github.com/tensorflow/tensorflow/issues/43527

如果您尝试将saved_graph.pb 转换为用于推理的frozn 图 - 您需要遵循问题#43527

答案 1 :(得分:0)

您可以使用 Keras 来帮助您获得冻结图,但是(截至 2021.01.04),这不起作用,您将遇到如上所述的问题 43527。

有一个解决方法 - 不使用 Keras。浏览 colab 教程: 张量流/模型/研究/object_detection/colab_tutorials/

具体通过:inference_from_saved_model_tf2_colab.ipynb 通过小的编辑,这将在本地运行 - 您不必在 colab 上运行。这很有效,它会向您展示在没有 Keras 问题的情况下使用模型的模式。