/// <summary> /// Initializes a new instance of the <see cref="OnnxModel"/> class. /// </summary> /// <param name="configuration">The configuration for the onnx model runner.</param> public OnnxModel(OnnxModelConfiguration configuration) { this.configuration = configuration; // The schemaDefinition is a ML.NET construct that allows us to specify the form // of the inputs. In this case we construct a schema definition programmatically // to reflect that the input is a vector of floats, of the sizes specified in the // configuration this.schemaDefinition = SchemaDefinition.Create(typeof(OnnxInputVector)); this.schemaDefinition[nameof(OnnxInputVector.Vector)].ColumnType = new VectorDataViewType(NumberDataViewType.Single, this.configuration.InputVectorSize); this.schemaDefinition[nameof(OnnxInputVector.Vector)].ColumnName = this.configuration.InputVectorName; // We create the onnxTransformer which will be used to score inputs var onnxEmptyInputDataView = this.context.Data.LoadFromEnumerable(new List <OnnxInputVector>(), this.schemaDefinition); var scoringEstimator = this.context.Transforms.ApplyOnnxModel( modelFile: configuration.ModelFileName, outputColumnNames: new[] { configuration.OutputVectorName }, inputColumnNames: new[] { configuration.InputVectorName }, shapeDictionary: configuration.ShapeDictionary, gpuDeviceId: configuration.GpuDeviceId, fallbackToCpu: false); this.onnxTransformer = scoringEstimator.Fit(onnxEmptyInputDataView); }
/// <summary> /// Initializes a new instance of the <see cref="OnnxModelRunner"/> class, based on a given configuration. /// </summary> /// <param name="pipeline">The pipeline to add the component to.</param> /// <param name="configuration">The component configuration.</param> /// <param name="name">An optional name for the component.</param> /// <remarks>The configuration parameter specifies the model filename, the /// name of the input and output vectors in that ONNX model, as well as /// the input vector size.</remarks> public OnnxModelRunner(Pipeline pipeline, OnnxModelConfiguration configuration, string name = nameof(OnnxModelRunner)) : base(pipeline, name) { this.inputVectorSize = configuration.InputVectorSize; this.onnxModel = new OnnxModel(configuration); }
/// <summary> /// Initializes a new instance of the <see cref="OnnxModelRunner"/> class, based on a given configuration. /// </summary> /// <param name="pipeline">The pipeline to add the component to.</param> /// <param name="configuration">The component configuration.</param> /// <remarks>The configuration parameter specifies the model filename, the /// name of the input and output vectors in that ONNX model, as well as /// the input vector size.</remarks> public OnnxModelRunner(Pipeline pipeline, OnnxModelConfiguration configuration) : base(pipeline) { this.inputVectorSize = configuration.InputVectorSize; this.onnxModel = new OnnxModel(configuration); }