【发布时间】:2017-12-16 12:49:17
【问题描述】:
系统信息 操作系统平台和发行版(例如,Linux Ubuntu 16.04):
Mac os Sierra (10.12.5)
TensorFlow 安装自:
使用点子
TensorFlow 版本(使用下面的命令): 1.2.1
问题:
我正在尝试保存和恢复从 Python 到 Python 训练的模型。
我将模型保存在三个 .chkpt 文件(元、索引和 data-000000-of-00001)中,我正在尝试将其读入我的会话中,使用 add_meta_graph_and_variables 保存模型,然后使用加载器再次读取它: loader.load(session,[tf.saved_model.tag_constants.TRAINING], pathToSaveModel).
这是我的代码:
首先,我使用保护程序恢复将包含“数据”、“索引”和“元”(元图和权重)的三个文件中的权重恢复到我的会话中:
with tf.Session(graph=tf.Graph()) as session:
##HERE IS THE CODE OF MY NETWORK (Very long)
session.run(tf.global_variables_initializer())
#Load
saver = tf.train.Saver()
saver.restore(session, "newModel.chkpt")
features = loadFeatures(["cat2.jpg"])
res = predictions.eval(
feed_dict={
x: features,
keep_prob: 1.0, })
print('Image {} has a prob {} '.format(image, res))
b = saved_model_builder.SavedModelBuilder(pathToSaveModel)
b.add_meta_graph_and_variables(session, [tf.saved_model.tag_constants.TRAINING])
b.save()
使用这段代码,我有一个很好的分类,最后是一个包含使用 add_meta_graph_and_variables 保存的模型的新文件夹:
现在,我想再次使用保存的模型对同一张图像进行分类。这次我使用加载器而不是恢复:
with tf.Session(graph=tf.Graph()) as session:
##HERE IS THE CODE OF MY NETWORK (Very long)
#session.run(tf.global_variables_initializer())
#Load
from tensorflow.python.saved_model import loader
loader.load(session, [tf.saved_model.tag_constants.TRAINING], pathToSaveModel)
features = loadFeatures(["cat2.jpg"])
res = predictions.eval(
feed_dict={
x: features,
keep_prob: 1.0, })
print('Image {} has a prob {} '.format(image, res))
问题来了:
FailedPreconditionError (see above for traceback): Attempting to use uninitialized value b_fcO
[[Node: b_fcO/read = Identity[T=DT_FLOAT, _class=["loc:@b_fcO"], _device="/job:localhost/replica:0/task:0/cpu:0"](b_fcO)]]
如果我尝试使用: session.run(tf.global_variables_initializer()) 那么它可以工作,但分类无效,我认为从一开始就没有导出/导入权重,并且在测试后很多我被困在这里的事情。
关于我做错了什么的任何线索?
提前致谢。
【问题讨论】:
标签: python tensorflow