How to convert NCHW to NHWC in tvm?

Hi, @tqchen, @srkreddy1238 is there any way i can convert NCHW to NHWC in tvm. I have tvm compiled model it takes input in the form of NHWC, and cv::Mat is giving in form of NCHW 4 dimension array.

I believe you can use topi.transpose. This allows you to rearrange the axes of an input tensor.

Something along the lines of:

t_nhwc = topi.transpose(t_nchw, axis=(0, 2, 3, 1))

Where (0, 1, 2, 3) == (n, c, h, w), rearranging to the desired (n, h, w, c) == (0, 2, 3, 1).

@myproject24

Yes, you can access topi operators for almost all directly from python

but, I doubt you are asking about transpose (some pre processing) while deployment from c++ ?
For CPP I don’t think we have a TVM runtime API to transpose now. As an other option you may alter the graph before frontend import to add necessary pre processing and then compile.

1 Like

Hi @joshherr in python this fine this will work, I need it in C++ TVM runtime API, can I do something similar in C++ also?

Yes @srkreddy1238 i am looking for C++ TVM runtime API to do transpose.

Hi @srkreddy1238 and @joshherr can I use?

inline Tensor transpose(const Tensor& x,
                        Array<Integer> axes,
                        std::string name = "tensor",
                        std::string tag = kInjective)

from topi\include\topi\transform.h header ?

No, it doesn’t work this way.
I advice to modify the original graph by adding preprocessing nodes before input and use for now.

We could think of new RFC in near future about pre-processing support before frontend.

OK thank you for your advice @srkreddy1238. i will modify the graph and check.