将预训练模型从 tfhub 转换为 tflite [英] Converting pretrained model from tfhub to tflite

查看:61
本文介绍了将预训练模型从 tfhub 转换为 tflite的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 openimages_v4/ssd/mobilenet_v2 转换为 tflite使用:

I'm trying to convert openimages_v4/ssd/mobilenet_v2 to tflite using:

$ 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 并得到同样的错误.

I also tried with tensorflow 1.15.0 and got the same error.

使用较新版本的 tensorflow 重新训练 openimages_v4/ssd/mobilenet_v2 模型会有帮助吗?如何找到用于训练该模型的原始代码或 tensorflow 版本?

Would retraining the openimages_v4/ssd/mobilenet_v2 model with a newer version of tensorflow help? How can I find the original code or tensorflow version used to train that model?

推荐答案

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

The default value for tags is 'serve', and the default value for signature_keys is 'serving_default'. You can override it using the tags param in the python API See https://www.tensorflow.org/lite/api_docs/python/tf/lite/TFLiteConverter#from_saved_model

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

Adding details on the failure after passing the correct tags and signature keys. Updated sample code

这看起来像一个旧模型.使用旧版本保存.

This looks like an old model. It is saved using an old version.

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

First, let's fix this saved model version issue. You need to re-save it

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点

Now if you tried converting, you won't see this problem, but you will see new issue :) From the error message log there are 2 points in the summary

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

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、Hashtables.

The new issue is because this model is using ops that TFLite doesn't support at the moment. Example, TensorArray,Hashtables.

使用 TF 选择模式可以支持其中一些操作,请参阅此处其他操作HashTableV2、LookupTableFindV2、LookupTableImportV2"可在 TFLite 中作为自定义操作使用.请参阅此answer了解如何启用它.

Some of these ops can be supported using TF select mode, see here The other ops "HashTableV2, LookupTableFindV2, LookupTableImportV2" are available in TFLite as custom ops. See this answer on how to enable it.

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

Also, TFLite team is working on adding support for hashtable as builtin ops, so soon you won't need to do the extra steps.

这篇关于将预训练模型从 tfhub 转换为 tflite的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