Tensor(*** shape=(?, 1, ?, 5693), dtype=float32) is not an element of this graph.

本文探讨了Flask框架与TensorFlow在多线程环境下出现的兼容性问题,详细解析了两种解决方法:一是禁用gevent异步处理,二是通过调用model._make_predict_function()确保模型在所有线程中正确加载。

Flask框架与TensorFlow不兼容的问题

因为主线程是子线程的关系,以及flask使用了gevent异步,导致在其他线程没有主线程里的模型,最终导致识别报错。

解决方法

解决的方法也很简单

第一种:

取消代码中的gevent

# from gevent import monkey;
# monkey.patch_all()

还有 启动时候取消gevent

gunicorn -w ${WORKER} -t 100 -k gevent -b 0.0.0.0:${PORT} server:app
gunicorn -w ${WORKER} -t 100 -b 0.0.0.0:${PORT} server:app  # 去掉gevent

就可以了

第二种:

在load_model 之后 加上
model._make_predict_function()
参见:https://github.com/keras-team/keras/issues/6462
这段代码即可。
搞定!

C:\Apps\Anaconda3\envs\tf\python.exe C:\Users\pfliu\Desktop\OpenCV\TF3_coding_strategy\computation_graph.py 2025-11-19 19:40:47.395925: I tensorflow/core/platform/cpu_feature_guard.cc:193] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations: AVX AVX2 To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags. 2025-11-19 19:40:47.841927: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1616] Created device /job:localhost/replica:0/task:0/device:GPU:0 with 5546 MB memory: -> device: 0, name: NVIDIA GeForce RTX 3050, pci bus id: 0000:01:00.0, compute capability: 8.6 Traceback (most recent call last): File "C:\Apps\Anaconda3\envs\tf\lib\site-packages\tensorflow\python\client\session.py", line 304, in __init__ self._unique_fetches.append(ops.get_default_graph().as_graph_element( File "C:\Apps\Anaconda3\envs\tf\lib\site-packages\tensorflow\python\framework\ops.py", line 3998, in as_graph_element return self._as_graph_element_locked(obj, allow_tensor, allow_operation) File "C:\Apps\Anaconda3\envs\tf\lib\site-packages\tensorflow\python\framework\ops.py", line 4077, in _as_graph_element_locked raise ValueError("Tensor %s is not an element of this graph." % obj) ValueError: Tensor Tensor("a/Read/ReadVariableOp:0", shape=(2,), dtype=float32) is not an element of this graph. During handling of the above exception, another exception occurred: Traceback (most recent call last): File "C:\Users\pfliu\Desktop\OpenCV\TF3_coding_strategy\computation_graph.py", line 19, in <module> print(sess.run(a)) File "C:\Apps\Anaconda3\envs\tf\lib\site-packages\tensorflow\python\client\session.py", line 968, in run result = self._run(None, fetches, feed_dict, options_ptr, File "C:\Apps\Anaconda3\envs\tf\lib\site-packages\tensorflow\python\client\session.py", line 1176, in _run fetch_handler = _FetchHandler( File "C:\Apps\Anaconda3\envs\tf\lib\site-packages\tensorflow\python\client\session.py", line 485, in __init__ self._fetch_mapper = _FetchMapper.for_fetch(fetches) File "C:\Apps\Anaconda3\envs\tf\lib\site-packages\tensorflow\python\client\session.py", line 276, in for_fetch return _ElementFetchMapper(fetches, contraction_fn) File "C:\Apps\Anaconda3\envs\tf\lib\site-packages\tensorflow\python\client\session.py", line 311, in __init__ raise ValueError(f'Argument `fetch` = {fetch} cannot be interpreted as ' ValueError: Argument `fetch` = <tf.Variable 'a:0' shape=(2,) dtype=float32> cannot be interpreted as a Tensor. (Tensor Tensor("a/Read/ReadVariableOp:0", shape=(2,), dtype=float32) is not an element of this graph.)
最新发布
11-20
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天马行空波

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值