纠正pb文件以将Tensorflow模型移至ML.NET [英] Correct pb file to move Tensorflow model into ML.NET

查看:208
本文介绍了纠正pb文件以将Tensorflow模型移至ML.NET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自己构建的TensorFlow模型(一个1D CNN),现在想在.NET中实现.
为此,我需要知道Input和Output节点.
当我在

如果我将模型另存为h5 model.save("Mn_pb_model.h5")并将其加载到Netron中以图形方式显示,则一切看起来正确:

但是,ML.NET将不接受h5格式,因此需要将其另存为pb.

在查看在ML.NET中采用TensorFlow的示例时,此

然后放大一点(在最右边),

如您所见,它看起来并不像应有的样子.
另外,输入节点和输出节点不正确,因此它不适用于ML.NET(我认为有些问题).
我正在使用TensorFlow中的

当我尝试获取

有四个原因我认为这是不正确的.

  • 当它作为h5上传时,它与图表有很大的不同(对我来说是正确的).
  • 正如您从前面看到的那样,我在整个过程中都使用1D卷积,这表明它已转为2D(并保持这种状态).
  • 此文件大小为128MB,而TensorFlow to ML.NET示例中的文件大小仅为252KB.甚至

    解决方案

    此答案由3部分组成:

    • 浏览其他程序
    • 不要通过其他程序
    • 操作级图概念图之间的差异(以及Netron为什么向您显示不同的图)

    1.通过其他程序:

    ML.net需要一个ONNX模型,而不是一个pb文件.

    有多种方法可以将模型从TensorFlow转换为可以在ML.net中加载的ONNX模型:

    此SO帖子也可以为您提供帮助:使用ML加载模型.NET与keras一起保存

    在这里,您将找到有关h5和pb文件格式,它们包含的内容等的更多信息. https://www.tensorflow.org/guide/keras/save_and_serialize#weights_only_saving_in_savedmodel_format

    2.但是您在问"TensorFlow->".ML.NET,而无需通过其他程序":

    2.A问题概述:

    首先,使用您提供的代码制作的 pl 文件格式,从您所说的来看,似乎与您在注释中提到的示例中使用的格式不同( https://docs.microsoft.com/zh-我们/dotnet/machine-learning/tutorials/text-classification-tf )

    是否可以尝试使用将通过 tf.saved_model.save 生成的 pb 文件?工作正常吗?

    有关此Microsoft博客文章的想法:

    来自

    和:

    您在图表中看到的TensorFlow .pb模型文件(以及labels.txt代码/ID)是您在Azure Cognitive中创建/培训的内容然后将Services Custom Vision导出为冻结的TensorFlow模型文件由ML.NET C#代码使用.

    因此,此 pb 文件是一种由Azure认知服务自定义视觉生成的文件.Perharps您也可以尝试这种方式吗?

    2.B现在,我们将尝试提供解决方案:

    实际上,在TensorFlow 1.x中,您可以使用 freeze_graph 轻松保存冻结的图.

    但是TensorFlow 2.x不支持 freeze_graph converter_variables_to_constants .

    您也可以在此处阅读一些有用的信息: Tensorflow 2.0:冻结图支持

    一些用户想知道如何在TF 2.x中进行操作:如何在tensorflow 2.0中冻结图形( https://leimao.github.io/blog/Save-Load-Inference-From-TF2-Frozen-Graph/

    如何将Keras模型保存为冻结图?(虽然已经在您的问题中链接了)

    操作级图概念图之间的差异(以及Netron为什么向您显示不同的图):

    正如@ mlneural03在对您的问题的评论中所说,Netron会根据您提供的文件格式显示不同的图形:

    • 如果加载h5文件,Netron将显示概念图
    • 如果加载pb文件,Netron将显示操作级图

    操作级图和概念图之间有什么区别?

    • 在TensorFlow中,操作级别图的节点表示操作("ops"),例如tf.add,tf.matmul,tf.linalg.inv等.
    • 概念图将向您显示模型的结构.

    那是完全不同的事情.

    "ops"是操作"的缩写.操作是执行计算的节点.

    因此,这就是为什么在Netron中加载pb fil时,会得到一个带有很多节点的非常大的图的原因:您会看到该图的所有计算节点.但是当您在Netron中加载h5文件时,您只是"查看模型的结构,模型的设计.

    在TensorFlow中,您可以使用TensorBoard查看图形:

    • 默认情况下,TensorBoard显示操作级图.
    • 要查看概念图,请在TensorBoard中选择"keras"标签.

    这里有一个Jupyter笔记本,非常清楚地说明了操作级图和概念图之间的区别: https://github.com/tensorflow/tensorflow/issues/39699

    简而言之:

    实际上没有问题,只是有点误会(没关系,我们不能一无所知).

    在Netron中加载 h5 文件和 pb 文件时,您希望看到相同的图形,但是必须失败,因为文件不包含相同的图形.这些图是显示同一模型的两种方式.

    用我们描述的方法创建的pb文件将是正确的pb文件,可以加载ML.NET,如我们所讨论的Microsoft教程中所述.因此,如果按照这些教程中的说明加载正确的 pb 文件,则将加载真实/真实模型.

    I have a TensorFlow model that I built (a 1D CNN) that I would now like to implement into .NET.
    In order to do so I need to know the Input and Output nodes.
    When I uploaded the model on Netron I get a different graph depending on my save method and the only one that looks correct comes from an h5 upload. Here is the model.summary():

    If I save the model as an h5 model.save("Mn_pb_model.h5") and load that into the Netron to graph it, everything looks correct:

    However, ML.NET will not accept h5 format so it needs to be saved as a pb.

    In looking through samples of adopting TensorFlow in ML.NET, this sample shows a TensorFlow model that is saved in a similar format to the SavedModel format - recommended by TensorFlow (and also recommended by ML.NET here "Download an unfrozen [SavedModel format] ..."). However when saving and loading the pb file into Netron I get this:

    And zoomed in a little further (on the far right side),

    As you can see, it looks nothing like it should.
    Additionally the input nodes and output nodes are not correct so it will not work for ML.NET (and I think something is wrong).
    I am using the recommended way from TensorFlow to determine the Input / Output nodes:

    When I try to obtain a frozen graph and load it into Netron, at first it looks correct, but I don't think that it is:

    There are four reasons I do not think this is correct.

    • it is very different from the graph when it was uploaded as an h5 (which looks correct to me).
    • as you can see from earlier, I am using 1D convolutions throughout and this is showing that it goes to 2D (and remains that way).
    • this file size is 128MB whereas the one in the TensorFlow to ML.NET example is only 252KB. Even the Inception model is only 56MB.
    • if I load the Inception model in TensorFlow and save it as an h5, it looks the same as from the ML.NET resource, yet when I save it as a frozen graph it looks different. If I take the same model and save it in the recommended SavedModel format, it shows up all messed up in Netron. Take any model you want and save it in the recommended SavedModel format and you will see for yourself (I've tried it on a lot of different models).

    Additionally in looking at the model.summary() of Inception with it's graph, it is similar to its graph in the same way my model.summary() is to the h5 graph.

    It seems like there should be an easier way (and a correct way) to save a TensorFlow model so it can be used in ML.NET.

    Please show that your suggested solution works: In the answer that you provide, please check that it works (load the pb model [this should also have a Variables folder in order to work for ML.NET] into Netron and show that it is the same as the h5 model, e.g., screenshot it). So that we are all trying the same thing, here is a link to a MNIST ML crash course example. It takes less than 30s to run the program and produces a model called my_model. From here you can save it according to your method and upload it to see the graph on Netron. Here is the h5 model upload:

    解决方案

    This answer is made of 3 parts:

    • going through other programs
    • NOT going through other programs
    • Difference between op-level graph and conceptual graph (and why Netron show you different graphs)

    1. Going through other programs:

    ML.net needs an ONNX model, not a pb file.

    There is several ways to convert your model from TensorFlow to an ONNX model you could load in ML.net :

    This SO post could help you too: Load model with ML.NET saved with keras

    And here you will find more informations on the h5 and pb files formats, what they contain, etc.: https://www.tensorflow.org/guide/keras/save_and_serialize#weights_only_saving_in_savedmodel_format

    2. But you are asking "TensorFlow -> ML.NET without going through other programs":

    2.A An overview of the problem:

    First, the pl file format you made using the code you provided from seems, from what you say, to not be the same as the one used in the example you mentionned in comment (https://docs.microsoft.com/en-us/dotnet/machine-learning/tutorials/text-classification-tf)

    Could to try to use the pb file that will be generated via tf.saved_model.save ? Is it working ?

    A thought about this microsoft blog post:

    From this page we can read:

    In ML.NET you can load a frozen TensorFlow model .pb file (also called "frozen graph def" which is essentially a serialized graph_def protocol buffer written to disk)

    and:

    That TensorFlow .pb model file that you see in the diagram (and the labels.txt codes/Ids) is what you create/train in Azure Cognitive Services Custom Vision then exporte as a frozen TensorFlow model file to be used by ML.NET C# code.

    So, this pb file is a type of file generated from Azure Cognitive Services Custom Vision. Perharps you could try this way too ?

    2.B Now, we'll try to provide the solution:

    In fact, in TensorFlow 1.x you could save a frozen graph easily, using freeze_graph.

    But TensorFlow 2.x does not support freeze_graph and converter_variables_to_constants.

    You could read some usefull informations here too: Tensorflow 2.0 : frozen graph support

    Some users are wondering how to do in TF 2.x: how to freeze graph in tensorflow 2.0 (https://github.com/tensorflow/tensorflow/issues/27614)

    There are some solutions however to create the pb file you could load in ML.net as you want:

    https://leimao.github.io/blog/Save-Load-Inference-From-TF2-Frozen-Graph/

    How to save Keras model as frozen graph? (already linked in your question though)

    Difference between op-level graph and conceptual graph (and why Netron show you different graphs):

    As @mlneural03 said in a comment to you question, Netron shows a different graph depending on what file format you give:

    • If you load a h5 file, Netron wil display the conceptual graph
    • If you load a pb file, Netron wil display the op-level graph

    What is the difference between a op-level graph and a conceptual graph ?

    • In TensorFlow, the nodes of the op-level graph represent the operations ("ops"), like tf.add , tf.matmul , tf.linalg.inv, etc.
    • The conceptual graph will show you your your model's structure.

    That's completely different things.

    "ops" is an abbreviation for "operations". Operations are nodes that perform the computations.

    So, that's why you get a very large graph with a lot of nodes when you load the pb fil in Netron: you see all the computation nodes of the graph. but when you load the h5 file in Netron, you "just" see your model's tructure, the design of your model.

    In TensorFlow, you can view your graph with TensorBoard:

    • By default, TensorBoard displays the op-level graph.
    • To view the coneptual graph, in TensorBoard, select the "keras" tag.

    There is a Jupyter Notebook that explains very clearly the difference between the op-level graph and the coneptual graph here: https://colab.research.google.com/github/tensorflow/tensorboard/blob/master/docs/graphs.ipynb

    You can also read this "issue" on the TensorFlow Github too, related to your question: https://github.com/tensorflow/tensorflow/issues/39699

    In a nutshell:

    In fact there is no problem, just a little misunderstanding (and that's OK, we can't know everything).

    You would like to see the same graphs when loading the h5 file and the pb file in Netron, but it has to be unsuccessful, because the files does not contains the same graphs. These graphs are two ways of displaying the same model.

    The pb file created with the method we described will be the correct pb file to load whith ML.NET, as described in the Microsoft's tutorial we talked about. SO, if you load you correct pb file as described in these tutorials, you wil load your real/true model.

    这篇关于纠正pb文件以将Tensorflow模型移至ML.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