Keras多線程機(jī)制與flask多線程沖突的解決方案
在使用flask部署Keras,tensorflow等框架時(shí)候,經(jīng)常出現(xiàn)
FailedPreconditionError: Attempting to use uninitialized value batchnormalization_
或者
Tensor Tensor("crf_1/cond/Merge:0", shape=(?, ?, 260), dtype=float32) is not an element of this graph.
使用keras.backend.clear_session()可能會(huì)導(dǎo)致前后兩處預(yù)測(cè)結(jié)果不一樣,因?yàn)閳D發(fā)生了變化。以下是解決方案。
graph = tf.get_default_graph() sess = tf.Session(graph=graph) def modelpredict(content): #keras.backend.clear_session() global graph global sess with sess.as_default(): with graph.as_default(): keras.model.predict()
補(bǔ)充:Flask與keras結(jié)合的幾個(gè)常見(jiàn)錯(cuò)誤
1、 ValueError: Tensor Tensor(“dense_1/Sigmoid:0”, shape=(?, 1), dtype=float32) is not an element of this graph.
在Flask中使用tensorflow的model,一在界面中調(diào)用 model.predict() 就報(bào)下面這個(gè)錯(cuò)誤,不過(guò)在單獨(dú)的 .py 文件中使用卻不報(bào)錯(cuò)。
ValueError: Tensor Tensor("dense_1/Sigmoid:0", shape=(?, 1), dtype=float32) is not an element of this graph.
添加如下代碼可以解決:
import tensorflow as tf graph = tf.get_default_graph() model = models.load_model(…………) # 使用處添加: global graph global model with graph.as_default(): model.predict() # 執(zhí)行預(yù)測(cè)函數(shù)
但是我當(dāng)時(shí)測(cè)試時(shí)又報(bào)了另一個(gè)bug,但是這個(gè)bug也不好解決,試了很多方法也沒(méi)解決,當(dāng)然最終還是可以解決的,具體解決方式參考第三點(diǎn)。
tensorflow.python.framework.errors_impl.FailedPreconditionError: Error while reading resource variable dense_1/bias from Container: localhost. This could mean that the variable was uninitialized. Not found: Resource localhost/dense_1/bias/class tensorflow::Var does not exist.
[[{{node dense_1/BiasAdd/ReadVariableOp}}]]
后來(lái)經(jīng)過(guò)N遍測(cè)試后找到了以下兩種解決方式,僅供參考:
方法一:
在調(diào)用前加載model和graph,但是這樣會(huì)導(dǎo)致程序每次調(diào)用都需要重新加載model,然后運(yùn)行速度就會(huì)很慢,不過(guò)這種修改方式是最簡(jiǎn)單的。
graph = tf.get_default_graph() model = models.load_model('./static/my_model2.h5') with graph.as_default(): result = model.predict(tokens_pad)
方法二:
在創(chuàng)建model后,先使用一遍 model.predict(),參數(shù)的大小和真實(shí)大小一致,這個(gè)是真正解決之道,同時(shí)不影響使用速率。
# 使用前: model = models.load_model('./static/my_model2.h5') # a 矩陣大小和 tokens_pad 一致 a = np.ones((1, 220)) model.predict(a) # 使用時(shí): global model result = model.predict(tokens_pad)
但是在使用后又遇到了 The Session graph is empty…… 的錯(cuò)誤即第二點(diǎn),不過(guò)估摸著這個(gè)是個(gè)例,應(yīng)該是程序問(wèn)題。
2、RuntimeError: The Session graph is empty. Add operations to the graph before calling run().
graph = tf.get_default_graph() with graph.as_default(): # 相關(guān)代碼 # 本次測(cè)試中是需要把調(diào)用包含model.predict()方法的方法的代碼放到這里
3、tensorflow.python.framework.errors_impl.FailedPreconditionError: Error while reading resource variable dense_1/bias from Container: localhost. This could mean that the variable was uninitialized. Not found: Resource localhost/dense_1/bias/class tensorflow::Var does not exist.[[{{node dense_1/BiasAdd/ReadVariableOp}}]]
這個(gè)錯(cuò)誤呢,也是TensorFlow和Flask結(jié)合使用時(shí)的常見(jiàn)錯(cuò)誤,解決方式如下:
from tensorflow.python.keras.backend import set_session # 程序開(kāi)始時(shí)聲明 sess = tf.Session() graph = tf.get_default_graph() # 在model加載前添加set_session set_session(sess) model = models.load_model(…………) # 每次使用有關(guān)TensorFlow的請(qǐng)求時(shí) # in each request (i.e. in each thread): global sess global graph with graph.as_default(): set_session(sess) model.predict(...) ————————————————
4、 Can't find libdevice directory ${CUDA_DIR}/nvvm/libdevice. This may result in compilation or runtime failures, if the program we try to run uses routines from libdevice
設(shè)置一下XLA_FLAGS指向你的cuda安裝目錄即可
os.environ["XLA_FLAGS"]="--xla_gpu_cuda_data_dir=/usr/local/cuda-10.0"
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- pycharm中報(bào)ModuleNotFoundError:No?module?named?'tensorflow'錯(cuò)誤解決
- TensorFlow2.1.0安裝過(guò)程中setuptools、wrapt等相關(guān)錯(cuò)誤指南
- 解決TensorFlow GPU版出現(xiàn)OOM錯(cuò)誤的問(wèn)題
- 解決tensorflow由于未初始化變量而導(dǎo)致的錯(cuò)誤問(wèn)題
- 解決tensorflow測(cè)試模型時(shí)NotFoundError錯(cuò)誤的問(wèn)題
- 在Flask使用TensorFlow的幾個(gè)常見(jiàn)錯(cuò)誤及解決
相關(guān)文章
使用Python解析FineReport模板數(shù)據(jù)集
這篇文章主要為大家詳細(xì)介紹了如何使用Python解析FineReport模板數(shù)據(jù)集,文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價(jià)值,感興趣的小伙伴可以了解下2023-12-12Python?Asyncio中Coroutines,Tasks,Future可等待對(duì)象的關(guān)系及作用
這篇文章主要介紹了Python?Asyncio中Coroutines,Tasks,Future可等待對(duì)象的關(guān)系及作用,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,需要的小伙伴可以參考一下2022-06-06使用Python快速打開(kāi)一個(gè)百萬(wàn)行級(jí)別的超大Excel文件的方法
這篇文章主要介紹了使用Python快速打開(kāi)一個(gè)百萬(wàn)行級(jí)別的超大Excel文件的方法,本文通過(guò)實(shí)例代碼給大家介紹的非常想詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03如何測(cè)試Python網(wǎng)站的訪問(wèn)速度,并且優(yōu)化Python網(wǎng)站的性能
本文使用網(wǎng)絡(luò)工具和Python測(cè)速庫(kù)進(jìn)行測(cè)試Python網(wǎng)站的訪問(wèn)速度,通過(guò)優(yōu)化代碼性能和優(yōu)化服務(wù)器性能以及優(yōu)化數(shù)據(jù)庫(kù)性能等有針對(duì)性地優(yōu)化Python網(wǎng)站的性能2024-01-01對(duì)python 多線程中的守護(hù)線程與join的用法詳解
今天小編就為大家分享一篇對(duì)python 多線程中的守護(hù)線程與join的用法詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-02-02自動(dòng)化測(cè)試Pytest單元測(cè)試框架的基本介紹
這篇文章主要介紹了Pytest單元測(cè)試框架的基本介紹,包含了Pytest的概念,Pytest特點(diǎn),其安裝流程步驟以及相關(guān)配置流程,有需要的朋友可以參考下2021-08-08Python3網(wǎng)絡(luò)爬蟲(chóng)開(kāi)發(fā)實(shí)戰(zhàn)之極驗(yàn)滑動(dòng)驗(yàn)證碼的識(shí)別
本節(jié)我們的目標(biāo)是用程序來(lái)識(shí)別并通過(guò)極驗(yàn)驗(yàn)證碼的驗(yàn)證,其步驟有分析識(shí)別思路、識(shí)別缺口位置、生成滑塊拖動(dòng)路徑,最后模擬實(shí)現(xiàn)滑塊拼合通過(guò)驗(yàn)證。需要的朋友可以參考下2019-08-08