在 F# 中运行 Onnx 模型

问题描述 投票:0回答:1

我目前正在尝试在.NET 中运行经过 python 训练的 Keras 模型。我已将此模型转换为 Onnx 模型。我正在尝试使用 ML.NET 来运行它。

input:
  name: input_6
  tensor: float32[unk_297,50]

  name: input_5
  tensor: float32[unk_298,50]

output:
  name: out
  tensor: float32[unk_299,1]

我一直在关注 Microsoft 的这篇文章,将代码从 C# 转换为 F#: https://learn.microsoft.com/en-us/azure/machine-learning/how-to-use-automl-onnx-model-dotnet?view=azureml-api-2

以下是我根据输入创建的 F# 类型:

#r "nuget: Microsoft.ML"
#r "nuget: Microsoft.ML.OnnxRuntime, 1.10.0"
#r "nuget: Microsoft.ML.OnnxTransformer"


open Microsoft.ML
open Microsoft.ML.Data
open Microsoft.ML.Transforms.Onnx
open Microsoft.ML.OnnxRuntime.Tensors
open System
open System.IO

[<CLIMutable>]
type OnnxInput = {
    [<ColumnName("input_5");>]
    input1: DenseTensor<float32>
    [<ColumnName("input_6")>]
    input2: DenseTensor<float32>
}


[<CLIMutable>]
type OnnxOutput = {
    [<ColumnName("out")>]
    output: DenseTensor<float32>
}

let onnxModelPath = @""
let mlContext = new MLContext()
let createFloat32DenseTensor (data: float32 array) (dimensions: int array) =
    let memory = Memory<float32>(data)
    let tensor = new DenseTensor<float32>(memory, dimensions)
    tensor
let data = Array.create 50 0f
let dimensions = [| 1; 50 |]

let init = createFloat32DenseTensor data dimensions
let initialOnnxInput = {
    input1 = init
    input2 = init
}
let getPredictionPipeline (mlContext: MLContext) =
    let inputColumns = [|"input_5"; "input_6"|]
    let outputColumns = [|"out"|]
    let onnxPredictionPipeline =
        mlContext.Transforms.ApplyOnnxModel(
                outputColumnNames = outputColumns,
                inputColumnNames = inputColumns,
                modelFile = onnxModelPath
                )
    let emptyDataView = mlContext.Data.LoadFromEnumerable<OnnxInput>([||])
    onnxPredictionPipeline.Fit(emptyDataView)

let onnxPredictionPipeline = getPredictionPipeline mlContext
let onnxPredictionEngine = mlContext.Model.CreatePredictionEngine<OnnxInput, OnnxOutput>(onnxPredictionPipeline)
let testinputName = createFloat32DenseTensor outputResult dimensions

let testInput = {
    input1 = createFloat32DenseTensor outputResult dimensions 
    input2 = createFloat32DenseTensor outputResult dimensions
    }

let prediction = onnxPredictionEngine.Predict testInput

当我跑步时:

let onnxPredictionPipeline = getPredictionPipeline mlContext

我收到错误:

System.ArgumentOutOfRangeException: Could not determine an IDataView type and registered custom types for member input1@ (Parameter 'rawType')

如何创建正确的类型来正确运行这个经过训练的 Onnx 模型?

c# .net neural-network f# onnx
1个回答
0
投票

用户尝试使用 ONNX 模型在 .NET 中运行经过 Python 训练的 Keras 模型。他们按照 Microsoft 文章将 C# 代码转换为 F#,并根据他们的输入创建了 F# 类型。他们在运行代码时遇到错误,并寻求帮助来创建正确的类型以成功运行经过训练的 ONNX 模型。

© www.soinside.com 2019 - 2024. All rights reserved.