예제 #1
0
def convert_jax_hlo(input_content, is_proto_format, **kwargs):
    """Converts a Jax hlo-based model using TF Lite converter."""
    model_flags = _model_flags_pb2.ModelFlags()
    model_flags.use_hlo_import = True
    if is_proto_format:
        model_flags.hlo_file_type = _model_flags_pb2.ModelFlags.HLO_PROTO
    else:
        model_flags.hlo_file_type = _model_flags_pb2.ModelFlags.HLO_TEXT

    toco_flags = build_toco_flags(**kwargs)
    data = toco_convert_protos(
        model_flags.SerializeToString(),
        toco_flags.SerializeToString(),
        input_content,
        None,  # debug_info_str, unused
        enable_mlir_converter=True)
    return data
    def _run(self, sess, in_tensor, out_tensor, should_succeed):
        """Use toco binary to check conversion from graphdef to tflite.

    Args:
      sess: Active TensorFlow session containing graph.
      in_tensor: TensorFlow tensor to use as input.
      out_tensor: TensorFlow tensor to use as output.
      should_succeed: Whether this is a valid conversion.
    """
        # Build all protos and extract graphdef
        graph_def = sess.graph_def
        toco_flags = toco_flags_pb2.TocoFlags()
        toco_flags.input_format = toco_flags_pb2.TENSORFLOW_GRAPHDEF
        toco_flags.output_format = toco_flags_pb2.TFLITE
        toco_flags.inference_input_type = types_pb2.FLOAT
        toco_flags.inference_type = types_pb2.FLOAT
        toco_flags.allow_custom_ops = True
        model_flags = model_flags_pb2.ModelFlags()
        input_array = model_flags.input_arrays.add()
        input_array.name = TensorName(in_tensor)
        input_array.shape.dims.extend(map(int, in_tensor.shape))
        model_flags.output_arrays.append(TensorName(out_tensor))
        # Shell out to run toco (in case it crashes)
        with tempfile.NamedTemporaryFile() as fp_toco, \
               tempfile.NamedTemporaryFile() as fp_model, \
               tempfile.NamedTemporaryFile() as fp_input, \
               tempfile.NamedTemporaryFile() as fp_output:
            fp_model.write(model_flags.SerializeToString())
            fp_toco.write(toco_flags.SerializeToString())
            fp_input.write(graph_def.SerializeToString())
            fp_model.flush()
            fp_toco.flush()
            fp_input.flush()
            tflite_bin = resource_loader.get_path_to_datafile(
                "toco_from_protos.par")
            cmdline = " ".join([
                tflite_bin, fp_model.name, fp_toco.name, fp_input.name,
                fp_output.name
            ])
            exitcode = os.system(cmdline)
            if exitcode == 0:
                stuff = fp_output.read()
                self.assertEqual(stuff is not None, should_succeed)
            else:
                self.assertFalse(should_succeed)
예제 #3
0
def build_model_flags(change_concat_input_ranges=False,
                      allow_nonexistent_arrays=False,
                      saved_model_dir=None,
                      saved_model_version=0,
                      saved_model_tags=None,
                      saved_model_exported_names=None,
                      **_):
    """Builds the model flags object from params.

  Args:
    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_nonexistent_arrays: Allow specifying array names that don't exist or
      are unused in the final graph. (default False)
    saved_model_dir: Filepath of the saved model to be converted. This value
      will be non-empty only when the saved model import path will be used.
      Otherwises, the graph def-based conversion will be processed.
    saved_model_version: SavedModel file format version of The saved model file
      to be converted. This value will be set only when the SavedModel import
      path will be used.
    saved_model_tags: Set of string saved model tags, formatted in the
      comma-separated value. This value will be set only when the SavedModel
      import path will be used.
    saved_model_exported_names: Names to be exported (default: export all) when
      the saved model import path is on. This value will be set only when the
      SavedModel import path will be used.

  Returns:
    model_flags: protocol buffer describing the model.
  """
    model_flags = _model_flags_pb2.ModelFlags()
    model_flags.change_concat_input_ranges = change_concat_input_ranges
    model_flags.allow_nonexistent_arrays = allow_nonexistent_arrays
    if saved_model_dir:
        model_flags.saved_model_dir = saved_model_dir
    model_flags.saved_model_version = saved_model_version
    if saved_model_tags:
        model_flags.saved_model_tags.extend(saved_model_tags)
    if saved_model_exported_names:
        model_flags.saved_model_exported_names.extend(
            saved_model_exported_names)
    return model_flags
