dotnet / machinelearning-samples

Samples for ML.NET, an open source and cross-platform machine learning framework for .NET.
https://dot.net/ml
MIT License
4.4k stars 2.65k forks source link

Convert Sentiment Analysis model to onnx #874

Open mountaingeru opened 3 years ago

mountaingeru commented 3 years ago

Hi,

I have been following the tutorial on "Sentiment Analysis" (https://docs.microsoft.com/es-es/dotnet/machine-learning/tutorials/sentiment-analysis), which is really nice, interesting and useful. However, I have stumbled on the first step further. I am trying to export the generated model to onnx with the following code:

            // Converts model to onnx
            ConvertModel2ONNX(mlContext, model, splitDataView.TestSet);

mlContext, model and splitDataView come from the functions explainned in the tutorial.

 public static void ConvertModel2ONNX(MLContext mlContext, ITransformer model, IDataView splitTestSet)
        {
            using (var stream = File.Create(_onnxPath))
            {
                mlContext.Model.ConvertToOnnx(model, splitTestSet, stream);
            }
        }

where _onnxPath is the output path for the onnx file.

However, I get the following error.

System.Collections.Generic.KeyNotFoundException
  HResult=0x80131577
  Message=The given key 'SentimentText_TextNormalizer' was not present in the dictionary.
  Source=System.Private.CoreLib
  StackTrace:
   at System.ThrowHelper.ThrowKeyNotFoundException[T](T key)
   at System.Collections.Generic.Dictionary`2.get_Item(TKey key)
   at Microsoft.ML.Model.OnnxConverter.OnnxContextImpl.GetVariableName(String colName)
   at Microsoft.ML.Transforms.Text.WordTokenizingTransformer.Mapper.SaveAsOnnx(OnnxContext ctx)
   at Microsoft.ML.Data.RowToRowMapperTransform.Microsoft.ML.Model.OnnxConverter.ISaveAsOnnx.SaveAsOnnx(OnnxContext ctx)
   at Microsoft.ML.Model.OnnxConverter.SaveOnnxCommand.ConvertTransformListToOnnxModel(OnnxContextImpl ctx, IChannel ch, IDataView inputData, IDataView outputData, LinkedList`1 transforms, HashSet`1 inputColumnNamesToDrop, HashSet`1 outputColumnNamesToDrop)
   at Microsoft.ML.OnnxExportExtensions.ConvertToOnnxProtobufCore(IHostEnvironment env, OnnxContextImpl ctx, ITransformer transform, IDataView inputData, String[] outputColumnNamesToKeep)
   at Microsoft.ML.OnnxExportExtensions.ConvertToOnnxProtobuf(ModelOperationsCatalog catalog, ITransformer transform, IDataView inputData, String[] outputColumns)
   at Microsoft.ML.OnnxExportExtensions.ConvertToOnnx(ModelOperationsCatalog catalog, ITransformer transform, IDataView inputData, Stream stream)
   at SentimentAnalysis.Program.ConvertModel2ONNX(MLContext mlContext, ITransformer model, IDataView splitTestSet) in C:\SentimentAnalysis\Program.cs:line 164
   at SentimentAnalysis.Program.Main(String[] args) in C:\SentimentAnalysis\Program.cs:line 41

What could be the reason? I do not know if this question has a place here. If not, please forgive me for the inconvenience and ignore my question. Thank you very much.

gguy-1src commented 1 year ago

Seems related to: https://github.com/dotnet/machinelearning/issues/6237 and could have been recently fixed with: https://github.com/dotnet/machinelearning/pull/6242