title: Correct pb file to move Tensorflow model into ML.NET
body: <p>I have a TensorFlow model that I built (a 1D CNN) that I would now like to implement into .NET.<br />
  In order to do so I need to know the Input and Output nodes.<br />
  When I uploaded the model on <a href="https://netron.app/" rel="nofollow noreferrer">Netron</a> 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 <code>model.summary()</code>:</p>
  <p><a href="https://i.stack.imgur.com/wLUfb.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/wLUfb.png" alt="enter image description here" /></a></p>
  <p>If I save the model as an h5 <code>model.save(&quot;Mn_pb_model.h5&quot;)</code> and load that into the Netron to graph it, everything looks correct:</p>
  <p><a href="https://i.stack.imgur.com/gV1CD.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/gV1CD.png" alt="enter image description here" /></a></p>
  <p>However, ML.NET will not accept h5 format so it needs to be saved as a pb.</p>
  <p>In looking through samples of adopting TensorFlow in ML.NET, this <a href="https://github.com/dotnet/samples/tree/master/machine-learning/tutorials/TextClassificationTF/sentiment_model" rel="nofollow noreferrer">sample</a> shows a TensorFlow model that is saved in a similar format to the <a href="https://www.tensorflow.org/guide/saved_model" rel="nofollow noreferrer">SavedModel</a> format - recommended by TensorFlow (and also recommended by ML.NET <a href="https://github.com/dotnet/machinelearning/blob/master/docs/samples/Microsoft.ML.Samples/Dynamic/TensorFlow/TextClassification.cs" rel="nofollow noreferrer">here</a> &quot;Download an unfrozen [SavedModel format] ...&quot;).  However when saving and loading the pb file into Netron I get this:</p>
  <p><a href="https://i.stack.imgur.com/1tk96.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/1tk96.png" alt="enter image description here" /></a></p>
  <p>And zoomed in a little further (on the far right side),</p>
  <p><a href="https://i.stack.imgur.com/LE0Ru.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/LE0Ru.png" alt="enter image description here" /></a></p>
  <p>As you can see, it looks nothing like it should.<br />
  Additionally the input nodes and output nodes are not correct so it will not work for ML.NET (and I think something is wrong).<br />
  I am using the <a href="https://www.tensorflow.org/lite/guide/faq" rel="nofollow noreferrer">recommended way</a> from TensorFlow to determine the Input / Output nodes:</p>
  <p><a href="https://i.stack.imgur.com/CyWxR.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/CyWxR.png" alt="enter image description here" /></a></p>
  <p>When I try to obtain a <a href="https://stackoverflow.com/questions/60974077/how-to-save-keras-model-as-frozen-graph">frozen</a> graph and load it into Netron, at first it looks correct, but I don't think that it is:</p>
  <p><a href="https://i.stack.imgur.com/jlPU6.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/jlPU6.png" alt="enter image description here" /></a></p>
  <p>There are four reasons I do not think this is correct.</p>
  <ul>
  <li>it is very different from the graph when it was uploaded as an h5 (which looks correct to me).</li>
  <li>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).</li>
  <li>this file size is 128MB whereas the one in the TensorFlow to ML.NET example is only 252KB.  Even the <a href="https://github.com/dotnet/machinelearning-samples/tree/master/samples/csharp/getting-started/DeepLearning_ImageClassification_TensorFlow/ImageClassification/assets/inputs/inception" rel="nofollow noreferrer">Inception</a> model is only 56MB.</li>
  <li>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.  <strong>If I take the same model and save it in the recommended <code>SavedModel</code> format, it shows up all messed up in Netron.</strong>  Take any model you want and save it in the recommended <code>SavedModel</code> format and you will see for yourself (I've tried it on a lot of different models).</li>
  </ul>
  <p>Additionally in looking at the <code>model.summary()</code> of Inception with it's graph, it is similar to its graph in the same way my <code>model.summary()</code> is to the h5 graph.</p>
  <p>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.</p>
  <p><strong>Please show that your suggested solution works:</strong>  In the answer that you provide, please check that it works (load the <code>pb</code> model [this should also have a <code>Variables</code> folder in order to work for ML.NET] into Netron and show that it is the same as the <code>h5</code> model, e.g., screenshot it).  So that we are all trying the same thing, here is a <a href="https://colab.research.google.com/github/google/eng-edu/blob/master/ml/cc/exercises/multi-class_classification_with_MNIST.ipynb?utm_source=mlcc&amp;utm_campaign=colab-external&amp;utm_medium=referral&amp;utm_content=multiclass_tf2-colab&amp;hl=en" rel="nofollow noreferrer">link</a> to a MNIST ML crash course example.  It takes less than 30s to run the program and produces a model called <code>my_model</code>.  From here you can save it according to your method and upload it to see the graph on Netron.  Here is the <code>h5</code> model upload:</p>
  <p><a href="https://i.stack.imgur.com/xTUOB.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/xTUOB.png" alt="enter image description here" /></a></p>