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)
| 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.") |