Publishing 2019 R1 content
[platform/upstream/dldt.git] / model-optimizer / mo / utils / utils.py
1 """
2  Copyright (c) 2018-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 import functools
17 import warnings
18 import logging as log
19 import numpy as np
20
21
22 def refer_to_faq_msg(question_num: int):
23     return '\n For more information please refer to Model Optimizer FAQ' \
24            ' (<INSTALL_DIR>/deployment_tools/documentation/docs/MO_FAQ.html),' \
25            ' question #{}. '.format(question_num)
26
27
28 class NamedAttrsClass:
29     def __init__(self, class_attrs: dict):
30         for key, val in class_attrs.items():
31             self.__setattr__(key, val)
32
33
34 def match_shapes(pattern: np.array, shape: np.array):
35     """ Check if shape matches shape pattern handling -1 and 0 in the pattern. """
36     # Elements with values -1 and 0 in pattern are just ignored.
37     # Other elements should match.
38     if pattern.size != shape.size:
39         return False
40     indices = [i for i, n in enumerate(pattern) if n not in [0, -1]]
41     return np.array_equal(pattern[indices], shape[indices])
42
43
44 def symm_match_shapes(shape1: np.array, shape2: np.array):
45     """ Check if shape matches shape pattern handling -1 and 0 in the pattern. """
46     # Elements with values -1 and 0 in both shapes are just ignored.
47     # Other elements should match. Undefined elements can be one side only.
48     return match_shapes(shape1, shape2) or match_shapes(shape2, shape1)
49
50
51 def deprecated_api(class_name=None):
52     def deprecated(func):
53         @functools.wraps(func)
54         def deprecation_message(*args, **kwargs):
55             warnings.simplefilter('always', DeprecationWarning)  # turn on filter
56             dep_msg = "Call to deprecated function {}. ".format(func.__name__)
57             if class_name is not None:
58                 dep_msg += "Please use {}.{} method".format(class_name.__name__, func.__name__)
59             warnings.warn(dep_msg, DeprecationWarning, stacklevel=2)
60             warnings.simplefilter('default', DeprecationWarning)  # reset filter
61             return func(*args, **kwargs)
62
63         return deprecation_message
64
65     return deprecated
66
67
68 def array_to_str(node, attr):
69     if not node.has_valid(attr):
70         return None
71     else:
72         return ','.join(map(str, node[attr]))
73
74
75 def shrink_str_value(value: np.array, max_symbols=100):
76     value = str(value)
77     if len(value) > max_symbols:
78         value = value.strip('\n')[:max_symbols - 3] + '...'
79     return value