Ben*_*rth 5 tensorflow tensorflow-lite tensorflow.js tensorflow2
我已经从谷歌下载了一个用于 Tensorflow.js (tfjs) 的预训练 PoseNet 模型,所以它是一个json文件。
但是,我想在Android上使用它,所以我需要.tflite模型。虽然有人在这里将类似的模型从 tfjs 移植到 tflite ,但我不知道他们转换了什么模型(PoseNet 有很多变体)。我想自己做这些步骤。另外,我不想运行一些有人上传到 stackOverflow 文件中的任意代码:
注意:小心不受信任的代码——TensorFlow 模型就是代码。有关详细信息,请参阅安全使用 TensorFlow。Tensorflow 文档
有谁知道任何方便的方法来做到这一点?
您可以通过查看 json 文件找出您拥有的 tfjs 格式。它经常说“图形模型”。他们的区别就在这里。
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)
Run Code Online (Sandbox Code Playgroud)
注意:这仅适用于图层模型格式,而不适用于问题中的图形模型格式。我已经在这里写下了它们之间的区别。
在 Mac 上,运行 pyenv ( fix ) 时会遇到问题,而在 Z-shell 上, pyenv 将无法正确加载 ( fix )。此外,一旦 pyenv 运行,请使用python -m pip install tensorflowjs代替pip install tensorflowjs,因为 pyenv 并没有为我更改 pip 使用的 python。
一旦你遵循了tensorflowjs_converter 指南,运行tensorflowjs_converter以验证它没有错误,并且应该只是警告你关于Missing input_path argument. 然后:
tensorflowjs_converter --input_format=tfjs_layers_model --output_format=keras tfjs_model.json hdf5_keras_model.hdf5
Run Code Online (Sandbox Code Playgroud)
.tflite使用TFLiteConverter直接转换为文件。以下在 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)
Run Code Online (Sandbox Code Playgroud)
或保存到 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
)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3213 次 |
| 最近记录: |