简体   繁体   English

拆分 ONNX DNN 模型

[英]Splitting an ONNX DNN Model

I'm trying to split DNN Models in order to execute part of the network on the edge and the rest on the cloud .我正在尝试拆分DNN 模型,以便在边缘执行部分网络,而在上执行其余部分。 Because it has to be cross-platform and work with every framework I need to do it directly starting from an ONNX model .因为它必须是跨平台的并且可以与每个框架一起工作,所以我需要直接从ONNX模型开始。

I know how to generate an ONNX model starting from tensorflow/keras and how to run an ONNX model, but I realized that is really hard to work on the ONNX file, like visualizing it and modify it.我知道如何从 tensorflow/keras 开始生成 ONNX 模型以及如何运行 ONNX 模型,但我意识到在 ONNX 文件上工作真的很难,比如可视化和修改它。

Is there someone that can help me understand how to split and ONNX model, or at least run part of an ONNX model (like from input to layer N and from layer N to the output)?有没有人可以帮助我理解如何拆分和 ONNX 模型,或者至少运行 ONNX 模型的一部分(例如从输入到 N 层以及从 N 层到输出)?

I'm starting from this situation:我从这种情况开始:

# load MobileNetV2 model
model = MobileNetV2()

# Export the model
tf.saved_model.save(model, "saved_model")  

# export to .onnx
!python -m tf2onnx.convert --saved-model saved_model --output mobilenet_v2.onnx --opset 7

# open the saved ONNX Model
print("Import ONNX Model..")
onnx_model = onnx.load("mobilenet_v2.onnx")
tf_rep = prepare(onnx_model, logging_level="WARN", auto_cast=True)

I tried to use sclblonnx but on models this big(although it's a small model) I can't really print the graph and when I list the inputs and outputs with textlist_inputs/list_outputs I don't really get how ther are interconnected.我尝试使用sclblonnx但在这么大的模型上(虽然它是一个小模型)我无法真正打印图表,当我使用 textlist_inputs/list_outputs 列出输入和输出时,我真的不知道它们是如何相互连接的。

Any help would be greatly appreciated.任何帮助将不胜感激。 Thank you in advance.先感谢您。

Onnx PythonAPI 规范中,您可以通过指定张量的输入名称和输出名称来拆分 onnx 模型。

The first thing you probably need to do is understand the underlining graph for the onnx model you have.您可能需要做的第一件事是了解您拥有的 onnx 模型的下划线

onnx_graph = onnx_model.graph

Will return the graph object.将返回图形对象。

After that, you need to understand where you want to separate this graph into two separate graphs (and so run two models).之后,您需要了解您想在哪里将此图分成两个单独的图(并运行两个模型)。

You can plot the graph with Netron (this is what sclblonnx does) or you can try to look inside manually by looking at您可以使用 Netron 绘制图形(这是 sclblonnx 所做的),或者您可以尝试通过查看手动查看内部

onnx_graph_nodes = onnx_graph.node

Of course looking at the graph inputs( onnx_graph.input ) and outputs ( onnx_graph.output ) is also important.当然,查看图形输入( onnx_graph.input )和输出( onnx_graph.output )也很重要。

If you look at the " merge " file from sclblonnx you will see the syntax details for diving into a graph as well as a "split" function at may help you.如果您查看来自 sclblonnx 的“ 合并”文件,您将看到用于深入图表的语法详细信息以及可能对您有所帮助的“拆分”功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM