Problems with SSD Mobilenet v2 UFF

Hi @AastaLLL,

Is there similar solution for SSD Inception V2? Everything I can find deals only with Mobilenet V2-based network.

I compiled a custom plugin for FlattenConcat, and also set

numClasses=2,
inputOrder=[0, 2, 1]

in the config for ssd_inception_v2_coco_2017_11_17 (I trained it with 1 class). However, the good old exception keeps popping up:

[TensorRT] INFO: Detected 1 input and 2 output network tensors.
python3: nmsPlugin.cpp:140: virtual void nvinfer1::plugin::DetectionOutput::configureWithFormat(const nvinfer1::Dims*, int, const nvinfer1::Dims*, int, nvinfer1::DataType, nvinfer1::PluginFormat, int): Assertion `numPriors * param.numClasses == inputDims[param.inputOrder[1]].d[0]' failed.

Changing inputOrder doesn’t help (any of the 6 permutations).

So, the solution for InceptionV2 SSD is as follows:

define concat nodes this way:

concat_priorbox = gs.create_node(name="concat_priorbox", op="ConcatV2", dtype=tf.float32, axis=2)
concat_box_loc = gs.create_plugin_node("concat_box_loc", op="FlattenConcat_TRT", dtype=tf.float32, axis=1, ignoreBatch=0)
concat_box_conf = gs.create_plugin_node("concat_box_conf", op="FlattenConcat_TRT", dtype=tf.float32, axis=1, ignoreBatch=0)

like here: TensorRT/config.py at v5.1.5 · NVIDIA/TensorRT · GitHub

NOT like from sample/python/uff_ssd from some older release… What is different is that

axis=1, ignoreBatch=0

are in concat_box_loc and concat_box_conf.

The correct inputOrder is [0, 2, 1]

And it works now.

Hi kirillfish,

Nice! Thanks for the sharing!

Hi Tonto5000!
Have your problem solved? I encountered the similar problem as follow when I have converted the .uff file form .pb file and then to do inference,My ssd_mobilenet_v2 is trained from VOC dataset with the old version Tensorflow Object Detection API.

[TensorRT] INFO: Detected 1 input and 2 output network tensors.
python: nmsPlugin.cpp:140: virtual void nvinfer1::plugin::DetectionOutput::configureWithFormat(const nvinfer1::Dims*, int, const nvinfer1::Dims*, int, nvinfer1::DataType, nvinfer1::PluginFormat, int): Assertion `numPriors * param.numClasses == inputDims[param.inputOrder[1]].d[0]' failed.
Aborted (core dumped)

The NMS part as follow in the .pbtxt file When I converted the .uff file

