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

作者:king的江鸟 时间:2023-09-12 02:10:51 

在使用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"

来源:https://blog.csdn.net/weixin_40939578/article/details/100154100

标签:Keras,多线程,flask,冲突
0
投稿

猜你喜欢

  • perl 删除数组元素的几种方法小结

    2023-07-14 18:31:11
  • 各种JavaScript开发工具比较

    2007-10-23 13:29:00
  • python 实现dict转json并保存文件

    2022-04-17 10:24:17
  • C# 如何调用python脚本

    2023-11-26 19:06:47
  • Selenium 安装和简单使用的实现

    2023-12-01 07:22:55
  • Python 统计列表中重复元素的个数并返回其索引值的实现方法

    2023-07-15 12:31:24
  • django 外键model的互相读取方法

    2021-06-16 20:54:51
  • 如何将ChatGPT整合到Word中

    2023-12-20 03:13:54
  • ASP中的301跳转和302跳转

    2008-08-06 18:40:00
  • MySQL 8.0.20 安装教程图文详解(windows 64位)

    2024-01-22 07:32:07
  • 关于Kotlin中SAM转换的那些事

    2022-02-09 15:14:38
  • 网马解密大讲堂——网马解密初级篇

    2009-09-16 14:45:00
  • AJAX实现web页面中级联菜单的设计

    2007-09-26 13:37:00
  • SQL Server上进行表设计时表的主键设计问题

    2010-06-24 16:10:00
  • 4款Javascript放大镜特效脚本

    2009-10-14 20:46:00
  • Python中的函数式编程:不可变的数据结构

    2023-09-05 07:16:35
  • GO语言中的常量

    2024-05-13 10:44:17
  • Jupyter notebook 更改文件打开的默认路径操作

    2023-02-04 13:59:21
  • Python安装官方whl包和tar.gz包的方法(推荐)

    2022-04-27 05:37:47
  • 如何使用XML实现多渠道接入网站的构架

    2008-09-05 17:13:00
  • asp之家 网络编程 m.aspxhome.com