Publishing 2019 R1 content
[platform/upstream/dldt.git] / tools / calibration / shape.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
18 class NchwShape:
19     def __init__(self, n: int, c: int, h: int, w: int):
20         self._n = n
21         self._c = c
22         self._h = h
23         self._w = w
24
25     @property
26     def layout(self) -> str:
27         return 'NCHW'
28
29     @property
30     def n(self) -> int:
31         return self._n
32
33     @property
34     def c(self) -> int:
35         return self._c
36
37     @property
38     def h(self) -> int:
39         return self._h
40
41     @property
42     def w(self) -> int:
43         return self._w
44
45
46 class ChwShape:
47     def __init__(self, c: int, h: int, w: int):
48         self._c = c
49         self._h = h
50         self._w = w
51
52     @property
53     def n(self) -> int:
54         return 1
55
56     @property
57     def layout(self) -> str:
58         return 'CHW'
59
60     @property
61     def c(self) -> int:
62         return self._c
63
64     @property
65     def h(self) -> int:
66         return self._h
67
68     @property
69     def w(self) -> int:
70         return self._w
71
72
73 class NcShape:
74     def __init__(self, n: int, c: int):
75         self._n = n
76         self._c = c
77
78     @property
79     def layout(self) -> str:
80         return 'NC'
81
82     @property
83     def n(self) -> int:
84         return self._n
85
86     @property
87     def c(self) -> int:
88         return self._c
89
90
91 class CShape:
92     def __init__(self, c: int):
93         self._n = 1
94         self._c = c
95
96     @property
97     def layout(self) -> str:
98         return 'C'
99
100     @property
101     def n(self) -> int:
102         return self._n
103
104     @property
105     def c(self) -> int:
106         return self._c
107
108
109 class Shape:
110     @staticmethod
111     def create(layout:str, dims):
112         if layout == 'NCHW':
113             return NchwShape(dims[0], dims[1], dims[2], dims[3])
114         if layout == 'CHW':
115             return ChwShape(dims[0], dims[1], dims[2])
116         elif layout == 'NC':
117             return NcShape(dims[0], dims[1])
118         elif layout == 'C':
119             return CShape(dims[0])
120         else:
121             raise ValueError("not supported layout '{}'".format(layout))