What is the best way to load a model in Tensorflow.NET

Viewed 2280

I saved a tensorflow.keras model in python and need to use in in C# / Tensorflow.NET 0.15

var net = tf.keras.models.load_model(net_name) does not seem to be implemented

var session = tf.Session.LoadFromSavedModel(net_name);
var graph = sess.graph;

seems to work but I have then a session / graph not a keras model

I would ideally like to call something like net.predict(x), how can I get there from a graph/session ?

1 Answers

Yes, i Did. The best way is to convert you package to the ONNX format. ONNX is a open source format that is supposed to run on any framework (tensorflow, torch...)

In python, add the package onnx and keras2onnx:

import onnx
import keras2onnx
import onnxruntime
   

    net_onnx = keras2onnx.convert_keras(net_keras)

    onnx.save_model(net_onnx, onnx_name)

Then in C# .NET, install the nuget Microsoft.ML.

var context = new MLContext();
var session = new InferenceSession(filename);
float[] sample;
int[] dims = new int[] { 1, sample_size};
var tensor = new DenseTensor<float>(sample,dims);
var xs = new List<NamedOnnxValue>()
{
  NamedOnnxValue.CreateFromTensor<float>("dense_input", tensor),
};

using (var results = session.Run(xs))
{
 // manipulate the results
}

Note that you need to call explicitly the fist layer or the input layer of your network to pass on the sample. best is to give it a nice name in Keras. You can check the name in python by running net_keras.summary()

Related