Parsing custom tensorflow model

Please provide complete information as applicable to your setup.

• Hardware Platform (Jetson / GPU) Jetson
• DeepStream Version 6.1.1
• JetPack Version (valid for Jetson only) 5.0.2
• TensorRT Version 8.4

I have a custom tf2 model like this.

`
Model: “model”


Layer (type) Output Shape Param # Connected to

input_1 (InputLayer) [(None, None, None, 0
3)]

random_flip (RandomFlip) (None, None, None, 0 [‘input_1[0][0]’]
3)

random_rotation (RandomRotatio (None, None, None, 0 [‘random_flip[0][0]’]
n) 3)

random_zoom (RandomZoom) (None, None, None, 0 [‘random_rotation[0][0]’]
3)

resizing (Resizing) (None, 64, 64, 3) 0 [‘random_zoom[0][0]’]

rescaling (Rescaling) (None, 64, 64, 3) 0 [‘resizing[0][0]’]

conv2d (Conv2D) (None, 62, 62, 64) 1792 [‘rescaling[0][0]’]

batch_normalization (BatchNorm (None, 62, 62, 64) 256 [‘conv2d[0][0]’]
alization)

max_pooling2d (MaxPooling2D) (None, 20, 20, 64) 0 [‘batch_normalization[0][0]’]

conv2d_1 (Conv2D) (None, 18, 18, 128) 73856 [‘max_pooling2d[0][0]’]

max_pooling2d_1 (MaxPooling2D) (None, 9, 9, 128) 0 [‘conv2d_1[0][0]’]

conv2d_2 (Conv2D) (None, 7, 7, 256) 295168 [‘max_pooling2d_1[0][0]’]

max_pooling2d_2 (MaxPooling2D) (None, 3, 3, 256) 0 [‘conv2d_2[0][0]’]

flatten (Flatten) (None, 2304) 0 [‘max_pooling2d_2[0][0]’]

dense (Dense) (None, 128) 295040 [‘flatten[0][0]’]

dense_1 (Dense) (None, 128) 295040 [‘flatten[0][0]’]

dropout (Dropout) (None, 128) 0 [‘dense[0][0]’]

dropout_1 (Dropout) (None, 128) 0 [‘dense_1[0][0]’]

gender_output (Dense) (None, 2) 258 [‘dropout[0][0]’]

age_output (Dense) (None, 1) 129 [‘dropout_1[0][0]’]

==================================================================================================
Total params: 961,539
Trainable params: 961,411
Non-trainable params: 128
`

and i am able to convert this model to onnx using this.

!python3 -m tf2onnx.convert --saved-model ./model/ --output model.onnx --opset 12

even i am able to convert the model into tensorrt engine using this.

/usr/src/tensorrt/bin/trtexec --onnx=./models/age_gender/age_gender_model.onnx --saveEngine=age_gender_model.onnx_b1_gpu0_fp32.engine --explicitBatch

but while running in deepstream i got this error.

