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>
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.
[url=viewtopic.php?t=14917]Ich möchte[/url] 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]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 [/code] Ich habe versucht, eine neuere Version von Cudnn und CUDA Toolkit zu installieren. Aber ich habe den Fehler bekommen.[code]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 [/code] Ich habe mehrere Versionen des ONNX ausprobiert.
Ich portiere einen Python-Code nach NodeJS, aber das Ausgabebild sieht rötlich aus.
Ich möchte opencv nicht in NodeJS verwenden. Ich verwende scharf. Irgendeine Idee, wie man das Problem beheben...
Ich portiere einen Python-Code nach NodeJS, aber das Ausgabebild sieht rötlich aus.
Ich möchte opencv nicht in NodeJS verwenden. Ich verwende scharf. Haben Sie eine Idee, wie Sie das Problem beheben...
Ich versuche, mein Python-Skript mit Auto-py-to-exe zu kompilieren, und es hat großartig funktioniert (unter Windows 10).
Aber als ich versuchte, das Programm auf einem anderen Computer auszuführen...
Ich möchte die Datei onnxruntime_cxx_api.h verwenden und gehe davon aus, dass ich dieses Projekt erstellen sollte, also folge ich diesem Tutorial
Ich habe gerade ./build.sh --config RelWithDebInfo...