Added unit tests and readme for model optimizer (#79)
[platform/upstream/dldt.git] / model-optimizer / mo / front / tf / extractors / const_test.py
1 """
2  Copyright (c) 2018 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 numpy as np
18 import tensorflow as tf
19 from generator import generator, generate
20
21 from mo.front.tf.extractors.const import tf_const_ext
22 from mo.utils.unittest.extractors import PB, BaseExtractorsTestingClass
23
24 dtypes = {"ints": [(tf.int8, np.int8),
25                    (tf.int16, np.int16),
26                    (tf.int32, np.int32),
27                    (tf.int64, np.int64)],
28
29           "uints": [(tf.uint8, np.uint8),
30                     (tf.uint16, np.uint16),
31                     ],
32           "floats": [(tf.float32, np.float32),
33                      (tf.float64, np.double)],
34
35           "bools": [(tf.bool, np.bool)],
36
37           "strings": [(tf.string, np.str)]}
38 if tf.__version__ > "1.4.0":
39     dtypes['uints'].extend([(tf.uint32, np.uint32), (tf.uint64, np.uint64)])
40
41
42 @generator
43 class ConstExtractorTest(BaseExtractorsTestingClass):
44     @classmethod
45     def setUpClass(cls):
46         cls.patcher = 'mo.front.tf.extractors.const.tf_const_infer'
47
48     @generate(*dtypes['ints'])
49     def test_const_ints(self, tf_dtype, np_dtype):
50         shape = [1, 1, 200, 50]
51         values = np.random.randint(low=np.iinfo(np_dtype).min, high=np.iinfo(np_dtype).max, size=shape, dtype=np_dtype)
52         tensor_proto = tf.make_tensor_proto(values=values, dtype=tf_dtype, shape=shape)
53         pb = PB({"attr": PB({
54             "value": PB({
55                 "tensor": PB({
56                     "dtype": tensor_proto.dtype,
57                     "tensor_shape": tensor_proto.tensor_shape,
58                     "tensor_content": tensor_proto.tensor_content
59                 })
60             })
61         })})
62         self.expected = {
63             'data_type': np_dtype,
64             'shape': np.asarray(shape, dtype=np.int),
65             'value': values
66         }
67         self.res = tf_const_ext(pb=pb)
68         self.res["infer"](None)
69         self.call_args = self.infer_mock.call_args
70         self.expected_call_args = None
71         self.compare()
72
73     @generate(*dtypes['uints'])
74     def test_const_uints(self, tf_dtype, np_dtype):
75         shape = [1, 1, 200, 50]
76         values = np.random.randint(low=np.iinfo(np_dtype).min, high=np.iinfo(np_dtype).max, size=shape, dtype=np_dtype)
77         tensor_proto = tf.make_tensor_proto(values=values, dtype=tf_dtype, shape=shape)
78         pb = PB({"attr": PB({
79             "value": PB({
80                 "tensor": PB({
81                     "dtype": tensor_proto.dtype,
82                     "tensor_shape": tensor_proto.tensor_shape,
83                 })
84             })
85         })})
86         if tf_dtype == tf.uint16:
87             setattr(pb.attr.value.tensor, "int_val", values.tolist())
88         else:
89             setattr(pb.attr.value.tensor, "tensor_content", tensor_proto.tensor_content)
90         self.expected = {
91             'data_type': np_dtype,
92             'shape': np.asarray(shape, dtype=np.int),
93             'value': values
94         }
95         self.res = tf_const_ext(pb=pb)
96         self.res["infer"](None)
97         self.call_args = self.infer_mock.call_args
98         self.expected_call_args = None
99         self.compare()
100
101     @generate(*dtypes['floats'])
102     def test_const_floats(self, tf_dtype, np_dtype):
103         shape = [1, 1, 200, 50]
104         values = np.random.uniform(low=np.finfo(np.float32).min, high=np.finfo(np.float32).max, size=shape).astype(
105             np_dtype)
106         tensor_proto = tf.make_tensor_proto(values=values, dtype=tf_dtype, shape=shape)
107         pb = PB({"attr": PB({
108             "value": PB({
109                 "tensor": PB({
110                     "dtype": tensor_proto.dtype,
111                     "tensor_shape": tensor_proto.tensor_shape,
112                     "tensor_content": tensor_proto.tensor_content
113                 })
114             })
115         })})
116         self.expected = {
117             'data_type': np_dtype,
118             'shape': np.asarray(shape, dtype=np.int),
119             'value': values
120         }
121         self.res = tf_const_ext(pb=pb)
122         self.res["infer"](None)
123         self.call_args = self.infer_mock.call_args
124         self.expected_call_args = None
125         self.compare()
126
127     # TODO: Check how to correctly handle tensor_proto with booleans. It has no tensor_content section
128     @generate(*dtypes['bools'])
129     def test_const_floats(self, tf_dtype, np_dtype):
130         shape = [1, 1, 50, 50]
131         values = np.random.choice(a=[True, False], size=shape, p=[0.5, 0.5])
132         tensor_proto = tf.make_tensor_proto(values=values, dtype=tf_dtype, shape=shape)
133         pb = PB({"attr": PB({
134             "value": PB({
135                 "tensor": PB({
136                     "dtype": tensor_proto.dtype,
137                     "tensor_shape": tensor_proto.tensor_shape,
138                     "bool_val": values.tolist()
139                 })
140             })
141         })})
142         self.expected = {
143             'data_type': np_dtype,
144             'shape': np.asarray(shape, dtype=np.int),
145             'value': values
146         }
147         self.res = tf_const_ext(pb=pb)
148         self.res["infer"](None)
149         self.call_args = self.infer_mock.call_args
150         self.expected_call_args = None
151         self.compare()
152         # TODO: Check how to correctly create tensor_proto with strings
153         # @generate(*dtypes['strings'])
154         # def test_const_floats(self, tf_dtype, np_dtype):
155         #     shape = [1, 1, 50, 50]
156         #     values = np.chararray(shape=shape)
157         #     values[:] = "bla"
158         #     tensor_proto = tf.make_tensor_proto(values=values, dtype=tf_dtype, shape=shape)
159         #     pb = PB({"attr": PB({
160         #         "value": PB({
161         #             "tensor": PB({
162         #                 "dtype": tensor_proto.dtype,
163         #                 "tensor_shape": tensor_proto.tensor_shape,
164         #                 "tensor_content": tensor_proto.tensor_content
165         #             })
166         #         })
167         #     })})
168         #     self.expected = {
169         #         'data_type': np_dtype,
170         #         'shape': np.asarray(shape, dtype=np.int),
171         #         'value': values
172         #     }
173         #     self.res = tf_const_ext(pb=pb)
174         #     self.res["infer"](None)
175         #     self.call_args = self.infer_mock.call_args
176         #     self.expected_call_args = None
177         #     self.compare()