2017-08-03 20 views
7

tôi đã quản lý để chuyển đổi một mô hình pre-đào tạo .ckpt để .pb định dạng (protobuf) sử dụng kịch bản này:Chuyển đổi tập tin .pb để .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 .. ") 

Bây giờ, những gì tôi muốn là thủ tục phó-verca. Làm thế nào tôi có thể tải các tập tin protobuf và chuyển nó sang định dạng .ckpt (checkpoint)?

Tôi cố gắng để làm điều đó với kịch bản sau đây nhưng nó luôn luôn thất bại:

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

Tôi tin rằng nó sẽ rất hữu ích để có những kịch bản chuyển đổi.

P.S: Các trọng số đã được nhúng vào tệp .pb.

Cảm ơn.

Trả lời

0

có vẻ như bạn chỉ có định nghĩa biểu đồ trong cả hai tệp, chứ không phải mô hình được cố định.

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

đông lạnh đồ thị thu được bằng cách sử dụng freeze_graph file

+0

Mô hình được tải từ khôi phục (sess, tf.train.latest_checkpoint (dir_path)) trong đó điểm kiểm tra (có trọng số) là. –

+0

Vâng! trong tập lệnh thứ hai bạn không tải bất kỳ mô hình nào, bạn chỉ cần nhập biểu đồ. Mặc dù bạn tải mô hình trong tập lệnh đầu tiên, nó không viết các biến vào tệp pb. –

+0

Ok Tôi đã thay đổi sess.graph_def thành sess.graph trong hàm tf.train.write_graph nhưng cũng may mắn. –

0

Nếu bạn tải một mô hình trong tập tin pb, hiện nó có thể được đào tạo lại như một mô hình pretrain. Tôi đã sử dụng "tf.global_variables()" để nhận các biến có thể được đào tạo, nhưng không có vars nào trả lại khi tôi tải một mô hình pb.

tf.global_variables() 
Các vấn đề liên quan