`
{‘input’: [‘file:///home/sensormatic/Videos/audience-001.mp4’], ‘configfile’: None, ‘pgie’: None, ‘no_display’: False, ‘file_loop’: False, ‘disable_probe’: False, ‘silent’: False}
Creating Pipeline

Creating streamux

Creating source_bin 0

Creating source bin
source-bin-00
Creating Pgie

Creating tiler

Creating nvvidconv

Creating nvosd

Creating nv3dsink

Adding elements to Pipeline

Linking elements in the Pipeline

Now playing…
0 : file:///home/sensormatic/Videos/audience-001.mp4
Starting pipeline

0:00:05.441777844 1837530 0x3f93b950 INFO nvinfer gstnvinfer.cpp:666:gst_nvinfer_logger: NvDsInferContext[UID 2]: Info from NvDsInferContextImpl::deserializeEngineAndBackend() <nvdsinfer_context_impl.cpp:1909> [UID = 2]: deserialized trt engine from :/home/sensormatic/Neo/deepstream_python_apps/apps/deepstream-raj_age_gender/models/age_gender/raj_age_gender_model.onnx_b1_gpu0_fp32.engine
WARNING: [TRT]: The getMaxBatchSize() function should not be used with an engine built from a network created with NetworkDefinitionCreationFlag::kEXPLICIT_BATCH flag. This function will always return 1.
INFO: [Implicit Engine Info]: layers num: 3
0 INPUT kFLOAT input_1 1x1x3
1 OUTPUT kFLOAT age_output 1
2 OUTPUT kFLOAT gender_output 2

0:00:05.513273462 1837530 0x3f93b950 INFO nvinfer gstnvinfer.cpp:666:gst_nvinfer_logger: NvDsInferContext[UID 2]: Info from NvDsInferContextImpl::generateBackendContext() <nvdsinfer_context_impl.cpp:2012> [UID = 2]: Use deserialized engine model: /home/sensormatic/Neo/deepstream_python_apps/apps/deepstream-raj_age_gender/models/age_gender/raj_age_gender_model.onnx_b1_gpu0_fp32.engine
0:00:05.513766713 1837530 0x3f93b950 ERROR nvinfer gstnvinfer.cpp:660:gst_nvinfer_logger: NvDsInferContext[UID 2]: Error in NvDsInferContextImpl::preparePreprocess() <nvdsinfer_context_impl.cpp:971> [UID = 2]: RGB/BGR input format specified but network input channels is not 3
ERROR: Infer Context prepare preprocessing resource failed., nvinfer error:NVDSINFER_CONFIG_FAILED
0:00:05.520475338 1837530 0x3f93b950 WARN nvinfer gstnvinfer.cpp:866:gst_nvinfer_start: error: Failed to create NvDsInferContext instance
0:00:05.520604747 1837530 0x3f93b950 WARN nvinfer gstnvinfer.cpp:866:gst_nvinfer_start: error: Config file path: age_gender_sgi_tensormeta_config.txt, NvDsInfer Error: NVDSINFER_CONFIG_FAILED
0:00:05.520753196 1837530 0x3f93b950 WARN GST_PADS gstpad.c:1142:gst_pad_set_active:secondary1-nvinference-engine:sink Failed to activate pad

**PERF: {‘stream0’: 0.0}

Error: gst-resource-error-quark: Failed to create NvDsInferContext instance (1): gstnvinfer.cpp(866): gst_nvinfer_start (): /GstPipeline:pipeline0/GstNvInfer:secondary1-nvinference-engine:
Config file path: age_gender_sgi_tensormeta_config.txt, NvDsInfer Error: NVDSINFER_CONFIG_FAILED
Exiting app

`

i have not mentioned the output layers here. if i mention the output blob names as age_output/gender_output it gives no output layers error.

if i remove the output blob parameter and ran i got the above error.

please find the sgie config.

`
[property]
gpu-id=0
net-scale-factor=1
onnx-file=/home/sensormatic/Neo/deepstream_python_apps/apps/deepstream-raj_age_gender/models/age_gender/age_gender_model.onnx
model-engine-file=./models/age_gender/raj_age_gender_model.onnx_b1_gpu0_fp32.engine
force-implicit-batch-dim=0
batch-size=1

0=FP32 and 1=INT8 2=FP16 mode

network-mode=0
network-type=1 # 0: Detector 1: Classifier 2: Segmentation 3: Instance Segmentation
#input-object-min-width=64
#input-object-min-height=64

1=Primary 2=Secondary

process-mode=2
model-color-format=1
gpu-id=0
gie-unique-id=2
operate-on-gie-id=1
operate-on-class-ids=0
#is-classifier=1
#output-blob-names=age_output/gender_output
#output-blob-names=fc1
classifier-async-mode=0
classifier-threshold=0.51
#scaling-filter=0
#scaling-compute-hw=0
output-tensor-meta=1
custom-lib-path=/opt/nvidia/deepstream/deepstream/lib/libnvds_infercustomparser.so
`

