Set ColumnName or VectorType during runtime in ML.net c#

Viewed 122

currently I'm writing a program to use trained ONNX models for object detection. I want to have the possibility to change a model (ideally during runtime) therefore I don't know all properties of the models during compiling but want to save them in one (or ideally more) Object(s) which is (are) created from a textfile at programstart (MachineLearningModelObject). But ML.net forces me to use a const for ColumnName or VectorType which I would like to set during runtime.

    public MachineLearningModelObject MachineLearningModelObject = new MachineLearningModelObject();

    public void LoadModel()
    {
        MLContext mLContext = new MLContext();
        var pipeline = mLContext.Transforms.ResizeImages(inputColumnName: MachineLearningModelObject.ImageInputColumnName, outputColumnName: MachineLearningModelObject.ImageOutputColumnName, imageWidth: MachineLearningModelObject.ImageWidth, imageHeight: MachineLearningModelObject.ImageHeight, resizing: Microsoft.ML.Transforms.Image.ImageResizingEstimator.ResizingKind.Fill)
            .Append(mLContext.Transforms.ExtractPixels(outputColumnName: MachineLearningModelObject.ImageOutputColumnName, scaleImage: 1f / 255f, interleavePixelColors: false))
            .Append(mLContext.Transforms.ApplyOnnxModel(
                shapeDictionary: new Dictionary<string, int[]>()
                {
                    { MachineLearningModelObject.ModelInputColumnName, MachineLearningModelObject.ModelInputVector },
                    { MachineLearningModelObject.ModelOutputColumnName, MachineLearningModelObject.ModelOutputVector}
                },
                inputColumnName: MachineLearningModelObject.ModelInputColumnName,
                outputColumnName: MachineLearningModelObject.ModelOutputColumnName,
                modelFile: MachineLearningModelObject.ModelName
                ));

        var model = pipeline.Fit(mLContext.Data.LoadFromEnumerable(new List<MLImageObject>()));
        var predictionEngine = mLContext.Model.CreatePredictionEngine<MLImageObject, MLObjectDetectionPrediction>(model);

    }

    class MLObjectDetectionPrediction
    {
        [VectorType(MLObjectDetectionPipeline.MachineLearningModelObject.ModelInputVector)] //Must be a const?
        [ColumnName(MLObjectDetectionPipeline.MachineLearningModelObject.ModelInputColumnName)] // Must be a const?
        public float[] Output { get; set; }

        [ColumnName("width")]
        public float ImageWidth { get; set; }

        [ColumnName("height")]
        public float ImageHeight { get; set; }
}

If I use a const everything works fine. But I can't figure out how to set these variables during runtime. Any hints or examples for my code are appreciated.

0 Answers
Related