Код: Выделить всё
using System.Drawing;
using System.Text.Json;
using Microsoft.ML;
using Microsoft.ML.OnnxRuntime;
using Microsoft.ML.OnnxRuntime.Tensors;
public static class Program {
static Tensor LoadImage(string imagePath)
{
var image = new Bitmap(imagePath);
var inputTensor = new DenseTensor(new[] { 1, 3, image.Height, image.Width });
for (int y = 0; y < image.Height; y++)
{
for (int x = 0; x < image.Width; x++)
{
Color pixel = image.GetPixel(x, y);
inputTensor[0, 0, y, x] = pixel.R / 255.0f;
inputTensor[0, 1, y, x] = pixel.G / 255.0f;
inputTensor[0, 2, y, x] = pixel.B / 255.0f;
}
}
return inputTensor;
}
public static void Main() {
Tensor image = LoadImage("demo01.jpg");
MLContext mlContext = new MLContext();
InferenceSession inferenceSession = new InferenceSession("depth_anything_v2_vits.onnx");
Console.WriteLine(JsonSerializer.Serialize(inferenceSession.InputNames));
var inputs = new NamedOnnxValue[] { NamedOnnxValue.CreateFromTensor("l_x_", image) };
Tensor output = inferenceSession.Run(inputs).First().AsTensor();
Bitmap outImage = new Bitmap(518, 518);
Console.WriteLine(output.Length);
for(int y = 0; y < 518; y++)
for(int x = 0; x < 518; x++) {
int r = (int)Math.Floor(output.ElementAt(x * y) * 50);
outImage.SetPixel(x, y, Color.FromArgb(255, r, 0, 0));
}
outImage.Save("out.png");
}
}
< /p>
Я попытался напрямую индексировать выход, используя [x, y], но это дает индекс из -за ошибки. < /p>
ожидается Количество вывода в выходе в размере 268,324
Модель-это глубина_анитшинг_v2_vits.onnx
https://github.com/fabio-sim/depth -Ва-onnx/releases/Tag/v2.0.0
Интересно, что не так с моим кодом?
Код: Выделить всё
Math.Floor(output.ElementAt(x * y) * 50)
Подробнее здесь: https://stackoverflow.com/questions/788 ... odel-fails
Мобильная версия