Skip to content

Consume a tensorflow regression model from .NET #6014

@jbilbrey

Description

@jbilbrey

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; }
}

}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions