Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

keras load_model raise error when executed a second time

I'm making a website, and sometimes, it calls a keras neural network. So I have a function that looks like that :

def network(campaign):
    from keras.models import load_model
    model = load_model("sunshade/neural_network/model.h5") #the line that fail the second time i call it

    #loading some data

    label = model.predict(images, batch_size = 128, verbose = 1)

    #some unrelated code...

This code works fine when I execute it the first time, but when I try to run a second time, it fails wieh this error :

Exception in thread Thread-31:
Traceback (most recent call last):
  File "/usr/lib/python3.4/dist-packages/tensorflow/python/client/session.py", line 930, in _run
    allow_operation=False)
  File "/usr/lib/python3.4/dist-packages/tensorflow/python/framework/ops.py", line 2414, in as_graph_element
    return self._as_graph_element_locked(obj, allow_tensor, allow_operation)
  File "/usr/lib/python3.4/dist-packages/tensorflow/python/framework/ops.py", line 2493, in _as_graph_element_locked
    raise ValueError("Tensor %s is not an element of this graph." % obj)
ValueError: Tensor Tensor("Placeholder_3:0", shape=(32,), dtype=float32) is not an element of this graph.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib64/python3.4/threading.py", line 920, in _bootstrap_inner
    self.run()
  File "/usr/lib64/python3.4/threading.py", line 868, in run
    self._target(*self._args, **self._kwargs)
  File "/home/ec2-user/SpyNet/poc/sunshadeDetector/sunshade/models.py", line 46, in launch_network
    network(self)
  File "/home/ec2-user/SpyNet/poc/sunshadeDetector/sunshade/neural_network/network.py", line 27, in network
    model = load_model("sunshade/neural_network/model.h5")
  File "/usr/local/lib64/python3.4/site-packages/keras/models.py", line 236, in load_model
    topology.load_weights_from_hdf5_group(f['model_weights'], model.layers)
  File "/usr/local/lib64/python3.4/site-packages/keras/engine/topology.py", line 3048, in load_weights_from_hdf5_group
    K.batch_set_value(weight_value_tuples)
  File "/usr/local/lib64/python3.4/site-packages/keras/backend/tensorflow_backend.py", line 2188, in batch_set_value
    get_session().run(assign_ops, feed_dict=feed_dict)
  File "/usr/lib/python3.4/dist-packages/tensorflow/python/client/session.py", line 778, in run
    run_metadata_ptr)
  File "/usr/lib/python3.4/dist-packages/tensorflow/python/client/session.py", line 933, in _run
    + e.args[0])
TypeError: Cannot interpret feed_dict key as Tensor: Tensor Tensor("Placeholder_3:0", shape=(32,), dtype=float32) is not an element of this graph.

By the way, I use django for the website part, but i don't think it's related. There must be some kind of thing that need to be closed, or re-initialized... I tried to use tf.Session(), and tf.reset_default_graph,but I still get errors. So now I have to restart my django server each time i want to use this function.

Do you have any idea ? In worst case scenario, may be I can make the model a singleton, so that i don't have to reload it each time...

like image 586
SuperMouette Avatar asked Aug 18 '17 12:08

SuperMouette


1 Answers

You can create a new sesstion and load the model to it.

from keras.models import load_model
import keras

def network(campaign):
    with keras.backend.get_session().graph.as_default():
        model = load_model("sunshade/neural_network/model.h5")
        label = model.predict(images, batch_size = 128, verbose = 1)
like image 78
Rajat Jain Avatar answered Sep 29 '22 21:09

Rajat Jain