2016-01-24 32 views
15

Tôi đang cố khôi phục mô hình TensorFlow. Tôi đi theo ví dụ này: http://nasdag.github.io/blog/2016/01/19/classifying-bees-with-google-tensorflow/Khôi phục mô hình TensorFlow

Vào cuối của mã trong ví dụ này tôi đã thêm những dòng này:

saver = tf.train.Saver() 
save_path = saver.save(sess, "model.ckpt") 
print("Model saved in file: %s" % save_path) 

Hai file được tạo ra: trạm kiểm soát và model.ckpt.

Trong một file python mới (tomas_bees_predict.py), tôi có mã này:

import tensorflow as tf 

saver = tf.train.Saver() 

with tf.Session() as sess: 
    # Restore variables from disk. 
    saver.restore(sess, "model.ckpt") 
    print("Model restored.") 

Tuy nhiên khi tôi thực thi mã, tôi nhận được lỗi này:

Traceback (most recent call last): 
    File "tomas_bees_predict.py", line 3, in <module> 
    saver = tf.train.Saver() 
    File "/usr/local/lib/python2.7/site-packages/tensorflow/python/training/saver.py", line 705, in __init__ 
raise ValueError("No variables to save") 

ValueError: Không có biến để lưu

Có cách nào để đọc tệp mode.ckpt và xem các biến nào được lưu không? Hoặc có thể ai đó có thể giúp tiết kiệm mô hình và khôi phục mô hình dựa trên ví dụ được mô tả ở trên?

EDIT 1:

Tôi nghĩ rằng tôi đã cố gắng chạy cùng mã để tái cấu trúc mô hình và tôi đã nhận được báo lỗi. Tôi nghĩ rằng nó có thể liên quan đến thực tế là mã được mô tả ở đây không được sử dụng các biến có tên là: http://nasdag.github.io/blog/2016/01/19/classifying-bees-with-google-tensorflow/

def weight_variable(shape): 
    initial = tf.truncated_normal(shape, stddev=0.1) 
    return tf.Variable(initial) 

def bias_variable(shape): 
    initial = tf.constant(0.1, shape=shape) 
    return tf.Variable(initial) 

Vì vậy, tôi đã làm thí nghiệm này. Tôi đã viết hai phiên bản của mã (có và không có biến được đặt tên) để lưu mô hình và mã để khôi phục mô hình.

tensor_save_named_vars.py:

import tensorflow as tf 

# Create some variables. 
v1 = tf.Variable(1, name="v1") 
v2 = tf.Variable(2, name="v2") 

# Add an op to initialize the variables. 
init_op = tf.initialize_all_variables() 

# Add ops to save and restore all the variables. 
saver = tf.train.Saver() 

# Later, launch the model, initialize the variables, do some work, save the 
# variables to disk. 
with tf.Session() as sess: 
    sess.run(init_op) 
    print "v1 = ", v1.eval() 
    print "v2 = ", v2.eval() 
    # Save the variables to disk. 
    save_path = saver.save(sess, "/tmp/model.ckpt") 
    print "Model saved in file: ", save_path 

tensor_save_not_named_vars.py:

import tensorflow as tf 

# Create some variables. 
v1 = tf.Variable(1) 
v2 = tf.Variable(2) 

# Add an op to initialize the variables. 
init_op = tf.initialize_all_variables() 

# Add ops to save and restore all the variables. 
saver = tf.train.Saver() 

# Later, launch the model, initialize the variables, do some work, save the 
# variables to disk. 
with tf.Session() as sess: 
    sess.run(init_op) 
    print "v1 = ", v1.eval() 
    print "v2 = ", v2.eval() 
    # Save the variables to disk. 
    save_path = saver.save(sess, "/tmp/model.ckpt") 
    print "Model saved in file: ", save_path 

tensor_restore.py:

import tensorflow as tf 

# Create some variables. 
v1 = tf.Variable(0, name="v1") 
v2 = tf.Variable(0, name="v2") 

