Publishing 2019 R1 content
[platform/upstream/dldt.git] / model-optimizer / extensions / front / onnx / detection_output.py
1 """
2  Copyright (c) 2019 Intel Corporation
3
4  Licensed under the Apache License, Version 2.0 (the "License");
5  you may not use this file except in compliance with the License.
6  You may obtain a copy of the License at
7
8       http://www.apache.org/licenses/LICENSE-2.0
9
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15 """
16
17 from mo.front.extractor import FrontExtractorOp
18 from mo.front.onnx.extractors.utils import onnx_attr
19 from mo.ops.op import Op
20 from mo.utils.error import Error
21
22
23 class DetectionOutputFrontExtractor(FrontExtractorOp):
24     op = 'DetectionOutput'
25     enabled = True
26
27     @staticmethod
28     def extract(node):
29         nms_threshold = onnx_attr(node, 'nms_threshold', 'f', default=0.0)
30         eta = onnx_attr(node, 'eta', 'f', default=0.0)
31         top_k = onnx_attr(node, 'top_k', 'i', default=-1)
32
33         code_type_values = {
34             b"CORNER": "caffe.PriorBoxParameter.CORNER",
35             b"CENTER_SIZE": "caffe.PriorBoxParameter.CENTER_SIZE",
36         }
37
38         code_type = onnx_attr(node, 'code_type', 's', default=code_type_values[b"CORNER"])
39         try:
40             code_type = code_type_values[code_type]
41         except KeyError:
42             raise Error("Incorrect value of code_type parameter {}".format(code_type))
43
44         resize_mode_values = {
45             b"": "",
46             b"WARP": "caffe.ResizeParameter.WARP",
47             b"FIT_SMALL_SIZE": "caffe.ResizeParameter.FIT_SMALL_SIZE",
48             b"FIT_LARGE_SIZE_AND_PAD": "caffe.ResizeParameter.FIT_LARGE_SIZE_AND_PAD",
49         }
50         resize_mode = onnx_attr(node, 'resize_mode', 's', default=b"")
51         try:
52             resize_mode = resize_mode_values[resize_mode]
53         except KeyError:
54             raise Error("Incorrect value of resize_mode parameter {}".format(resize_mode))
55
56         pad_mode_values = {
57             b"": "",
58             b"CONSTANT": "caffe.ResizeParameter.CONSTANT",
59             b"MIRRORED": "caffe.ResizeParameter.MIRRORED",
60             b"REPEAT_NEAREST": "caffe.ResizeParameter.REPEAT_NEAREST"
61         }
62         pad_mode = onnx_attr(node, 'pad_mode', 's', default=b"")
63         try:
64             pad_mode = pad_mode_values[pad_mode]
65         except KeyError:
66             raise Error("Incorrect value of pad_mode parameter {}".format(pad_mode))
67
68         interp_mode_values = {
69             b"": "",
70             b"LINEAR": "caffe.ResizeParameter.LINEAR",
71             b"AREA": "caffe.ResizeParameter.AREA",
72             b"NEAREST": "caffe.ResizeParameter.NEAREST",
73             b"CUBIC": "caffe.ResizeParameter.CUBIC",
74             b"LANCZOS4": "caffe.ResizeParameter.LANCZOS4"
75         }
76         interp_mode = onnx_attr(node, 'interp_mode', 's', default=b"")
77         try:
78             interp_mode = interp_mode_values[interp_mode]
79         except KeyError:
80             raise Error("Incorrect value of interp_mode parameter {}".format(interp_mode))
81
82         attrs = {
83             'num_classes': onnx_attr(node, 'num_classes', 'i', default=0),
84             'share_location': onnx_attr(node, 'share_location', 'i', default=0),
85             'background_label_id': onnx_attr(node, 'background_label_id', 'i', default=0),
86             'code_type': code_type,
87             'variance_encoded_in_target': onnx_attr(node, 'variance_encoded_in_target', 'i', default=0),
88             'keep_top_k': onnx_attr(node, 'keep_top_k', 'i', default=0),
89             'confidence_threshold':  onnx_attr(node, 'confidence_threshold', 'f', default=0),
90             'visualize_threshold': onnx_attr(node, 'visualize_threshold', 'f', default=0.6),
91             # nms_param
92             'nms_threshold': nms_threshold,
93             'top_k': top_k,
94             'eta': eta,
95             # save_output_param.resize_param
96             'prob': onnx_attr(node, 'prob', 'f', default=0),
97             'resize_mode': resize_mode,
98             'height': onnx_attr(node, 'height', 'i', default=0),
99             'width': onnx_attr(node, 'width', 'i', default=0),
100             'height_scale': onnx_attr(node, 'height_scale', 'i', default=0),
101             'width_scale': onnx_attr(node, 'width_scale', 'i', default=0),
102             'pad_mode': pad_mode,
103             'pad_value': onnx_attr(node, 'pad_value', 's', default=""),
104             'interp_mode': interp_mode,
105             'input_width': onnx_attr(node, 'input_width', 'i', default=1),
106             'input_height': onnx_attr(node, 'input_height', 'i', default=1),
107             'normalized': onnx_attr(node, 'normalized', 'i', default=1),
108         }
109
110         # update the attributes of the node
111         Op.get_op_class_by_name(__class__.op).update_node_stat(node, attrs)
112         return __class__.enabled