I attempted to use DepthAnything V2 model to approximate depth from given images.
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<float> LoadImage(string imagePath)
{
var image = new Bitmap(imagePath);
var inputTensor = new DenseTensor<float>(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<float> 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<float> output = inferenceSession.Run(inputs).First().AsTensor<float>();
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");
}
}
Instead of expected depth output, I instead receive this:
I attempted to directly index output using [x, y] but that gives an index out of bounds error.
there is expected amount of output in output in size of 268,324
The model is depth_anything_v2_vits.onnx
https://github.com/fabio-sim/Depth-Anything-ONNX/releases/tag/v2.0.0
I wonder what’s wrong with my code?
Math.Floor(output.ElementAt(x * y) * 50)
is a quick hack during debugging process in case it looks off