tf.contrib.lite.TocoConverter

Class TocoConverter

Defined in tensorflow/contrib/lite/python/lite.py.

Convert a TensorFlow model into output_format using TOCO.

This is used to convert from a TensorFlow GraphDef or SavedModel into either a TFLite FlatBuffer or graph visualization.

Attributes:

  • inference_type: Target data type of real-number arrays in the output file. Must be {FLOAT, QUANTIZED_UINT8}. (default FLOAT)
  • inference_input_type: Target data type of real-number input arrays. Allows for a different type for input arrays in the case of quantization. Must be {FLOAT, QUANTIZED_UINT8}. (default inference_type)
  • output_format: Output file format. Currently must be {TFLITE, GRAPHVIZ_DOT}. (default TFLITE)
  • quantized_input_stats: Dict of strings representing input tensor names mapped to tuple of integers representing the mean and standard deviation of the training data (e.g., {"foo" : (0., 1.)}). Only need if inference_type is QUANTIZED_UINT8. (default {})
  • default_ranges_stats: Tuple of integers representing (min, max) range values for all arrays without a specified range. Intended for experimenting with quantization via "dummy quantization". (default None)
  • drop_control_dependency: Boolean indicating whether to drop control dependencies silently. This is due to TFLite not supporting control dependencies. (default True)
  • reorder_across_fake_quant: Boolean indicating whether to reorder FakeQuant nodes in unexpected locations. Used when the location of the FakeQuant nodes is preventing graph transformations necessary to convert the graph. Results in a graph that differs from the quantized training graph, potentially causing differing arithmetic behavior. (default False)
  • change_concat_input_ranges: Boolean to change behavior of min/max ranges for inputs and outputs of the concat operator for quantized models. Changes the ranges of concat operator overlap when true. (default False)
  • allow_custom_ops: Boolean indicating whether to allow custom operations. When false any unknown operation is an error. When true, custom ops are created for any op that is unknown. The developer will need to provide these to the TensorFlow Lite runtime with a custom resolver. (default False)
  • quantize_weights: Boolean indicating whether to store weights as quantized weights followed by dequantize operations. Computation is still done in float, but reduces model size (at the cost of accuracy and latency). (default False)
  • dump_graphviz_dir: Full filepath of folder to dump the graphs at various stages of processing GraphViz .dot files. Preferred over --output_format=GRAPHVIZ_DOT in order to keep the requirements of the output file. (default None)
  • dump_graphviz_video: Boolean indicating whether to dump the graph after every graph transformation. (default False)

Example usage:

# Converting a GraphDef from session. converter = lite.TocoConverter.from_session(sess, in_tensors, out_tensors) tflite_model = converter.convert() open("converted_model.tflite", "wb").write(tflite_model)

# Converting a GraphDef from file. converter = lite.TocoConverter.from_frozen_graph( graph_def_file, input_arrays, output_arrays) tflite_model = converter.convert() open("converted_model.tflite", "wb").write(tflite_model)

# Converting a SavedModel. converter = lite.TocoConverter.from_saved_model(saved_model_dir) tflite_model = converter.convert()

Methods

__init__

__init__(
    graph_def,
    input_tensors,
    output_tensors
)

Constructor for TocoConverter.

Args:

  • graph_def: Frozen TensorFlow GraphDef.
  • input_tensors: List of input tensors. Type and shape are computed using foo.get_shape() and foo.dtype.
  • output_tensors: List of output tensors (only .name is used from this).

convert

convert()

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 for dimension in input_tensor.

from_frozen_graph

@classmethod
from_frozen_graph(
    cls,
    graph_def_file,
    input_arrays,
    output_arrays,
    input_shapes=None
)

Creates a TocoConverter class from a file containing a frozen GraphDef.

Args:

  • graph_def_file: Full filepath of file containing frozen GraphDef.
  • input_arrays: List of input tensors to freeze graph with.
  • output_arrays: List of output tensors to freeze graph with.
  • input_shapes: Dict of strings representing input tensor names to list of integers representing input shapes (e.g., {"foo" : [1, 16, 16, 3]}). Automatically determined when input shapes is None (e.g., {"foo" : None}). (default None)

Returns:

TocoConverter class.

Raises:

  • ValueError: Unable to parse input file. The graph is not frozen. input_arrays or output_arrays contains an invalid tensor name.

from_keras_model_file

@classmethod
from_keras_model_file(
    cls,
    model_file,
    input_arrays=None,
    input_shapes=None,
    output_arrays=None
)

Creates a TocoConverter class from a tf.keras model file.

Args:

  • model_file: Full filepath of HDF5 file containing the tf.keras model.
  • input_arrays: List of input tensors to freeze graph with. Uses input arrays from SignatureDef when none are provided. (default None)
  • input_shapes: Dict of strings representing input tensor names to list of integers representing input shapes (e.g., {"foo" : [1, 16, 16, 3]}). Automatically determined when input shapes is None (e.g., {"foo" : None}). (default None)
  • output_arrays: List of output tensors to freeze graph with. Uses output arrays from SignatureDef when none are provided. (default None)

Returns:

TocoConverter class.

from_saved_model

@classmethod
from_saved_model(
    cls,
    saved_model_dir,
    input_arrays=None,
    input_shapes=None,
    output_arrays=None,
    tag_set=None,
    signature_key=None
)

Creates a TocoConverter class from a SavedModel.

Args:

  • saved_model_dir: SavedModel directory to convert.
  • input_arrays: List of input tensors to freeze graph with. Uses input arrays from SignatureDef when none are provided. (default None)
  • input_shapes: Dict of strings representing input tensor names to list of integers representing input shapes (e.g., {"foo" : [1, 16, 16, 3]}). Automatically determined when input shapes is None (e.g., {"foo" : None}). (default None)
  • output_arrays: List of output tensors to freeze graph with. Uses output arrays from SignatureDef when none are provided. (default None)
  • tag_set: Set of tags identifying the MetaGraphDef within the SavedModel to analyze. All tags in the tag set must be present. (default set("serve"))
  • signature_key: Key identifying SignatureDef containing inputs and outputs. (default DEFAULT_SERVING_SIGNATURE_DEF_KEY)

Returns:

TocoConverter class.

from_session

@classmethod
from_session(
    cls,
    sess,
    input_tensors,
    output_tensors
)

Creates a TocoConverter class from a TensorFlow Session.

Args:

  • sess: TensorFlow Session.
  • input_tensors: List of input tensors. Type and shape are computed using foo.get_shape() and foo.dtype.
  • output_tensors: List of output tensors (only .name is used from this).

Returns:

TocoConverter class.

get_input_arrays

get_input_arrays()

Returns a list of the names of the input tensors.

Returns:

List of strings.