# Add ops to save and restore all the variables. 
saver = tf.train.Saver() 

# Later, launch the model, use the saver to restore variables from disk, and 
# do some work with the model. 
with tf.Session() as sess: 
    # Restore variables from disk. 
    saver.restore(sess, "/tmp/model.ckpt") 
    print "Model restored." 
    print "v1 = ", v1.eval() 
    print "v2 = ", v2.eval() 

Đây là những gì tôi nhận được khi tôi thực thi mã này:

$ python tensor_save_named_vars.py 

I tensorflow/core/common_runtime/local_device.cc:40] Local device intra op parallelism threads: 4 
I tensorflow/core/common_runtime/direct_session.cc:58] Direct session inter op parallelism threads: 4 
v1 = 1 
v2 = 2 
Model saved in file: /tmp/model.ckpt 

$ python tensor_restore.py 

I tensorflow/core/common_runtime/local_device.cc:40] Local device intra op parallelism threads: 4 
I tensorflow/core/common_runtime/direct_session.cc:58] Direct session inter op parallelism threads: 4 
Model restored. 
v1 = 1 
v2 = 2 

$ python tensor_save_not_named_vars.py 

I tensorflow/core/common_runtime/local_device.cc:40] Local device intra op parallelism threads: 4 
I tensorflow/core/common_runtime/direct_session.cc:58] Direct session inter op parallelism threads: 4 
v1 = 1 
v2 = 2 
Model saved in file: /tmp/model.ckpt 

$ python tensor_restore.py 
I tensorflow/core/common_runtime/local_device.cc:40] Local device intra op parallelism threads: 4 
I tensorflow/core/common_runtime/direct_session.cc:58] Direct session inter op parallelism threads: 4 
W tensorflow/core/common_runtime/executor.cc:1076] 0x7ff953881e40 Compute status: Not found: Tensor name "v2" not found in checkpoint files /tmp/model.ckpt 
    [[Node: save/restore_slice_1 = RestoreSlice[dt=DT_INT32, preferred_shard=-1, _device="/job:localhost/replica:0/task:0/cpu:0"](_recv_save/Const_0, save/restore_slice_1/tensor_name, save/restore_slice_1/shape_and_slice)]] 
W tensorflow/core/common_runtime/executor.cc:1076] 0x7ff953881e40 Compute status: Not found: Tensor name "v1" not found in checkpoint files /tmp/model.ckpt 
    [[Node: save/restore_slice = RestoreSlice[dt=DT_INT32, preferred_shard=-1, _device="/job:localhost/replica:0/task:0/cpu:0"](_recv_save/Const_0, save/restore_slice/tensor_name, save/restore_slice/shape_and_slice)]] 
Traceback (most recent call last): 
    File "tensor_restore.py", line 14, in <module> 
    saver.restore(sess, "/tmp/model.ckpt") 
    File "/usr/local/lib/python2.7/site-packages/tensorflow/python/training/saver.py", line 891, in restore 
    sess.run([self._restore_op_name], {self._filename_tensor_name: save_path}) 
    File "/usr/local/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 368, in run 
    results = self._do_run(target_list, unique_fetch_targets, feed_dict_string) 
    File "/usr/local/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 444, in _do_run 
    e.code) 
tensorflow.python.framework.errors.NotFoundError: Tensor name "v2" not found in checkpoint files /tmp/model.ckpt 
    [[Node: save/restore_slice_1 = RestoreSlice[dt=DT_INT32, preferred_shard=-1, _device="/job:localhost/replica:0/task:0/cpu:0"](_recv_save/Const_0, save/restore_slice_1/tensor_name, save/restore_slice_1/shape_and_slice)]] 
