From a754f01ad98e1b55b3b5c68f6a1ce56079667681 Mon Sep 17 00:00:00 2001 From: Ramana Radhakrishnan Date: Thu, 16 Apr 2020 23:43:34 +0100 Subject: [PATCH 1/2] Restructure imports in tflite frontend. These python modules are needed for every tflite file parsed. Factorize out imports of the common most ones. Now that the import of operator is common, asserts can be commonized. Loses 473 lines of duplication. --- python/tvm/relay/frontend/tflite.py | 218 ++-------------------------- 1 file changed, 15 insertions(+), 203 deletions(-) diff --git a/python/tvm/relay/frontend/tflite.py b/python/tvm/relay/frontend/tflite.py index d489bd34f7ac..66e2aa82c153 100644 --- a/python/tvm/relay/frontend/tflite.py +++ b/python/tvm/relay/frontend/tflite.py @@ -32,6 +32,20 @@ from .common import ExprTable from .common import infer_shape as _infer_shape +# A note on tflite imports. Operator specific imports of modules +# need to be with the operator. General imports that are common across +# multiple operators and which are very common should be in the +# block below. +try: + from tflite.Operator import Operator as Operator + from tflite.TensorType import TensorType as TensorType + from tflite.BuiltinOperator import BuiltinOperator as BuiltinOperator + from tflite.BuiltinOptions import BuiltinOptions as BuiltinOptions + from tflite.ActivationFunctionType import ActivationFunctionType as ActivationFunctionType +except ImportError: + raise ImportError("The tflite package must be installed") + + __all__ = ['from_tflite'] class TensorWrapper(object): @@ -46,12 +60,6 @@ class OperatorConverter(object): """Operator Converted for converting TFLite ops to Relay ops""" def __init__(self, model, subgraph, exp_tab): - try: - from tflite.BuiltinOperator import BuiltinOperator - from tflite.BuiltinOptions import BuiltinOptions - from tflite.ActivationFunctionType import ActivationFunctionType - except ImportError: - raise ImportError("The tflite package must be installed") self.model = model self.subgraph = subgraph @@ -160,6 +168,7 @@ def convert_op_to_relay(self): op_code_str = self.get_op_code_str(op) output_tensors = self.get_output_tensors(op) + assert isinstance(op, Operator) ret = self.convert_map[op_code_str](op) if len(output_tensors) == 1: @@ -172,10 +181,6 @@ def convert_op_to_relay(self): def get_op_code_str(self, op): """Get TFLite ops string representation""" - try: - from tflite.BuiltinOperator import BuiltinOperator - except ImportError: - raise ImportError("The tflite package must be installed") op_code_list_idx = op.OpcodeIndex() op_code_id = self.model.OperatorCodes(op_code_list_idx).BuiltinCode() @@ -231,11 +236,6 @@ def get_tensor_value(self, tensor_wrapper): """Get tensor buffer value from given tensor wrapper""" assert isinstance(tensor_wrapper, TensorWrapper) - try: - from tflite.TensorType import TensorType - except ImportError: - raise ImportError("The tflite package must be installed") - if tensor_wrapper.tensor.Type() == TensorType.UINT8: return np.frombuffer(tensor_wrapper.buffer.DataAsNumpy(), dtype=np.uint8).reshape( tensor_wrapper.tensor.ShapeAsNumpy()) @@ -256,11 +256,6 @@ def get_tensor_value(self, tensor_wrapper): def get_tensor_type_str(self, tensor_type): """Get tensor type string representation when given TFLite tensor type""" - try: - from tflite.TensorType import TensorType - except ImportError: - raise ImportError("The tflite package must be installed") - if tensor_type == TensorType.UINT8: return "uint8" if tensor_type == TensorType.FLOAT32: @@ -288,12 +283,7 @@ def has_same_qnn_params(self, lhs_tensor, rhs_tensor): def is_quantized(self, op): """Check if an input tensor is quantized.""" - try: - from tflite.Operator import Operator - except ImportError: - raise ImportError("The tflite package must be installed") - assert isinstance(op, Operator) input_tensors = self.get_input_tensors(op) first_tensor = input_tensors[0] return first_tensor.qnn_params is not None @@ -334,13 +324,10 @@ def convert_max_pool2d(self, op): def convert_reshape(self, op): """Convert TFLite reshape""" try: - from tflite.BuiltinOptions import BuiltinOptions - from tflite.Operator import Operator from tflite.ReshapeOptions import ReshapeOptions except ImportError: raise ImportError("The tflite package must be installed") - assert isinstance(op, Operator) input_tensors = self.get_input_tensors(op) assert input_tensors, "input tensors should not be empty" input_tensor = input_tensors[0] @@ -367,8 +354,6 @@ def convert_reshape(self, op): def _convert_resize(self, method, op): """Generic method to Convert TFLite RESIZE operators""" try: - from tflite.BuiltinOptions import BuiltinOptions - from tflite.Operator import Operator from tflite.ResizeBilinearOptions import ResizeBilinearOptions # ResizeNearestNeighborOptions was added in tflite v1.13 tflite_ver = 1120 @@ -378,7 +363,6 @@ def _convert_resize(self, method, op): except ImportError: raise ImportError("The tflite package must be installed") - assert isinstance(op, Operator) input_tensors = self.get_input_tensors(op) assert len(input_tensors) == 2, "input tensors length should be 2" @@ -421,14 +405,10 @@ def convert_resize_nearest_neighbor(self, op): def convert_l2_normalization(self, op): """Convert TFLite L2_NORMALIZATION """ try: - from tflite.Operator import Operator - from tflite.BuiltinOptions import BuiltinOptions from tflite.L2NormOptions import L2NormOptions - from tflite.ActivationFunctionType import ActivationFunctionType except ImportError: raise ImportError("The tflite package must be installed") - assert isinstance(op, Operator) input_tensors = self.get_input_tensors(op) assert len(input_tensors) == 1, "input tensors length should be 1" input_tensor = input_tensors[0] @@ -467,13 +447,10 @@ def convert_l2_normalization(self, op): def convert_lrn(self, op): """Convert TFLite LOCAL_RESPONSE_NORMALIZATION """ try: - from tflite.Operator import Operator - from tflite.BuiltinOptions import BuiltinOptions from tflite.LocalResponseNormalizationOptions import LocalResponseNormalizationOptions except ImportError: raise ImportError("The tflite package must be installed") - assert isinstance(op, Operator) if self.is_quantized(op): raise tvm.error.OpNotImplemented( 'TFlite quantized LRN operator is not supported yet.') @@ -503,12 +480,6 @@ def convert_lrn(self, op): def convert_logistic(self, op): """Convert TFLite LOGISTIC""" - try: - from tflite.Operator import Operator - except ImportError: - raise ImportError("The tflite package must be installed") - - assert isinstance(op, Operator) input_tensors = self.get_input_tensors(op) assert len(input_tensors) == 1, "input tensors length should be 1" @@ -529,12 +500,6 @@ def convert_logistic(self, op): def convert_softmax(self, op): """Convert TFLite softmax""" - try: - from tflite.Operator import Operator - except ImportError: - raise ImportError("The tflite package must be installed") - - assert isinstance(op, Operator) input_tensors = self.get_input_tensors(op) assert len(input_tensors) == 1, "input tensors length should be 1" @@ -564,12 +529,6 @@ def convert_softmax(self, op): def convert_tanh(self, op): """Convert TFLite TANH""" - try: - from tflite.Operator import Operator - except ImportError: - raise ImportError("The tflite package must be installed") - - assert isinstance(op, Operator) input_tensors = self.get_input_tensors(op) assert len(input_tensors) == 1, "input tensors length should be 1" @@ -581,12 +540,6 @@ def convert_tanh(self, op): def convert_relu(self, op): """Convert TFLite ReLU""" - try: - from tflite.Operator import Operator - except ImportError: - raise ImportError("The tflite package must be installed") - - assert isinstance(op, Operator) input_tensors = self.get_input_tensors(op) assert len(input_tensors) == 1, "input tensors length should be 1" @@ -598,12 +551,6 @@ def convert_relu(self, op): def convert_hard_swish(self, op): """Convert TFLite Hard swish""" - try: - from tflite.Operator import Operator - except ImportError: - raise ImportError("The tflite package must be installed") - assert isinstance(op, Operator) - input_tensors = self.get_input_tensors(op) assert len(input_tensors) == 1, "input tensors length should be 1" input_tensor = input_tensors[0] @@ -635,14 +582,10 @@ def _hard_swish(data): def convert_concatenation(self, op): """Convert TFLite concatenation""" try: - from tflite.Operator import Operator from tflite.ConcatenationOptions import ConcatenationOptions - from tflite.BuiltinOptions import BuiltinOptions - from tflite.ActivationFunctionType import ActivationFunctionType except ImportError: raise ImportError("The tflite package must be installed") - assert isinstance(op, Operator) input_tensors = self.get_input_tensors(op) assert len(input_tensors) >= 1, "input tensors should greater than 1" in_exprs = [self.get_expr(input_tensor.tensor_idx) for input_tensor in input_tensors] @@ -683,12 +626,6 @@ def convert_concatenation(self, op): def _convert_unary_elemwise(self, relay_op, op): """Generic method to convert TFLite unary elemwise functions""" - try: - from tflite.Operator import Operator - except ImportError: - raise ImportError("The tflite package must be installed") - - assert isinstance(op, Operator) input_tensors = self.get_input_tensors(op) assert len(input_tensors) == 1, "input tensors length should be 1" @@ -784,12 +721,6 @@ def convert_neg(self, op): def convert_elu(self, op): """Convert TFLite ELU""" - try: - from tflite.Operator import Operator - except ImportError: - raise ImportError("The tflite package must be installed") - assert isinstance(op, Operator) - if self.is_quantized(op): raise tvm.error.OpNotImplemented( 'TFlite quantized ELU operator is not supported yet.') @@ -807,12 +738,6 @@ def convert_elu(self, op): def convert_square(self, op): """Convert TFLite SQUARE""" - try: - from tflite.Operator import Operator - except ImportError: - raise ImportError("The tflite package must be installed") - - assert isinstance(op, Operator) input_tensors = self.get_input_tensors(op) assert len(input_tensors) == 1, "input tensors length should be 1" input_tensor = input_tensors[0] @@ -834,17 +759,13 @@ def convert_square(self, op): def _convert_elemwise(self, relay_op, op): """Generic method to Convert TFLite elemwise""" try: - from tflite.Operator import Operator from tflite.AddOptions import AddOptions from tflite.SubOptions import SubOptions from tflite.MulOptions import MulOptions from tflite.DivOptions import DivOptions - from tflite.BuiltinOptions import BuiltinOptions - from tflite.ActivationFunctionType import ActivationFunctionType except ImportError: raise ImportError("The tflite package must be installed") - assert isinstance(op, Operator) input_tensors = self.get_input_tensors(op) assert len(input_tensors) == 2, "input tensors length should be 2" @@ -1025,12 +946,6 @@ def convert_not_equal(self, op): def _convert_logical_binary(self, relay_op, op): """Generic method to convert logical binary ops""" - try: - from tflite.Operator import Operator - except ImportError: - raise ImportError("The tflite package must be installed") - - assert isinstance(op, Operator) input_tensors = self.get_input_tensors(op) assert len(input_tensors) == 2, "input tensors length should be 2" @@ -1052,12 +967,6 @@ def convert_logical_or(self, op): def convert_zeros_like(self, op): """Convert TFLite ZEROS LIKE""" - try: - from tflite.Operator import Operator - except ImportError: - raise ImportError("The tflite package must be installed") - - assert isinstance(op, Operator) input_tensors = self.get_input_tensors(op) assert len(input_tensors) == 1, "input tensors length should be 1" @@ -1070,13 +979,10 @@ def convert_zeros_like(self, op): def _convert_reduce(self, relay_op, op): """Generic method to Convert TFLite MEAN operators""" try: - from tflite.BuiltinOptions import BuiltinOptions - from tflite.Operator import Operator from tflite.ReducerOptions import ReducerOptions except ImportError: raise ImportError("The tflite package must be installed") - assert isinstance(op, Operator) input_tensors = self.get_input_tensors(op) assert len(input_tensors) == 2, "input tensors length should be 2" @@ -1135,15 +1041,10 @@ def _convert_reduce_any(self, op): def convert_fully_connected(self, op): """Convert TFLite fully connected""" try: - from tflite.Operator import Operator from tflite.FullyConnectedOptions import FullyConnectedOptions - from tflite.BuiltinOptions import BuiltinOptions - from tflite.TensorType import TensorType - from tflite.ActivationFunctionType import ActivationFunctionType except ImportError: raise ImportError("The tflite package must be installed") - assert isinstance(op, Operator) input_tensors = self.get_input_tensors(op) assert len(input_tensors) >= 2, "input tensors length should be >= 2" @@ -1237,13 +1138,10 @@ def convert_fully_connected(self, op): def convert_squeeze(self, op): """Convert TFLite squeeze""" try: - from tflite.BuiltinOptions import BuiltinOptions - from tflite.Operator import Operator from tflite.SqueezeOptions import SqueezeOptions except ImportError: raise ImportError("The tflite package must be installed") - assert isinstance(op, Operator) input_tensors = self.get_input_tensors(op) output_tensors = self.get_output_tensors(op) assert len(input_tensors) == 1, "input tensors length should be 1" @@ -1264,10 +1162,6 @@ def convert_squeeze(self, op): def convert_fused_activation_function(self, in_expr, fused_activation_fn): """Convert TFLite fused activation function""" - try: - from tflite.ActivationFunctionType import ActivationFunctionType - except ImportError: - raise ImportError("The tflite package must be installed") assert fused_activation_fn != ActivationFunctionType.NONE if fused_activation_fn == ActivationFunctionType.RELU6: return _op.clip(in_expr, a_min=0, a_max=6) @@ -1284,17 +1178,12 @@ def convert_fused_activation_function(self, in_expr, fused_activation_fn): def convert_conv(self, op, conv_type): """convolution implementation.""" try: - from tflite.BuiltinOptions import BuiltinOptions - from tflite.ActivationFunctionType import ActivationFunctionType - from tflite.TensorType import TensorType - from tflite.Operator import Operator from tflite.Conv2DOptions import Conv2DOptions from tflite.DepthwiseConv2DOptions import DepthwiseConv2DOptions from tflite.Padding import Padding except ImportError: raise ImportError("The tflite package must be installed") - assert isinstance(op, Operator) input_tensors = self.get_input_tensors(op) assert len(input_tensors) >= 2, "input tensors length should be >= 2" @@ -1454,13 +1343,10 @@ def convert_conv(self, op, conv_type): def convert_split(self, op): """split implementation.""" try: - from tflite.BuiltinOptions import BuiltinOptions - from tflite.Operator import Operator from tflite.SplitOptions import SplitOptions except ImportError: raise ImportError("The tflite package must be installed") - assert isinstance(op, Operator) input_tensors = self.get_input_tensors(op) assert len(input_tensors) == 2, "input tensors length should be == 2" @@ -1490,12 +1376,6 @@ def convert_split(self, op): def convert_slice(self, op): """Convert TFLite SLICE""" - try: - from tflite.Operator import Operator - except ImportError: - raise ImportError("The tflite package must be installed") - - assert isinstance(op, Operator) input_tensors = self.get_input_tensors(op) assert len(input_tensors) == 3, "input tensors length should be == 3" input_tensor = input_tensors[0] @@ -1519,12 +1399,6 @@ def convert_slice(self, op): def convert_transpose(self, op): """transpose implementation.""" - try: - from tflite.Operator import Operator - except ImportError: - raise ImportError("The tflite package must be installed") - - assert isinstance(op, Operator) input_tensors = self.get_input_tensors(op) assert len(input_tensors) == 2, "input tensors length should be 2" input_tensor = input_tensors[0] @@ -1545,13 +1419,10 @@ def convert_transpose(self, op): def convert_cast(self, op): """Convert TFLite CAST""" try: - from tflite.Operator import Operator - from tflite.BuiltinOptions import BuiltinOptions from tflite.CastOptions import CastOptions except ImportError: raise ImportError("The tflite package must be installed") - assert isinstance(op, Operator) input_tensors = self.get_input_tensors(op) assert len(input_tensors) == 1, "input tensors length should be 1" input_tensor = input_tensors[0] @@ -1569,12 +1440,6 @@ def convert_cast(self, op): def convert_tile(self, op): """tile implementation.""" - try: - from tflite.Operator import Operator - except ImportError: - raise ImportError("The tflite package must be installed") - - assert isinstance(op, Operator) input_tensors = self.get_input_tensors(op) assert len(input_tensors) == 2, "input tensors length should be 2" input_tensor = input_tensors[0] @@ -1591,12 +1456,6 @@ def convert_tile(self, op): def convert_topk_v2(self, op): """ Convert TFLite TOPK_v2 """ - try: - from tflite.Operator import Operator - except ImportError: - raise ImportError("The tflite package must be installed") - - assert isinstance(op, Operator) input_tensors = self.get_input_tensors(op) assert len(input_tensors) == 2, "input tensors length should be 2" input_tensor = input_tensors[0] @@ -1610,15 +1469,11 @@ def convert_topk_v2(self, op): def convert_pool2d(self, op, pool_type): """pool2d implementation.""" try: - from tflite.BuiltinOptions import BuiltinOptions - from tflite.ActivationFunctionType import ActivationFunctionType - from tflite.Operator import Operator from tflite.Pool2DOptions import Pool2DOptions from tflite.Padding import Padding except ImportError: raise ImportError("The tflite package must be installed") - assert isinstance(op, Operator) input_tensors = self.get_input_tensors(op) assert len(input_tensors) == 1, "input tensors length should be 1" input_tensor = input_tensors[0] @@ -1689,12 +1544,6 @@ def convert_pool2d(self, op, pool_type): def convert_pad(self, op): """Convert TFLite PAD""" - try: - from tflite.Operator import Operator - except ImportError: - raise ImportError("The tflite package must be installed") - - assert isinstance(op, Operator) input_tensors = self.get_input_tensors(op) assert len(input_tensors) == 2, "input tensors length should be 2" @@ -1740,8 +1589,6 @@ def convert_floor_mod(self, op): def convert_mirror_pad(self, op): """Convert TFLite MIRROR_PAD""" try: - from tflite.Operator import Operator - from tflite.BuiltinOptions import BuiltinOptions from tflite.MirrorPadOptions import MirrorPadOptions except ImportError: raise ImportError("The tflite package must be installed") @@ -1751,7 +1598,6 @@ def convert_mirror_pad(self, op): raise tvm.error.OpNotImplemented( 'TFlite quantized MIRROR_PAD operator is not supported yet.') - assert isinstance(op, Operator) input_tensors = self.get_input_tensors(op) assert len(input_tensors) == 2, "input tensors length should be 2" @@ -1778,13 +1624,10 @@ def convert_mirror_pad(self, op): def convert_pack(self, op): """Convert TFLite pack""" try: - from tflite.BuiltinOptions import BuiltinOptions - from tflite.Operator import Operator from tflite.PackOptions import PackOptions except ImportError: raise ImportError("The tflite package must be installed") - assert isinstance(op, Operator) input_tensors = self.get_input_tensors(op) assert len(input_tensors) >= 1, "input tensors should greater than 1" in_exprs = [self.get_expr(input_tensor.tensor_idx) for input_tensor in input_tensors] @@ -1805,13 +1648,10 @@ def convert_pack(self, op): def convert_unpack(self, op): """Convert TFLite unpack""" try: - from tflite.BuiltinOptions import BuiltinOptions - from tflite.Operator import Operator from tflite.UnpackOptions import UnpackOptions except ImportError: raise ImportError("The tflite package must be installed") - assert isinstance(op, Operator) input_tensors = self.get_input_tensors(op) assert len(input_tensors) == 1, "input tensors length should be 1" input_tensor = input_tensors[0] @@ -1848,12 +1688,7 @@ def convert_unpack(self, op): def convert_batch_to_space_nd(self, op): """batch_to_space_nd implementation.""" - try: - from tflite.Operator import Operator - except ImportError: - raise ImportError("The tflite package must be installed") - assert isinstance(op, Operator) input_tensors = self.get_input_tensors(op) assert len(input_tensors) == 3, "input tensors length should be 3" @@ -1901,12 +1736,6 @@ def convert_batch_to_space_nd(self, op): def convert_space_to_batch_nd(self, op): """space_to_batch_nd implementation.""" - try: - from tflite.Operator import Operator - except ImportError: - raise ImportError("The tflite package must be installed") - - assert isinstance(op, Operator) input_tensors = self.get_input_tensors(op) assert len(input_tensors) == 3, "input tensors length should be 3" @@ -1959,13 +1788,10 @@ def convert_space_to_batch_nd(self, op): def convert_depth_to_space(self, op): """Convert TFLite DEPTH_TO_SPACE""" try: - from tflite.BuiltinOptions import BuiltinOptions - from tflite.Operator import Operator from tflite.DepthToSpaceOptions import DepthToSpaceOptions except ImportError: raise ImportError("The tflite package must be installed") - assert isinstance(op, Operator) input_tensors = self.get_input_tensors(op) assert len(input_tensors) == 1, "input tensors length should be 1" @@ -1984,13 +1810,10 @@ def convert_depth_to_space(self, op): def convert_space_to_depth(self, op): """Convert TFLite SPACE_TO_DEPTH""" try: - from tflite.BuiltinOptions import BuiltinOptions - from tflite.Operator import Operator from tflite.SpaceToDepthOptions import SpaceToDepthOptions except ImportError: raise ImportError("The tflite package must be installed") - assert isinstance(op, Operator) input_tensors = self.get_input_tensors(op) assert len(input_tensors) == 1, "input tensors length should be 1" @@ -2008,12 +1831,6 @@ def convert_space_to_depth(self, op): def convert_prelu(self, op): """Convert TFLite PReLU""" - try: - from tflite.Operator import Operator - except ImportError: - raise ImportError("The tflite package must be installed") - - assert isinstance(op, Operator) input_tensors = self.get_input_tensors(op) assert len(input_tensors) == 2, "input tensors length should be 2" @@ -2031,15 +1848,11 @@ def convert_prelu(self, op): def convert_transpose_conv(self, op): """Convert TFLite TRANSPOSE_CONV""" try: - from tflite.BuiltinOptions import BuiltinOptions - from tflite.TensorType import TensorType - from tflite.Operator import Operator from tflite.TransposeConvOptions import TransposeConvOptions from tflite.Padding import Padding except ImportError: raise ImportError("The tflite package must be installed") - assert isinstance(op, Operator) input_tensors = self.get_input_tensors(op) assert len(input_tensors) == 3, "input tensors length should be 3" @@ -2394,7 +2207,6 @@ def from_tflite(model, shape_dict, dtype_dict): try: import tflite.Model import tflite.SubGraph - import tflite.BuiltinOperator except ImportError: raise ImportError("The tflite package must be installed") assert isinstance(model, tflite.Model.Model) From 093c31401e85f914abe5ed11f8794476a127fdb2 Mon Sep 17 00:00:00 2001 From: Ramana Radhakrishnan Date: Fri, 17 Apr 2020 21:31:03 +0100 Subject: [PATCH 2/2] Only restrict to tflite.Operator --- python/tvm/relay/frontend/tflite.py | 72 +++++++++++++++++++++++------ 1 file changed, 57 insertions(+), 15 deletions(-) diff --git a/python/tvm/relay/frontend/tflite.py b/python/tvm/relay/frontend/tflite.py index 66e2aa82c153..a2e090408e92 100644 --- a/python/tvm/relay/frontend/tflite.py +++ b/python/tvm/relay/frontend/tflite.py @@ -32,20 +32,6 @@ from .common import ExprTable from .common import infer_shape as _infer_shape -# A note on tflite imports. Operator specific imports of modules -# need to be with the operator. General imports that are common across -# multiple operators and which are very common should be in the -# block below. -try: - from tflite.Operator import Operator as Operator - from tflite.TensorType import TensorType as TensorType - from tflite.BuiltinOperator import BuiltinOperator as BuiltinOperator - from tflite.BuiltinOptions import BuiltinOptions as BuiltinOptions - from tflite.ActivationFunctionType import ActivationFunctionType as ActivationFunctionType -except ImportError: - raise ImportError("The tflite package must be installed") - - __all__ = ['from_tflite'] class TensorWrapper(object): @@ -60,6 +46,12 @@ class OperatorConverter(object): """Operator Converted for converting TFLite ops to Relay ops""" def __init__(self, model, subgraph, exp_tab): + try: + from tflite.BuiltinOperator import BuiltinOperator + from tflite.BuiltinOptions import BuiltinOptions + from tflite.ActivationFunctionType import ActivationFunctionType + except ImportError: + raise ImportError("The tflite package must be installed") self.model = model self.subgraph = subgraph @@ -167,6 +159,10 @@ def convert_op_to_relay(self): op = self.subgraph.Operators(op_idx) op_code_str = self.get_op_code_str(op) output_tensors = self.get_output_tensors(op) + try: + from tflite.Operator import Operator + except ImportError: + raise ImportError("The tflite package must be installed") assert isinstance(op, Operator) ret = self.convert_map[op_code_str](op) @@ -181,6 +177,10 @@ def convert_op_to_relay(self): def get_op_code_str(self, op): """Get TFLite ops string representation""" + try: + from tflite.BuiltinOperator import BuiltinOperator + except ImportError: + raise ImportError("The tflite package must be installed") op_code_list_idx = op.OpcodeIndex() op_code_id = self.model.OperatorCodes(op_code_list_idx).BuiltinCode() @@ -236,6 +236,11 @@ def get_tensor_value(self, tensor_wrapper): """Get tensor buffer value from given tensor wrapper""" assert isinstance(tensor_wrapper, TensorWrapper) + try: + from tflite.TensorType import TensorType + except ImportError: + raise ImportError("The tflite package must be installed") + if tensor_wrapper.tensor.Type() == TensorType.UINT8: return np.frombuffer(tensor_wrapper.buffer.DataAsNumpy(), dtype=np.uint8).reshape( tensor_wrapper.tensor.ShapeAsNumpy()) @@ -256,6 +261,11 @@ def get_tensor_value(self, tensor_wrapper): def get_tensor_type_str(self, tensor_type): """Get tensor type string representation when given TFLite tensor type""" + try: + from tflite.TensorType import TensorType + except ImportError: + raise ImportError("The tflite package must be installed") + if tensor_type == TensorType.UINT8: return "uint8" if tensor_type == TensorType.FLOAT32: @@ -283,7 +293,6 @@ def has_same_qnn_params(self, lhs_tensor, rhs_tensor): def is_quantized(self, op): """Check if an input tensor is quantized.""" - input_tensors = self.get_input_tensors(op) first_tensor = input_tensors[0] return first_tensor.qnn_params is not None @@ -324,6 +333,7 @@ def convert_max_pool2d(self, op): def convert_reshape(self, op): """Convert TFLite reshape""" try: + from tflite.BuiltinOptions import BuiltinOptions from tflite.ReshapeOptions import ReshapeOptions except ImportError: raise ImportError("The tflite package must be installed") @@ -354,6 +364,7 @@ def convert_reshape(self, op): def _convert_resize(self, method, op): """Generic method to Convert TFLite RESIZE operators""" try: + from tflite.BuiltinOptions import BuiltinOptions from tflite.ResizeBilinearOptions import ResizeBilinearOptions # ResizeNearestNeighborOptions was added in tflite v1.13 tflite_ver = 1120 @@ -405,7 +416,9 @@ def convert_resize_nearest_neighbor(self, op): def convert_l2_normalization(self, op): """Convert TFLite L2_NORMALIZATION """ try: + from tflite.BuiltinOptions import BuiltinOptions from tflite.L2NormOptions import L2NormOptions + from tflite.ActivationFunctionType import ActivationFunctionType except ImportError: raise ImportError("The tflite package must be installed") @@ -447,6 +460,7 @@ def convert_l2_normalization(self, op): def convert_lrn(self, op): """Convert TFLite LOCAL_RESPONSE_NORMALIZATION """ try: + from tflite.BuiltinOptions import BuiltinOptions from tflite.LocalResponseNormalizationOptions import LocalResponseNormalizationOptions except ImportError: raise ImportError("The tflite package must be installed") @@ -583,6 +597,8 @@ def convert_concatenation(self, op): """Convert TFLite concatenation""" try: from tflite.ConcatenationOptions import ConcatenationOptions + from tflite.BuiltinOptions import BuiltinOptions + from tflite.ActivationFunctionType import ActivationFunctionType except ImportError: raise ImportError("The tflite package must be installed") @@ -763,6 +779,8 @@ def _convert_elemwise(self, relay_op, op): from tflite.SubOptions import SubOptions from tflite.MulOptions import MulOptions from tflite.DivOptions import DivOptions + from tflite.BuiltinOptions import BuiltinOptions + from tflite.ActivationFunctionType import ActivationFunctionType except ImportError: raise ImportError("The tflite package must be installed") @@ -979,6 +997,7 @@ def convert_zeros_like(self, op): def _convert_reduce(self, relay_op, op): """Generic method to Convert TFLite MEAN operators""" try: + from tflite.BuiltinOptions import BuiltinOptions from tflite.ReducerOptions import ReducerOptions except ImportError: raise ImportError("The tflite package must be installed") @@ -1042,6 +1061,9 @@ def convert_fully_connected(self, op): """Convert TFLite fully connected""" try: from tflite.FullyConnectedOptions import FullyConnectedOptions + from tflite.BuiltinOptions import BuiltinOptions + from tflite.TensorType import TensorType + from tflite.ActivationFunctionType import ActivationFunctionType except ImportError: raise ImportError("The tflite package must be installed") @@ -1138,6 +1160,7 @@ def convert_fully_connected(self, op): def convert_squeeze(self, op): """Convert TFLite squeeze""" try: + from tflite.BuiltinOptions import BuiltinOptions from tflite.SqueezeOptions import SqueezeOptions except ImportError: raise ImportError("The tflite package must be installed") @@ -1162,6 +1185,10 @@ def convert_squeeze(self, op): def convert_fused_activation_function(self, in_expr, fused_activation_fn): """Convert TFLite fused activation function""" + try: + from tflite.ActivationFunctionType import ActivationFunctionType + except ImportError: + raise ImportError("The tflite package must be installed") assert fused_activation_fn != ActivationFunctionType.NONE if fused_activation_fn == ActivationFunctionType.RELU6: return _op.clip(in_expr, a_min=0, a_max=6) @@ -1178,6 +1205,9 @@ def convert_fused_activation_function(self, in_expr, fused_activation_fn): def convert_conv(self, op, conv_type): """convolution implementation.""" try: + from tflite.BuiltinOptions import BuiltinOptions + from tflite.ActivationFunctionType import ActivationFunctionType + from tflite.TensorType import TensorType from tflite.Conv2DOptions import Conv2DOptions from tflite.DepthwiseConv2DOptions import DepthwiseConv2DOptions from tflite.Padding import Padding @@ -1343,6 +1373,7 @@ def convert_conv(self, op, conv_type): def convert_split(self, op): """split implementation.""" try: + from tflite.BuiltinOptions import BuiltinOptions from tflite.SplitOptions import SplitOptions except ImportError: raise ImportError("The tflite package must be installed") @@ -1419,6 +1450,7 @@ def convert_transpose(self, op): def convert_cast(self, op): """Convert TFLite CAST""" try: + from tflite.BuiltinOptions import BuiltinOptions from tflite.CastOptions import CastOptions except ImportError: raise ImportError("The tflite package must be installed") @@ -1469,6 +1501,8 @@ def convert_topk_v2(self, op): def convert_pool2d(self, op, pool_type): """pool2d implementation.""" try: + from tflite.BuiltinOptions import BuiltinOptions + from tflite.ActivationFunctionType import ActivationFunctionType from tflite.Pool2DOptions import Pool2DOptions from tflite.Padding import Padding except ImportError: @@ -1589,6 +1623,7 @@ def convert_floor_mod(self, op): def convert_mirror_pad(self, op): """Convert TFLite MIRROR_PAD""" try: + from tflite.BuiltinOptions import BuiltinOptions from tflite.MirrorPadOptions import MirrorPadOptions except ImportError: raise ImportError("The tflite package must be installed") @@ -1624,6 +1659,7 @@ def convert_mirror_pad(self, op): def convert_pack(self, op): """Convert TFLite pack""" try: + from tflite.BuiltinOptions import BuiltinOptions from tflite.PackOptions import PackOptions except ImportError: raise ImportError("The tflite package must be installed") @@ -1648,6 +1684,7 @@ def convert_pack(self, op): def convert_unpack(self, op): """Convert TFLite unpack""" try: + from tflite.BuiltinOptions import BuiltinOptions from tflite.UnpackOptions import UnpackOptions except ImportError: raise ImportError("The tflite package must be installed") @@ -1788,6 +1825,7 @@ def convert_space_to_batch_nd(self, op): def convert_depth_to_space(self, op): """Convert TFLite DEPTH_TO_SPACE""" try: + from tflite.BuiltinOptions import BuiltinOptions from tflite.DepthToSpaceOptions import DepthToSpaceOptions except ImportError: raise ImportError("The tflite package must be installed") @@ -1810,6 +1848,7 @@ def convert_depth_to_space(self, op): def convert_space_to_depth(self, op): """Convert TFLite SPACE_TO_DEPTH""" try: + from tflite.BuiltinOptions import BuiltinOptions from tflite.SpaceToDepthOptions import SpaceToDepthOptions except ImportError: raise ImportError("The tflite package must be installed") @@ -1848,6 +1887,8 @@ def convert_prelu(self, op): def convert_transpose_conv(self, op): """Convert TFLite TRANSPOSE_CONV""" try: + from tflite.BuiltinOptions import BuiltinOptions + from tflite.TensorType import TensorType from tflite.TransposeConvOptions import TransposeConvOptions from tflite.Padding import Padding except ImportError: @@ -2207,6 +2248,7 @@ def from_tflite(model, shape_dict, dtype_dict): try: import tflite.Model import tflite.SubGraph + import tflite.BuiltinOperator except ImportError: raise ImportError("The tflite package must be installed") assert isinstance(model, tflite.Model.Model)