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

Tensorflow 将预训练模型从tfhub转换为tflite,tensorflow,tensorflow-lite,Tensorflow,Tensorflow Lite,我正在尝试使用以下方法转换为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

我正在尝试使用以下方法转换为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()]
我还尝试了tensorflow 1.15.0,得到了相同的错误


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

标记的默认值为“服务”,签名密钥的默认值为“服务默认”。您可以使用python API中的标记参数覆盖它 看见

编辑: 在传递正确的标记和签名密钥后添加有关失败的详细信息。 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'])
现在,如果您尝试转换,将不会看到此问题,但会看到新问题:) 从错误消息日志中,总结中有两点

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

新问题是因为此模型使用的是TFLite目前不支持的ops。 例如,TensorArray、哈希表

使用TF选择模式可以支持其中一些操作,请参阅 其他操作“HashTableV2、LookupTableFindV2、LookupTableImportV2”在TFLite中作为自定义操作提供。 有关如何启用它,请参见此


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

您有保存的模型格式的模型吗?我建议您尝试使用Yeah,它是保存的模型格式,使用python接口时会出现相同的错误。没有答案,但有人建议您尝试“玩”一下:也许converter=tf.lite.TFLiteConverter。从保存的模型(保存的模型\u dir,签名\u key=['default'])将发挥神奇的作用。@AlexK。我试过了,但仍然得到
RuntimeError:在SavedModel中找不到与标记{'serve'}关联的MetaGraphDef。要检查SavedModel中的可用标记集,请使用SavedModel CLI:“saved_model_CLI”available_tags:[set()]
因为
saved_model_CLI
正在返回带有标记集的
MetaGraphDef:“
,我尝试使用
converter=tf.lite.TFLiteConverter。从\u saved_model(saved_model_dir,signature_keys=['default',],tags=set())
-这没有返回任何错误,因此这是一些进步。但是,
tflite\u model=converter.convert()
给出了一个错误:
E tensorflow/core/grappler/grappler\u item\u builder.cc:669]Init节点hub\u input/index\u to\u string/table\u Init在图形中不存在
您能用python在PC上从初始TF图形推断吗?也许是不直截了当的。我把答案编辑得更详细了。请检查一下。@KarimNosseir谢谢!我用tensorflow 2.4绕过了保存的模型,它确实走得更远了。添加TFLITE_内置项并选择_TF_OPS后,它只会抱怨TF.HashTableV2、TF.LookupTableFindV2和TF.LookupTableImportV2,但我不确定如何使用github问题中的代码。我贴了一封信。
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