簡體   English   中英

將 ONNX model 轉換為 TensorFlow Lite

[英]Converting ONNX model to TensorFlow Lite

我有一些ONNX Model Zoo的模型。 我想在 TensorFlow Lite (Android) 應用程序中使用此處的模型,但在弄清楚如何轉換模型時遇到了問題。

From what I've read, the process I need to follow is to convert the ONNX model to a TensorFlow model, then convert that TensorFlow model to a TensorFlow Lite model.

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 凍結圖。 從這里我不確定 go 在哪里。 當我搜索時,我找到了很多針對 TensorFlow 1.x 的答案(我只有在我發現無法執行的示例之后才意識到)。 我正在嘗試使用 TensorFlow 2.x。

如果重要的話,我開始使用的特定 model 就在這里

根據 ReadMe.md,輸入的形狀為 (1x3x416x416),output 的形狀為 (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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM