Publishing 2019 R1 content
[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._precision = data['precision']
29         self._type = data['type']
30
31         self._input_ports = Layer.__init_ports(data, 'input')
32         self._output_ports = Layer.__init_ports(data, 'output')
33
34         self._inputs = list()
35         self._outputs = list()
36
37         blobs = data['blobs'] if 'blobs' in data else data
38         self._weights = Weights(int(blobs['weights']['offset']), int(blobs['weights']['size'])) if 'weights' in blobs else Weights(0, 0)
39         self._biases = Biases(int(blobs['biases']['offset']), int(blobs['biases']['size'])) if 'biases' in blobs else Biases(0, 0)
40
41     @staticmethod
42     def __init_ports(data: dict, key: str) -> dict:
43         result_ports = dict()
44         if (key in data) and ('port' in data[key]):            
45             ports = data[key]['port']
46             if type(ports) is list:
47                 for port_dict in ports:
48                     id = int(port_dict['id'])
49                     result_ports[id] = Port(id, list(map(int, port_dict['dim'])))
50             elif type(ports) is collections.OrderedDict:
51                 id = int(ports['id'])
52                 result_ports[id] = Port(id, list(map(int, ports['dim'])))
53             else:
54                 raise ValueError("unexpected ports type '{}'".format(type(ports)))
55         return result_ports
56
57     def init(self, inputs: list, outputs: list):
58         self._inputs = inputs
59         self._outputs = outputs
60
61     @property
62     def id(self) -> int:
63         return self._id
64
65     @property
66     def name(self) -> str:
67         return self._name
68
69     @property
70     def precision(self) -> str:
71         return self._precision
72
73     @property
74     def type(self) -> str:
75         return self._type
76
77     @property
78     def input_ports(self):
79         return self._input_ports
80
81     @property
82     def output_ports(self):
83         return self._output_ports
84
85     @property
86     def inputs(self) -> list:
87         return self._inputs
88
89     @property
90     def outputs(self) -> list:
91         return self._outputs
92
93     @property
94     def weights(self):
95         return self._weights
96
97     @property
98     def biases(self):
99         return self._biases