濮阳杆衣贸易有限公司

主頁(yè) > 知識(shí)庫(kù) > Keras多線(xiàn)程機(jī)制與flask多線(xiàn)程沖突的解決方案

Keras多線(xiàn)程機(jī)制與flask多線(xiàn)程沖突的解決方案

熱門(mén)標(biāo)簽:小蘇云呼電話(huà)機(jī)器人 北京電銷(xiāo)外呼系統(tǒng)加盟 北瀚ai電銷(xiāo)機(jī)器人官網(wǎng)手機(jī)版 儋州電話(huà)機(jī)器人 地圖標(biāo)注面積 市場(chǎng)上的電銷(xiāo)機(jī)器人 朝陽(yáng)手機(jī)外呼系統(tǒng) 所得系統(tǒng)電梯怎樣主板設(shè)置外呼 佛山400電話(huà)辦理

在使用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è)參考,也希望大家多多支持腳本之家。

您可能感興趣的文章:
  • flask開(kāi)啟多線(xiàn)程的具體方法
  • python進(jìn)階之多線(xiàn)程對(duì)同一個(gè)全局變量的處理方法
  • windows安裝TensorFlow和Keras遇到的問(wèn)題及其解決方法
  • 解決python多線(xiàn)程報(bào)錯(cuò):AttributeError: Can''t pickle local object問(wèn)題

標(biāo)簽:金融催收 寧夏 定西 龍巖 云南 江蘇 商丘 酒泉

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Keras多線(xiàn)程機(jī)制與flask多線(xiàn)程沖突的解決方案》,本文關(guān)鍵詞  Keras,多,線(xiàn)程,機(jī)制,與,flask,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《Keras多線(xiàn)程機(jī)制與flask多線(xiàn)程沖突的解決方案》相關(guān)的同類(lèi)信息!
  • 本頁(yè)收集關(guān)于Keras多線(xiàn)程機(jī)制與flask多線(xiàn)程沖突的解決方案的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    尖扎县| 通州市| 巴林左旗| 胶州市| 扶余县| 龙胜| 丹江口市| 天等县| 荃湾区| 五峰| 天长市| 竹溪县| 原平市| 普安县| 峨边| 仁寿县| 咸阳市| 阿克| 同心县| 禄劝| 东城区| 同仁县| 工布江达县| 贵德县| 宁化县| 石景山区| 潢川县| 富裕县| 玉门市| 富顺县| 象山县| 台江县| 富川| 广宁县| 南部县| 绥宁县| 玉溪市| 利川市| 石泉县| 绥化市| 祁连县|