imageNet – failed to initialize on custom model

I have a custom model based on transfer learning (Resnet50). It has 7 classes for image classification. I have used tensorflow-onnx conversion to get a onnx file.

I then followed the tutorial from Dusty-nv Jetson AI Fundamentals - S3E3 - Training Image Classification Models.

However, I put my onnx file and my label files in the jetson-inference folder, then I follow the tutorials that says to:

magenet --model=models/pave/model_1.onnx --labels=labels/labels.txt --input_blob=input_0 --output_blob=output_0 /dev/video0

I get the following error:

[TRT]    4: [network.cpp::validate::3062] Error Code 4: Internal Error (Network has dynamic or shape inputs, but no optimization profile has been defined.)
[TRT]    device GPU, failed to build CUDA engine
[TRT]    device GPU, failed to load models/pave/model_1.onnx
[TRT]    failed to load models/pave/model_1.onnx
[TRT]    imageNet -- failed to initialize.
imagenet:  failed to initialize imageNet

Any help would be appreciated!

Hi @joaquin.bh3, the model was exported with dynamic shapes, but should have static input shapes. It expects ONNX models that were exported from PyTorch like here:

https://github.com/dusty-nv/jetson-inference/blob/master/docs/pytorch-collect.md#training-your-model

The input/output layer names are also probably different along with the pre-processing coefficients and formats.

It’s a pleasure to get your response, Dusty_nv!

Dang, so I am confused since the imagenet example says other networks can be added using --network including resnet50.

For my case, if I fix the input of my model (which is originally Tensorflow, resnet50, , would I be able to feed it through the imagenet example? or it’s best not to keep trying?

I don’t see in the imagenet.py why it wouldn’t work…

For instance, below is the conversion from h5 to ONNX.

import tf2onnx
import onnxruntime as rt
from tensorflow.keras.models import load_model
import tensorflow as tf

model = load_model('test512.h5')

spec = (tf.TensorSpec((None, 224, 224, 3), tf.float32, name="input"),)
output_path = model.name + ".onnx"

onnx_model, _ = tf2onnx.convert.from_keras(model, input_signature=spec, opset=13)
onnx.save(onnx_model, "model_3.onnx")

I’m not personally familiar with tf2onnx and don’t see in it’s documentation a flag to explicitly force static shapes, but I wonder if you changed the input dims to (1, 224, 224, 3) if that would make static-shaped input as opposed to dynamic. I also found this post:

Thanks very much, Dusty_nv. It did solve that error of dynamic input.

I believe its reading the model and labels but I get a constant 38.8% and a class 0 for any of the pictures or webcam that I test. Any ideas what it could be?

OK, cool that you got past the dynamic shapes. Next, I would check that the pre-processing applies matches what TensorFlow does:

For example, these coefficients for mean pixel subtraction and normalization are the same ones that PyTorch uses. Also check that your model expects NCHW data layout.

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