Reputation: 21
I am really struggling with the fact that i don't know how can I load and use a TensorFlow image classifier model in c#.
To be more precise I want to use a model trained in Teachable Machine that can be exported in multiple TensorFlow formats. And if possible an example code will be really helpful.
I have tried to ask the same question but it got closed, I really need to find out how can I load and use the model so please let the question open. Thanks a lot for the support.
Upvotes: 2
Views: 11813
Reputation: 670
I used TensorFlowSharpin the past, but that library is still stuck with TensorFlow 1.x, and development seems quite dead. Because of that, I moved on to ML.NET.
Please see this article about how to use ML.NET with C# (disclaimer: I was the author of that article). Below is the gist of it.
Convert saved_model
to onnx
Install tf2onnx
.
pip install tf2onnx
Use it to convert saved_model
to onnx
.
python -m tf2onnx.convert --saved-model <path to saved_model folder> --output "model.onnx"
Use ML.NET to make prediction
Install the necessary packages.
dotnet add package Microsoft.ML
dotnet add package Microsoft.ML.OnnxRuntime
dotnet add package Microsoft.ML.OnnxTransformer
Define the input and output model classes.
public class Input
{
[VectorType(<input size>)]
[ColumnName("<input node name>")]
public float[] Data { get; set; } // Remember to change the type if your model does not use float
}
public class Output
{
[VectorType(<output size>)]
[ColumnName("<output node name>")]
public float[] Data { get; set; } // Remember to change the type if your model does not use float
}
Load the model in onnx
format.
using Microsoft.ML;
var modelPath = "<path to onnx model>";
var outputColumnNames = new[] { "<output name>" };
var inputColumnNames = new[] { "<input name>" };
var mlContext = new MLContext();
var pipeline = _mlContext.Transforms.ApplyOnnxModel(outputColumnNames, inputColumnNames, modelPath);
Make prediction
var input_data= ... // code to load input into an array
var input = new Input { Data = data };
var dataView = mlContext.Data.LoadFromEnumerable(new[] { input });
var transformedValues = pipeline.Fit(dataView).Transform(dataView);
var output = mlContext.Data.CreateEnumerable<Output>(transformedValues, reuseRowObject: false);
// code to use the result in output
Upvotes: 3
Reputation: 5404
If you need only to evaluate model in C# code, then you need some lightweight library that can be used as loader and evaluator for your network.
There are many good libraries to work with tensorflow models in C#, like:
Unfortunately, all existing libraries has pre-defined logic for training model too, so that is a little ballast for your task.
To accomplish your purposes, please see this detailed article about how to load TF model into your C# application using TensorFlowSharp.
Upvotes: 3