您可以通过查看 json 文件找出您的 tfjs 格式。它经常说“图形模型”。它们之间的区别是here。
从 tfjs 图模型到 SavedModel(比较常见)
由Patrick Levin 使用tfjs-to-tf。
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
注意:这仅适用于层模型格式,不适用于问题中的图形模型格式。我写了他们之间的区别here。
-
Install 并使用 tensorflowjs-convert 将
.json 文件转换为 Keras HDF5 文件(来自另一个 SO thread)。
在 mac 上,您将面临运行 pyenv (fix) 的问题,而在 Z-shell 上,pyenv 将无法正确加载 (fix)。此外,一旦 pyenv 运行,请使用 python -m pip install tensorflowjs 而不是 pip install tensorflowjs,因为 pyenv 并没有为我更改 pip 使用的 python。
一旦您关注了tensorflowjs_converter guide,运行tensorflowjs_converter 以验证它是否正常工作,并且应该只警告您Missing input_path argument。那么:
tensorflowjs_converter --input_format=tfjs_layers_model --output_format=keras tfjs_model.json hdf5_keras_model.hdf5
- 将 Keras HDF5 文件转换为 SavedModel(标准 Tensorflow 模型文件)或使用 TFLiteConverter 直接转换为
.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
)