gpt4 book ai didi

python - keras load_model 第二次执行时引发错误

转载 作者:太空狗 更新时间:2023-10-30 01:11:58 25 4
gpt4 key购买 nike

我正在制作一个网站,有时它会调用 keras 神经网络。所以我有一个看起来像这样的函数:

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

这段代码在我第一次执行时工作正常,但是当我尝试第二次运行时,它失败了,出现了这个错误:

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.

顺便说一句,我在网站部分使用了django,但我认为这没有关系。一定有某种东西需要关闭或重新初始化...我尝试使用 tf.Session() 和 tf.reset_default_graph,但我仍然会出错。所以现在每次我想使用这个功能时我都必须重新启动我的 django 服务器。

你有什么想法吗?在最坏的情况下,可能我可以将模型设为单例,这样我就不必每次都重新加载它...

最佳答案

您可以创建一个新 session 并将模型加载到其中。

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)

关于python - keras load_model 第二次执行时引发错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45756297/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com