Search code examples
pythonneural-networktflearn

TFLearn: Error in loading 2 different saved models one after another


I have 2 different neural network models, trained and saved using TFLearn. When I run each script, the saved models are loaded properly. I need a system where, the second model should be called after the output of the first model. But when I try to load the second model after the first model has been loaded, it gives me the following error:

NotFoundError (see above for traceback): Key val_loss_2 not found in checkpoint [[Node: save_6/RestoreV2_42 = RestoreV2[dtypes=[DT_FLOAT], _device="/job:localhost/replica:0/task:0/cpu:0"](_arg_save_6/Const_0_0, save_6/RestoreV2_42/tensor_names, save_6/RestoreV2_42/shape_and_slices)]]

The second model is properly loaded if I comment out the loading of the first model, or if I run the 2 scripts separately. Any idea why this error is happening?

The code structure is something like ..

from second_model_file import check_second_model

def run_first_model(input):
    features = convert_to_features(input)
    model = tflearn.DNN(get_model())
    model.load("model1_path/model1")   # relative path
    pred = model.predict(features)
    ...
    if pred == certain_value:
       check_second_model()

The second_model_file.py is something similar:

def check_second_model():
    input_var = get_input_var()
    model2 = tflearn.DNN(regression_model())
    model2.load("model2_path/model2")   # relative path  
    pred = model2.predict(input_var)
    #other stuff  ......     

The models have been saved in different folders and so each have their own checkpoint file


Solution

  • Well, okay I found the solution. It was hidden in the discussion on this thread . I used tf.reset_default_graph() before building the second network and model and it worked. Hope this helps someone else too.

    New code:

    import tensorflow as tf
    
    def check_second_model():
        input_var = get_input_var()
        tf.reset_default_graph()
        model2 = tflearn.DNN(regression_model())
        model2.load("model2_path/model2")   # relative path  
        pred = model2.predict(input_var)
    

    Though I intuitively understand why this solution works, I would be happy if someone can explain me better why it is designed such.