使用Keras做OCR时报错:ValueError: Tensor Tensor is not an element of this graph
现象
项目使用 Flask + Keras + Tensorflow
同样的代码在机器A和B上都能正常运行,但在机器C上就会报如下异常。机器A和B的环境是先安装的,运行、调试成功后才尝试在C上跑。
File "/Users/qhl/anaconda3/lib/python3.6/site-packages/keras/models.py", line 1025, in predict
steps=steps)
File "/Users/qhl/anaconda3/lib/python3.6/site-packages/keras/engine/training.py", line 1832, in predict
self._make_predict_function()
File "/Users/qhl/anaconda3/lib/python3.6/site-packages/keras/engine/training.py", line 1029, in _make_predict_function
**kwargs)
File "/Users/qhl/anaconda3/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py", line 2502, in function
return Function(inputs, outputs, updates=updates, **kwargs)
File "/Users/qhl/anaconda3/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py", line 2445, in __init__
with tf.control_dependencies(self.outputs):
File "/Users/qhl/anaconda3/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 4863, in control_dependencies
return get_default_graph().control_dependencies(control_inputs)
File "/Users/qhl/anaconda3/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 4481, in control_dependencies
c = self.as_graph_element(c)
File "/Users/qhl/anaconda3/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 3478, in as_graph_element
return self._as_graph_element_locked(obj, allow_tensor, allow_operation)
File "/Users/qhl/anaconda3/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 3557, in _as_graph_element_locked
raise ValueError("Tensor %s is not an element of this graph." % obj)
ValueError: Tensor Tensor("Output/Softmax:0", shape=(?, 3062), dtype=float32) is not an element of this graph.
查找原因
由于Tensorflow有可能会用到显卡,因此首先怀疑是显卡或驱动的原因导致的。在C机器上做过如下尝试:
- 重装过CPU和GPU版本的Tensorflow
- 重装过显卡驱动以及CUBA
- 操作系统也更换过Windows、Deepin、Ubuntu
- 应用代码也同步过多次,确保A/B/C机器上的代码一模一样
- 也查看过Tensorflow和Keras以及h5py的版本号
最终还是报一样的错。经过代码跟踪、上网搜索,发现是由于Tensorflow在多线程模式下的一个bug。而Flask最新版(1.0.2)默认改为多线程模式了,以前是默认单线程模式。而且巧的是,Flask 1.0版就是在我安装C机器环境前才发布的,装A/B机器时还是0.12。
解决办法
1. 据说改为Theano为backend可以解决。我没试过
2. 修改当前的default graph。这里有个大讨论可以参考:https://github.com/keras-team/keras/issues/2397。具体做法:
在加载或构建你的model后添加
graph = tf.get_default_graph()
在执行model.predict()方法前
global graph
with graph.as_default():
(... do inference here ...)
这样就可以支持多线程模式了
3. 也可以强制将Flask改为单线程模式。
if __name__ == '__main__':
app.run(host="0.0.0.0", port=8080, threaded=False)
使用Keras做OCR时报错:ValueError: Tensor Tensor is not an element of this graph的更多相关文章
- 解决在django中应用keras模型时出现的ValueError("Tensor %s is not an element of this graph." % obj)问题
用keras训练好模型,再在django初始化加载模型,这个过程没有问题,但是在调用到模型执行model.predict()的时候就报错: raise ValueError("Tensor ...
- unittest中的方法调用时报错ValueError: no such test method in <class 'mytestcase.MyTestCase'>: runTest
调用unittest中的方法时报错: ValueError: no such test method in <class 'mytestcase.MyTestCase'>: runTest ...
- unittest 中的方法调用时报错 ValueError: no such test method in <class 'mytestcase.MyTestCase'>: runTest
1.调用unittest中的方法时报错: ValueError: no such test method in <class 'mytestcase.MyTestCase'>: runTe ...
- uwsgi手动安装时报错ValueError: invalid literal for int() with base 10: '32_1'
安装uwsgi,安装步骤如下 wget https://projects.unbit.it/downloads/uwsgi-latest.tar.gz tar zxvf uwsgi-latest.ta ...
- python生成测试报告HTMLTestRunner时报错ValueError: write to closed file的解决办法
使用HTMLTestRunner时出现了以下问题: self.stream.write(output.encode('utf8')) ValueError: write to closed file ...
- 做Webservice时报错java.util.List是接口, 而 JAXB 无法处理接口。
Caused by: com.sun.xml.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExc ...
- Django整合Keras报错:ValueError: Tensor Tensor("Placeholder:0", shape=(3, 3, 1, 32), dtype=float32) is not an element of this graph.解决方法
本人在写Django RESful API时,碰到一个难题,老出现,整合Keras,报如下错误:很纠结,探索找资料近一个星期,皇天不负有心人,解决了 Internal Server Error: /p ...
- 将keras模型在django中应用时出现的小问题——ValueError: Tensor Tensor("dense_2/Softmax:0", shape=(?, 8), dtype=float32) is not an element of this graph.
本文原出处(感谢作者提供):https://zhuanlan.zhihu.com/p/27101000 将keras模型在django中应用时出现的小问题 王岳王院长 10 个月前 keras 一个做 ...
- keras 报错 ValueError: Tensor conversion requested dtype int32 for Tensor with dtype float32: 'Tensor("embedding_1/random_uniform:0", shape=(5001, 128), dtype=float32)'
在服务器上训练并保存模型,复制到本地之后load_model()报错: ValueError: Tensor conversion requested dtype int32 for Tensor w ...
随机推荐
- 洛谷——P3275 [SCOI2011]糖果
P3275 [SCOI2011]糖果 差分约束模板题,基本思路就是$d[v]+w[v,u]<=d[u]$,$Spfa$更新方法, 有点套路的是要建立原点,即图中不存在的点来向每个点加边,但同样这 ...
- UVA - 11175 From D to E and Back(思路)
题目: 思路: 如图E:图中a.b.c.d是有向图D中的顶点,如果ac.bc都指向cd,而ac又指向ce,那bc同样应该有一条指向ce的边不然就不能从图D转换来.所以直接枚举顶点就可以了. 代码: # ...
- vcenter6.5安装问题
vcenter6.5 有2个安装包1个vim (windows系统) 1个vcsa(linux) 安装vcsa遇到如下问题: 1.版本bug 网上一般找到的镜像是VMware-VCSA-all-6 ...
- python环境配置以及基本知识
python---一种解释型语言(脚本语言),具有代码简洁.入门简单.开发效率高的优点.当然不可避免的有着暴露源码.执行效率低的缺点,但毕竟瑕不掩瑜,在数据是无比宝贵的财富的当下,无疑是一门优秀的编成 ...
- 手机通过Charles用线上域名访问PC本地项目
最近调试微信公众号,由于微信授权需要线上正式环境的域名,笔者想把手机公众号网页重定向到PC本地localhost调试. 该方法通过Charles代理转发,适用所有需要域名重定向的场景,例如手机通过在线 ...
- C#创建excel并释放资源
using System; using Microsoft.Office.Interop.Excel; using Excel = Microsoft.Office.Interop.Excel; us ...
- 【Codeforces 411A】Password Check
[链接] 我是链接,点我呀:) [题意] 题意 [题解] 傻逼模拟题 [代码] import java.io.*; import java.util.*; public class Main { st ...
- ace & web ide & web code editor
ace & web ide & web code editor web ide https://ace.c9.io/ https://github.com/ajaxorg/ace ht ...
- codeforces 371D
#include<stdio.h> #define N 210000 struct node { int x,next; __int64 count,vec; }pre[N]; int n ...
- 30、Java并发性和多线程-阿姆达尔定律
以下内容转自http://ifeve.com/amdahls-law/: 阿姆达尔定律可以用来计算处理器平行运算之后效率提升的能力.阿姆达尔定律因Gene Amdal 在1967年提出这个定律而得名. ...