2017-08-03 19 views
7

मैं इस स्क्रिप्ट का उपयोग करने के लिए .pb (Protobuf) स्वरूप एक पूर्व प्रशिक्षित .ckpt मॉडल में परिवर्तित करने में कामयाब रहे:रूपांतरण .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 .. ") 

अब, मैं क्या चाहते है उपाध्यक्ष प्रक्रिया। मैं protobuf फ़ाइल कैसे लोड कर सकता हूं और इसे .ckpt (चेकपॉइंट) प्रारूप में परिवर्तित कर सकता हूं?

मैं ऐसा करने के लिए निम्न स्क्रिप्ट के साथ कोशिश कर रहा हूँ लेकिन यह हमेशा विफल रहता है:

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") 

मुझे विश्वास है कि यह इन रूपांतरण स्क्रिप्ट के लिए बहुत मददगार होगा।

पीएस: वजन पहले ही .pb फ़ाइल में एम्बेड किए गए हैं।

धन्यवाद।

उत्तर

0

ऐसा लगता है कि आपको केवल दोनों फाइलों में ग्राफ परिभाषा मिली है, न कि जमे हुए मॉडल।

# 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) 

जमे हुए ग्राफ, आप PB फ़ाइल में एक मॉडल लोड तो freeze_graph file

+0

मॉडल बहाल (सेक्स, tf.train.latest_checkpoint (dir_path)) जहां चौकी (वजन के साथ) है से भरी हुई है। –

+0

अच्छा! दूसरी लिपि में आपने कोई मॉडल लोड नहीं किया है, आपने अभी ग्राफ आयात किया है। घटना है कि आप पहली स्क्रिप्ट में मॉडल लोड करते हैं, यह पीबी फ़ाइल में चर नहीं लिखता है। –

+0

ठीक है मैंने tess.train.write_graph फ़ंक्शन में sess.graph में sess.graph_def को बदल दिया लेकिन एक ही भाग्य। –

0

का उपयोग कर प्राप्त यह एक pretrain मॉडल के रूप में retrained जा सकता है करता है। मैंने "tf.global_variables()" को वेरिएबल्स प्राप्त करने के लिए इस्तेमाल किया जिन्हें प्रशिक्षित किया जा सकता है, लेकिन जब मैं एक पीबी मॉडल लोड करता हूं तो कोई भी वर्जन वापस नहीं होता है।

tf.global_variables()