将预训练模型从 tfhub 转换为 tflite

2024-04-29

我正在尝试转换openimages_v4/ssd/mobilenet_v2 https://tfhub.dev/google/openimages_v4/ssd/mobilenet_v2/1到 tflite 使用:

$ pip3 install tensorflow==2.4.0
$ tflite_convert --saved_model_dir=openimages_v4_ssd_mobilenet_v2_1 --output_file=/tmp/openimages_v4_ssd_mobilenet_v2_1.tflite

但它给出了这个错误:

<stacktrace snipped ..>
RuntimeError: MetaGraphDef associated with tags {'serve'} could not be found in SavedModel. To inspect available tag-sets in the SavedModel, please use the SavedModel CLI: `saved_model_cli`
available_tags: [set()]

输出来自saved_model_cli:

# saved_model_cli show --dir openimages_v4_ssd_mobilenet_v2_1 --all
2021-01-09 23:32:57.635704: W tensorflow/stream_executor/platform/default/dso_loader.cc:60] Could not load dynamic library 'libcudart.so.11.0'; dlerror: libcudart.so.11.0: cannot open shared object file: No such file or directory
2021-01-09 23:32:57.635772: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.

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

signature_def['default']:
  The given SavedModel SignatureDef contains the following input(s):
    inputs['images'] tensor_info:
        dtype: DT_FLOAT
        shape: (-1, -1, -1, 3)
        name: hub_input/image_tensor:0
  The given SavedModel SignatureDef contains the following output(s):
    outputs['detection_boxes'] tensor_info:
        dtype: DT_FLOAT
        shape: (-1, 4)
        name: hub_input/strided_slice:0
    outputs['detection_class_entities'] tensor_info:
        dtype: DT_STRING
        shape: (-1, 1)
        name: hub_input/index_to_string_Lookup:0
    outputs['detection_class_labels'] tensor_info:
        dtype: DT_INT64
        shape: (-1, 1)
        name: hub_input/strided_slice_2:0
    outputs['detection_class_names'] tensor_info:
        dtype: DT_STRING
        shape: (-1, 1)
        name: hub_input/index_to_string_1_Lookup:0
    outputs['detection_scores'] tensor_info:
        dtype: DT_FLOAT
        shape: (-1, 1)
        name: hub_input/strided_slice_1:0
  Method name is:

我也尝试使用tensorflow 1.15.0并得到同样的错误。

使用较新版本的 TensorFlow 重新训练 openimages_v4/ssd/mobilenet_v2 模型是否有帮助?如何找到用于训练该模型的原始代码或张量流版本?


标签的默认值为“serve”,signature_keys 的默认值为“serving_default”。您可以使用 python API 中的标签参数覆盖它 看https://www.tensorflow.org/lite/api_docs/python/tf/lite/TFLiteConverter#from_saved_model https://www.tensorflow.org/lite/api_docs/python/tf/lite/TFLiteConverter#from_saved_model

编辑: 在传递正确的标签和签名密钥后添加有关失败的详细信息。 EDIT2:更新了示例代码

这看起来像一个旧模型。它是使用旧版本保存的。

首先,让我们解决这个保存的模型版本问题。 你需要重新保存

MODEL_DIR = 'model_path'
SIGNATURE_KEYS = ['default']
SIGNATURE_TAGS = set()
saved_model = tf.saved_model.load(MODEL_DIR, tags=SIGNATURE_TAGS)
tf.saved_model.save(saved_model, 'new_model_path', signatures=saved_model.signatures)
# You can now convert like this.
converter = tf.lite.TFLiteConverter.from_saved_model(
      'new_model_path', signature_keys=SIGNATURE_KEYS, tags=['serve'])

现在,如果您尝试转换,您将不会看到此问题,但您会看到新问题:) 从错误信息日志总结有2点

Some ops are not supported by the native TFLite runtime, you can enable TF kernels fallback using TF Select. See instructions: https://www.tensorflow.org/lite/guide/ops_select
Flex ops: TensorArrayGatherV3, TensorArrayReadV3, TensorArrayScatterV3, TensorArraySizeV3, TensorArrayV3, TensorArrayWriteV3

and

Some ops in the model are custom ops, See instructions to implement custom ops: https://www.tensorflow.org/lite/guide/ops_custom
Custom ops: HashTableV2, LookupTableFindV2, LookupTableImportV2

新问题是因为该模型正在使用 TFLite 目前不支持的操作。 例如,TensorArray、哈希表。

其中一些操作可以使用 TF 选择模式来支持,请参阅here https://www.tensorflow.org/lite/guide/ops_select其他操作“HashTableV2、LookupTableFindV2、LookupTableImportV2”在 TFLite 中可作为自定义操作使用。 看到这个answer https://github.com/tensorflow/tensorflow/issues/37844#issuecomment-605336360关于如何启用它。

此外,TFLite 团队正在努力添加对哈希表作为内置操作的支持,因此很快您将不需要执行额外的步骤。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

将预训练模型从 tfhub 转换为 tflite 的相关文章

随机推荐