Convert SSD-Mobilenet_v3 large to UFF

Hi everyone!
If I use the ssd_mobilenet_v3_large model,how should I write the config.py file when converting the pb file to uff file?
Can someone help me and give me an example? thanks!!!

Hi,

Sorry that we haven’t tried ssd_mobilenet_v3_large before.
Based on our experience, you should be able to convert the model by updating the following item:

  • Dimension of Input
  • InputOrder of NMS
  • NumClasses of NMS

Please let us know if you meet any issue when doing so.

Thanks.

Hi,AastaLLL
When I use the ssd_mobilenet_v3_large model,I’m able to convert the pb file to uff file use the follow config.py.

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=[None, 3, 300, 300]  
	shape=[None, 3, 320, 320]
    )

    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=0.3,  # was 1e-8
        nmsThreshold=0.6,
        topK=100,
        keepTopK=100,
        numClasses=91,  # was 91
        inputOrder=[0,2,1],
	#inputOrder=[1,0,2],
        confSigmoid=1,
        isNormalized=1
    )

    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
    )

    namespace_plugin_map = {
        "MultipleGridAnchorGenerator": PriorBox,
        "Postprocessor": NMS,
        "Preprocessor": Input,
        "ToFloat": Input,
        "image_tensor": Input,
	#"MultipleGridAnchorGenerator/Concatenate": concat_priorbox,
        #"MultipleGridAnchorGenerator/Identity": concat_priorbox,
	#"Concatenate/concat": concat_priorbox,
        "Concatenate": concat_priorbox,
	"concat": concat_box_loc,
	"concat_1": concat_box_conf
	#"Concatenate": concat_priorbox,
	#"concat": concat_box_loc,
        #"Squeeze": 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

when have converted the whole .pbtxt file only have follow parts,which is obviously different from the ssd_mobilenet_v2’s .pbfile

version: 1
descriptor_core_version: 1
descriptors {
  id: "tensorflow_extension"
  version: 1
}
descriptors {
  id: "custom"
  version: 1
}
graphs {
  id: "main"
  nodes {
    id: "NMS"
    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: 0.3
      }
    }
    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: 91
      }
    }
    fields {
      key: "shareLocation_u_int"
      value {
        i: 1
      }
    }
    fields {
      key: "topK_u_int"
      value {
        i: 100
      }
    }
    fields {
      key: "varianceEncodedInTarget_u_int"
      value {
        i: 0
      }
    }
  }
  nodes {
    id: "MarkOutput_0"
    inputs: "NMS"
    operation: "MarkOutput"
  }
}

And then when building the engine,I encountered the follow problem

[TensorRT] INFO: UFFParser: parsing NMS
[libprotobuf FATAL /home/erisuser/p4sw/sw/gpgpu/MachineLearning/DIT/externals/protobuf/aarch64/10.0/include/google/protobuf/repeated_field.h:1408] CHECK failed: (index) < (current_size_): 
Traceback (most recent call last):
  File "main_my_MultPic.py", line 42, in <module>
    parser.parse('tmp.uff', network)
RuntimeError: CHECK failed: (index) < (current_size_):

And the line 42 in my “main_my_MultPic.py” is like this

with trt.Builder(TRT_LOGGER) as builder, builder.create_network() as network, trt.UffParser() as parser:
        builder.max_workspace_size = 1 << 28
        builder.max_batch_size = 1
        builder.fp16_mode = True
        parser.register_input('Input', model.dims)
        parser.register_output('MarkOutput_0')
42      parser.parse('tmp.uff', network)
        engine = builder.build_cuda_engine(network)
        buf = engine.serialize()
        with open(model.TRTbin, 'wb') as f:
            f.write(buf)

Hi,

We can convert the ssd_mobilenet_v3_large model into .uff successfully.

config.py

