I’m currently working on a configurable input/output pipelining system for neural networks so we can run inference on arbitrary models with OpenCV and handle any pre/post processing without hardcoding in C++.

Reading around I’ve run into some conflicting information about NCHW and NHWC tensor formats. I know OpenCV uses NCHW and Tensorflow uses NCHW.

From what I understand OpenCV deals with this conflict by automatically inserting a transposition at the head of the graph (or possibly before each convolution?). What I don’t understand is how the decision to transpose is made.

I know that dnn::readNet() dispatches to dnn::readNetTensorflow() if the file has a “.pb” or “.pbtxt” suffix. Is the NHWC → NCHW transposition done based on this simple filename check, or is there some more complex logic?

From my experiments it appears that a Tensorflow “Frozen Graph” that is converted to ONNX will be in NHWC format. Is OpenCV able to detect tensor shape for an ONNX model and insert the appropriate transpositions?

I’d appreciate it if anyone clarify where the transpositions are inserted into the graph (i.e. just in-front or for all convolutions?) and what the condition for automatic transposition is (i.e. file extension, graph inspection, etc.?)

Thanks