一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

腳本之家,腳本語言編程技術及教程分享平臺!
分類導航

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

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

Keras多線程機制與flask多線程沖突的解決方案

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

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

在使用flask部署Keras,tensorflow等框架時候,經常出現

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()可能會導致前后兩處預測結果不一樣,因為圖發生了變化。以下是解決方案。

?
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()

補充:Flask與keras結合的幾個常見錯誤

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

在Flask中使用tensorflow的model,一在界面中調用 model.predict() 就報下面這個錯誤,不過在單獨的 .py 文件中使用卻不報錯。

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()
    # 執行預測函數

但是我當時測試時又報了另一個bug,但是這個bug也不好解決,試了很多方法也沒解決,當然最終還是可以解決的,具體解決方式參考第三點。

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}}]]

后來經過N遍測試后找到了以下兩種解決方式,僅供參考:

方法一:

在調用前加載model和graph,但是這樣會導致程序每次調用都需要重新加載model,然后運行速度就會很慢,不過這種修改方式是最簡單的。

?
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)

方法二:

在創建model后,先使用一遍 model.predict(),參數的大小和真實大小一致,這個是真正解決之道,同時不影響使用速率。

?
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)
 
# 使用時:
global model
result = model.predict(tokens_pad)

但是在使用后又遇到了 The Session graph is empty…… 的錯誤即第二點,不過估摸著這個是個例,應該是程序問題。

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():
        # 相關代碼
        # 本次測試中是需要把調用包含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}}]]

這個錯誤呢,也是TensorFlow和Flask結合使用時的常見錯誤,解決方式如下:

?
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
# 程序開始時聲明
sess = tf.Session()
graph = tf.get_default_graph()
 
# 在model加載前添加set_session
set_session(sess)
model = models.load_model(…………)
 
# 每次使用有關TensorFlow的請求時
# 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

設置一下XLA_FLAGS指向你的cuda安裝目錄即可

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

以上為個人經驗,希望能給大家一個參考,也希望大家多多支持服務器之家。

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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 天天干夜夜拍 | 精品一区二区三区在线播放 | 国产成人夜色影视视频 | 日韩在线观看网站 | 青草免费在线观看 | 日韩一级免费毛片 | 2021最新国产成人精品免费 | 国产福利你懂的 | 忘忧草秋观看未满十八 | 丝袜兔女郎被啪在线观看91 | 国内自拍网红在综合图区 | 四虎论坛 | 色老太bbbbb| 变态 另类 人妖小说 | www亚洲国产| 亚洲精品91 | 每天都要睡男人(nph) | 色老板视频在线观看 | 日韩日韩日韩手机看片自拍 | 女人被男人躁得好爽免费视频 | 国产成人精品一区二三区在线观看 | 91麻豆精品国产自产在线观看 | 555www成人网| 毛片免费观看的视频 | 日韩免费在线视频 | 我的漂亮朋友在线观看全集免费 | 变态女王麻麻小说在线阅读 | 视频免费视频观看网站 | 日本中文字幕一区二区高清在线 | 久久中文字幕免费高清 | 亲爱的客栈第二季免费观看完整版 | 国产情侣偷国语对白 | 国产播放啪视频免费视频 | 成人在线一区二区三区 | 亚洲区在线播放 | 欧美日韩国产中文字幕 | 高人先生免费观看全集 | 日本国产在线视频 | 国色天香社区视频免费高清在线观看 | 男人最爱看的网站 | 丝瓜茄子绿巨人秋葵榴莲污 |