Error when launching mobilebert on Xamarin

  Kiến thức lập trình

I have a problem with running google/mobilebert-uncased in Xamarin.
I get an error System.DllNotFoundException Message=onnxruntime assembly:<unknown assembly> type:<unknown type> member:(null) in the line session = new InferenceSession(modelPath);.
I am using Visual Studio 2022 and .NET Standard 2.1. My Microsoft.ML.OnnxRuntime.dll file is located at objDebug110androidassets.
And I took all the files from google/mobilebert-uncased on the official HuggingFace site (except I compiled the model from .bin to .onnx).

My code:

using System.Collections.Generic;
using FastBertTokenizer;
using Microsoft.ML.Data;
using Microsoft.ML.OnnxRuntime;
using Microsoft.ML.OnnxRuntime.Tensors;
using Xamarin.Forms;
using System.Linq;
using System.Threading.Tasks;

namespace manipulatorMobileApp.Bert
{
    public class BertInput
    {
        [ColumnName("input_ids")]
        public long[] InputIds { get; set; }

        [ColumnName("attention_mask")]
        public long[] AttentionMask { get; set; }
    }
    public class MobileBert
    {
        public static async Task<string> PredictWord(string text)
        {
            InferenceSession session = null;
            try
            {
                BertTokenizer tokenizer = new BertTokenizer();
                string tokenizerFilePath = DependencyService.Get<IFileHelper>().GetFilePath("tokenizer.json");
                await tokenizer.LoadTokenizerJsonAsync(tokenizerFilePath);
                var (inputIds, attentionMask, tokenTypeIds) = tokenizer.Encode(text);

                BertInput bertInput = new BertInput()
                {
                    InputIds = inputIds.ToArray(),
                    AttentionMask = attentionMask.ToArray(),
                };

                string modelPath = DependencyService.Get<IFileHelper>().GetFilePath("mobilebert.onnx");

                DenseTensor<long> inputTensor = new DenseTensor<long>(bertInput.InputIds,
                    new[] { 1, bertInput.InputIds.Length });
                DenseTensor<long> attentionTensor = new DenseTensor<long>(bertInput.AttentionMask,
                    new[] { 1, bertInput.AttentionMask.Length });

                List<NamedOnnxValue> input = new List<NamedOnnxValue>
                {
                    NamedOnnxValue.CreateFromTensor("input_ids", inputTensor),
                    NamedOnnxValue.CreateFromTensor("attention_mask", attentionTensor)
                };

                session = new InferenceSession(modelPath);
                var output = session.Run(input);

                var prediction = output.First().AsEnumerable<long>().ToArray();
                var decoded = tokenizer.Decode(prediction);
                var predictedWord = decoded.Split(' ').FirstOrDefault();

                return predictedWord;
            }
            finally
            {
                if (session != null)
                {
                    session.Dispose();
                }
            }
        }
    }
}

Libraries I use:

If you need more information, I’m happy to provide it to you.

I’ve tried re-converting the model to .onnx, I’ve also reinstalled the libraries and tried different combinations of them. I also downloaded the latest versions of Visual C++ Redistributable.

I have to ask for your help as I have not found an answer to this question on other forums. Thank you and feel free to ask for more information if you also decide to look into this issue!

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

LEAVE A COMMENT