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 from tensorflow.core.framework import types_pb2 as tf_types
20 # mapping between TF data type and numpy data type and function to extract data from TF tensor
21 _tf_np_mapping = [('DT_BOOL', np.bool, lambda pb: pb.bool_val, lambda x: bool_cast(x)),
22 ('DT_INT8', np.int8, lambda pb: pb.int_val, lambda x: np.int8(x)),
23 ('DT_INT16', np.int16, lambda pb: pb.int_val, lambda x: np.int16(x)),
24 ('DT_INT32', np.int32, lambda pb: pb.int_val, lambda x: np.int32(x)),
25 ('DT_INT64', np.int64, lambda pb: pb.int64_val, lambda x: np.int64(x)),
26 ('DT_UINT8', np.uint8, lambda pb: pb.uint8_val, lambda x: np.uint8(x)),
27 ('DT_UINT16', np.uint16, lambda pb: pb.int_val, lambda x: np.uint16(x)),
28 ('DT_UINT32', np.uint32, lambda pb: pb.uint32_val, lambda x: np.uint32(x)),
29 ('DT_UINT64', np.uint64, lambda pb: pb.uint64_val, lambda x: np.uint64(x)),
30 ('DT_FLOAT', np.float32, lambda pb: pb.float_val, lambda x: np.float32(x)),
31 ('DT_DOUBLE', np.double, lambda pb: pb.double_val, lambda x: np.double(x)),
32 ('DT_STRING', np.str, lambda pb: pb.string_val, lambda x: np.str(x)),
35 tf_data_type_decode = {getattr(tf_types, tf_dt): (np_type, func) for tf_dt, np_type, func, cast in _tf_np_mapping if
36 hasattr(tf_types, tf_dt)}
38 tf_data_type_cast = {np_type: cast for tf_dt, np_type, func, cast in _tf_np_mapping if hasattr(tf_types, tf_dt)}
42 if isinstance(x, str):
43 return False if x.lower() in ['false', '0'] else True if x.lower() in ['true', '1'] else 'unknown_boolean_cast'