1# pylint: disable=g-bad-file-header 2# Copyright 2015 The TensorFlow Authors. All Rights Reserved. 3# 4# Licensed under the Apache License, Version 2.0 (the "License"); 5# you may not use this file except in compliance with the License. 6# You may obtain a copy of the License at 7# 8# http://www.apache.org/licenses/LICENSE-2.0 9# 10# Unless required by applicable law or agreed to in writing, software 11# distributed under the License is distributed on an "AS IS" BASIS, 12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13# See the License for the specific language governing permissions and 14# limitations under the License. 15# ============================================================================== 16"""Utilities to remove unneeded nodes from a GraphDefs.""" 17 18import copy 19 20from google.protobuf import text_format 21 22from tensorflow.core.framework import attr_value_pb2 23from tensorflow.core.framework import graph_pb2 24from tensorflow.core.framework import node_def_pb2 25from tensorflow.python.framework import graph_util 26from tensorflow.python.platform import gfile 27 28 29def strip_unused(input_graph_def, input_node_names, output_node_names, 30 placeholder_type_enum): 31 """Removes unused nodes from a GraphDef. 32 33 Args: 34 input_graph_def: A graph with nodes we want to prune. 35 input_node_names: A list of the nodes we use as inputs. 36 output_node_names: A list of the output nodes. 37 placeholder_type_enum: The AttrValue enum for the placeholder data type, or 38 a list that specifies one value per input node name. 39 40 Returns: 41 A `GraphDef` with all unnecessary ops removed. 42 43 Raises: 44 ValueError: If any element in `input_node_names` refers to a tensor instead 45 of an operation. 46 KeyError: If any element in `input_node_names` is not found in the graph. 47 """ 48 for name in input_node_names: 49 if ":" in name: 50 raise ValueError(f"Name '{name}' appears to refer to a Tensor, not an " 51 "Operation.") 52 53 # Here we replace the nodes we're going to override as inputs with 54 # placeholders so that any unused nodes that are inputs to them are 55 # automatically stripped out by extract_sub_graph(). 56 not_found = {name for name in input_node_names} 57 inputs_replaced_graph_def = graph_pb2.GraphDef() 58 for node in input_graph_def.node: 59 if node.name in input_node_names: 60 not_found.remove(node.name) 61 placeholder_node = node_def_pb2.NodeDef() 62 placeholder_node.op = "Placeholder" 63 placeholder_node.name = node.name 64 if isinstance(placeholder_type_enum, list): 65 input_node_index = input_node_names.index(node.name) 66 placeholder_node.attr["dtype"].CopyFrom( 67 attr_value_pb2.AttrValue(type=placeholder_type_enum[ 68 input_node_index])) 69 else: 70 placeholder_node.attr["dtype"].CopyFrom( 71 attr_value_pb2.AttrValue(type=placeholder_type_enum)) 72 if "_output_shapes" in node.attr: 73 placeholder_node.attr["_output_shapes"].CopyFrom(node.attr[ 74 "_output_shapes"]) 75 if "shape" in node.attr: 76 placeholder_node.attr["shape"].CopyFrom(node.attr["shape"]) 77 inputs_replaced_graph_def.node.extend([placeholder_node]) 78 else: 79 inputs_replaced_graph_def.node.extend([copy.deepcopy(node)]) 80 81 if not_found: 82 raise KeyError(f"The following input nodes were not found: {not_found}.") 83 84 output_graph_def = graph_util.extract_sub_graph(inputs_replaced_graph_def, 85 output_node_names) 86 return output_graph_def 87 88 89def strip_unused_from_files(input_graph, input_binary, output_graph, 90 output_binary, input_node_names, output_node_names, 91 placeholder_type_enum): 92 """Removes unused nodes from a graph file.""" 93 94 if not gfile.Exists(input_graph): 95 print("Input graph file '" + input_graph + "' does not exist!") 96 return -1 97 98 if not output_node_names: 99 print("You need to supply the name of a node to --output_node_names.") 100 return -1 101 102 input_graph_def = graph_pb2.GraphDef() 103 mode = "rb" if input_binary else "r" 104 with gfile.GFile(input_graph, mode) as f: 105 if input_binary: 106 input_graph_def.ParseFromString(f.read()) 107 else: 108 text_format.Merge(f.read(), input_graph_def) 109 110 output_graph_def = strip_unused(input_graph_def, 111 input_node_names.split(","), 112 output_node_names.split(","), 113 placeholder_type_enum) 114 115 if output_binary: 116 with gfile.GFile(output_graph, "wb") as f: 117 f.write(output_graph_def.SerializeToString()) 118 else: 119 with gfile.GFile(output_graph, "w") as f: 120 f.write(text_format.MessageToString(output_graph_def)) 121 print("%d ops in the final graph." % len(output_graph_def.node)) 122