[英]Convert TF 2 saved model to frozen graph - no attribute model.inputs[0]
我通过 TF 2.4(w/CUDA 11.0、Python 3.7)/models/research/object_detection 教程创建/训练了 model。 没有错误,似乎可以正常运行 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))
output:
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))
output:
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'
此外,model cli 似乎有效:
!saved_model_cli show --dir {model_path} --all
缩写为 output:
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')
我的 model 是坏的还是我在这里做错了什么? 我应该使用 tf.keras 来加载 model 吗?
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
https://github.com/tensorflow/tensorflow/issues/43527
如果您尝试将 saved_graph.pb 转换为用于推理的冻结图 - 您需要关注问题 #43527
您可以使用 Keras 来帮助您获得冻结图,但是(截至 2021 年 1 月 4 日),这不起作用,您将遇到问题 43527,如前所述。
有一个解决方法 - 不使用 Keras。Go 通过 colab 教程:tensorflow/models/research/object_detection/colab_tutorials/
具体来说,go 通过:inference_from_saved_model_tf2_colab.ipynb 稍作修改,这将在本地运行 - 您不必在 colab 上运行。 这很好用,它会向您展示使用 model 而没有 Keras 问题的模式。
你可以改变
full_model = full_model.get_concrete_function(
tf.TensorSpec(model.inputs[0].shape, model.inputs[0].dtype))
到
full_model = full_model.get_concrete_function(
tf.TensorSpec(model.signatures['serving_default'].inputs[0].shape.as_list(), model.signatures['serving_default'].inputs[0].dtype.name))
. 详情请见https://github.com/tensorflow/models/issues/8966#issuecomment-1017052562 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.