Add support for onnx FasterRCNN model (#3393)

* Add support for onnx FasterRCNN model

* Add documentation
This commit is contained in:
Maxim Vafin authored and GitHub committed 2020-12-01 18:45:08 +03:00
1 parent 86e5461d4b
commit bbb7478460
7 files changed
+67 -15

No files matched your search

@@ -0,0 +1,20 @@
[
{
"custom_attributes":
{
"max_detections_per_image": 100,
"max_delta_log_wh": 4.135166645050049,
"score_threshold": 0.05,
"nms_threshold": 0.5,
"post_nms_count": 2000,
"input_fpn_heads": ["486", "454", "422", "390"],
"do_outputs": ["6371", "6373", "6375"],
"box_regressions_input_node": "2614",
"class_predicitons_node": "2615",
"ROIFeatureExtractor2_input": "2335",
"ROIFeatureExtractor2_output": "2592"
},
"id": "ONNXMaskRCNNReplacement",
"match_kind": "general"
}
]
@@ -6,7 +6,14 @@
"max_delta_log_wh": 4.135166645050049,
"score_threshold": 0.05,
"nms_threshold": 0.5,
"post_nms_count": 2000
"post_nms_count": 2000,
"input_fpn_heads": ["486", "454", "422", "390"],
"do_outputs": ["6530", "6532", "6534"],
"box_regressions_input_node": "2773",
"class_predicitons_node": "2774",
"ROIFeatureExtractor1_output": "6795",
"ROIFeatureExtractor2_input": "2490",
"ROIFeatureExtractor2_output": "2751"
},
"id": "ONNXMaskRCNNReplacement",
"match_kind": "general"
@@ -28,8 +28,6 @@ from mo.graph.graph import Graph
from mo.graph.graph import Node
from mo.ops.reshape import Reshape
input_fpn_heads = ('486', '454', '422', '390')
class ONNXMaskRCNNTransformation(FrontReplacementFromConfigFileGeneral):
"""
@@ -45,24 +43,25 @@ class ONNXMaskRCNNTransformation(FrontReplacementFromConfigFileGeneral):
replacement_id = 'ONNXMaskRCNNReplacement'
def run_before(self):
# the node "2774" which is used in this transformation is of op SoftMaxONNX. But operations of op SoftMaxONNX
# the class_predicitons_node which is used in this transformation is of op SoftMaxONNX. But operations of op SoftMaxONNX
# will be replaced with a transformation SoftmaxONNXFrontReplacer
return [SoftmaxONNXFrontReplacer]
def transform_graph(self, graph: Graph, replacement_descriptions: dict):
insert_ExperimentalDetectronROIFeatureExtractor2(graph)
insert_ExperimentalDetectronROIFeatureExtractor2(graph, replacement_descriptions)
insert_do(graph, replacement_descriptions)
insert_ExperimentalDetectronROIFeatureExtractor1(graph)
insert_ExperimentalDetectronROIFeatureExtractor1(graph, replacement_descriptions)
def insert_do(graph: Graph, replacement_descriptions):
do_outputs = ['6530', '6532', '6534']
def insert_do(graph: Graph, replacement_descriptions: dict):
do_outputs = replacement_descriptions['do_outputs']
prior_boxes_node = Node(graph, 'ROIFeatureExtractor_2')
num_classes = 81
box_regressions_input_node = Node(graph, replacement_descriptions['box_regressions_input_node'])
box_regressions_node = create_op_node_with_second_input(graph, Reshape, int64_array([-1, 4 * num_classes]),
dict(name='box_regressions'), Node(graph, '2773'))
dict(name='box_regressions'), box_regressions_input_node)
class_predicitons_node = Node(graph, '2774')
class_predicitons_node = Node(graph, replacement_descriptions['class_predicitons_node'])
im_info_node = Parameter(graph, {"name": 'im_info', 'shape': int64_array([1, 3])}).create_node()
do_node = ExperimentalDetectronDetectionOutput(graph, {'name': 'DetectionOutput',
@@ -92,8 +91,12 @@ def insert_do(graph: Graph, replacement_descriptions):
do_node.out_port(1).get_connection().insert_node(Cast(graph, {'dst_type': np.int64}).create_node())
def insert_ExperimentalDetectronROIFeatureExtractor1(graph: Graph):
old_output_node = Node(graph, '6795')
def insert_ExperimentalDetectronROIFeatureExtractor1(graph: Graph, replacement_descriptions: dict):
if 'ROIFeatureExtractor1_output' not in replacement_descriptions:
# In case of Faster-RCNN this transformation is not needed and this attribute shouldn't be set
return
input_fpn_heads = replacement_descriptions['input_fpn_heads']
old_output_node = Node(graph, replacement_descriptions['ROIFeatureExtractor1_output'])
input_fpn_head_nodes = [Node(graph, node_id) for node_id in input_fpn_heads]
fpn_roi_align = ExperimentalDetectronROIFeatureExtractor(graph, {'name': 'ROIFeatureExtractor_1',
'distribute_rois_between_levels': 1,
@@ -110,8 +113,9 @@ def insert_ExperimentalDetectronROIFeatureExtractor1(graph: Graph):
old_output_node.out_port(0).get_connection().set_source(fpn_roi_align.out_port(0))
def insert_ExperimentalDetectronROIFeatureExtractor2(graph: Graph):
old_output_node = Node(graph, '2751')
def insert_ExperimentalDetectronROIFeatureExtractor2(graph: Graph, replacement_descriptions: dict):
input_fpn_heads = replacement_descriptions['input_fpn_heads']
old_output_node = Node(graph, replacement_descriptions['ROIFeatureExtractor2_output'])
input_fpn_head_nodes = [Node(graph, node_id) for node_id in input_fpn_heads]
fpn_roi_align = ExperimentalDetectronROIFeatureExtractor(graph, {'name': 'ROIFeatureExtractor_2',
'distribute_rois_between_levels': 1,
@@ -121,7 +125,7 @@ def insert_ExperimentalDetectronROIFeatureExtractor2(graph: Graph):
'pyramid_scales': int64_array(
[4, 8, 16, 32, 64]),
'sampling_ratio': 2, }).create_node()
fpn_roi_align.in_port(0).connect(Node(graph, '2490').out_port(0))
fpn_roi_align.in_port(0).connect(Node(graph, replacement_descriptions['ROIFeatureExtractor2_input']).out_port(0))
for ind, fpn_node in enumerate(input_fpn_head_nodes):
fpn_roi_align.in_port(ind + 1).connect(fpn_node.out_port(0))