Files
openvino/model-optimizer/extensions/ops/non_max_suppression.py
T

122 lines
5.3 KiB
Python
Raw Normal View History

2019-10-04 19:26:43 +03:00
"""
2021-01-29 10:08:06 +03:00
Copyright (C) 2018-2021 Intel Corporation
2019-10-04 19:26:43 +03:00
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
2020-02-11 22:48:49 +03:00
import logging as log
2019-10-04 19:26:43 +03:00
import numpy as np
2019-10-04 19:26:43 +03:00
from mo.front.common.partial_infer.utils import int64_array
2021-01-29 10:08:06 +03:00
from mo.front.extractor import bool_to_str
2019-10-04 19:26:43 +03:00
from mo.graph.graph import Node, Graph
from mo.middle.passes.convert_data_type import np_data_type_to_destination_type
2019-10-04 19:26:43 +03:00
from mo.ops.op import Op
from mo.utils.error import Error
2019-10-04 19:26:43 +03:00
class NonMaxSuppression(Op):
op = 'NonMaxSuppression'
def __init__(self, graph: Graph, attrs: dict):
mandatory_props = {
2020-06-30 14:04:31 +03:00
'type': self.op,
'op': self.op,
'version': 'opset5',
2020-06-30 14:04:31 +03:00
'infer': self.infer,
'output_type': np.int64,
2020-02-11 22:48:49 +03:00
'box_encoding': 'corner',
2019-10-04 19:26:43 +03:00
'in_ports_count': 5,
2020-02-11 22:48:49 +03:00
'sort_result_descending': 1,
'force_precision_in_ports': {
2020-06-22 11:52:00 +03:00
2: 'int64'},
2020-02-11 22:48:49 +03:00
'type_infer': self.type_infer,
2019-10-04 19:26:43 +03:00
}
super().__init__(graph, mandatory_props, attrs)
version = self.get_opset()
if version in ['opset1', 'opset3', 'opset4']:
self.attrs['out_ports_count'] = 1
elif version == 'opset5':
self.attrs['out_ports_count'] = 3
else:
raise Error('Unsupported operation opset version "{}"'.format(version))
2019-10-04 19:26:43 +03:00
2020-06-30 14:04:31 +03:00
def backend_attrs(self):
version = self.get_opset()
if version in ['opset3', 'opset4', 'opset5']:
2021-01-29 10:08:06 +03:00
return [('sort_result_descending', lambda node: bool_to_str(node, 'sort_result_descending')),
'box_encoding',
2020-06-30 14:04:31 +03:00
('output_type', lambda node: np_data_type_to_destination_type(node.output_type))]
elif version == 'opset1':
2021-01-29 10:08:06 +03:00
return [('sort_result_descending', lambda node: bool_to_str(node, 'sort_result_descending')),
'box_encoding']
2020-02-11 22:48:49 +03:00
else:
2020-06-30 14:04:31 +03:00
raise Error('Unsupported operation opset version "{}"'.format(version))
2019-10-04 19:26:43 +03:00
@staticmethod
def infer(node: Node):
num_of_inputs = len(node.in_ports())
opset = node.get_opset()
max_num_of_inputs = 6 if opset == 'opset5' else 5
input_msg_fmt = 'NonMaxSuppression node {} from {} must have from 2 to {} inputs'
inputs_msg = input_msg_fmt.format(node.soft_get('name', node.id), opset, max_num_of_inputs)
assert 2 <= num_of_inputs <= max_num_of_inputs, inputs_msg
2019-10-04 19:26:43 +03:00
boxes_shape = node.in_port(0).data.get_shape()
assert boxes_shape is not None, 'The shape of tensor with boxes is not defined'
scores_shape = node.in_port(1).data.get_shape()
assert scores_shape is not None, 'The shape of tensor with scores is not defined'
assert len(boxes_shape) == 3, 'Length of tensors with boxes must be equal to 3'
assert len(scores_shape) == 3, 'Length of tensors with scores must be equal to 3'
# According to the specification of the operation NonMaxSuppression,
# the input 'max_output_boxes_per_class' (port 2) is optional, with default value 0.
if num_of_inputs >= 3:
max_output_boxes_per_class = node.in_port(2).data.get_value()
else:
max_output_boxes_per_class = 0
if not max_output_boxes_per_class:
2020-02-11 22:48:49 +03:00
log.info('Set default "max_output_boxes_per_class" for node {} to number of boxes'.format(node.name))
max_output_boxes_per_class = boxes_shape[1]
2019-10-04 19:26:43 +03:00
num_classes = scores_shape[1]
num_input_boxes = boxes_shape[1]
assert scores_shape[2] == num_input_boxes, 'Number of boxes mismatch'
if node.get_opset() in ['opset4', 'opset5']:
2020-06-30 14:04:31 +03:00
max_number_of_boxes = min(num_input_boxes, max_output_boxes_per_class) * boxes_shape[0] * num_classes
else:
max_number_of_boxes = min(num_input_boxes, boxes_shape[0] * max_output_boxes_per_class * num_classes)
2020-02-11 22:48:49 +03:00
node.out_port(0).data.set_shape(int64_array([max_number_of_boxes, 3]))
if opset == 'opset5':
num_of_outputs = len([port for port in node.out_ports().values() if not port.disconnected()])
if num_of_outputs >= 2 and node.has_port('out', 1):
node.out_port(1).data.set_shape(int64_array([max_number_of_boxes, 3]))
if num_of_outputs >= 3 and node.has_port('out', 2):
node.out_port(2).data.set_shape(int64_array(1))
2020-02-11 22:48:49 +03:00
@staticmethod
def type_infer(node):
opset = node.get_opset()
if opset == 'opset5':
node.out_port(0).set_data_type(node.output_type)
if node.has_port('out', 1):
node.out_port(1).set_data_type(np.float32)
if node.has_port('out', 2):
node.out_port(2).set_data_type(np.int64)
elif opset in ['opset3', 'opset4']:
2020-06-22 11:52:00 +03:00
node.out_port(0).set_data_type(node.output_type)
else:
2020-06-22 11:52:00 +03:00
node.out_port(0).set_data_type(np.int64)