Onnxruntime kann nicht laden `CUDNN64_9.DLL` in .NETC#

Ein Treffpunkt für C#-Programmierer
Anonymous
 Onnxruntime kann nicht laden `CUDNN64_9.DLL` in .NET

Post by Anonymous »

Ich möchte Microsoft.ml.onnxRuntime.gpu Version 1.20.1 auf der OnNX -Website in Dotnet 9.0.300 verwenden. Das Tutorial verwendet CPU, möchte es jedoch an der GPU ändern. Mein geänderter Code wirft jedoch einen Fehler. Meine GPU ist RTX 2060. < /P>
Mein vollständiger Code < /p>

Code: Select all

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Microsoft.ML.OnnxRuntime.Tensors;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing;
using SixLabors.ImageSharp.Drawing.Processing;
using SixLabors.Fonts;
using Microsoft.ML.OnnxRuntime;
using SixLabors.ImageSharp.Formats;

// keep in mind almost all of the classes are disposable.
string modelFilePath = "FasterRCNN-10.onnx";
string imageFilePath = "demo.jpg";
string outImageFilePath = "output.jpg";
using Image image = Image.Load(imageFilePath, out IImageFormat format);
float ratio = 800f / Math.Min(image.Width, image.Height);
using Stream imageStream = new MemoryStream();
image.Mutate(x => x.Resize((int)(ratio * image.Width), (int)(ratio * image.Height)));
image.Save(imageStream, format);
var paddedHeight = (int)(Math.Ceiling(image.Height / 32f) * 32f);
var paddedWidth = (int)(Math.Ceiling(image.Width / 32f) * 32f);
var mean = new[] { 102.9801f, 115.9465f, 122.7717f };
var _sessionOptions = new SessionOptions();
_sessionOptions.GraphOptimizationLevel = GraphOptimizationLevel.ORT_ENABLE_ALL;
_sessionOptions.AppendExecutionProvider_CUDA();

// Preprocessing image
// We use DenseTensor for multi-dimensional access
DenseTensor input = new(new[] { 3, paddedHeight, paddedWidth });
image.ProcessPixelRows(accessor =>
{
for (int y = paddedHeight - accessor.Height; y < accessor.Height; y++)
{
Span pixelSpan = accessor.GetRowSpan(y);
for (int x = paddedWidth - accessor.Width; x < accessor.Width; x++)
{
input[0, y, x] = pixelSpan[x].B - mean[0];
input[1, y, x] = pixelSpan[x].G - mean[1];
input[2, y, x] = pixelSpan[x].R - mean[2];
}
}
});
using var inputOrtValue = OrtValue.CreateTensorValueFromMemory(OrtMemoryInfo.DefaultInstance,
input.Buffer, new long[] { 3, paddedHeight, paddedWidth });

var inputs = new Dictionary
{
{ "image", inputOrtValue }
};

using var session = new InferenceSession(modelFilePath,_sessionOptions);
using var runOptions = new RunOptions();
using IDisposableReadOnlyCollection results = session.Run(runOptions, inputs, session.OutputNames);

var boxesSpan = results[0].GetTensorDataAsSpan();
var labelsSpan = results[1].GetTensorDataAsSpan();
var confidencesSpan = results[2].GetTensorDataAsSpan();

const float minConfidence = 0.7f;
var predictions = new List
();

for (int i = 0; i < boxesSpan.Length - 4; i += 4)
{
var index = i / 4;
if (confidencesSpan[index] >= minConfidence)
{
predictions.Add(new Prediction
{
Box = new Box(boxesSpan[i], boxesSpan[i + 1], boxesSpan[i + 2], boxesSpan[i + 3]),
Label = LabelMap.Labels[labelsSpan[index]],
Confidence = confidencesSpan[index]
});
}
}

using var outputImage = File.OpenWrite(outImageFilePath);
Font font = SystemFonts.CreateFont("Arial", 16);
foreach (var p in predictions)
{
image.Mutate(x =>
{
x.DrawPolygon(Color.Red, 2f, new PointF[] {
new PointF(p.Box.Xmin, p.Box.Ymin),
new PointF(p.Box.Xmax, p.Box.Ymin),
new PointF(p.Box.Xmax, p.Box.Ymax),
new PointF(p.Box.Xmin, p.Box.Ymax),
new PointF(p.Box.Xmin, p.Box.Ymin)
});
x.DrawText($"{p.Label}, {p.Confidence:0.00}", font, Color.White, new PointF(p.Box.Xmin, p.Box.Ymin));
});
}
image.Save(outputImage, format);

< /code>
Der Fehler < /p>
PS E:\Temp\test-onnx> dotnet run
E:\Temp\test-onnx\Prediction.cs(3,20): warning CS8618: Non-nullable property 'Box' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.
E:\Temp\test-onnx\Prediction.cs(4,23): warning CS8618: Non-nullable property 'Label' must contain a non-null value when exiting constructor.  Consider adding the 'required' modifier or declaring the property as nullable.
Unhandled exception. Microsoft.ML.OnnxRuntime.OnnxRuntimeException: [ErrorCode:RuntimeException] D:\a\_work\1\s\onnxruntime\core\session\provider_bridge_ort.cc:1539 onnxruntime::ProviderLibrary::Get [ONNXRuntimeError] : 1 : FAIL : LoadLibrary failed with error 126 "" when trying to load "E:\Temp\test-onnx\bin\Debug\net9.0\runtimes\win-x64\native\onnxruntime_providers_cuda.dll"

at Microsoft.ML.OnnxRuntime.SessionOptions.AppendExecutionProvider_CUDA(Int32 deviceId)
at Program.$(String[] args) in E:\Temp\test-onnx\Program.cs:line 29
Ich habe versucht, eine neuere Version von Cudnn und CUDA Toolkit zu installieren. Aber ich habe den Fehler bekommen.

Code: Select all

nvcc -V
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2025 NVIDIA Corporation
Built on Wed_Apr__9_19:29:17_Pacific_Daylight_Time_2025
Cuda compilation tools, release 12.9, V12.9.41
Build cuda_12.9.r12.9/compiler.35813241_0
Ich habe mehrere Versionen des ONNX ausprobiert.

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post