python - keras load_model raise error when executed a second time -
i'm making website, , sometimes, calls keras neural network. have function looks :
def network(campaign): keras.models import load_model model = load_model("sunshade/neural_network/model.h5") #the line fail second time call #loading data label = model.predict(images, batch_size = 128, verbose = 1) #some unrelated code...
this code works fine when execute first time, when try run second time, fails wieh 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 not element of graph." % obj) valueerror: tensor tensor("placeholder_3:0", shape=(32,), dtype=float32) not element of graph. during handling of above exception, 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 tensor: tensor tensor("placeholder_3:0", shape=(32,), dtype=float32) not element of graph.
by way, use django website part, don't think it's related. there must kind of thing need closed, or re-initialized... tried use tf.session(), , tf.reset_default_graph,but still errors. have restart django server each time want use function.
do have idea ? in worst case scenario, may can make model singleton, don't have reload each time...
Comments
Post a Comment