def from_tensorflow_frozen_model(frozen_file, output_nodes=[], preprocessor=None, **kwargs): """ Converts a TensorFlow frozen graph to a UFF model. Args: frozen_file (str): The path to the frozen TensorFlow graph to convert. output_nodes (list(str)): The names of the outputs of the graph. If not provided, graphsurgeon is used to automatically deduce output nodes. output_filename (str): The UFF file to write. preprocessor (str): The path to a preprocessing script that will be executed before the converter. This script should define a ``preprocess`` function which accepts a graphsurgeon DynamicGraph and modifies it in place. write_preprocessed (bool): If set to True, the converter will write out the preprocessed graph as well as a TensorBoard visualization. Must be used in conjunction with output_filename. text (bool): If set to True, the converter will also write out a human readable UFF file. Must be used in conjunction with output_filename. quiet (bool): If set to True, suppresses informational messages. Errors may still be printed. list_nodes (bool): If set to True, the converter displays a list of all nodes present in the graph. debug_mode (bool): If set to True, the converter prints verbose debug messages. return_graph_info (bool): If set to True, this function returns the graph input and output nodes in addition to the serialized UFF graph. Returns: serialized UFF MetaGraph (str) OR, if return_graph_info is set to True, serialized UFF MetaGraph (str), graph inputs (list(tensorflow.NodeDef)), graph outputs (list(tensorflow.NodeDef)) """ graphdef = GraphDef() with tf.gfile.GFile(frozen_file, "rb") as frozen_pb: graphdef.ParseFromString(frozen_pb.read()) return from_tensorflow(graphdef, output_nodes, preprocessor, **kwargs)
def __post_init__(self, model_path: Path): self.graph = tf.Graph() with self.graph.as_default(): od_graph_def = GraphDef() with GFile(str(model_path / "frozen_inference_graph.pb"), "rb") as fid: serialized_graph = fid.read() od_graph_def.ParseFromString(serialized_graph) tf.import_graph_def(od_graph_def, name="")
def load_frozen_graph(self, model_path): ''' Function to load the frozen protobuf file from the disk and parse it to retrieve the unserialized graph_def Arguments - model_path : A string having the path of the tensorflow model(.pb). Returns - detection_graph : The unserialized graph_def that holds the network architecture. ''' detection_graph = Graph() with detection_graph.as_default(): od_graph_def = GraphDef() with gfile.GFile(model_path, 'rb') as fid: serialized_graph = fid.read() od_graph_def.ParseFromString(serialized_graph) import_graph_def(od_graph_def, name='') return detection_graph
def optimize_frozen_graph(logdir: str, frozen_graph: str): with FastGFile(path_join(logdir, frozen_graph), mode='rb') as frozen_graph_file: frozen_graph_def = GraphDef() frozen_graph_def.ParseFromString(frozen_graph_file.read()) optimized_frozen_graph_def = optimize_for_inference( frozen_graph_def, ["Reshape"], ["softmax"], []) optimized_frozen_graph_as_bytes = optimized_frozen_graph_def.SerializeToString( ) optimized_frozen_graph = frozen_graph.replace("_frozen", "_frozen_optimized") optimized_frozen_graph_path = path_join(logdir, optimized_frozen_graph) with open(optimized_frozen_graph_path, mode='wb') as optimized_frozen_graph_file: optimized_frozen_graph_file.write(optimized_frozen_graph_as_bytes) return optimized_frozen_graph_path
def Model_from_Kinetica(self, Model_ID): from tensorflow import GraphDef, Graph, import_graph_def h_db = self.h_db response = h_db.get_records( table_name='TFmodel', encoding="binary", options={'expression': "model_id=\"" + Model_ID + "\""}) records = gpudb.GPUdbRecord.decode_binary_data( response["type_schema"], response["records_binary"]) record = records[0] record["model_binary"] graph_def = GraphDef() graph_def.ParseFromString(record["model_binary"]) graph = Graph() with graph.as_default(): # The name var will prefix every op/nodes in your graph # Since we load everything in a new graph, this is not needed import_graph_def(graph_def) return graph
def main(args): with GFile(args.frozen_model_path, "rb") as f: graph_def = GraphDef() graph_def.ParseFromString(f.read()) with Session() as sess: # Then, we import the graph_def into a new Graph and returns it with Graph().as_default() as graph: import_graph_def(graph_def, name='') signature = predict_signature_def( inputs={'image_batch': graph.get_tensor_by_name('image_batch:0'), 'phase_train': graph.get_tensor_by_name('phase_train:0')}, outputs={'embeddings': graph.get_tensor_by_name('embeddings:0')} ) builder = saved_model_builder.SavedModelBuilder(args.output_model_dir) builder.add_meta_graph_and_variables( sess=sess, tags=[tag_constants.SERVING], signature_def_map={'serving_default': signature} ) builder.save()
default=None, help='Compiler output node names') args = dict2attr(parser.parse_args()) if not args.pre_process: raise ValueError( 'please provide --pre_process input. Valid option: resnet50, inception_v1, inception_v3, inception_v4, squeezenet' ) if args.quantize: from tensorflow import GraphDef from tensorflow.contrib import decent_q input_graph_def = GraphDef() with open(args.model, "rb") as f: input_graph_def.ParseFromString(f.read()) if os.path.isdir(args.output_dir): print('Cleaning model artifacts in {}'.format( os.path.abspath(args.output_dir))) filesToClean = [ os.path.join(os.path.abspath(args.output_dir), f) for f in os.listdir(args.output_dir) ] for f in filesToClean: if os.path.isfile(f): os.remove(f) elif os.path.isdir(f): rmtree(f) else: os.makedirs(args.output_dir)