Converting NHWC to NCHW

Converting a NHWC Tensorflow 2.8 model to NCHW ONNX model for later generating Tensor RT file

I’ve got a nice model with input [1,H,W,C], which works pretty well. Now, I want to convert it to ONNX for later using trtgen ang getting the final file for RT inferencing. (Yes, I know there’s another way to tranform but that’s the way I leant)

The problem is I never before realized TensorRT excepts channel first for properly working…
Is there any fast solution a can use to adapt my initial model to that format or, shall I train again the model with the desired format?

Windows 10

TensorRT Version: 7.2
GPU Type: RTX 3035 Ti
Nvidia Driver Version: 528.24
CUDA Version: 11.2
CUDNN Version:
Operating System + Version:
Python Version (if applicable):
TensorFlow Version (if applicable): 2.8
PyTorch Version (if applicable):
Baremetal or Container (if container which image + tag):

I found the answer.
The problem was I read it was necessary using changing the input layer and retraining the model from the begining, but it is not necessary.
When you have a trained model NHCW and want it to be INPUT NCHW, the only thing you have to do is to get the ‘old’ model and just add two layers before:

inputs = tf.keras.layers.Input((3,IMG_HEIGHT, IMG_WIDTH))#((IMG_HEIGHT, IMG_WIDTH, 3))

inputs = tf.keras.layers.Permute((2,3,1))(inputs)
Then, add the initial model, with the the trained weights and, that’s all.

Maybe there’s is some utility somewhere but, honestly, couldn’t find it.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.