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"
(资源库 www.zyku.net)
原文链接:https://blog.csdn.net/weixin_40939578/article/details/100154100
栏 目:Python教程
下一篇:python 中的collections.OrderedDict() 用法
本文标题:Keras多线程机制与flask多线程冲突的解决方案
本文地址:https://www.zyku.net/python/9787.html
您可能感兴趣的文章
- 10-17airpods pro更改名称方法分享
- 04-22苹果11录屏添加到控制中心设置方法
- 01-12时光闹钟-时光闹钟应用软件功能介绍
- 03-25华为手机误删短信恢复教程
- 01-11会玩-会玩应用软件功能介绍
- 11-22Office 2010/2013 图标显示不正常的解
- 09-24小米平板5pro怎么设置屏幕刷新率
- 01-11小学数学课堂笔记-小学数学课堂笔记应
- 07-07MariaDB 的二进制包安装方式
- 09-17真我gtneo2怎么设置游戏模式
- 01-11裕华红管家-裕华红管家应用软件功能介
- 12-10网易云音乐本地歌曲怎么上传到云盘
- 01-12指令农场少儿编程-指令农场少儿编程应
- 04-24华为nova8保持唤醒状态方法
- 09-22腾讯会议更换个人专属888会议号教程
- 09-19华为手机套餐限额取消步骤介绍
- 04-02华为p40指纹无法识别解决方法
- 09-25华为手表fitnew设置微信消息通知步骤
- 05-10php判断数组是否为空的实例方法
- 02-22小米10开启开发者选项教程
最近更新
阅读排行
猜你喜欢
- 01-08真我GT2pro语音助手怎么开启
- 01-13三节课企业版-三节课企业版应用软件功
- 02-10Python基础知识学习之类的继承
- 11-16iphone手机清理储存空间步骤介绍
- 09-18PHP实现替换Word里面变量并导出PDF图
- 11-25苹果xr怎么设置悬浮球
- 02-20sublime text3 字体大小设置教程
- 01-28小米10关闭锁屏后断开数据方法
- 12-23HTML <colgroup> 标签
- 09-14小米11pro在哪关闭内容中心