Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TensorFlow: NotFoundError: Key not found in checkpoint

Tags:

tensorflow

I've been training a TensorFlow model for about a week with occasional sessions of fine-tuning.

Today when I tried to finetune the model I got the error:

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

Using inspect_checkpoint.py I see that the checkpoint file now has two empty layers in it:

...
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]
...

How do I fix this problem?

SOLUTION:

Following suggestion by mrry below edited for clarity:

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'))
like image 436
empty Avatar asked Oct 11 '17 21:10

empty


1 Answers

Fortunately, it doesn't look like your checkpoint is corrupt, but rather some of the variables in your program have been renamed. I'm assuming that the checkpoint value named "loss/total_loss/avg" should be restored to a variable named "conv_classifier/loss/total_loss/avg". You can solve this by passing a custom var_list when you create your tf.train.Saver.

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)

You can then use saver.restore() to restore your model. Alternatively, you can use this approach to restore the model and a default-constructed tf.train.Saver to save it in the canonical format.

like image 143
mrry Avatar answered Sep 26 '22 02:09

mrry