there is the error tip, can you set infer-dims? please refer to the doc.

how do i set None,None,None,0,3 as infer-dims

why is the input resolution so small? is the input data image? what is “1,1,3” separately? you need to set “channel; height; width;” to infer-dims.

but see my input_1 layer input is (None, None, None, 0, 3), how do i set these dimension using infer-dims?

did you try infer-dims=1;1;3? why is the input resolution so small? is the input data image? what is “1,1,3” separately?

i did not set infer-dims in config. i am not sure about of input, the model has the preprocessing layers in it.

The model has the input size (None,None,None,3)

you can use Netron tool to check the onnx model’s input.

netron giving the same input

did you test tf2 or onnx model by thirdpart tools? can model give the result inference results? is the input data image? The nvinfer plugin only can accept batched NV12/RGBA buffers from upstream. please refer to the doc.

i have tried inferencing using tf2, onnx and openvino all are working fine and input is h264 sample video only.

@fanzh TF models uses channel last convention do i need to change it to channel first.

and can you look into the model summary and let me know how do i set the dynamic (none) shaped in infer-dims.

I set infer-dims=3;64;64 and ran but got the below error.

WARNING: Backend context bufferIdx(0) request dims:1x3x64x64 is out of range, [min: 1x1x1x3, max: 1x1x1x3]
0:00:06.413139473 3388440 0x1aad9950 WARN nvinfer gstnvinfer.cpp:643:gst_nvinfer_logger: NvDsInferContext[UID 2]: Warning from NvDsInferContextImpl::checkBackendParams() <nvdsinfer_context_impl.cpp:1865> [UID = 2]: backend can not support dims:3x64x64
0:00:06.413437906 3388440 0x1aad9950 WARN nvinfer gstnvinfer.cpp:643:gst_nvinfer_logger: NvDsInferContext[UID 2]: Warning from NvDsInferContextImpl::generateBackendContext() <nvdsinfer_context_impl.cpp:2018> [UID = 2]: deserialized backend context :/home/sensormatic/Neo/deepstream_python_apps/apps/deepstream-raj_age_gender/models/age_gender/raj_age_gender_model.onnx_b1_gpu0_fp32.engine failed to match config params, trying rebuild
0:00:06.417884263 3388440 0x1aad9950 INFO nvinfer gstnvinfer.cpp:646:gst_nvinfer_logger: NvDsInferContext[UID 2]: Info from NvDsInferContextImpl::buildModel() <nvdsinfer_context_impl.cpp:1923> [UID = 2]: Trying to create engine from model files
WARNING: [TRT]: onnx2trt_utils.cpp:367: Your ONNX model has been generated with INT64 weights, while TensorRT does not natively support INT64. Attempting to cast down to INT32.
ERROR: [TRT]: 4: [network.cpp::validate::3004] Error Code 4: Internal Error (input_1: for dimension number 3 in profile 0 does not match network definition (got min=64, opt=64, max=64), expected min=opt=max=3).)
ERROR: Build engine failed from config file
ERROR: failed to build trt engine.
0:00:09.158256821 3388440 0x1aad9950 ERROR nvinfer gstnvinfer.cpp:640:gst_nvinfer_logger: NvDsInferContext[UID 2]: Error in NvDsInferContextImpl::buildModel() <nvdsinfer_context_impl.cpp:1943> [UID = 2]: build engine file failed
0:00:09.224558126 3388440 0x1aad9950 ERROR nvinfer gstnvinfer.cpp:640:gst_nvinfer_logger: NvDsInferContext[UID 2]: Error in NvDsInferContextImpl::generateBackendContext() <nvdsinfer_context_impl.cpp:2029> [UID = 2]: build backend context failed
0:00:09.224654351 3388440 0x1aad9950 ERROR nvinfer gstnvinfer.cpp:640:gst_nvinfer_logger: NvDsInferContext[UID 2]: Error in NvDsInferContextImpl::initialize() <nvdsinfer_context_impl.cpp:1266> [UID = 2]: generate backend failed, check config file settings
0:00:09.224728879 3388440 0x1aad9950 WARN nvinfer gstnvinfer.cpp:846:gst_nvinfer_start: error: Failed to create NvDsInferContext instance
0:00:09.224774031 3388440 0x1aad9950 WARN nvinfer gstnvinfer.cpp:846:gst_nvinfer_start: error: Config file path: age_gender_sgi_config.txt, NvDsInfer Error: NVDSINFER_CONFIG_FAILED
0:00:09.224954768 3388440 0x1aad9950 WARN GST_PADS gstpad.c:1142:gst_pad_set_active:secondary1-nvinference-engine:sink Failed to activate pad

