You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I've converted a Pytorch model to an ONNX file and want to use it in a C# project.
But I'm running into this problem:
System.IO.FileLoadException: 'Could not load file or assembly 'Microsoft.ML.OnnxRuntime, Version=1.19.2.0, Culture=neutral, PublicKeyToken=f27f157f0a5b7bb6' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)'
Library versions on C# are as follows: 1.19.2 for Microsoft.ML.OnnxRuntime.Managed
1.19.2 for Microsoft.ML.OnnxRuntime
On Python, I've used the latest version at the time for converting the Pythoch model which was 1.16.2
This is the code:
var session = new InferenceSession("MobileNetV2.onnx"); //the model
var resizedImage = new Bitmap(bmp, new Size(299, 299)); //bmp is the bitmap of the image
var inputTensor = new DenseTensor<float>(new[] { 1, 3, 299, 299 }); // A tensor with Batch size = 1, Channels = 3, Height = 299, Width = 299
//the batch size has to be checked
//filling the tensors
for (int y = 0; y < resizedImage.Height; y++)
{
for (int x = 0; x < resizedImage.Width; x++)
{
// grayscale value (R=G=B)
var pixel = resizedImage.GetPixel(x, y).R / 255.0f;
inputTensor[0, 0, y, x] = pixel; // Channel 1
inputTensor[0, 1, y, x] = pixel; // Channel 2
inputTensor[0, 2, y, x] = pixel; // Channel 3
}
}
var inputName = session.InputMetadata.Keys.First();
var inputs = new List<NamedOnnxValue>
{
NamedOnnxValue.CreateFromTensor(inputName, inputTensor)
};
// running the model and getting the output
var results = session.Run(inputs);
var outputName = session.OutputMetadata.Keys.First();
var prediction = results.First(x => x.Name == outputName).AsEnumerable<float>().ToArray();
// prediction from the model
MessageBox.Show($"Prediction: {prediction[0]}", "Prediction Result", MessageBoxButtons.OK, MessageBoxIcon.Information);
I've double-checked the managed and unmanaged libraries and they are fine.
I went through the DLL locations and versions and the .csproj file for the project and everything seems to be fine.
I checked the architecture of the dll using dependencywalker and they have the same architecture.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I've converted a Pytorch model to an ONNX file and want to use it in a C# project.
But I'm running into this problem:
System.IO.FileLoadException: 'Could not load file or assembly 'Microsoft.ML.OnnxRuntime, Version=1.19.2.0, Culture=neutral, PublicKeyToken=f27f157f0a5b7bb6' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)'
Library versions on C# are as follows:
1.19.2 for Microsoft.ML.OnnxRuntime.Managed
1.19.2 for Microsoft.ML.OnnxRuntime
On Python, I've used the latest version at the time for converting the Pythoch model which was 1.16.2
This is the code:
I've double-checked the managed and unmanaged libraries and they are fine.
I went through the DLL locations and versions and the .csproj file for the project and everything seems to be fine.
I checked the architecture of the dll using dependencywalker and they have the same architecture.
Beta Was this translation helpful? Give feedback.
All reactions