Keras多线程机制与flask多线程冲突的解决方案

 更新时间:2021年05月28日 14:55:24   作者:king的江鸟  
这篇文章主要介绍了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()可能会导致前后两处预测结果不一样,因为图发生了变化。以下是解决方案。

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.

添加如下代码可以解决:

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,然后运行速度就会很慢,不过这种修改方式是最简单的。

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(),参数的大小和真实大小一致,这个是真正解决之道,同时不影响使用速率。

# 使用前:
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().

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结合使用时的常见错误,解决方式如下:

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安装目录即可

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

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • 使用PyCharm批量爬取小说的完整代码

    使用PyCharm批量爬取小说的完整代码

    这篇文章主要介绍了使用PyCharm批量爬取小说,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-04-04
  • Python获取Redis所有Key以及内容的方法

    Python获取Redis所有Key以及内容的方法

    今天小编就为大家分享一篇Python获取Redis所有Key以及内容的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-02-02
  • 在PyCharm下使用 ipython 交互式编程的方法

    在PyCharm下使用 ipython 交互式编程的方法

    今天小编就为大家分享一篇在PyCharm下使用 ipython 交互式编程的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-01-01
  • Python爬虫——爬取豆瓣电影Top250代码实例

    Python爬虫——爬取豆瓣电影Top250代码实例

    这篇文章主要介绍了Python爬取豆瓣电影Top250实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-04-04
  • 详解python-docx处理Word必备工具

    详解python-docx处理Word必备工具

    这篇文章主要介绍了python-docx处理Word必备工具,我主要讲讲自己用到的几个内容是怎么设置的,对python-docx处理Word的相关知识感兴趣的朋友一起看看吧
    2021-10-10
  • 浅谈Python中chr、unichr、ord字符函数之间的对比

    浅谈Python中chr、unichr、ord字符函数之间的对比

    chr、unichr、ord在Python中都可以被用作字符类型转换,这里我们就来浅谈Python中chr、unichr、ord字符函数之间的对比,需要的朋友可以参考下
    2016-06-06
  • Python 基础教程之闭包的使用方法

    Python 基础教程之闭包的使用方法

    这篇文章主要介绍了Python 基础教程之闭包的使用方法的相关资料,希望大家通过本文能帮助到大家,需要的朋友可以参考下
    2017-09-09
  • python创建与遍历List二维列表的方法

    python创建与遍历List二维列表的方法

    这篇文章主要介绍了python创建与遍历List二维列表的方法,本文给大家介绍的非常详细,具有一定的参考借鉴价值 ,需要的朋友可以参考下
    2019-08-08
  • 为什么Python中没有

    为什么Python中没有"a++"这种写法

    一开始学习 Python 的时候习惯性的使用 C 中的 a++ 这种写法,发现会报 SyntaxError: invalid syntax 错误,为什么 Python 没有自增运算符的这种写法呢?下面小编给大家带来本文帮助大家了解下这方面的知识
    2018-11-11
  • 教你怎么用Python生成九宫格照片

    教你怎么用Python生成九宫格照片

    过年过节大家的朋友圈是不是特别热闹,每当小编看见朋友圈有这种九宫格的照片就觉得特别秀,一直想自己什么时候也能来秀一个,所以直接拿这个练练手,酷炸朋友圈一波,直接进入主题,需要的朋友可以参考下
    2021-05-05

最新评论