Skip to content

Consume a tensorflow regression model from .NET #6014

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
jbilbrey opened this issue Dec 5, 2021 · 1 comment
Closed

Consume a tensorflow regression model from .NET #6014

jbilbrey opened this issue Dec 5, 2021 · 1 comment

Comments

@jbilbrey
Copy link

jbilbrey commented Dec 5, 2021

System information

using Visual Studio 2022
Python 3.8.12
Tensorflow 2.5
Runtime Environment:
OS Name: Windows
OS Version: 10.0.19043
.NET SDKs installed:
5.0.400 [C:\Program Files\dotnet\sdk]
6.0.100 [C:\Program Files\dotnet\sdk]
Issue
I am trying to create a simple end-to-end problem. I create a simple model in Python and import the saved_model.pb into .NET. I would like to consume this model. Please help me by providing the solution that I can solve this problem.

What did you do?
I created a simple Python script in for the data set. Successfuly trained and evaluated the model and then exported it. The model can be loaded but I cannot configure the pipeline to make prediction.
What happened?
Now I try to consume the model with ML.NET Console.
What did you expect?
Cannot consume the model, because not able to create the proper pipeline.

I included the python script and the .NET program file in this repo.
https://github.com/jbilbrey/repo

using Microsoft.ML;
using Microsoft.ML.Data;
using System;
using System.IO;
using Microsoft.ML.Transforms;
using System.Linq;

namespace TestDeleteMe
{
class Program
{

    static string dataset = Path.Combine(Directory.GetCurrentDirectory(), "mpg.txt");        
    static readonly string _modelPath = Path.Combine(Environment.CurrentDirectory, "model");

    static void Main(string[] args)
    {
        var mlContext = new MLContext();

        Console.WriteLine("The path to the model is... " + _modelPath.ToString());  // debug only
        //load tensorflow model
        var tensorFlowModel = mlContext.Model.LoadTensorFlowModel(_modelPath);
        
        var schema = tensorFlowModel.GetModelSchema();
        var inputSchema = tensorFlowModel.GetInputSchema();
                           
        Console.WriteLine("The data is..." + inputSchema.ToString());

        var reader = mlContext.Data.CreateTextLoader(new[] {
            new TextLoader.Column("dense_input_1", DataKind.Single, new[] {new TextLoader.Range(1,9)}),          
        }, separatorChar: '\t', hasHeader: true);

        // read the data
        var data = reader.Load(dataset);

        // print data to screen
        var inputs = mlContext.Data.CreateEnumerable<InputData>(data, reuseRowObject: false).ToArray();

        // print the data to the console
        for (int i = 0; i < inputs.Length; i++)
        {
            //var predictedLabel = engine.Predict(inputs[i]);

            for (int j = 0; j < inputs[i].Features.Length; j++)
            {
                Console.Write(inputs[i].Features[j]);
                Console.Write(" ");
            }
            //Console.WriteLine(predictedLabel.Output[0]);
        }

        ////////////// fit the model /////////////////// NEED HELP HERE to consume/use the model!!!

        ///var estimator = tensorFlowModel.ScoreTensorFlowModel("Predict", "dense_input_1").Fit(data);

    }
}

class InputData
{
    [ColumnName("dense_input_1"), VectorType(9)]
    public float[]? Features { get; set; }
}


class OutputData
{
    [ColumnName("Output"), VectorType(1)]
    public float[]? Prediction { get; set; }
}

}

@jbilbrey
Copy link
Author

I found the solution in this post.

#5487

@ghost ghost locked as resolved and limited conversation to collaborators Mar 17, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant