从一维数组重建多维张量

问题描述 投票:0回答:2

我正在尝试在 C# 中手动解码 Yolo 对象检测 ONNX 模型的输出。 Netron 描述输出如下:

type: float32[1,3,80,80,19]

但在 C# 代码中,我收到的模型输出是一维数组:

 float[364800]

和 364800 = 1 * 3 * 80 * 80 * 19

我的编程经验是使用 VB.NET 和少量的 C#。我是 ML 和对象检测的新手,我没有太多使用张量或 Python 的经验,因此我正在尝试用 C# 构建解决方案。

有人可以指出我重建多维张量数组的正确方向,以便我可以迭代结果吗?一维数组中的数据如何存储?

我还想知道我是否会以艰难的方式去做。如果 .NET 世界中有某种张量操作工具,我会很高兴知道它。

感谢您的帮助!

c# arrays tensor onnx
2个回答
0
投票

如果您调用 Tensor.Dimensions.Length,您将获得 float32[1,3,80,80,19] 值。对于张量的迭代和处理,这里有一个迭代除法然后返回新张量结果的例子:

    public static Tensor<float> DivideTensorByFloat(float[] data, float value, int[] dimensions)
    {
        for (int i = 0; i < data.Length; i++)
        {
            data[i] = data[i] / value;
        }

        return CreateTensor(data, dimensions);
    }

    public static DenseTensor<T> CreateTensor<T>(T[] data, int[] dimensions)
    {
        var tensor = new DenseTensor<T>(data, dimensions);
        return tensor;
    }

    DivideTensorByFloat(Tensor.ToArray(), value, Tensor.Dimensions.ToArray());

-1
投票

您可以使用 numpy 在 python 中处理数组。 安装

pip install numpy

为了将一维数组转换为多维数组,您可以使用以下方式进行转换:

oned_array = np.arange(6)
oned_array:
array([0,1,2,3,4,5])

multi_dimn_array = oned_array .reshape((3,2))
multi_dimn_array :
array([[0, 1],
       [2, 3],
       [4, 5]])

有用的资源:

  1. 麻木
  2. Scipy
© www.soinside.com 2019 - 2024. All rights reserved.