machinelearning icon indicating copy to clipboard operation
machinelearning copied to clipboard

How to define multiple inputs of different data types when load tensorflow model by ML.Net

Open zdz72113 opened this issue 4 years ago • 4 comments

System information

  • OS & Version: Windows 10
  • ML.NET Version: ML.NET v1.7.1
  • .NET Version: .NET 6.0

Issue

  • What did you do? I want to load a tensorflow model structured_data preprocessing_layers by ML.Net. I can get the input and output info as following, but I don't know how to define the inputs in ML.net. image

  • What happened? I defined the inputs in ML.net as source code below but encoutered exception "Tensorflow.InvalidArgumentError:“Invalid reduction dimension (1 for input with 1 dimension(s) [[{{node model/category_encoding_1/Min}}]]” ": image

Source code / logs

  • Python Code: https://github.com/tensorflow/docs/blob/master/site/en/tutorials/structured_data/preprocessing_layers.ipynb and i use
model.save('my_pet_classifier')

to save the model to disk.

  • .Net Code:
internal class Program
    {
        static readonly string _modelPath = Path.Combine(Environment.CurrentDirectory, "tf_model");
        static void Main(string[] args)
        {
            MLContext mlContext = new MLContext(seed: 1);

            TensorFlowModel tensorFlowModel = mlContext.Model.LoadTensorFlowModel(_modelPath);

            var pipeline = tensorFlowModel.ScoreTensorFlowModel(outputColumnNames: new[] { "StatefulPartitionedCall" }, inputColumnNames: new[] { "serving_default_Age", "serving_default_Breed1",
                    "serving_default_Color1", "serving_default_Color2", "serving_default_Fee", "serving_default_FurLength", "serving_default_Gender", "serving_default_Health",
                    "serving_default_MaturitySize", "serving_default_PhotoAmt", "serving_default_Sterilized", "serving_default_Type", "serving_default_Vaccinated" }, addBatchDimensionInput: false);

            IDataView dataView = mlContext.Data.LoadFromEnumerable(new List<Pet>());
            ITransformer model = pipeline.Fit(dataView);

            var engine = mlContext.Model.CreatePredictionEngine<Pet, PetAdoptedPrediction>(model);

            var review = new Pet()
            {
                Age = new Int64[] { 3L  },
                Breed1 = new string[] { "Tabby" },

                Color1 = new string[] {  "Black" },
                Color2 = new string[] { "White"  },
                Fee = new float[] { 100f },
                FurLength = new string[] { "Short" },
                Gender = new string[] { "Male" },
                Health = new string[] { "Healthy" },
                MaturitySize = new string[] { "Small" },
                PhotoAmt = new float[] { 2f },
                Sterilized = new string[] { "No" },
                Type = new string[] { "Cat" },
                Vaccinated = new string[] { "No" },
            };
            var prediction = engine.Predict(review);
        }
    }

    public class Pet
    {
        [VectorType(1)]
        [ColumnName("serving_default_Age")]
        public Int64[] Age { get; set; }

        [VectorType(1)]
        [ColumnName("serving_default_Breed1")]
        public string[] Breed1 { get; set; }

        [VectorType(1)]
        [ColumnName("serving_default_Color1")]
        public string[] Color1 { get; set; }

        [VectorType(1)]
        [ColumnName("serving_default_Color2")]
        public string[] Color2 { get; set; }

        [VectorType(1)]
        [ColumnName("serving_default_Fee")]
        public float[] Fee { get; set; }

        [VectorType(1)]
        [ColumnName("serving_default_FurLength")]
        public string[] FurLength { get; set; }

        [VectorType(1)]
        [ColumnName("serving_default_Gender")]
        public string[] Gender { get; set; }

        [VectorType(1)]
        [ColumnName("serving_default_Health")]
        public string[] Health { get; set; }

        [VectorType(1)]
        [ColumnName("serving_default_MaturitySize")]
        public string[] MaturitySize { get; set; }

        [VectorType(1)]
        [ColumnName("serving_default_PhotoAmt")]
        public float[] PhotoAmt { get; set; }

        [VectorType(1)]
        [ColumnName("serving_default_Sterilized")]
        public string[] Sterilized { get; set; }

        [VectorType(1)]
        [ColumnName("serving_default_Type")]
        public string[] Type { get; set; }

        [VectorType(1)]
        [ColumnName("serving_default_Vaccinated")]
        public string[] Vaccinated { get; set; }
    }

    public class PetAdoptedPrediction
    {
        [VectorType(1)]
        [ColumnName("StatefulPartitionedCall")]
        public float[] Prediction { get; set; }
    }

zdz72113 avatar Oct 18 '22 06:10 zdz72113

@zdz72113 are you still having issues with this? If so could you upload your saved tensorflow model so we can take a look at it? It probably has to do with setting or not setting the "BatchInput" flag.

michaelgsharp avatar Nov 28 '22 18:11 michaelgsharp

This issue has been marked needs-author-action and may be missing some important information.

ghost avatar Nov 28 '22 18:11 ghost

This issue has been automatically marked no-recent-activity because it has not had any activity for 14 days. It will be closed if no further activity occurs within 14 more days. Any new comment (by anyone, not necessarily the author) will remove no-recent-activity.

ghost avatar Dec 12 '22 21:12 ghost

This issue will now be closed since it had been marked no-recent-activity but received no further activity in the past 14 days. It is still possible to reopen or comment on the issue, but please note that the issue will be locked if it remains inactive for another 30 days.

ghost avatar Dec 27 '22 00:12 ghost