**PERF: {‘stream0’: 0.0}

Error: gst-resource-error-quark: Failed to create NvDsInferContext instance (1): /dvs/git/dirty/git-master_linux/deepstream/sdk/src/gst-plugins/gst-nvinfer/gstnvinfer.cpp(846): gst_nvinfer_start (): /GstPipeline:pipeline0/GstNvInfer:secondary1-nvinference-engine:
Config file path: age_gender_sgi_config.txt, NvDsInfer Error: NVDSINFER_CONFIG_FAILED
Exiting app

the is the error tip, it is because infer-dims 's setting is inconsistent with the model’s dimension. you can try infer-dims=1;1;3.

still same error

from the log, the input dimension is 1x1x3, can you share the new error log? or can you share the onnx model by forum private email?

model.onnx (3.68 MB)

model.onnx (3.7 MB)

please set infer-dims=64;64;3

@fanzh I have already tried this, and got the below error

{‘input’: [‘file:///home/sensormatic/Videos/audience-001_annotated.mp4’], ‘configfile’: None, ‘pgie’: None, ‘no_display’: False, ‘file_loop’: False, ‘disable_probe’: False, ‘silent’: False}
Creating Pipeline

Creating streamux

Creating source_bin 0

Creating source bin
source-bin-00
Creating Pgie

Creating tiler

Creating nvvidconv

Creating nvosd

Creating nv3dsink

Adding elements to Pipeline

Linking elements in the Pipeline

Now playing…
0 : file:///home/sensormatic/Videos/audience-001_annotated.mp4
Starting pipeline

WARNING: [TRT]: Using an engine plan file across different models of devices is not recommended and is likely to affect performance or even cause errors.
0:00:07.208327208 319651 0xa80a350 INFO nvinfer gstnvinfer.cpp:666:gst_nvinfer_logger: NvDsInferContext[UID 2]: Info from NvDsInferContextImpl::deserializeEngineAndBackend() <nvdsinfer_context_impl.cpp:1909> [UID = 2]: deserialized trt engine from :/home/sensormatic/Neo/deepstream_python_apps/apps/deepstream-raj_age_gender/models/age_gender/raj_age_gender_model.onnx_b1_gpu0_fp32.engine
WARNING: [TRT]: The getMaxBatchSize() function should not be used with an engine built from a network created with NetworkDefinitionCreationFlag::kEXPLICIT_BATCH flag. This function will always return 1.
INFO: [Implicit Engine Info]: layers num: 3
0 INPUT kFLOAT input_1 1x1x3
1 OUTPUT kFLOAT age_output 1
2 OUTPUT kFLOAT gender_output 2

WARNING: Backend context bufferIdx(0) request dims:1x64x64x3 is out of range, [min: 1x1x1x3, max: 1x1x1x3]
0:00:07.309755487 319651 0xa80a350 WARN nvinfer gstnvinfer.cpp:663:gst_nvinfer_logger: NvDsInferContext[UID 2]: Warning from NvDsInferContextImpl::checkBackendParams() <nvdsinfer_context_impl.cpp:1865> [UID = 2]: backend can not support dims:64x64x3
0:00:07.309861183 319651 0xa80a350 WARN nvinfer gstnvinfer.cpp:663:gst_nvinfer_logger: NvDsInferContext[UID 2]: Warning from NvDsInferContextImpl::generateBackendContext() <nvdsinfer_context_impl.cpp:2018> [UID = 2]: deserialized backend context :/home/sensormatic/Neo/deepstream_python_apps/apps/deepstream-raj_age_gender/models/age_gender/raj_age_gender_model.onnx_b1_gpu0_fp32.engine failed to match config params, trying rebuild
0:00:07.315189144 319651 0xa80a350 INFO nvinfer gstnvinfer.cpp:666:gst_nvinfer_logger: NvDsInferContext[UID 2]: Info from NvDsInferContextImpl::buildModel() <nvdsinfer_context_impl.cpp:1923> [UID = 2]: Trying to create engine from model files
WARNING: [TRT]: onnx2trt_utils.cpp:367: Your ONNX model has been generated with INT64 weights, while TensorRT does not natively support INT64. Attempting to cast down to INT32.
WARNING: [TRT]: The getMaxBatchSize() function should not be used with an engine built from a network created with NetworkDefinitionCreationFlag::kEXPLICIT_BATCH flag. This function will always return 1.
WARNING: [TRT]: The getMaxBatchSize() function should not be used with an engine built from a network created with NetworkDefinitionCreationFlag::kEXPLICIT_BATCH flag. This function will always return 1.
0:00:52.597136802 319651 0xa80a350 INFO nvinfer gstnvinfer.cpp:666:gst_nvinfer_logger: NvDsInferContext[UID 2]: Info from NvDsInferContextImpl::buildModel() <nvdsinfer_context_impl.cpp:1955> [UID = 2]: serialize cuda engine to file: /home/sensormatic/Neo/deepstream_python_apps/apps/deepstream-raj_age_gender/models/age_gender/age_gender_model.onnx_b1_gpu0_fp32.engine successfully
WARNING: [TRT]: The getMaxBatchSize() function should not be used with an engine built from a network created with NetworkDefinitionCreationFlag::kEXPLICIT_BATCH flag. This function will always return 1.
INFO: [Implicit Engine Info]: layers num: 3
0 INPUT kFLOAT input_1 64x64x3
1 OUTPUT kFLOAT age_output 1
2 OUTPUT kFLOAT gender_output 2

ERROR: [TRT]: 3: Cannot find binding of given name: fc1
0:00:52.609376249 319651 0xa80a350 WARN nvinfer gstnvinfer.cpp:663:gst_nvinfer_logger: NvDsInferContext[UID 2]: Warning from NvDsInferContextImpl::checkBackendParams() <nvdsinfer_context_impl.cpp:1876> [UID = 2]: Could not find output layer ‘fc1’ in engine
0:00:52.716346998 319651 0xa80a350 ERROR nvinfer gstnvinfer.cpp:660:gst_nvinfer_logger: NvDsInferContext[UID 2]: Error in NvDsInferContextImpl::preparePreprocess() <nvdsinfer_context_impl.cpp:971> [UID = 2]: RGB/BGR input format specified but network input channels is not 3
ERROR: Infer Context prepare preprocessing resource failed., nvinfer error:NVDSINFER_CONFIG_FAILED
0:00:52.734337575 319651 0xa80a350 WARN nvinfer gstnvinfer.cpp:866:gst_nvinfer_start: error: Failed to create NvDsInferContext instance
0:00:52.734424391 319651 0xa80a350 WARN nvinfer gstnvinfer.cpp:866:gst_nvinfer_start: error: Config file path: age_gender_sgi_config.txt, NvDsInfer Error: NVDSINFER_CONFIG_FAILED

**PERF: {‘stream0’: 0.0}

Error: gst-resource-error-quark: Failed to create NvDsInferContext instance (1): gstnvinfer.cpp(866): gst_nvinfer_start (): /GstPipeline:pipeline0/GstNvInfer:secondary1-nvinference-engine:
Config file path: age_gender_sgi_config.txt, NvDsInfer Error: NVDSINFER_CONFIG_FAILED
Exiting app

even i tried with this output-blob-names=age_output/gender_output