예제 #4
0
def convert_jax_hlo(input_content, input_names, is_proto_format, **kwargs):
    """Converts a Jax hlo-based model using TFLite converter."""
    model_flags = _model_flags_pb2.ModelFlags()
    model_flags.use_hlo_import = True
    if is_proto_format:
        model_flags.hlo_file_type = _model_flags_pb2.ModelFlags.HLO_PROTO
    else:
        model_flags.hlo_file_type = _model_flags_pb2.ModelFlags.HLO_TEXT

    # Build input names.
    for input_name in input_names:
        input_array = model_flags.input_arrays.add()
        input_array.name = input_name

    conversion_flags = build_conversion_flags(**kwargs)
    data = convert(model_flags.SerializeToString(),
                   conversion_flags.SerializeToString(),
                   input_data_str=input_content,
                   debug_info_str=None,
                   enable_mlir_converter=True)
    return data
예제 #5
0
파일: convert.py 프로젝트: mrax714/nearme
def convert_saved_model(saved_model_dir=None,
                        saved_model_version=0,
                        saved_model_tags=None,
                        saved_model_exported_names=None,
                        **kwargs):
  """Converts a saved_model using TF Lite converter."""
  model_flags = _model_flags_pb2.ModelFlags()
  if saved_model_dir:
    model_flags.saved_model_dir = saved_model_dir
  model_flags.saved_model_version = saved_model_version
  if saved_model_tags:
    model_flags.saved_model_tags.extend(saved_model_tags)
  if saved_model_exported_names:
    model_flags.saved_model_exported_names.extend(saved_model_exported_names)
  toco_flags = build_toco_flags(**kwargs)
  data = toco_convert_protos(
      model_flags.SerializeToString(),
      toco_flags.SerializeToString(),
      None,  # input_data, unused
      None,  # debug_info_str, unused
      enable_mlir_converter=True)
  return data
