如何在Tensorflow r12中通过文件名恢复模型?

如何在Tensorflow r12中通过文件名恢复模型?,tensorflow,Tensorflow,我已经运行了分布式mnist示例: 虽然我已经设定了 saver=tf.train.saver(max\u to\u keep=0) 在以前的版本中,像r11一样,我能够运行每个检查点模型并评估模型的精度。这给了我一个精度和全局步骤(或迭代)的进度图 在r12之前,tensorflow检查点模型保存在两个文件中,model.ckpt-1234和model-ckpt-1234.meta。可以通过像sosaver.restore(sess,'model.ckpt-1234')那样传递model.c

我已经运行了分布式mnist示例:

虽然我已经设定了

saver=tf.train.saver(max\u to\u keep=0)

在以前的版本中,像r11一样,我能够运行每个检查点模型并评估模型的精度。这给了我一个精度和全局步骤(或迭代)的进度图

在r12之前,tensorflow检查点模型保存在两个文件中,
model.ckpt-1234
model-ckpt-1234.meta
。可以通过像so
saver.restore(sess,'model.ckpt-1234')
那样传递
model.ckpt-1234
文件名来恢复模型

但是,我注意到在r12中,现在有三个输出文件
model.ckpt-1234.data-00000-of-000001
model.ckpt-1234.index
,和
model.ckpt-1234.meta

我看到restore文档中说应该为restore提供一个路径,比如
/train/path/model.ckpt
,而不是文件名。有没有办法一次加载一个检查点文件来评估它?我已尝试传递
model.ckpt-1234.data-00000-of-000001
model.ckpt-1234.index
model.ckpt-1234.meta
文件,但出现如下错误:

W tensorflow/core/util/tensor\u slice\u reader.cc:95]无法打开logdir/2016-12-08-13-54/model.ckpt-0.data-00000-of-00001:数据丢失:不是sstable(错误的幻数):可能您的文件格式不同,需要使用不同的还原操作符?

NotFoundError(回溯见上文):在检查点文件logdir/2016-12-08-13-54/model.ckpt-0.index中未找到张量名称“hid_b”
[[Node:save/RestoreV2_1=RestoreV2[dtypes=[DT_FLOAT],[u device=“/job:localhost/replica:0/task:0/cpu:0”]([u recv_save/Const_0,save/RestoreV2_1/tensor_name,save/RestoreV2_1/shape_和_切片)]

W tensorflow/core/util/tensor\u slice\u reader.cc:95]无法打开logdir/2016-12-08-13-54/model.ckpt-0.meta:数据丢失:不是sstable(错误的幻数):可能您的文件格式不同,需要使用不同的还原操作符?

我在OSX Sierra上运行,通过pip安装了tensorflow r12

任何指导都会有帮助


谢谢。

好的,我可以回答我自己的问题。我发现我的python脚本在我的路径中添加了一个额外的“/”,因此我执行: saver.restore(sess,“/path/to/train//model.ckpt-1234”)

不知怎么的,这导致了tensorflow的问题

当我移除它时,呼叫: saver.restore(sess,“/path/to/trian/model.ckpt-1234”)


它按预期工作。

我还使用了Tensorlfow r0.12,我认为保存和恢复模型没有任何问题。下面是一个简单的代码,您可以尝试一下:

import tensorflow as tf

# Create some variables.
v1 = tf.Variable(tf.random_normal([784, 200], stddev=0.35), name="v1")
v2 = tf.Variable(tf.random_normal([784, 200], stddev=0.35), name="v2")

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

# 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)
  # Do some work with the model.

  # Save the variables to disk.
  save_path = saver.save(sess, "/tmp/model.ckpt")
  print("Model saved in file: %s" % save_path)

# 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.")
  # Do some work with the model

尽管在r0.12中,检查点存储在多个文件中,但您可以使用通用前缀来恢复它,在您的情况下,该前缀为“model.ckpt”。

R12已更改检查点格式。您应该以旧格式保存模型

import tensorflow as tf
from tensorflow.core.protobuf import saver_pb2
...
saver = tf.train.Saver(write_version = saver_pb2.SaverDef.V1)
saver.save(sess, './model.ckpt', global_step = step)
根据报告:

新的检查点格式成为tf.train.Saver中的默认格式。旧V1 检查点仍然是可读的;由write_版本控制 参数tf.train.Saver现在默认写入新的V2中 格式。它显著降低了所需的峰值内存和延迟 恢复期间发生的错误


请参阅中的详细信息。

我是TF的新手,遇到了相同的问题。阅读完袁妈的评论后,我将“.index”与“.data-00000-of-00001”文件一起复制到同一个“train\ckpt”文件夹中。然后成功了! 因此,在恢复模型时,.index文件就足够了。
我在Win7、r12上使用了TF。

您可以这样还原模型:

saver = tf.train.import_meta_graph('./src/models/20170512-110547/model-20170512-110547.meta')
            saver.restore(sess,'./src/models/20170512-110547/model-20170512-110547.ckpt-250000'))
其中路径“/src/models/20170512-110547/”包含三个文件:

model-20170512-110547.meta
model-20170512-110547.ckpt-250000.index
model-20170512-110547.ckpt-250000.data-00000-of-00001
如果在一个目录中有多个检查点,例如:路径中有检查点文件 /20170807-231648/:

checkpoint     
model-20170807-231648-0.data-00000-of-00001   
model-20170807-231648-0.index    
model-20170807-231648-0.meta   
model-20170807-231648-100000.data-00000-of-00001   
model-20170807-231648-100000.index   
model-20170807-231648-100000.meta
您可以看到有两个检查点,因此可以使用:

saver =    tf.train.import_meta_graph('/home/tools/Tools/raoqiang/facenet/models/facenet/20170807-231648/model-20170807-231648-0.meta')

saver.restore(sess,tf.train.latest_checkpoint('/home/tools/Tools/raoqiang/facenet/models/facenet/20170807-231648/'))

仅使用model.ckpt-1234


至少它对我有效

我必须添加tf.train.import\u meta\u graph
,tf.Session()作为sess:saver=tf.train.import\u meta\u graph('/tmp/model.ckpt.meta')saver.restore(sess,/tmp/model.ckpt”)
我认为最后两行中的注释应该放在最上面并用粗体显示。最重要的是一眼就能看出你的答案是否与其他人的问题相符。“data-0000-of-0001”部分到底是什么意思?对不起,优化器是个问题,我发现有一些限制,如果只有一个检查点,你就不能直接从目录还原模型,但是你可以试试这个:saver=tf.train.import_meta_graph('/home/tools/tools/raoqiang/facenet/src/models/20170512-110547/model-20170512-110547.meta')saver.restore(sess,'/home/tools/tools/raoqiang/facenet/src/models/20170512-110547/model-20170512-110547.ckpt-250000')