现象

项目使用 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的更多相关文章

  1. 解决在django中应用keras模型时出现的ValueError("Tensor %s is not an element of this graph." % obj)问题

    用keras训练好模型,再在django初始化加载模型,这个过程没有问题,但是在调用到模型执行model.predict()的时候就报错: raise ValueError("Tensor ...

  2. unittest中的方法调用时报错ValueError: no such test method in <class 'mytestcase.MyTestCase'>: runTest

    调用unittest中的方法时报错: ValueError: no such test method in <class 'mytestcase.MyTestCase'>: runTest ...

  3. unittest 中的方法调用时报错 ValueError: no such test method in <class 'mytestcase.MyTestCase'>: runTest

    1.调用unittest中的方法时报错: ValueError: no such test method in <class 'mytestcase.MyTestCase'>: runTe ...

  4. 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 ...

  5. python生成测试报告HTMLTestRunner时报错ValueError: write to closed file的解决办法

    使用HTMLTestRunner时出现了以下问题: self.stream.write(output.encode('utf8')) ValueError: write to closed file ...

  6. 做Webservice时报错java.util.List是接口, 而 JAXB 无法处理接口。

    Caused by: com.sun.xml.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExc ...

  7. 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 ...

  8. 将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 一个做 ...

  9. 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 ...

随机推荐

  1. 洛谷——P1972 [SDOI2009]HH的项链(线段树)

    P1972 [SDOI2009]HH的项链 HH 有一串由各种漂亮的贝壳组成的项链.HH 相信不同的贝壳会带来好运,所以每次散步完后,他都会随意取出一段贝壳,思考它们所表达的含义.HH 不断地收集新的 ...

  2. UVA - 1615 Highway(贪心-区间选点问题)

    题目: 给定平面上n(n≤105)个点和一个值D,要求在x轴上选出尽量少的点,使得对于给定的每个点,都有一个选出的点离它的欧几里得距离不超过D. 思路: 先自己造区间,然后贪心选点就可以了.之前做过一 ...

  3. [Python数据结构] 使用List实现Stack

    [Python数据结构] 使用List实现Stack 1. Stack 堆栈(Stack)又称为栈或堆叠,是计算机科学中一种特殊的串列形式的抽象数据类型(ADT),其特殊之处在于只能允许在阵列的一端进 ...

  4. <MySQL>入门三 数据定义语言 DDL

    -- DDL 数据定义语言 /* 库和表的管理 一.库的管理:创建.修改.删除 二.表的管理:创建.修改.删除 创建:create 修改:alter 删除:drop */ 1.库的管理 -- 库的管理 ...

  5. 55.fielddata内存控制以及circuit breaker断路器

    课程大纲 fielddata加载 fielddata内存限制 监控fielddata内存使用 circuit breaker 一.fielddata加载 fielddata加载到内存的过程是lazy加 ...

  6. 个人 NABCD

    失物招领APP个人NABCD Need: 在朋友圈中,QQZone中我们长仓可以看到失物招领这方面的信息,又没有学校中专门使用的失物招领平台,所以根据这个信息,决定开发一款解决这方面问题的APP来满足 ...

  7. LINUX-挂载一个文件系统

    mount /dev/hda2 /mnt/hda2 挂载一个叫做hda2的盘 - 确定目录 '/ mnt/hda2' 已经存在 umount /dev/hda2 卸载一个叫做hda2的盘 - 先从挂载 ...

  8. oldboy python 3.5 week 1

    #!/usr/bin/env python # -*- coding:utf-8 -*- ------------------------------------------- name = inpu ...

  9. SHA256兼容性

    SHA-2是一个加密哈希(Cryptographic Hash)函数的一个集合,包括SHA-224,SHA256和SHA-512.在SHA-256中的256代表哈希(Hash)输出或者摘要的位尺寸(即 ...

  10. POJ 1811 大整数素数判断 Miller_Rabin

    #include <cstdio> #include <cstring> #include <cmath> #include <ctime> #incl ...