TripleAntigen
TripleAntigen

Reputation: 2301

Recreating multidimensional tensor from one dimensional array

I am trying to manually decode the output of a Yolo object detection ONNX model in C#. Netron describes the output as follows:

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

But in C# code the model output I receive is a single dimensional array:

 float[364800]

and 364800 = 1 * 3 * 80 * 80 * 19

My programming experience has been with VB.NET and a smattering of C#. I'm new to ML and object detection and I don't have much experience working with tensors or Python, hence I am trying to build a solution in C#.

Can somebody point me in the right direction in reconstructing the multidimensional tensor array so I can iterate over the results? How would the data in the one dimensional array be stored?

Also I wondered if I might be doing it the hard way. If there is some kind of tensor manipulation tool in the .NET world I would be happy to know about it.

Thanks for any help!

Upvotes: 0

Views: 995

Answers (2)

Cassie Breviu
Cassie Breviu

Reputation: 581

If you call Tensor.Dimensions.Length you will get the float32[1,3,80,80,19] value. For iteration over the tensor and processing, here is an example of iterating to divide and then return a new tensor result:

    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());

Upvotes: 0

PRAVEEN KUMAR
PRAVEEN KUMAR

Reputation: 39

You can use numpy to work with arrays in python. install

pip install numpy

Inorder to convert from 1D to multi-dimentional array you can conver using the following way:

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]])

Useful Resource:

  1. Numpy
  2. Scipy

Upvotes: -1

Related Questions