예제 #6
0
def build_toco_convert_protos(input_tensors,
                              output_tensors,
                              inference_type=dtypes.float32,
                              inference_input_type=None,
                              input_format=lite_constants.TENSORFLOW_GRAPHDEF,
                              input_shapes=None,
                              output_format=lite_constants.TFLITE,
                              quantized_input_stats=None,
                              default_ranges_stats=None,
                              drop_control_dependency=True,
                              reorder_across_fake_quant=False,
                              allow_custom_ops=False,
                              change_concat_input_ranges=False,
                              post_training_quantize=False,
                              quantize_to_float16=False,
                              dump_graphviz_dir=None,
                              dump_graphviz_video=False,
                              target_ops=None,
                              allow_nonexistent_arrays=False,
                              debug_info=None,
                              conversion_summary_dir=None,
                              saved_model_dir=None,
                              saved_model_version=0,
                              saved_model_tags=None,
                              saved_model_exported_names=None,
                              select_user_tf_ops=None):
    """Builds protocol buffers describing a conversion of a model using TOCO.

  Typically this is to convert from TensorFlow GraphDef to TFLite, in which
  case the default `input_format` and `output_format` are sufficient.

  Args:
    input_tensors: List of input tensors. Type and shape are computed using
      `foo.shape` and `foo.dtype`.
    output_tensors: List of output tensors (only .name is used from this).
    inference_type: Data type of numeric arrays, excluding the input layer.
      (default tf.float32, must be in {tf.float32, tf.int8, tf.uint8})
    inference_input_type: Data type of the numeric arrays in the input layer. If
      `inference_input_type` is in {tf.int8, tf.uint8}, then
      `quantized_input_stats` must be provided. (default is the value assigned
      to `inference_type`, must be in {tf.float32, tf.int8, tf.uint8})
    input_format: Type of data to read.
      (default TENSORFLOW_GRAPHDEF, must be in {TENSORFLOW_GRAPHDEF})
    input_shapes: Input array shape. (default None, must be None or a list of
      the same length as `input_tensors`.)
    output_format: Output file format. (default TFLITE, must be in
    {TFLITE, GRAPHVIZ_DOT})
    quantized_input_stats: Map of input tensor names to a tuple of floats
      representing the mean and standard deviation of the training data.
      (e.g., {"foo" : (0., 1.)}). Required if `inference_input_type` is tf.int8
        or tf.uint8. (default None)
    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)
    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)
    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)
    post_training_quantize: Boolean indicating whether to quantize the weights
      of the converted float model. Model size will be reduced and there will be
      latency improvements (at the cost of accuracy). (default False)
    quantize_to_float16: Boolean indicating whether to convert float buffers to
      float16. (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)
    target_ops: Experimental flag, subject to change. Set of OpsSet options
      indicating which converter to use. (default set([OpsSet.TFLITE_BUILTINS]))
    allow_nonexistent_arrays: Allow specifying array names that don't exist or
      are unused in the final graph. (default False)
    debug_info: `GraphDebugInfo` proto containing the stack traces for the
      original nodes referred by the converted graph.
    conversion_summary_dir: A string, the path to the generated conversion logs.
    saved_model_dir: Filepath of the saved model to be converted. This value
      will be non-empty only when the saved model import path will be used.
      Otherwises, the graph def-based conversion will be processed.
    saved_model_version: SavedModel file format version of The saved model file
      to be converted. This value will be set only when the SavedModel import
      path will be used.
    saved_model_tags: Set of string saved model tags, formatted in the
      comma-separated value. This value will be set only when the SavedModel
      import path will be used.
    saved_model_exported_names: Names to be exported (default: export all) when
      the saved model import path is on. This value will be set only when the
      SavedModel import path will be used.
    select_user_tf_ops: List of user's defined TensorFlow ops need to be
      supported in the TensorFlow Lite runtime. These ops will be supported as
      select TensorFlow ops.

  Returns:
    model_flags, toco_flags, debug_info: three protocol buffers describing the
      conversion process and debug information.

  Raises:
    ValueError:
      If the input tensor type is unknown
      Missing mean_values or std_dev_values
    RuntimeError: If TOCO fails to convert (in which case the runtime error's
      error text will contain the TOCO error log)
  """
    toco = build_toco_flags(inference_type, inference_input_type, input_format,
                            output_format, default_ranges_stats,
                            drop_control_dependency, reorder_across_fake_quant,
                            allow_custom_ops, post_training_quantize,
                            quantize_to_float16, dump_graphviz_dir,
                            dump_graphviz_video, target_ops,
                            conversion_summary_dir, select_user_tf_ops)
    model = _model_flags_pb2.ModelFlags()
    model.change_concat_input_ranges = change_concat_input_ranges
    for idx, input_tensor in enumerate(input_tensors):
        input_array = model.input_arrays.add()
        if saved_model_dir:
            input_array.name = input_tensor.name
        else:
            input_array.name = util.get_tensor_name(input_tensor)
        input_array.data_type = convert_tensor_tf_type_to_tflite_type(
            input_tensor.dtype, usage="input type of the TensorFlow model")

        if _requires_input_stats(toco) and quantized_input_stats:
            input_array.mean_value, input_array.std_value = quantized_input_stats[
                idx]

        if input_shapes is None:
            shape = input_tensor.shape
        else:
            shape = input_shapes[idx]

        if shape.rank is not None:
            # Create shapes with -1 for unknown dimensions.
            dims = []
            for dim in shape:
                if (dim is None or (isinstance(dim, tensor_shape.Dimension)
                                    and dim.value is None)):
                    dims.append(-1)
                else:
                    dims.append(int(dim))
            input_array.shape.dims.extend(dims)
            input_array.shape.unknown_rank = False
        else:
            input_array.shape.unknown_rank = True

    for output_tensor in output_tensors:
        if saved_model_dir:
            model.output_arrays.append(output_tensor.name)
        else:
            model.output_arrays.append(util.get_tensor_name(output_tensor))

    model.allow_nonexistent_arrays = allow_nonexistent_arrays

    if saved_model_dir:
        model.saved_model_dir = saved_model_dir
    model.saved_model_version = saved_model_version
    if saved_model_tags:
        model.saved_model_tags.extend(saved_model_tags)
    if saved_model_exported_names:
        model.saved_model_exported_names.extend(saved_model_exported_names)

    return model, toco, debug_info
