TensorFlow:NotFoundError:检查点中找不到密钥

时间:2017-10-11 21:14:20

标签: tensorflow

我一直在训练TensorFlow模型大约一周,偶尔进行微调。

今天当我试图微调模型时,我得到了错误:

tensorflow.python.framework.errors_impl.NotFoundError: Key conv_classifier/loss/total_loss/avg not found in checkpoint
 [[Node: save/RestoreV2_37 = RestoreV2[dtypes=[DT_FLOAT], _device="/job:localhost/replica:0/task:0/cpu:0"](_arg_save/Const_0_0, save/RestoreV2_37/tensor_names, save/RestoreV2_37/shape_and_slices)]]

使用inspect_checkpoint.py我看到检查点文件现在有两个空图层:

...
conv_decode4/ort_weights/Momentum (DT_FLOAT) [7,7,64,64]
loss/cross_entropy/avg (DT_FLOAT) []
loss/total_loss/avg (DT_FLOAT) []
up1/up_filter (DT_FLOAT) [2,2,64,64]
...

如何解决此问题?

解决方案:

为了清晰起见,以下mrry的建议编辑:

code_to_checkpoint_variable_map = {var.op.name: var for var in tf.global_variables()}
for code_variable_name, checkpoint_variable_name in {
     "inference/conv_classifier/weight_loss/avg" : "loss/weight_loss/avg",
     "inference/conv_classifier/loss/total_loss/avg" : "loss/total_loss/avg",
     "inference/conv_classifier/loss/cross_entropy/avg": "loss/cross_entropy/avg",
}.items():
    code_to_checkpoint_variable_map[checkpoint_variable_name] = code_to_checkpoint_variable_map[code_variable_name]
    del code_to_checkpoint_variable_map[code_variable_name]

saver = tf.train.Saver(code_to_checkpoint_variable_map)
saver.restore(sess, tf.train.latest_checkpoint('./logs'))

1 个答案:

答案 0 :(得分:5)

幸运的是,您的检查点看起来并非已损坏,而是程序中的某些变量已重命名。我假设名为"loss/total_loss/avg"的检查点值应该还原为名为"conv_classifier/loss/total_loss/avg"的变量。您可以在创建tf.train.Saver时传递自定义var_list来解决此问题。

name_to_var_map = {var.op.name: var for var in tf.global_variables()}

name_to_var_map["loss/total_loss/avg"] = name_to_var_map[
    "conv_classifier/loss/total_loss/avg"]
del name_to_var_map["conv_classifier/loss/total_loss/avg"]

# Depending on how the names have changed, you may also need to do:
# name_to_var_map["loss/cross_entropy/avg"] = name_to_var_map[
#     "conv_classifier/loss/cross_entropy/avg"]
# del name_to_var_map["conv_classifier/loss/cross_entropy/avg"]

saver = tf.train.Saver(name_to_var_map)

然后,您可以使用saver.restore()恢复模型。或者,您可以使用此方法还原模型和默认构造的tf.train.Saver以将其保存为规范格式。