#
# Copyright 1993-2019 NVIDIA Corporation.  All rights reserved.
#
# NOTICE TO LICENSEE:
#
# This source code and/or documentation ("Licensed Deliverables") are
# subject to NVIDIA intellectual property rights under U.S. and
# international Copyright laws.
#
# These Licensed Deliverables contained herein is PROPRIETARY and
# CONFIDENTIAL to NVIDIA and is being provided under the terms and
# conditions of a form of NVIDIA software license agreement by and
# between NVIDIA and Licensee ("License Agreement") or electronically
# accepted by Licensee.  Notwithstanding any terms or conditions to
# the contrary in the License Agreement, reproduction or disclosure
# of the Licensed Deliverables to any third party without the express
# written consent of NVIDIA is prohibited.
#
# NOTWITHSTANDING ANY TERMS OR CONDITIONS TO THE CONTRARY IN THE
# LICENSE AGREEMENT, NVIDIA MAKES NO REPRESENTATION ABOUT THE
# SUITABILITY OF THESE LICENSED DELIVERABLES FOR ANY PURPOSE.  IT IS
# PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY OF ANY KIND.
# NVIDIA DISCLAIMS ALL WARRANTIES WITH REGARD TO THESE LICENSED
# DELIVERABLES, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY,
# NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE.
# NOTWITHSTANDING ANY TERMS OR CONDITIONS TO THE CONTRARY IN THE
# LICENSE AGREEMENT, IN NO EVENT SHALL NVIDIA BE LIABLE FOR ANY
# SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, OR ANY
# DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
# WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
# ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
# OF THESE LICENSED DELIVERABLES.
#
# U.S. Government End Users.  These Licensed Deliverables are a
# "commercial item" as that term is defined at 48 C.F.R. 2.101 (OCT
# 1995), consisting of "commercial computer software" and "commercial
# computer software documentation" as such terms are used in 48
# C.F.R. 12.212 (SEPT 1995) and is provided to the U.S. Government
# only as a commercial end item.  Consistent with 48 C.F.R.12.212 and
# 48 C.F.R. 227.7202-1 through 227.7202-4 (JUNE 1995), all
# U.S. Government End Users acquire the Licensed Deliverables with
# only those rights set forth herein.
#
# Any use of the Licensed Deliverables in individual and commercial
# software must include, in the user documentation and internal
# comments to the code, the above Disclaimer and U.S. Government End
# Users Notice.
#

import graphsurgeon as gs
import tensorflow as tf

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

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)

namespace_plugin_map = {
    "image_tensor": Input,
    "concat": concat_box_loc,
    "concat_1": concat_box_conf
}

def preprocess(dynamic_graph):
    all_identity_nodes = dynamic_graph.find_nodes_by_op("Identity")
    dynamic_graph.forward_inputs(all_identity_nodes)

    # Now create a new graph by collapsing namespaces
    dynamic_graph.collapse_namespaces(namespace_plugin_map)
    # Remove the outputs, so we just have a single output node (NMS).
    dynamic_graph.remove(dynamic_graph.graph_outputs, remove_exclusive_dependencies=False)

    Squeeze = dynamic_graph.find_nodes_by_op('Squeeze')
    dynamic_graph.forward_inputs(Squeeze)
sudo python3 /usr/lib/python3.6/dist-packages/uff/bin/convert_to_uff.py frozen_inference_graph.pb -o mv3.uff -O concat_box_loc -O concat_box_conf -O anchors -p config.py

However, you will need to implement FusedBatchNormV3 on your own.
TensorRT doesn’t support it right now.

[TensorRT] ERROR: UffParser: Validator error: BoxPredictor_5/ClassPredictor_depthwise/BatchNorm/FusedBatchNormV3: Unsupported operation _FusedBatchNormV3

I’m going to see if we can replace it with FusedBatchNorm.
Will update more information with you later.

Thanks.

Hi,

Sorry that the FusedBatchNormV3 is not supported by current TensorRT.
Maybe you can try to convert the ssd_mobilenet_v3_large model to onnx format to see if helps.

Check this comment for more information:
https://devtalk.nvidia.com/default/topic/1066445/tensorrt/tensorrt-6-0-1-tensorflow-1-14-no-conversion-function-registered-for-layer-fusedbatchnormv3-yet/post/5403567/#5403567

Thanks.

Ok,thanks!!