我有来自Google的下载为Tensorflow.js (tfjs)准备的PoseNet模型,所以它是json文件。
然而,我想在Android上使用它,所以我需要.tflite
模型。虽然有人将类似的模型从tfjs移植到tflite 这里,但我不知道他们转换了什么模型(PoseNet有很多变体)。我想自己做这些步骤。另外,我不想运行一些被上传到stackOverflow文件中的任意代码:
注意:对不可信的代码要小心--TensorFlow模型是代码。有关详细信息,请参阅安全使用TensorFlow。Tensorflow博士
有谁知道做这件事的方便方法吗?
发布于 2020-06-23 22:38:55
通过查看json文件,您可以了解tfjs的格式。它常说“图形模型”。它们之间的区别是这里。
从tfjs图形模型到SavedModel (更常见)
import tfjs_graph_converter.api as tfjs
tfjs.graph_model_to_saved_model(
"savedmodel/posenet/mobilenet/float/050/model-stride16.json",
"realsavedmodel"
)
# Code below taken from https://www.tensorflow.org/lite/convert/python_api
converter = tf.lite.TFLiteConverter.from_saved_model("realsavedmodel")
tflite_model = converter.convert()
# Save the TF Lite model.
with tf.io.gfile.GFile('model.tflite', 'wb') as f:
f.write(tflite_model)
从tfjs层模型到SavedModel
注意:--这只适用于图层模型格式,而不是问题中的图形模型格式。我写了他们之间的区别,这里。
在mac上,您将面临运行pyenv (修复)和Z-shell的问题,pyenv无法正确加载(修复)。而且,一旦pyenv运行,使用python -m pip install tensorflowjs
而不是pip install tensorflowjs
,因为pyenv没有为我更改pip使用的python。
一旦您遵循了转换器导轨,运行tensorflowjs_converter
来验证它是否正常工作,并且应该警告您注意Missing input_path argument
。然后:
tensorflowjs_converter --input_format=tfjs_layers_model --output_format=keras tfjs_model.json hdf5_keras_model.hdf5
.tflite
文件。以下内容在Python文件中运行:# Convert the model.
model = tf.keras.models.load_model('hdf5_keras_model.hdf5')
converter = tf.lite.TFLiteConverter.from_keras_model(model)
tflite_model = converter.convert()
# Save the TF Lite model.
with tf.io.gfile.GFile('model.tflite', 'wb') as f:
f.write(tflite_model)
或者保存到SavedModel中:
# Convert the model.
model = tf.keras.models.load_model('hdf5_keras_model.hdf5')
tf.keras.models.save_model(
model, filepath, overwrite=True, include_optimizer=True, save_format=None,
signatures=None, options=None
)
https://stackoverflow.com/questions/62544836
复制相似问题