我有一些用于ONNX Model Zoo的模型。我想在TensorFlow Lite(Android)应用程序中使用此处的模型,但是在弄清楚如何转换模型时遇到了问题。
从我的阅读中,我需要遵循的过程是将ONNX模型转换为TensorFlow模型,然后将该TensorFlow模型转换为TensorFlow Lite模型。
import onnx
from onnx_tf.backend import prepare
import tensorflow as tf
onnx_model = onnx.load('./some-model.onnx')
tf_rep = prepare(onnx_model)
tf_rep.export_graph("some-model.pb")
执行完上述操作后,我有一个文件some-model.pb,我相信其中包含一个TensorFlow冻结图。从这里我不确定要去哪里。当我搜索时,我发现很多针对TensorFlow 1.x的答案(只有在发现示例后我才意识到执行该答案)。我正在尝试使用TensorFlow 2.x.
如果有关系,这里是我要开始使用的特定模型。
根据ReadMe.md,输入形状为(1x3x416x416),输出形状为(1x125x13x13)。
我知道了 我可以使用下面的代码完成转换。
import tensorflow as tf
converter = tf.compat.v1.lite.TFLiteConverter.from_frozen_graph('model.pb', #TensorFlow freezegraph
input_arrays=['input.1'], # name of input
output_arrays=['218'] # name of output
)
converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS,
tf.lite.OpsSet.SELECT_TF_OPS]
# tell converter which type of optimization techniques to use
converter.optimizations = [tf.lite.Optimize.DEFAULT]
tf_lite_model = converter.convert()
open('model.tflite', 'wb').write(tf_lite_model)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句