激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

腳本之家,腳本語言編程技術(shù)及教程分享平臺(tái)!
分類導(dǎo)航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服務(wù)器之家 - 腳本之家 - Python - Keras多線程機(jī)制與flask多線程沖突的解決方案

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

2021-11-17 10:18king的江鳥 Python

這篇文章主要介紹了Keras多線程機(jī)制與flask多線程沖突的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

在使用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ā)生了變化。以下是解決方案。

?
1
2
3
4
5
6
7
8
9
10
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è)常見錯(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ò)誤,不過在單獨(dú)的 .py 文件中使用卻不報(bào)錯(cuò)。

ValueError: Tensor Tensor("dense_1/Sigmoid:0", shape=(?, 1), dtype=float32) is not an element of this graph.

添加如下代碼可以解決:

?
1
2
3
4
5
6
7
8
9
10
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也不好解決,試了很多方法也沒解決,當(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}}]]

后來經(jīng)過N遍測(cè)試后找到了以下兩種解決方式,僅供參考:

方法一:

在調(diào)用前加載model和graph,但是這樣會(huì)導(dǎo)致程序每次調(diào)用都需要重新加載model,然后運(yùn)行速度就會(huì)很慢,不過這種修改方式是最簡(jiǎn)單的。

?
1
2
3
4
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í)不影響使用速率。

?
1
2
3
4
5
6
7
8
9
# 使用前:
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),不過估摸著這個(gè)是個(gè)例,應(yīng)該是程序問題。

2、RuntimeError: The Session graph is empty. Add operations to the graph before calling run().

?
1
2
3
4
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í)的常見錯(cuò)誤,解決方式如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
from tensorflow.python.keras.backend import set_session
# 程序開始時(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安裝目錄即可

?
1
os.environ["XLA_FLAGS"]="--xla_gpu_cuda_data_dir=/usr/local/cuda-10.0"

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持服務(wù)器之家。

原文鏈接:https://blog.csdn.net/weixin_40939578/article/details/100154100

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 欧美精品电影一区 | 久久影院yy6080 | 久久骚| 国产免费一区二区三区 | 草久免费| 欧美一级淫片免费视频1 | 久久在线 | 天天色图片 | 午夜视频福利 | av在线影片| 线观看免费完整aaa 久久不雅视频 | 福利在线小视频 | 久久久www成人免费毛片 | 曰韩黄色片| 中午日产幕无线码1区 | 免费黄色在线 | 毛片免费在线观看视频 | 中文字幕专区高清在线观看 | 亚洲国产女同久久 | 黄污网站在线 | 12av电影| 92自拍视频 | 久综合 | 黄色免费高清网站 | 在线免费91 | 天天干干| 日韩在线播放中文字幕 | 欧美 亚洲 激情 | 欧美一级三级在线观看 | 97香蕉超级碰碰久久免费软件 | 色骚综合| 久久91精品视频 | 美女毛片在线观看 | 国产精品成人免费一区久久羞羞 | 极品一级片 | 人人做人人看 | 欧产日产国产精品乱噜噜 | 成人视屏免费看 | 91性视频| 国产精品久久久久久久娇妻 | 99这里有精品 |