Caused by op u'save/restore_slice_1', defined at: 
    File "tensor_restore.py", line 8, in <module> 
    saver = tf.train.Saver() 
    File "/usr/local/lib/python2.7/site-packages/tensorflow/python/training/saver.py", line 713, in __init__ 
    restore_sequentially=restore_sequentially) 
    File "/usr/local/lib/python2.7/site-packages/tensorflow/python/training/saver.py", line 432, in build 
    filename_tensor, vars_to_save, restore_sequentially, reshape) 
    File "/usr/local/lib/python2.7/site-packages/tensorflow/python/training/saver.py", line 191, in _AddRestoreOps 
    values = self.restore_op(filename_tensor, vs, preferred_shard) 
    File "/usr/local/lib/python2.7/site-packages/tensorflow/python/training/saver.py", line 106, in restore_op 
    preferred_shard=preferred_shard) 
    File "/usr/local/lib/python2.7/site-packages/tensorflow/python/ops/io_ops.py", line 189, in _restore_slice 
    preferred_shard, name=name) 
    File "/usr/local/lib/python2.7/site-packages/tensorflow/python/ops/gen_io_ops.py", line 271, in _restore_slice 
    preferred_shard=preferred_shard, name=name) 
    File "/usr/local/lib/python2.7/site-packages/tensorflow/python/ops/op_def_library.py", line 664, in apply_op 
    op_def=op_def) 
    File "/usr/local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 1834, in create_op 
    original_op=self._default_original_op, op_def=op_def) 
    File "/usr/local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 1043, in __init__ 
    self._traceback = _extract_stack() 

Vì vậy, có lẽ mã gốc (xem liên kết bên ngoài ở trên) có thể được sửa đổi để một cái gì đó như thế này:

def weight_variable(shape): 
    initial = tf.truncated_normal(shape, stddev=0.1) 
    weight_var = tf.Variable(initial, name="weight_var") 
    return weight_var 

def bias_variable(shape): 
    initial = tf.constant(0.1, shape=shape) 
    bias_var = tf.Variable(initial, name="bias_var") 
    return bias_var 

Nhưng sau đó câu hỏi Tôi có: đang khôi phục các biến weight_var và bias_var đủ để thực hiện dự đoán? Tôi đã luyện tập trên máy tính mạnh mẽ với GPU và tôi muốn sao chép mô hình vào máy tính ít mạnh mẽ hơn mà không cần GPU để chạy dự đoán.

+0

có thể trùng lặp của [Tensorflow: Làm thế nào để khôi phục lại một mô hình đã lưu trước đó (python)] (http: // stackoverflow.com/questions/33759623/tensorflow-how-to-restore-a-previously-saved-model-python) – mrry

+0

Nó tương tự, nhưng nó không phải là một bản sao. – Tomas

Trả lời

12

Có câu hỏi tương tự ở đây: Tensorflow: how to save/restore a model? TLDR; bạn cần phải tạo cấu trúc mô hình sử dụng cùng một chuỗi các lệnh TensorFlow API trước khi sử dụng đối tượng Saver để khôi phục lại các trọng

Đây là tối ưu, hãy làm theo Github issue #696 cho sự tiến bộ vào việc này dễ dàng hơn

+1

Đối với hồ sơ: Sự cố đã bị đóng hơn một năm trước và có vẻ như việc lưu trữ cấu trúc mô hình được hỗ trợ ngay bây giờ. – bluenote10

+0

"bạn cần phải tạo lại cấu trúc mô hình bằng cách sử dụng cùng một chuỗi lệnh TensorFlow API trước khi sử dụng đối tượng Saver để khôi phục các trọng số" bạn có thể giải thích thêm về điều này không? – Chaine

+3

@Chaine có một tùy chọn tốt hơn, sử dụng MetaGraph hoặc mô hình đã lưu –

1

đảm bảo việc kê khai của tf.train. Trình tiết kiệm() có trong tf.Session() là sess

0

Sự cố này phải do các biến thể phạm vi tên khi tạo gấp đôi cùng một mạng.

đặt lệnh:

tf.reset_default_graph()

trước khi tạo mạng

Các vấn đề liên quan