您可以通过查看 json 文件来了解您的 tfjs 格式。它经常说“图模型”。它们之间的区别是here.
从tfjs图模型到SavedModel(比较常见)
Use tfjs 到 tf by 帕特里克·莱文.
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
Note:这仅适用于图层模型格式,不适用于问题中的图形模型格式。我写了他们之间的区别here.
-
Install并使用tensorflowjs-convert来转换
.json
文件转换为 Keras HDF5 文件(来自另一个所以线程).
在 Mac 上,您将遇到运行 pyenv 的问题(fix)并且在 Z-shell 上,pyenv 将无法正确加载(fix)。另外,一旦 pyenv 运行,请使用python -m pip install tensorflowjs
代替pip install tensorflowjs
,因为 pyenv 没有为我更改 pip 使用的 python。
一旦您遵循了tensorflowjs_converter 指南, run tensorflowjs_converter
验证它的工作没有错误,并且应该只是警告你Missing input_path argument
. Then:
tensorflowjs_converter --input_format=tfjs_layers_model --output_format=keras tfjs_model.json hdf5_keras_model.hdf5
- 将 Keras HDF5 文件转换为 SavedModel(标准 Tensorflow 模型文件)或直接转换为
.tflite
文件使用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
)