예제 #7
0
def build_toco_convert_protos(input_tensors,
                              output_tensors,
                              inference_type=lite_constants.FLOAT,
                              inference_input_type=None,
                              input_format=lite_constants.TENSORFLOW_GRAPHDEF,
                              input_shapes=None,
                              output_format=lite_constants.TFLITE,
                              quantized_input_stats=None,
                              default_ranges_stats=None,
                              drop_control_dependency=True,
                              reorder_across_fake_quant=False,
                              allow_custom_ops=False,
                              custom_opdefs=None,
                              change_concat_input_ranges=False,
                              post_training_quantize=False,
                              quantize_to_float16=False,
                              dump_graphviz_dir=None,
                              dump_graphviz_video=False,
                              target_ops=None,
                              allow_nonexistent_arrays=False,
                              debug_info=None,
                              conversion_summary_dir=None,
                              saved_model_dir=None,
                              saved_model_version=0,
                              saved_model_tags=None,
                              saved_model_exported_names=None):
    """Builds protocol buffers describing a conversion of a model using TOCO.

  Typically this is to convert from TensorFlow GraphDef to TFLite, in which
  case the default `input_format` and `output_format` are sufficient.

  Args:
    input_tensors: List of input tensors. Type and shape are computed using
      `foo.shape` and `foo.dtype`.
    output_tensors: List of output tensors (only .name is used from this).
    inference_type: Target data type of real-number arrays in the output file.
      Must be `{tf.float32, tf.uint8, tf.int8}`.  (default tf.float32)
    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
      `{tf.float32, tf.uint8, tf.int8}`. (default `inference_type`)
    input_format: Type of data to read Currently must be
      `{TENSORFLOW_GRAPHDEF}`. (default TENSORFLOW_GRAPHDEF)
    input_shapes: Input array shape. It needs to be a list of the same length as
      `input_tensors`, or None. (default None)
    output_format: Output file format. Currently must be `{TFLITE,
      GRAPHVIZ_DOT}`. (default TFLITE)
    quantized_input_stats: List of tuples of floats representing the mean and
      standard deviation. Each tuple maps to the corresponding input tensor.
      Only need if `inference_input_type` is `QUANTIZED_UINT8` or `INT8`.
      real_input_value = (quantized_input_value - mean_value) / std_dev_value.
      (default None)
    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)
    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)
    custom_opdefs: List of strings representing custom ops OpDefs that are
      included in the GraphDef. Required when using custom operations with the
      MLIR-based converter. (default None)
    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)
    post_training_quantize: Boolean indicating whether to quantize the weights
      of the converted float model. Model size will be reduced and there will be
      latency improvements (at the cost of accuracy). (default False)
    quantize_to_float16: Boolean indicating whether to convert float buffers to
      float16. (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)
    target_ops: Experimental flag, subject to change. Set of OpsSet options
      indicating which converter to use. (default set([OpsSet.TFLITE_BUILTINS]))
    allow_nonexistent_arrays: Allow specifying array names that don't exist or
      are unused in the final graph. (default False)
    debug_info: `GraphDebugInfo` proto containing the stack traces for the
      original nodes referred by the converted graph.
    conversion_summary_dir: A string, the path to the generated conversion logs.
    saved_model_dir: Filepath of the saved model to be converted. This value
      will be non-empty only when the saved model import path will be used.
      Otherwises, the graph def-based conversion will be processed.
    saved_model_version: SavedModel file format version of The saved model file
      to be converted. This value will be set only when the SavedModel import
      path will be used.
    saved_model_tags: Set of string saved model tags, formatted in the
      comma-separated value. This value will be set only when the SavedModel
      import path will be used.
    saved_model_exported_names: Names to be exported (default: export all) when
      the saved model import path is on. This value will be set only when the
      SavedModel import path will be used.

  Returns:
    model_flags, toco_flags, debug_info: three protocol buffers describing the
      conversion process and debug information.

  Raises:
    ValueError:
      If the input tensor type is unknown
      Missing mean_values or std_dev_values
    RuntimeError: If TOCO fails to convert (in which case the runtime error's
      error text will contain the TOCO error log)
  """
    toco = _toco_flags_pb2.TocoFlags()
    toco.input_format = input_format
    toco.output_format = output_format
    toco.inference_type = util.convert_dtype_to_tflite_type(inference_type)
    if inference_input_type:
        toco.inference_input_type = util.convert_dtype_to_tflite_type(
            inference_input_type)
    else:
        toco.inference_input_type = toco.inference_type
    toco.drop_control_dependency = drop_control_dependency
    toco.reorder_across_fake_quant = reorder_across_fake_quant
    toco.allow_custom_ops = allow_custom_ops
    if custom_opdefs:
        toco.custom_opdefs.extend(custom_opdefs)
    toco.post_training_quantize = post_training_quantize
    toco.quantize_to_float16 = quantize_to_float16
    if default_ranges_stats:
        toco.default_ranges_min = default_ranges_stats[0]
        toco.default_ranges_max = default_ranges_stats[1]
    if dump_graphviz_dir:
        toco.dump_graphviz_dir = dump_graphviz_dir
    toco.dump_graphviz_include_video = dump_graphviz_video
    if conversion_summary_dir:
        toco.conversion_summary_dir = conversion_summary_dir
    if target_ops:
        if set(target_ops) == set(
            [OpsSet.TFLITE_BUILTINS, OpsSet.SELECT_TF_OPS]):
            toco.enable_select_tf_ops = True
        elif set(target_ops) == set([OpsSet.SELECT_TF_OPS]):
            toco.enable_select_tf_ops = True
            toco.force_select_tf_ops = True

    model = _model_flags_pb2.ModelFlags()
    model.change_concat_input_ranges = change_concat_input_ranges
    for idx, input_tensor in enumerate(input_tensors):
        input_array = model.input_arrays.add()
        input_array.name = util.get_tensor_name(input_tensor)
        input_array.data_type = util.convert_dtype_to_tflite_type(
            input_tensor.dtype)

        if _requires_input_stats(toco) and quantized_input_stats:
            input_array.mean_value, input_array.std_value = quantized_input_stats[
                idx]

        if input_shapes is None:
            shape = input_tensor.shape
        else:
            shape = input_shapes[idx]

        # Create shapes with -1 for unknown dimensions.
        dims = []
        for dim in shape:
            if (dim is None or (isinstance(dim, tensor_shape.Dimension)
                                and dim.value is None)):
                dims.append(-1)
            else:
                dims.append(int(dim))
        input_array.shape.dims.extend(dims)

    for output_tensor in output_tensors:
        model.output_arrays.append(util.get_tensor_name(output_tensor))

    model.allow_nonexistent_arrays = allow_nonexistent_arrays

    if saved_model_dir:
        model.saved_model_dir = saved_model_dir
    model.saved_model_version = saved_model_version
    if saved_model_tags:
        model.saved_model_tags.extend(saved_model_tags)
    if saved_model_exported_names:
        model.saved_model_exported_names.extend(saved_model_exported_names)

    return model, toco, debug_info
