2 Copyright (c) 2018 Intel Corporation
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
8 http://www.apache.org/licenses/LICENSE-2.0
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.
18 import tensorflow as tf
19 from generator import generator, generate
21 from mo.front.tf.extractors.const import tf_const_ext
22 from mo.utils.unittest.extractors import PB, BaseExtractorsTestingClass
24 dtypes = {"ints": [(tf.int8, np.int8),
27 (tf.int64, np.int64)],
29 "uints": [(tf.uint8, np.uint8),
30 (tf.uint16, np.uint16),
32 "floats": [(tf.float32, np.float32),
33 (tf.float64, np.double)],
35 "bools": [(tf.bool, np.bool)],
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)])
43 class ConstExtractorTest(BaseExtractorsTestingClass):
46 cls.patcher = 'mo.front.tf.extractors.const.tf_const_infer'
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)
56 "dtype": tensor_proto.dtype,
57 "tensor_shape": tensor_proto.tensor_shape,
58 "tensor_content": tensor_proto.tensor_content
63 'data_type': np_dtype,
64 'shape': np.asarray(shape, dtype=np.int),
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
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)
81 "dtype": tensor_proto.dtype,
82 "tensor_shape": tensor_proto.tensor_shape,
86 if tf_dtype == tf.uint16:
87 setattr(pb.attr.value.tensor, "int_val", values.tolist())
89 setattr(pb.attr.value.tensor, "tensor_content", tensor_proto.tensor_content)
91 'data_type': np_dtype,
92 'shape': np.asarray(shape, dtype=np.int),
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
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(
106 tensor_proto = tf.make_tensor_proto(values=values, dtype=tf_dtype, shape=shape)
107 pb = PB({"attr": PB({
110 "dtype": tensor_proto.dtype,
111 "tensor_shape": tensor_proto.tensor_shape,
112 "tensor_content": tensor_proto.tensor_content
117 'data_type': np_dtype,
118 'shape': np.asarray(shape, dtype=np.int),
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
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({
136 "dtype": tensor_proto.dtype,
137 "tensor_shape": tensor_proto.tensor_shape,
138 "bool_val": values.tolist()
143 'data_type': np_dtype,
144 'shape': np.asarray(shape, dtype=np.int),
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
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)
158 # tensor_proto = tf.make_tensor_proto(values=values, dtype=tf_dtype, shape=shape)
159 # pb = PB({"attr": PB({
162 # "dtype": tensor_proto.dtype,
163 # "tensor_shape": tensor_proto.tensor_shape,
164 # "tensor_content": tensor_proto.tensor_content
169 # 'data_type': np_dtype,
170 # 'shape': np.asarray(shape, dtype=np.int),
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