Correct pb file to move Tensorflow model into ML.NET

前端 未结 1 1487
执念已碎
执念已碎 2020-12-20 18:01

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

1条回答
  •  误落风尘
    2020-12-20 18:41

    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 :

    • With WinMLTools tools: https://docs.microsoft.com/en-us/windows/ai/windows-ml/convert-model-winmltools
    • With MMdnn: https://github.com/microsoft/MMdnn
    • With tf2onnx: https://github.com/onnx/tensorflow-onnx
    • If trained with Keras, with keras2onnx: https://github.com/onnx/keras-onnx

    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.

    0 讨论(0)
提交回复
热议问题