예제 #8
0
def build_toco_convert_protos(input_tensors,
                              output_tensors,
                              inference_type=lite_constants.FLOAT,
                              inference_input_type=None,
                              input_format=lite_constants.TENSORFLOW_GRAPHDEF,
                              input_shapes=None,
                              output_format=lite_constants.TFLITE,
                              quantized_input_stats=None,
                              default_ranges_stats=None,
                              drop_control_dependency=True,
                              reorder_across_fake_quant=False,
                              allow_custom_ops=False,
                              change_concat_input_ranges=False,
                              post_training_quantize=False,
                              dump_graphviz_dir=None,
                              dump_graphviz_video=False,
                              target_ops=None,
                              allow_nonexistent_arrays=False):
  """Builds protocol buffers describing a conversion of a model using TOCO.

  Typically this is to convert from TensorFlow GraphDef to TFLite, in which
  case the default `input_format` and `output_format` are sufficient.

  Args:
    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).
    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`)
    input_format: Type of data to read Currently must be
      `{TENSORFLOW_GRAPHDEF}`. (default TENSORFLOW_GRAPHDEF)
    input_shapes: Input array shape. It needs to be a list of the same length
      as `input_tensors`, or None. (default None)
    output_format: Output file format. Currently must be `{TFLITE,
      GRAPHVIZ_DOT}`. (default TFLITE)
    quantized_input_stats: List of tuples of floats representing the mean and
      standard deviation. Each tuple maps to the corresponding input tensor.
      Only need if `inference_input_type` is `QUANTIZED_UINT8`.
      real_input_value = (quantized_input_value - mean_value) / std_dev_value.
      (default None)
    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)
    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)
    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)
    post_training_quantize: Boolean indicating whether to quantize the weights
      of the converted float model. Model size will be reduced and there will be
      latency improvements (at the cost of accuracy).
      (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)
    target_ops: Experimental flag, subject to change. Set of OpsSet
      options indicating which converter to use.
      (default set([OpsSet.TFLITE_BUILTINS]))
    allow_nonexistent_arrays: Allow specifying array names that don't exist
      or are unused in the final graph. (default False)

  Returns:
    model_flags, toco_flags: two protocol buffers describing the conversion
    process.

  Raises:
    ValueError: If the input tensor type is unknown
    RuntimeError: If TOCO fails to convert (in which case the runtime error's
      error text will contain the TOCO error log)
  """
  toco = _toco_flags_pb2.TocoFlags()
  toco.input_format = input_format
  toco.output_format = output_format
  toco.inference_type = inference_type
  if inference_input_type:
    toco.inference_input_type = inference_input_type
  else:
    toco.inference_input_type = toco.inference_type
  toco.drop_control_dependency = drop_control_dependency
  toco.reorder_across_fake_quant = reorder_across_fake_quant
  toco.allow_custom_ops = allow_custom_ops
  toco.post_training_quantize = post_training_quantize
  if default_ranges_stats:
    toco.default_ranges_min = default_ranges_stats[0]
    toco.default_ranges_max = default_ranges_stats[1]
  if dump_graphviz_dir:
    toco.dump_graphviz_dir = dump_graphviz_dir
  toco.dump_graphviz_include_video = dump_graphviz_video
  if target_ops:
    if set(target_ops) == set([OpsSet.TFLITE_BUILTINS, OpsSet.SELECT_TF_OPS]):
      toco.allow_flex_ops = True
    elif set(target_ops) == set([OpsSet.SELECT_TF_OPS]):
      toco.allow_flex_ops = True
      toco.force_flex_ops = True

  model = _model_flags_pb2.ModelFlags()
  model.change_concat_input_ranges = change_concat_input_ranges
  for idx, input_tensor in enumerate(input_tensors):
    input_array = model.input_arrays.add()
    if toco.inference_input_type == lite_constants.QUANTIZED_UINT8:
      input_array.mean_value, input_array.std_value = quantized_input_stats[idx]
    input_array.name = tensor_name(input_tensor)
    if input_shapes is None:
      shape = input_tensor.get_shape()
    else:
      shape = input_shapes[idx]
    input_array.shape.dims.extend(map(int, shape))

  for output_tensor in output_tensors:
    model.output_arrays.append(tensor_name(output_tensor))

  model.allow_nonexistent_arrays = allow_nonexistent_arrays

  return model, toco