如何从 Tensorflow.js (.json) 模型转换为 Tensorflow (SavedModel) 或 Tensorflow Lite (.tflite) 模型? [英] How to convert from Tensorflow.js (.json) model into Tensorflow (SavedModel) or Tensorflow Lite (.tflite) model?

查看:127
本文介绍了如何从 Tensorflow.js (.json) 模型转换为 Tensorflow (SavedModel) 或 Tensorflow Lite (.tflite) 模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经下载 来自 Google 的用于 Tensorflow.js (tfjs) 的预训练 PoseNet 模型,因此它是一个 json 文件.

I have downloaded a pre-trained PoseNet model for Tensorflow.js (tfjs) from Google, so its a json file.

但是,我想在 Android 上使用它,所以我需要 .tflite 模型.尽管有人将类似的模型从 tfjs 移植到 tflite 这里,我不知道他们转换了什么模型(PoseNet 有很多变体).我想自己做这些步骤.另外,我不想运行一些有人上传到 stackOverflow 文件中的任意代码:

However, I want to use it on Android, so I need the .tflite model. Although someone has 'ported' a similar model from tfjs to tflite here, I have no idea what model (there are many variants of PoseNet) they converted. I want to do the steps myself. Also, I don't want to run some arbitrary code someone uploaded into a file in stackOverflow:

注意:小心不受信任的代码——TensorFlow 模型就是代码.有关详细信息,请参阅安全使用 TensorFlow.Tensorflow 文档

Caution: Be careful with untrusted code—TensorFlow models are code. See Using TensorFlow Securely for details. Tensorflow docs

有谁知道这样做的方便方法吗?

Does anyone know any convenient ways to do this?

推荐答案

您可以通过查看 json 文件找出您拥有的 tfjs 格式.它经常说图形模型".它们之间的区别是这里.

You can find out what tfjs format you have by looking in the json file. It often says "graph-model". The difference between them are here.

通过 tfjs-to-tf/github.com/patlevin" rel="noreferrer">Patrick Levin.

Use tfjs-to-tf by Patrick Levin.

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

注意:这仅适用于层模型格式,而不适用于问题中的图形模型格式.我在这里写了它们之间的区别.


From tfjs layers model to SavedModel

Note: This will only work for layers model format, not graph model format as in the question. I've written the difference between them here.

  1. 安装并使用tensorflowjs-convert将.json文件转换成Keras HDF5文件(来自另一个SO线程).
  1. Install and use tensorflowjs-convert to convert the .json file into a Keras HDF5 file (from another SO thread).

在 Mac 上,您会遇到运行 pyenv 的问题(修复) 并且在 Z-shell 上,pyenv 将无法正确加载(修复).此外,一旦 pyenv 运行,请使用 python -m pip install tensorflowjs 而不是 pip install tensorflowjs,因为 pyenv 并没有为我更改 pip 使用的 python.

On mac, you'll face issues running pyenv (fix) and on Z-shell, pyenv won't load correctly (fix). Also, once pyenv is running, use python -m pip install tensorflowjs instead of pip install tensorflowjs, because pyenv did not change python used by pip for me.

一旦你遵循了tensorflowjs_converter 指南,运行 tensorflowjs_converter 以验证它是否可以正常工作错误,应该只是警告您 缺少 input_path 参数.然后:

Once you've followed the tensorflowjs_converter guide, run tensorflowjs_converter to verify it works with no errors, and should just warn you about Missing input_path argument. Then:

tensorflowjs_converter --input_format=tfjs_layers_model --output_format=keras tfjs_model.json hdf5_keras_model.hdf5

  1. 使用 TFLiteConverter.以下在 Python 文件中运行:
  1. Convert the Keras HDF5 file into a SavedModel (standard Tensorflow model file) or directly into .tflite file using the TFLiteConverter. The following runs in a Python file:

# 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:

or to save to a 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
)

这篇关于如何从 Tensorflow.js (.json) 模型转换为 Tensorflow (SavedModel) 或 Tensorflow Lite (.tflite) 模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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