[Dist/Debian] Prepare for GCC >= 10
[platform/upstream/dldt.git] / tools / utils / layer.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
17 import collections
18
19 from .biases import Biases
20 from .weights import Weights
21 from .port import Port
22
23
24 class Layer:
25     def __init__(self, data: dict):
26         self._id = int(data['id'])
27         self._name = data['name']
28         self._type = data['type']
29
30         self._input_ports = Layer.__init_ports(data, 'input')
31         self._output_ports = Layer.__init_ports(data, 'output')
32
33         self._inputs = list()
34         self._outputs = list()
35
36         blobs = data['blobs'] if 'blobs' in data else data
37         self._weights = Weights(int(blobs['weights']['offset']), int(blobs['weights']['size'])) if 'weights' in blobs else Weights(0, 0)
38         self._biases = Biases(int(blobs['biases']['offset']), int(blobs['biases']['size'])) if 'biases' in blobs else Biases(0, 0)
39
40     @staticmethod
41     def __init_ports(data: dict, key: str) -> dict:
42         result_ports = dict()
43         if (key in data) and ('port' in data[key]):            
44             ports = data[key]['port']
45             if type(ports) is list:
46                 for port_dict in ports:
47                     id = int(port_dict['id'])
48                     result_ports[id] = Port(id, list(map(int, port_dict['dim'])))
49             elif type(ports) is collections.OrderedDict:
50                 id = int(ports['id'])
51                 result_ports[id] = Port(id, list(map(int, ports['dim'])))
52             else:
53                 raise ValueError("unexpected ports type '{}'".format(type(ports)))
54         return result_ports
55
56     def init(self, inputs: list, outputs: list):
57         self._inputs = inputs
58         self._outputs = outputs
59
60     @property
61     def id(self) -> int:
62         return self._id
63
64     @property
65     def name(self) -> str:
66         return self._name
67
68     @property
69     def type(self) -> str:
70         return self._type
71
72     @property
73     def input_ports(self):
74         return self._input_ports
75
76     @property
77     def output_ports(self):
78         return self._output_ports
79
80     @property
81     def inputs(self) -> list:
82         return self._inputs
83
84     @property
85     def outputs(self) -> list:
86         return self._outputs
87
88     @property
89     def weights(self):
90         return self._weights
91
92     @property
93     def biases(self):
94         return self._biases