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

Method convert

tensorflow/lite/python/lite.py:386–452  ·  view source on GitHub ↗

Converts a TensorFlow GraphDef based on instance variables. Returns: The converted data in serialized format. Raises: ValueError: Multiple concrete functions are specified. Input shape is not specified. Invalid quantization parameters.

(self)

Source from the content-addressed store, hash-verified

384 return cls([concrete_func])
385
386 def convert(self):
387 """Converts a TensorFlow GraphDef based on instance variables.
388
389 Returns:
390 The converted data in serialized format.
391
392 Raises:
393 ValueError:
394 Multiple concrete functions are specified.
395 Input shape is not specified.
396 Invalid quantization parameters.
397 """
398 # TODO(b/130297984): Add support for converting multiple function.
399 if len(self._funcs) != 1:
400 raise ValueError("This converter can only convert a single "
401 "ConcreteFunction. Converting multiple functions is "
402 "under development.")
403
404 frozen_func = _convert_to_constants.convert_variables_to_constants_v2(
405 self._funcs[0], lower_control_flow=False)
406 input_tensors = [
407 tensor for tensor in frozen_func.inputs
408 if tensor.dtype != _dtypes.resource
409 ]
410 output_tensors = frozen_func.outputs
411
412 # Run a Grappler pass.
413 graph_def = frozen_func.graph.as_graph_def()
414 graph_def = _run_graph_optimizations(
415 graph_def,
416 input_tensors,
417 output_tensors,
418 config=self._grappler_config(),
419 graph=frozen_func.graph)
420
421 # Checks dimensions in input tensor.
422 for tensor in input_tensors:
423 # Note that shape_list might be empty for scalar shapes.
424 shape_list = tensor.shape.as_list()
425 if None in shape_list[1:]:
426 raise ValueError(
427 "None is only supported in the 1st dimension. Tensor '{0}' has "
428 "invalid shape '{1}'.".format(_get_tensor_name(tensor), shape_list))
429 elif shape_list and shape_list[0] is None:
430 # Set the batch size to 1 if undefined.
431 shape = tensor.shape.as_list()
432 shape[0] = 1
433 tensor.set_shape(shape)
434
435 self._validate_quantization()
436 self._validate_representative_dataset()
437 self._debug_info = _get_debug_info(
438 _build_debug_info_func(self._funcs[0].graph), graph_def)
439 converter_kwargs = self._get_base_converter_args()
440
441 # Converts model.
442 result = _toco_convert_impl(
443 input_data=graph_def,

Callers

nothing calls this directly

Calls 11

as_graph_defMethod · 0.80
_grappler_configMethod · 0.80
_get_tensor_nameFunction · 0.50
as_listMethod · 0.45
formatMethod · 0.45
set_shapeMethod · 0.45

Tested by

no test coverage detected