def extract(cls, node):
        attrs = dict(output_size=onnx_attr(node, 'output_size', 'i', 7),
                     sampling_ratio=onnx_attr(node, 'sampling_ratio', 'i', 2),
                     aligned=onnx_attr(node, 'aligned', 'i', 0),
                     num_classes=onnx_attr(node, 'num_classes', 'i', 81),
                     post_nms_count=onnx_attr(node, 'post_nms_count', 'i', 2000),
                     score_threshold=onnx_attr(node, 'score_threshold', 'f', 0.05),
                     pyramid_scales=np.array(onnx_attr(node, 'pyramid_scales', 'ints', [4, 8, 16, 32, 64]),
                                             dtype=np.int64),
                     )

        ExperimentalDetectronROIFeatureExtractor.update_node_stat(node, attrs)
        return cls.enabled
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',
            'output_size': 7,
            'pyramid_scales': int64_array([4, 8, 16, 32, 64]),
            'sampling_ratio': 2,
            'in_ports_count': 5
        }).create_node()
    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))

    old_output_node.out_port(0).get_connection().set_source(
        fpn_roi_align.out_port(0))
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',
            'output_size': 14,
            'pyramid_scales': int64_array([4, 8, 16, 32, 64]),
            'sampling_ratio': 2,
            'in_ports_count': 5
        }).create_node()
    fpn_roi_align.in_port(0).connect(
        Node(graph, 'DetectionOutput').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))

    old_output_node.out_port(0).get_connection().set_source(
        fpn_roi_align.out_port(0))
def insert_experimental_layers(graph: Graph, input_fpn_heads: list, inp: str,
                               out: str):
    old_output_node = Node(graph, out)
    output_name = old_output_node.soft_get('name', old_output_node.id)
    old_output_node_name = output_name + '/old'
    rename_node(old_output_node, old_output_node_name)

    input_fpn_head_nodes = [
        Node(graph, node_id) for node_id in input_fpn_heads
    ]
    fpn_roi_align = ExperimentalDetectronROIFeatureExtractor(
        graph, {
            'name': output_name,
            'output_size': 7,
            'pyramid_scales': int64_array([4, 8, 16, 32, 64]),
            'sampling_ratio': 2,
        }).create_node()
    rename_node(fpn_roi_align, output_name)
    fpn_roi_align.in_port(0).connect(Node(graph, inp).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))

    old_output_node.out_port(0).get_connection().set_source(
        fpn_roi_align.out_port(0))