MCPcopy Create free account
hub / github.com/DeepRec-AI/DeepRec / convert

Method convert

tensorflow/lite/python/lite.py:871–995  ·  view source on GitHub ↗

Converts a TensorFlow GraphDef based on instance variables. Returns: The converted data in serialized format. Either a TFLite Flatbuffer or a Graphviz graph depending on value in `output_format`. Raises: ValueError: Input shape is not specified. None value

(self)

Source from the content-addressed store, hash-verified

869 return object.__getattribute__(self, name)
870
871 def convert(self):
872 """Converts a TensorFlow GraphDef based on instance variables.
873
874 Returns:
875 The converted data in serialized format. Either a TFLite Flatbuffer or a
876 Graphviz graph depending on value in `output_format`.
877
878 Raises:
879 ValueError:
880 Input shape is not specified.
881 None value for dimension in input_tensor.
882 """
883 # Checks dimensions in input tensor.
884 if self._has_valid_tensors():
885 for tensor in self._input_tensors:
886 shape = tensor.shape
887 if not shape:
888 raise ValueError("Provide an input shape for input array "
889 "'{0}'.".format(_get_tensor_name(tensor)))
890 # Note that shape_list might be empty for scalar shapes.
891 shape_list = shape.as_list()
892 if None in shape_list[1:]:
893 raise ValueError(
894 "None is only supported in the 1st dimension. Tensor '{0}' has "
895 "invalid shape '{1}'.".format(
896 _get_tensor_name(tensor), shape_list))
897 elif shape_list and shape_list[0] is None:
898 self._set_batch_size(batch_size=1)
899
900 # Get quantization stats. Ensures there is one stat per name if the stats
901 # are specified.
902 if self.quantized_input_stats:
903 quantized_stats = []
904 invalid_stats = []
905 for name in self.get_input_arrays():
906 if name in self.quantized_input_stats:
907 quantized_stats.append(self.quantized_input_stats[name])
908 else:
909 invalid_stats.append(name)
910
911 if invalid_stats:
912 raise ValueError("Quantization input stats are not available for input "
913 "tensors '{0}'.".format(",".join(invalid_stats)))
914 else:
915 quantized_stats = None
916
917 self._validate_quantization()
918 self._validate_representative_dataset()
919
920 toco_inference_input_type = self.inference_input_type
921 inference_input_type = self.inference_input_type
922 inference_output_type = self.inference_output_type
923 post_training_optimize = self._is_post_training_optimize()
924 if post_training_optimize:
925 # Post training optimizations require that TOCO outputs a float model.
926 if self.inference_type != constants.FLOAT:
927 raise ValueError(
928 "`optimizations` require that `inference_type` is set to float.")

Callers 15

testFloatMethod · 0.45
testStringMethod · 0.45
testQuantizationMethod · 0.45
testScalarValidMethod · 0.45
testFunctionsMethod · 0.45
testFloatMethod · 0.45
testControlFlowMethod · 0.45
testStaticRnnMethod · 0.45
testLoopMethod · 0.45
testDynamicRnnMethod · 0.45
testKerasLSTMMethod · 0.45

Calls 15

_has_valid_tensorsMethod · 0.95
_set_batch_sizeMethod · 0.95
get_input_arraysMethod · 0.95
_grappler_configMethod · 0.80
_get_tensor_nameFunction · 0.50

Tested by 15

testFloatMethod · 0.36
testStringMethod · 0.36
testQuantizationMethod · 0.36
testScalarValidMethod · 0.36
testFunctionsMethod · 0.36
testFloatMethod · 0.36
testControlFlowMethod · 0.36
testStaticRnnMethod · 0.36
testLoopMethod · 0.36
testDynamicRnnMethod · 0.36
testKerasLSTMMethod · 0.36