Creates a TFLiteConverter 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_s
(cls,
graph_def_file,
input_arrays,
output_arrays,
input_shapes=None)
| 634 | |
| 635 | @classmethod |
| 636 | def from_frozen_graph(cls, |
| 637 | graph_def_file, |
| 638 | input_arrays, |
| 639 | output_arrays, |
| 640 | input_shapes=None): |
| 641 | """Creates a TFLiteConverter class from a file containing a frozen GraphDef. |
| 642 | |
| 643 | Args: |
| 644 | graph_def_file: Full filepath of file containing frozen GraphDef. |
| 645 | input_arrays: List of input tensors to freeze graph with. |
| 646 | output_arrays: List of output tensors to freeze graph with. |
| 647 | input_shapes: Dict of strings representing input tensor names to list of |
| 648 | integers representing input shapes (e.g., {"foo" : [1, 16, 16, 3]}). |
| 649 | Automatically determined when input shapes is None (e.g., {"foo" : |
| 650 | None}). (default None) |
| 651 | |
| 652 | Returns: |
| 653 | TFLiteConverter class. |
| 654 | |
| 655 | Raises: |
| 656 | IOError: |
| 657 | File not found. |
| 658 | Unable to parse input file. |
| 659 | ValueError: |
| 660 | The graph is not frozen. |
| 661 | input_arrays or output_arrays contains an invalid tensor name. |
| 662 | input_shapes is not correctly defined when required |
| 663 | """ |
| 664 | with _ops.Graph().as_default(): |
| 665 | with _session.Session() as sess: |
| 666 | # Read GraphDef from file. |
| 667 | if not _file_io.file_exists(graph_def_file): |
| 668 | raise IOError("File '{0}' does not exist.".format(graph_def_file)) |
| 669 | with _file_io.FileIO(graph_def_file, "rb") as f: |
| 670 | file_content = f.read() |
| 671 | |
| 672 | try: |
| 673 | graph_def = _graph_pb2.GraphDef() |
| 674 | graph_def.ParseFromString(file_content) |
| 675 | except (_text_format.ParseError, DecodeError): |
| 676 | try: |
| 677 | print("Ignore 'tcmalloc: large alloc' warnings.") |
| 678 | |
| 679 | if not isinstance(file_content, str): |
| 680 | if PY3: |
| 681 | file_content = file_content.decode("utf-8") |
| 682 | else: |
| 683 | file_content = file_content.encode("utf-8") |
| 684 | graph_def = _graph_pb2.GraphDef() |
| 685 | _text_format.Merge(file_content, graph_def) |
| 686 | except (_text_format.ParseError, DecodeError): |
| 687 | raise IOError( |
| 688 | "Unable to parse input file '{}'.".format(graph_def_file)) |
| 689 | |
| 690 | # Handles models with custom TFLite ops that cannot be resolved in |
| 691 | # TensorFlow. |
| 692 | load_model_in_session = True |
| 693 | try: |