How to change the shape of a tensor?

import numpy as np

# Create a 3-channel 4x4 image data
image_data = np.array([[[1, 2, 3, 4],
                        [5, 6, 7, 8],
                        [9, 10, 11, 12],
                        [13, 14, 15, 16]],

                       [[17, 18, 19, 20],
                        [21, 22, 23, 24],
                        [25, 26, 27, 28],
                        [29, 30, 31, 32]],

                       [[33, 34, 35, 36],
                        [37, 38, 39, 40],
                        [41, 42, 43, 44],
                        [45, 46, 47, 48]]], dtype=np.uint8)

# CHW arrangement
print("CHW arrangement:")
print(image_data)

# HWC arrangement
image_data_hwc = np.transpose(image_data, (1, 2, 0))
print("\nHWC arrangement:")
print(image_data_hwc)

# Flatten CHW-arranged data
flat_data_chw = image_data.flatten()
print("Flattened data (CHW arrangement):")
print(flat_data_chw)

# Flatten HWC-arranged data
flat_data_hwc = image_data_hwc.flatten()
print("\nFlattened data (HWC arrangement):")
print(flat_data_hwc)

I want to implement such a function, but I searched and couldn’t find such an API.

I have tried the following code

// Create a 2 × 2 × 2 tensor
TensorShape shape = new TensorShape(2, 2, 2);
int[] data = new int[] { 1, 2, 3, 4, 10, 20, 30, 40 };
TensorInt tensor = new TensorInt(shape, data);

// Create a copy of the tensor with the new shape 2 × 1 × 4
TensorShape newShape = new TensorShape(2, 1, 4);
Tensor reshapedTensor = tensor.ShallowReshape(newShape);

But it doesn’t do what I want

Samples\Do an operation on a tensor

  • op.Transpose(…)
    Samples\Convert textures to tensors
  • TextureConverter.ToTensor(texture, new TextureTransform().SetTensorLayout(TensorLayout.NHWC));
  • tensor.shallowreshape does a in-place reshape of the tensor

你可以给我一个完整的实例代码吗?

  • Samples\Convert textures to tensors
  • TextureConverter.ToTensor(texture, new TextureTransform().SetTensorLayout(TensorLayout.NHWC));
    So, how to use ops.Transpose code?
    void ChangeTensorStorageOrder()
    {
        // Create a 2 × 2 × 2 tensor
        TensorShape shape = new TensorShape(3, 4, 4);
        float[] f = new float[48];
        for (int n = 0; n < 48; n++)
        {
            f[n] = n+1;
        }
        TensorFloat tensor = new TensorFloat(shape, f);

        m_Allocator = new TensorCachingAllocator();

        // CreateOps allows direct operations on tensors.
        s_Ops = WorkerFactory.CreateOps(backendType, m_Allocator);

        // Execute an operator on the input tensor.
        //var outputTensor = s_Ops.ArgMax(m_InputTensor, axis: 0, keepdim: true);
        // Ensure tensor is on CPU before indexing.
        Tensor tf=  s_Ops.Transpose(tensor);
        tf.MakeReadable();
        float[] sd = (tf as TensorFloat).ToReadOnlyArray();
        string str = "";
        foreach (var v in sd)
        {
            str += v + ",";
        }
        Debug.Log(str);
    }

I have tried the above code, but it does not meet my requirements. I don’t know how to set the form I want to convert. The output is as follows

    void ChangeTensorStorageOrder()
    {
        // Create a 2 × 2 × 2 tensor
        TensorShape shape = new TensorShape(1,3, 4, 4);
        float[] f = new float[48];
        for (int n = 0; n < 48; n++)
        {
            f[n] = n+1;
        }
        TensorFloat tensor = new TensorFloat(shape, f);

        m_Allocator = new TensorCachingAllocator();

        // CreateOps allows direct operations on tensors.
        s_Ops = WorkerFactory.CreateOps(backendType, m_Allocator);

        // Execute an operator on the input tensor.
        //var outputTensor = s_Ops.ArgMax(m_InputTensor, axis: 0, keepdim: true);
        // Ensure tensor is on CPU before indexing.
        //Tensor tf=  s_Ops.Transpose(tensor);
        TextureTransform settings = new TextureTransform().SetDimensions(4, 4, 3).SetTensorLayout(TensorLayout.NHWC);
        var vbt= TextureConverter.ToTexture(tensor);
        var tf = TextureConverter.ToTensor(vbt, settings);
        // 创建形状为 [1, image.height, image.width, 3] 的张量
        
        tf.MakeReadable();
        float[] sd = (tf as TensorFloat).ToReadOnlyArray();
        string str = "";
        foreach (var v in sd)
        {
            str += v + ",";
        }
        Debug.Log(str);
    }

The above code can achieve my requirements, their conversion is correct, but I have to convert the tensor into a chw texture first, and then convert the texture into an hwc tensor. Is there a better way? For example, can you operate it directly through ops? But I didn’t find an api that can be used


This method can only convert tensor data with channels up to 4. I want to convert a chw data of 2016, 28, 28 into hwc data of 28, 28, 2016. I don’t know how to operate.

To learn how to use Transpose, you can refer here: