utils.py 2.42 KB
Newer Older
openvino-pushbot's avatar
openvino-pushbot committed
1
"""
2
 Copyright (c) 2018-2019 Intel Corporation
openvino-pushbot's avatar
openvino-pushbot committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16

 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.
"""

17 18
import numpy as np

openvino-pushbot's avatar
openvino-pushbot committed
19
from mo.graph.graph import Node
Alexey Suhov's avatar
Alexey Suhov committed
20
from mo.utils.error import Error
openvino-pushbot's avatar
openvino-pushbot committed
21 22 23


def onnx_attr(node: Node, name: str, field: str, default=None, dst_type=None):
Alexey Suhov's avatar
Alexey Suhov committed
24
    """ Retrieves ONNX attribute with name `name` from ONNX protobuf `node.pb`.
openvino-pushbot's avatar
openvino-pushbot committed
25 26
        The final value is casted to dst_type if attribute really exists.
        The function returns `default` otherwise.
Alexey Suhov's avatar
Alexey Suhov committed
27
    """
openvino-pushbot's avatar
openvino-pushbot committed
28 29 30 31 32
    attrs = [a for a in node.pb.attribute if a.name == name]
    if len(attrs) == 0:
        # there is no requested attribute in the protobuf message
        return default
    elif len(attrs) > 1:
Alexey Suhov's avatar
Alexey Suhov committed
33 34
        raise Error('Found multiple entries for attribute name {} when at most one is expected. Protobuf message with '
                    'the issue: {}.', name, node.pb)
openvino-pushbot's avatar
openvino-pushbot committed
35 36 37 38 39 40 41
    else:
        res = getattr(attrs[0], field)
        if dst_type is not None:
            return dst_type(res)
        else:
            return res

Alexey Suhov's avatar
Alexey Suhov committed
42 43 44 45 46 47 48 49 50 51

def get_backend_pad(pads, spatial_dims, axis):
    return [x[axis] for x in pads[spatial_dims]]


def get_onnx_autopad(auto_pad):
    auto_pad = auto_pad.decode().lower()
    if auto_pad == 'notset':
        auto_pad = None
    return auto_pad
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73


def get_onnx_datatype_as_numpy(value):
    datatype_to_numpy = {
                         1: np.float32,
                         9: np.bool,
                         11: np.double,
                         10: np.float16,
                         5: np.int16,
                         6: np.int32,
                         7: np.int64,
                         3: np.int8,
                         8: np.ubyte,
                         4: np.uint16,
                         12: np.uint32,
                         13: np.uint64,
                         2: np.uint8,
                         }
    try:
        return datatype_to_numpy[value]
    except KeyError:
        raise Error("Incorrect value {} for Datatype enum".format(value))