2017-08-03 4 views
7

ich es geschafft haben, eine vortrainiert .ckpt Modell zu konvertieren zu .pb (protobuf) Format dieses Skript:Umwandlung von .pb Datei .ckpt (tensorflow)

import os 
import tensorflow as tf 

# Get the current directory 
dir_path = os.path.dirname(os.path.realpath(__file__)) 
print "Current directory : ", dir_path 
save_dir = dir_path + '/Protobufs' 

graph = tf.get_default_graph() 

# Create a session for running Ops on the Graph. 
sess = tf.Session() 

print("Restoring the model to the default graph ...") 
saver = tf.train.import_meta_graph(dir_path + '/model.ckpt.meta') 
saver.restore(sess,tf.train.latest_checkpoint(dir_path)) 
print("Restoring Done .. ") 

print "Saving the model to Protobuf format: ", save_dir 

#Save the model to protobuf (pb and pbtxt) file. 
tf.train.write_graph(sess.graph_def, save_dir, "Binary_Protobuf.pb", False) 
tf.train.write_graph(sess.graph_def, save_dir, "Text_Protobuf.pbtxt", True) 
print("Saving Done .. ") 

Nun, was ich will, ist das Vize-Verca-Verfahren. Wie kann ich die protobuf Datei laden und in das .ckpt (Checkpoint) Format konvertieren?

Ich versuche, dass mit dem folgende Skript zu tun, aber es funktioniert nicht immer:

import tensorflow as tf 
import argparse 

# Pass the filename as an argument 
parser = argparse.ArgumentParser() 
parser.add_argument("--frozen_model_filename", default="/path-to-pb-file/Binary_Protobuf.pb", type=str, help="Pb model file to import") 
args = parser.parse_args() 

    # We load the protobuf file from the disk and parse it to retrieve the 
    # unserialized graph_def 
with tf.gfile.GFile(args.frozen_model_filename, "rb") as f: 
    graph_def = tf.GraphDef() 
    graph_def.ParseFromString(f.read()) 

    #saver=tf.train.Saver() 
    with tf.Graph().as_default() as graph: 

     tf.import_graph_def(
      graph_def, 
      input_map=None, 
      return_elements=None, 
      name="prefix", 
      op_dict=None, 
      producer_op_list=None 
     ) 
     sess = tf.Session(graph=graph) 
     saver=tf.train.Saver() 
     save_path = saver.save(sess, "path-to-ckpt/model.ckpt") 
     print("Model saved to chkp format") 

Ich glaube, dass es sehr hilfreich wäre, diese Umwandlung Skripte zu haben.

S.: Die Gewichtungen sind bereits in die .pb-Datei eingebettet.

Danke.

Antwort

0

Es scheint, dass Sie nur die Grafikdefinition in beiden Dateien erhalten haben, nicht das eingefrorene Modell.

# This two lines only save the graph as proto file; it doesn't save the variables and their values. 
tf.train.write_graph(sess.graph_def, save_dir, "Binary_Protobuf.pb", False) 
tf.train.write_graph(sess.graph_def, save_dir, "Text_Protobuf.pbtxt", True) 

gefrorener Graph wird erhalten unter Verwendung von den freeze_graph file

+0

wird das Modell aus wiederherstellen (SESS, tf.train.latest_checkpoint (dir_path)) geladen, in dem der Kontrollpunkt (mit den Gewichten) ist. –

+0

Gut! Im zweiten Skript hast du kein Modell geladen, du hast gerade das Diagramm importiert. Obwohl Sie das Modell im ersten Skript laden, schreibt es die Variablen nicht in die PB-Datei. –

+0

Ok, ich habe sess.graph_def auf sess.graph in tf.train.write_graph Funktion geändert, aber das gleiche Glück. –

0

Wenn Sie ein Modell in pb Datei laden, funktioniert kann es als pretrain Modell umgeschult werden. Ich habe "tf.global_variables()" verwendet, um Variablen zu erhalten, die trainiert werden können, aber es gibt keine vars-Rückgabe, wenn ich ein Pb-Modell lade.

tf.global_variables() 
Verwandte Themen