graphs {
  id: "main"
  nodes {
    id: "NMS"
    inputs: "Squeeze"
    inputs: "concat_priorbox"
    inputs: "concat_box_loc"
    operation: "_NMS_TRT"
    fields {
      key: "backgroundLabelId_u_int"
      value {
        i: 0
      }
    }
    fields {
      key: "confSigmoid_u_int"
      value {
        i: 1
      }
    }
    fields {
      key: "confidenceThreshold_u_float"
      value {
        d: 1e-08
      }
    }
    fields {
      key: "dtype"
      value {
        dtype: DT_FLOAT32
      }
    }
    fields {
      key: "inputOrder_u_ilist"
      value {
        i_list {
          val: 0
          val: 2
          val: 1
        }
      }
    }
    fields {
      key: "isNormalized_u_int"
      value {
        i: 1
      }
    }
    fields {
      key: "keepTopK_u_int"
      value {
        i: 100
      }
    }
    fields {
      key: "nmsThreshold_u_float"
      value {
        d: 0.6
      }
    }
    fields {
      key: "numClasses_u_int"
      value {
        i: 20
      }
    }
    fields {
      key: "shareLocation_u_int"
      value {
        i: 1
      }
    }
    fields {
      key: "topK_u_int"
      value {
        i: 100
      }
    }
    fields {
      key: "varianceEncodedInTarget_u_int"
      value {
        i: 0
      }
    }
  }

And my config.py file like this

import graphsurgeon as gs
import tensorflow as tf

path = 'model/ssd_mobilenet_v2_coco_2018_03_29/frozen_inference_graph.pb'
TRTbin = 'TRT_ssd_mobilenet_v2_coco_2018_03_29.bin'
output_name = ['NMS']
dims = [3,300,300]
layout = 7

def add_plugin(graph):
    all_assert_nodes = graph.find_nodes_by_op("Assert")
    graph.remove(all_assert_nodes, remove_exclusive_dependencies=True)

    all_identity_nodes = graph.find_nodes_by_op("Identity")
    graph.forward_inputs(all_identity_nodes)

    Input = gs.create_node(
        name="Input",
        op="Placeholder",
        dtype=tf.float32,
        shape=[1, 3, 300, 300]
    )

    PriorBox = gs.create_plugin_node(
        name="GridAnchor",
        op="GridAnchor_TRT",
        dtype=tf.float32,
        minSize=0.2,
        maxSize=0.95,
        aspectRatios=[1.0, 2.0, 0.5, 3.0, 0.33],
        variance=[0.1,0.1,0.2,0.2],
        featureMapShapes=[19, 10, 5, 3, 2, 1],
        numLayers=6
    )

    NMS = gs.create_plugin_node(
        name="NMS",
        op="NMS_TRT",
        dtype=tf.float32,
        shareLocation=1,
        varianceEncodedInTarget=0,
        backgroundLabelId=0,
        confidenceThreshold=1e-8,
        nmsThreshold=0.6,
        topK=100,
        keepTopK=100,
        numClasses=20,
	inputOrder=[0, 2, 1],
        #inputOrder=[1, 0, 2],
        confSigmoid=1,
        isNormalized=1,
        #scoreConverter="SIGMOID"
    )

    concat_priorbox = gs.create_node(
        name="concat_priorbox",
        op="ConcatV2",
        dtype=tf.float32,
        axis=2
    )

    concat_box_loc = gs.create_plugin_node(
        "concat_box_loc",
        op="FlattenConcat_TRT",
        dtype=tf.float32, 
        axis=1, 
        ignoreBatch=0
    )

    concat_box_conf = gs.create_plugin_node(
        "concat_box_conf",
        op="FlattenConcat_TRT",
        dtype=tf.float32, #之前应该就是没有确定这个dataType,所以出现无法转换成tensorFow数据类型的提示
        axis=1, 
        ignoreBatch=0
    )

    namespace_plugin_map = {
        "MultipleGridAnchorGenerator": PriorBox,
        "Postprocessor": NMS,
        "Preprocessor": Input,
        "ToFloat": Input,
        "image_tensor": Input,
        "Concatenate": concat_priorbox,
        "concat": concat_box_loc,
        "concat_1": concat_box_conf
    }

    #graph.collapse_namespaces(namespace_plugin_map)
    #graph.remove(graph.graph_outputs, remove_exclusive_dependencies=False)
    #graph.find_nodes_by_op("NMS_TRT")[0].input.remove("Input")

    #all_assert_nodes = graph.find_nodes_by_op("Assert")
    #graph.remove(all_assert_nodes, remove_exclusive_dependencies=True)

    #all_identity_nodes = graph.find_nodes_by_op("Identity")
    #graph.forward_inputs(all_identity_nodes)

    graph.collapse_namespaces(namespace_plugin_map)
    graph.remove(graph.graph_outputs, remove_exclusive_dependencies=False)
    graph.find_nodes_by_op("NMS_TRT")[0].input.remove("Input")
 
    return graph

And where can I find the nmsPlugin.cpp? Can you help me? thans!!!

Hi ashispapu how have you solved your problems,I encountered the similar problems can you Explain it?The NMS part as follow in the .pbtxt file When I converted the .uff file.

graphs {
  id: "main"
  nodes {
    id: "NMS"
    inputs: "Squeeze"
    inputs: "concat_priorbox"
    inputs: "concat_box_loc"
    operation: "_NMS_TRT"
    fields {
      key: "backgroundLabelId_u_int"
      value {
        i: 0
      }
    }
    fields {
      key: "confSigmoid_u_int"
      value {
        i: 1
      }
    }
    fields {
      key: "confidenceThreshold_u_float"
      value {
        d: 1e-08
      }
    }
    fields {
      key: "dtype"
      value {
        dtype: DT_FLOAT32
      }
    }
    fields {
      key: "inputOrder_u_ilist"
      value {
        i_list {
          val: 0
          val: 2
          val: 1
        }
      }
    }
    fields {
      key: "isNormalized_u_int"
      value {
        i: 1
      }
    }
    fields {
      key: "keepTopK_u_int"
      value {
        i: 100
      }
    }
    fields {
      key: "nmsThreshold_u_float"
      value {
        d: 0.6
      }
    }
    fields {
      key: "numClasses_u_int"
      value {
        i: 20
      }
    }
    fields {
      key: "shareLocation_u_int"
      value {
        i: 1
      }
    }
    fields {
      key: "topK_u_int"
      value {
        i: 100
      }
    }
    fields {
      key: "varianceEncodedInTarget_u_int"
      value {
        i: 0
      }
    }

Different from others is my NMS part in the .pbtxt like this:

inputs: "Squeeze"
    inputs: "concat_priorbox"
    inputs: "concat_box_loc"

Others like this:

inputs: "concat_box_conf"
    inputs: "Squeeze"
    inputs: "concat_priorbox"

can you help me,thanks very much!!!

Hi
I have solved my problem,the problem lies in the “numClasses=21,” and “inputOrder=[0, 2, 1],” firstly the inputOrder=[0, 1, 2] should correctly according to the NMS part in the .pbtxt file,secondly,the numClasses should added 1 to the detecte class number,for example:your detecte class number is 20,you should added 1 for the background.

Hope this is helpful for ones who encountered the same problem as me!!!

1 Like

I am facing a similar issue, I have trained ssd_inception_v2_coco model with 6 classes with dimensions(418, 418):
here in my temporary .pbtxt file, the order of NMS_TRT is

nodes {
    id: "NMS"
    inputs: "concat_priorbox"
    inputs: "Squeeze"
    inputs: "concat_box_conf"
    operation: "_NMS_TRT"

and I am providing parameters for conversion:

'ssd_inception_v2_coco': {
        'input_pb':   os.path.abspath(os.path.join(
                          DIR_NAME, 'ssd_inception_v2_coco.pb')),
        'tmp_uff':    os.path.abspath(os.path.join(
                          DIR_NAME, 'tmp_inception_v2_coco.uff')),
        'output_bin': os.path.abspath(os.path.join(
                          DIR_NAME, 'TRT_ssd_inception_v2_coco.bin')),        
        'num_classes': 7,
        'min_size': 0.2,
        'max_size': 0.95,        
        'input_order': [2, 0, 1], # for custom

and getting error in the last step as:-

DEBUG [/usr/lib/python3.6/dist-packages/uff/converters/tensorflow/converter.py:96] Marking ['NMS'] as outputs
No. nodes: 721
UFF Output written to /home/sys-admin/Downloads/Projects/tensorrt_demos/ssd/tmp_inception_v2_coco.uff
UFF Text Output written to /home/sys-admin/Downloads/Projects/tensorrt_demos/ssd/tmp_inception_v2_coco.pbtxt
[TensorRT] INFO: Detected 1 inputs and 2 output network tensors.
python3: nmsPlugin.cpp:139: virtual void nvinfer1::plugin::DetectionOutput::configureWithFormat(const nvinfer1::Dim
s*, int, const nvinfer1::Dims*, int, nvinfer1::DataType, nvinfer1::PluginFormat, int): Assertion `numPriors * numLocClasses * 4 == inputDims[param.inputOrder[0]].d[0]' failed.
./build_engines.sh: line 5: 19996 Aborted                 (core dumped) python3 build_engine.py ${model}

where I am doing wrong???

Hi,

First, you will need to update the class number into 6:

config.py

numClasses=91,

The you might also need to update the input order.
Just make sure the order of inputs are {loc_data, conf_data, priorbox_data}.

inputOrder=[0, 2, 1]

You can check this page for more information:
https://github.com/NVIDIA/TensorRT/tree/master/plugin/nmsPlugin

Thanks.

I faced the same issue. All works if I use 300x300, but if I set higher resoltuion, for example 480x480, appears this error. How can I fix it?
Here is my config: import tensorflow as tfimport tensorrt as trtimport graphsurgeon as gspa - Pastebin.com

It’s Inception but exactly the same happens with MobilenetV1. Any resolution increase belongs to:

python3: nmsPlugin.cpp:139: virtual void nvinfer1::plugin::DetectionOutput::configureWithFormat(const nvinfer1::Dims*, int, const nvinfer1::Dims*, int, nvinfer1::DataType, nvinfer1::PluginFormat, int): Assertion `numPriors * numLocClasses * 4 == inputDims[param.inputOrder[0]].d[0]’ failed.

1 Like

Hi, @WildChlamydia @AastaLLL
I encountered the same problem, did you solve it? How can I optimise model for custom resolution?
Please give my some hints for modification, Sincere thanks for you!

Hi.

I have TensorRT version 7, and it doesn’t have any FlattenConcat files in “/usr/src/tensorrt/samples/python/uff_ssd” directory.

3 Likes

Hi,

I got reason, why FlattenConcat is not in the directory, because FlattenConcat is by default installed in the TensorRT 7.1.

1 Like

@alan1yiud,
Did you happen to find the solution to this problem??

I am unable to run inference sign sampleUFFSSD c++ version to reproduce the detections using custom trained ssd_inception_2017_11_17 model.

Standard model works fine. but not cuśtom trained.
This problem is only with the c++ code but not with the python version. in python it works perfectly.

Do you know the solution for this??

@WildChlamydia,
Did you solve this issue???

I am facing the same problem with custom trained mobilenet model.

This only works if the SSD only has 1 class (faces in my case), check Jeroen Bédorf’s tutorial first for better understanding

Hi, I solved this issue and created a GitHub repository with all the fixed python scripts, also many useful sources that explain how to solve common issues:

Link is here: brokenerk / TRT-SSD-MobileNetV2

Hope it’ll help you