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

Function toco_convert

tensorflow/lite/testing/toco_convert.py:77–170  ·  view source on GitHub ↗

Convert a model's graph def into a tflite model. NOTE: this currently shells out to the toco binary, but we would like convert to Python API tooling in the future. Args: options: An Options instance. graph_def: A GraphDef object. input_tensors: List of input tensor tuples `(name,

(options, graph_def, input_tensors, output_tensors, **kwargs)

Source from the content-addressed store, hash-verified

75
76
77def toco_convert(options, graph_def, input_tensors, output_tensors, **kwargs):
78 """Convert a model's graph def into a tflite model.
79
80 NOTE: this currently shells out to the toco binary, but we would like
81 convert to Python API tooling in the future.
82
83 Args:
84 options: An Options instance.
85 graph_def: A GraphDef object.
86 input_tensors: List of input tensor tuples `(name, shape, type)`.
87 output_tensors: List of output tensors (names).
88 **kwargs: Extra options to be passed.
89
90 Returns:
91 output tflite model, log_txt from conversion
92 or None, log_txt if it did not convert properly.
93 """
94 # Convert ophint ops if presented.
95 graph_def = tf.lite.experimental.convert_op_hints_to_stubs(
96 graph_def=graph_def)
97 graph_def_str = graph_def.SerializeToString()
98
99 extra_toco_options = kwargs.get(
100 "extra_toco_options", generate_examples_lib.ExtraTocoOptions())
101 test_params = kwargs.get("test_params", {})
102 input_arrays = [x[0] for x in input_tensors]
103 data_types = [
104 generate_examples_lib.TF_TYPE_INFO[x[2]][1] for x in input_tensors]
105
106 if test_params.get("fully_quantize", False):
107 with tempfile.NamedTemporaryFile() as graphdef_file:
108 graphdef_file.write(graph_def_str)
109 graphdef_file.flush()
110
111 input_shapes = generate_examples_lib.get_input_shapes_map(input_tensors)
112 converter = tf.lite.TocoConverter.from_frozen_graph(
113 graphdef_file.name, input_arrays, output_tensors, input_shapes)
114
115 def representative_dataset(input_tensors):
116 calibration_inputs = []
117 for _, shape, _ in input_tensors:
118 if shape:
119 dims = [dim.value for dim in shape.dims]
120 calibration_inputs.append(
121 np.random.uniform(-1, 1, tuple(dims)).astype(np.float32))
122 return calibration_inputs
123
124 def representative_dataset_gen():
125 for _ in range(100):
126 yield representative_dataset(input_tensors)
127
128 converter.target_spec.supported_ops = [
129 tf.lite.OpsSet.TFLITE_BUILTINS_INT8
130 ]
131 converter.representative_dataset = representative_dataset_gen
132 if extra_toco_options.inference_input_type:
133 converter.inference_input_type = (
134 extra_toco_options.inference_input_type)

Callers

nothing calls this directly

Calls 9

toco_optionsFunction · 0.85
SerializeToStringMethod · 0.45
getMethod · 0.45
writeMethod · 0.45
flushMethod · 0.45
from_frozen_graphMethod · 0.45
convertMethod · 0.45
formatMethod · 0.45
readMethod · 0.45

Tested by

no test coverage detected