def evaluate_frozen_graph(filename, input_arrays, output_arrays): """Returns a function that evaluates the frozen graph on input data. Args: filename: 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. Returns: Lambda function ([np.ndarray data] : [np.ndarray result]). """ with _session.Session().as_default() as sess: with _file_io.FileIO(filename, "rb") as f: file_content = f.read() graph_def = _graph_pb2.GraphDef() graph_def.ParseFromString(file_content) _import_graph_def(graph_def, name="") inputs = _convert_saved_model.get_tensors_from_tensor_names( sess.graph, input_arrays) outputs = _convert_saved_model.get_tensors_from_tensor_names( sess.graph, output_arrays) return lambda input_data: sess.run(outputs, dict(zip(inputs, input_data)))
def evaluate_frozen_graph(filename, input_arrays, output_arrays): """Returns a function that evaluates the frozen graph on input data. Args: filename: 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. Returns: Lambda function ([np.ndarray data] : [np.ndarray result]). """ with _session.Session().as_default() as sess: with _file_io.FileIO(filename, "rb") as f: file_content = f.read() graph_def = _graph_pb2.GraphDef() graph_def.ParseFromString(file_content) _import_graph_def(graph_def, name="") inputs = _convert_saved_model.get_tensors_from_tensor_names( sess.graph, input_arrays) outputs = _convert_saved_model.get_tensors_from_tensor_names( sess.graph, output_arrays) return lambda input_data: sess.run(outputs, dict(zip(inputs, input_data)))
def testGetTensorsInvalid(self): in_tensor = array_ops.placeholder( shape=[1, 16, 16, 3], dtype=dtypes.float32) _ = in_tensor + in_tensor sess = session.Session() with self.assertRaises(ValueError) as error: convert_saved_model.get_tensors_from_tensor_names(sess.graph, ["invalid-input"]) self.assertEqual("Invalid tensors 'invalid-input' were found.", str(error.exception))
def testGetTensorsInvalid(self): in_tensor = array_ops.placeholder(shape=[1, 16, 16, 3], dtype=dtypes.float32) _ = in_tensor + in_tensor sess = session.Session() with self.assertRaises(ValueError) as error: convert_saved_model.get_tensors_from_tensor_names( sess.graph, ["invalid-input"]) self.assertEqual("Invalid tensors 'invalid-input' were found.", str(error.exception))
def testGetTensorsValid(self): in_tensor = array_ops.placeholder( shape=[1, 16, 16, 3], dtype=dtypes.float32) _ = in_tensor + in_tensor sess = session.Session() tensors = convert_saved_model.get_tensors_from_tensor_names( sess.graph, ["Placeholder"]) self.assertEqual("Placeholder:0", tensors[0].name)
def testGetTensorsValid(self): in_tensor = array_ops.placeholder(shape=[1, 16, 16, 3], dtype=dtypes.float32) _ = in_tensor + in_tensor sess = session.Session() tensors = convert_saved_model.get_tensors_from_tensor_names( sess.graph, ["Placeholder"]) self.assertEqual("Placeholder:0", tensors[0].name)
def from_keras_model_file(cls, model_file, input_arrays=None, input_shapes=None, output_arrays=None): """Creates a TocoConverter class from a tf.keras model file. Args: model_file: Full filepath of HDF5 file containing the tf.keras model. input_arrays: List of input tensors to freeze graph with. Uses input arrays from SignatureDef when none are provided. (default None) input_shapes: Dict of strings representing input tensor names to list of integers representing input shapes (e.g., {"foo" : [1, 16, 16, 3]}). Automatically determined when input shapes is None (e.g., {"foo" : None}). (default None) output_arrays: List of output tensors to freeze graph with. Uses output arrays from SignatureDef when none are provided. (default None) Returns: TocoConverter class. """ _keras.backend.clear_session() _keras.backend.set_learning_phase(False) keras_model = _keras.models.load_model(model_file) sess = _keras.backend.get_session() # Get input and output tensors. if input_arrays: input_tensors = get_tensors_from_tensor_names( sess.graph, input_arrays) else: input_tensors = keras_model.inputs if output_arrays: output_tensors = get_tensors_from_tensor_names( sess.graph, output_arrays) else: output_tensors = keras_model.outputs set_tensor_shapes(input_tensors, input_shapes) graph_def = _freeze_graph(sess, output_tensors) return cls(graph_def, input_tensors, output_tensors)
def from_keras_model_file(cls, model_file, input_arrays=None, input_shapes=None, output_arrays=None): """Creates a TocoConverter class from a tf.keras model file. Args: model_file: Full filepath of HDF5 file containing the tf.keras model. input_arrays: List of input tensors to freeze graph with. Uses input arrays from SignatureDef when none are provided. (default None) input_shapes: Dict of strings representing input tensor names to list of integers representing input shapes (e.g., {"foo" : [1, 16, 16, 3]}). Automatically determined when input shapes is None (e.g., {"foo" : None}). (default None) output_arrays: List of output tensors to freeze graph with. Uses output arrays from SignatureDef when none are provided. (default None) Returns: TocoConverter class. """ _keras.backend.clear_session() _keras.backend.set_learning_phase(False) keras_model = _keras.models.load_model(model_file) sess = _keras.backend.get_session() # Get input and output tensors. if input_arrays: input_tensors = get_tensors_from_tensor_names(sess.graph, input_arrays) else: input_tensors = keras_model.inputs if output_arrays: output_tensors = get_tensors_from_tensor_names(sess.graph, output_arrays) else: output_tensors = keras_model.outputs set_tensor_shapes(input_tensors, input_shapes) graph_def = _freeze_graph(sess, output_tensors) return cls(graph_def, input_tensors, output_tensors)
def from_frozen_graph(cls, graph_def_file, input_arrays, output_arrays, input_shapes=None): """Creates a TocoConverter class from a file containing a frozen GraphDef. Args: graph_def_file: Full filepath of file containing TensorFlow GraphDef. input_arrays: List of input tensors to freeze graph with. output_arrays: List of output tensors to freeze graph with. input_shapes: Dict of strings representing input tensor names to list of integers representing input shapes (e.g., {"foo" : [1, 16, 16, 3]}). Automatically determined when input shapes is None (e.g., {"foo" : None}). (default None) Returns: TocoConverter class. Raises: ValueError: Unable to parse input file. The graph is not frozen. input_arrays or output_arrays contains an invalid tensor name. """ with _session.Session() as sess: sess.run(global_variables_initializer()) # Read GraphDef from file. graph_def = _graph_pb2.GraphDef() with open(graph_def_file, "rb") as f: file_content = f.read() try: graph_def.ParseFromString(file_content) except (_text_format.ParseError, DecodeError): try: print("Ignore 'tcmalloc: large alloc' warnings.") if not isinstance(file_content, str): if PY3: file_content = file_content.decode('utf-8') else: file_content = file_content.encode('utf-8') _text_format.Merge(file_content, graph_def) except (_text_format.ParseError, DecodeError): raise ValueError("Unable to parse input file '{}'.".format( graph_def_file)) sess.graph.as_default() import_graph_def(graph_def, name="") # Get input and output tensors. input_tensors = get_tensors_from_tensor_names( sess.graph, input_arrays) output_tensors = get_tensors_from_tensor_names( sess.graph, output_arrays) set_tensor_shapes(input_tensors, input_shapes) # Check if graph is frozen. if not _is_frozen_graph(sess): raise ValueError( "Please freeze the graph using freeze_graph.py") # Create TocoConverter class. return cls(sess.graph_def, input_tensors, output_tensors)
def from_frozen_graph(cls, graph_def_file, input_arrays, output_arrays, input_shapes=None): """Creates a TocoConverter class from a file containing a frozen GraphDef. Args: graph_def_file: Full filepath of file containing TensorFlow GraphDef. input_arrays: List of input tensors to freeze graph with. output_arrays: List of output tensors to freeze graph with. input_shapes: Dict of strings representing input tensor names to list of integers representing input shapes (e.g., {"foo" : [1, 16, 16, 3]}). Automatically determined when input shapes is None (e.g., {"foo" : None}). (default None) Returns: TocoConverter class. Raises: ValueError: Unable to parse input file. The graph is not frozen. input_arrays or output_arrays contains an invalid tensor name. """ with _session.Session() as sess: sess.run(global_variables_initializer()) # Read GraphDef from file. graph_def = _graph_pb2.GraphDef() with open(graph_def_file, "rb") as f: file_content = f.read() try: graph_def.ParseFromString(file_content) except (_text_format.ParseError, DecodeError): try: print("Ignore 'tcmalloc: large alloc' warnings.") if not isinstance(file_content, str): if PY3: file_content = file_content.decode('utf-8') else: file_content = file_content.encode('utf-8') _text_format.Merge(file_content, graph_def) except (_text_format.ParseError, DecodeError): raise ValueError( "Unable to parse input file '{}'.".format(graph_def_file)) sess.graph.as_default() import_graph_def(graph_def, name="") # Get input and output tensors. input_tensors = get_tensors_from_tensor_names(sess.graph, input_arrays) output_tensors = get_tensors_from_tensor_names(sess.graph, output_arrays) set_tensor_shapes(input_tensors, input_shapes) # Check if graph is frozen. if not _is_frozen_graph(sess): raise ValueError("Please freeze the graph using freeze_graph.py.") # Create TocoConverter class. return cls(sess.graph_def, input_tensors, output_tensors)