public OnnxModelConfigurator(IOnnxModel onnxModel)
 {
     mlContext = new MLContext();
     // Model creation and pipeline definition for images needs to run just once,
     // so calling it from the constructor:
     mlModel = SetupMlNetModel(onnxModel);
 }
        private ITransformer SetupMlNetModel(IOnnxModel onnxModel)
        {
            var dataView = _mlContext.Data
                           .LoadFromEnumerable(new List <TFeature>());

            var pipeline = _mlContext.Transforms
                           .ApplyOnnxModel(modelFile: onnxModel.ModelPath, outputColumnNames: onnxModel.ModelOutput, inputColumnNames: onnxModel.ModelInput, gpuDeviceId: 0);

            var mlNetModel = pipeline.Fit(dataView);

            return(mlNetModel);
        }
        private ITransformer SetupMlNetModel(IOnnxModel onnxModel)
        {
            var dataView = mlContext.Data.LoadFromEnumerable(new List <ImageInputData>());

            var pipeline = mlContext.Transforms.ResizeImages(resizing: ImageResizingEstimator.ResizingKind.Fill, outputColumnName: onnxModel.ModelInput, imageWidth: ImageSettings.imageWidth, imageHeight: ImageSettings.imageHeight, inputColumnName: nameof(ImageInputData.Image))
                           .Append(mlContext.Transforms.ExtractPixels(outputColumnName: onnxModel.ModelInput))
                           .Append(mlContext.Transforms.ApplyOnnxModel(modelFile: onnxModel.ModelPath, outputColumnName: onnxModel.ModelOutput, inputColumnName: onnxModel.ModelInput));

            var mlNetModel = pipeline.Fit(dataView);

            return(mlNetModel);
        }
        private ITransformer SetupMlNetModel(IOnnxModel onnxModel)
        {
            var dataView = mlContext.Data.LoadFromEnumerable(new List <ImageInputData>());

            // If the input flattened image data order is different from im.read() check "interleavePixelColors" to true
            var pipeline = mlContext.Transforms.ResizeImages(resizing: ImageResizingEstimator.ResizingKind.Fill, outputColumnName: onnxModel.ModelInput, imageWidth: ImageSettings.imageWidth, imageHeight: ImageSettings.imageHeight, inputColumnName: nameof(ImageInputData.Image))
                           .Append(mlContext.Transforms.ExtractPixels(outputColumnName: onnxModel.ModelInput, interleavePixelColors: true, orderOfExtraction: ImagePixelExtractingEstimator.ColorsOrder.ARGB, scaleImage: 1.0f / 255.0f))
                           .Append(mlContext.Transforms.ApplyOnnxModel(modelFile: onnxModel.ModelPath, outputColumnName: onnxModel.ModelOutput, inputColumnName: onnxModel.ModelInput));

            var mlNetModel = pipeline.Fit(dataView);

            return(mlNetModel);
        }
Example #5
0
        public ITransformer LoadOnnx(IOnnxModel model)
        {
            var dataview = ml.Data.LoadFromEnumerable(new List <ImageNetData>());

            var estimator = ml.Transforms.ResizeImages(
                inputColumnName: nameof(ImageNetData.Image),
                outputColumnName: model.InputName,
                imageWidth: ImageNetSettings.Width,
                imageHeight: ImageNetSettings.Height,
                resizing: ResizingKind.Fill
                )
                            .Append(ml.Transforms.ExtractPixels(outputColumnName: model.InputName))
                            .Append(ml.Transforms.ApplyOnnxModel(model.OutputName, model.InputName, model.ModelPath, 1, true));

            var transformer = estimator.Fit(dataview);

            return(transformer);
        }
Example #6
0
 public OnnxModelConfigurator(IOnnxModel onnxModel)
 {
     _mlContext = new MLContext();
     _mlModel   = SetupMlNetModel(onnxModel);
 }