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



我正在尝试使用将openimages_v4/ssd/mobilenet_v2转换为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模型会有帮助吗?如何找到用于训练该模型的原始代码或tensorflow版本?

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

编辑:在传递正确的标记和签名密钥后添加有关失败的详细信息。第2版:更新的样本代码

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

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

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

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。

使用TF选择模式可以支持其中一些操作,请参阅此处其他操作";HashTableV2、LookupTableFindV2、LookupTableImportV2";在TFLite中作为自定义操作提供。请参阅关于如何启用它的答案。

此外,TFLite团队正在努力添加对hashtable作为内置操作的支持,所以很快您就不需要执行额外的步骤了。

相关内容

  • 没有找到相关文章

最新更新