本人在写Django RESful API时,碰到一个难题,老出现,整合Keras,报如下错误;很纠结,探索找资料近一个星期,皇天不负有心人,解决了

Internal Server Error: /pic/analysis/
Traceback (most recent call last):
File "D:\AI\Python35\lib\site-packages\tensorflow\python\client\session.py", line 1064, in _run
allow_operation=False)
File "D:\AI\Python35\lib\site-packages\tensorflow\python\framework\ops.py", line 3035, in as_graph_element
return self._as_graph_element_locked(obj, allow_tensor, allow_operation)
File "D:\AI\Python35\lib\site-packages\tensorflow\python\framework\ops.py", line 3114, in _as_graph_element_locked
raise ValueError("Tensor %s is not an element of this graph." % obj)
ValueError: Tensor Tensor("Placeholder:0", shape=(3, 3, 1, 32), dtype=float32) is not an element of this graph. During handling of the above exception, another exception occurred: Traceback (most recent call last):
File "D:\AI\Python35\lib\site-packages\django\core\handlers\exception.py", line 35, in inner
response = get_response(request)
File "D:\AI\Python35\lib\site-packages\django\core\handlers\base.py", line 128, in _get_response
response = self.process_exception_by_middleware(e, request)
File "D:\AI\Python35\lib\site-packages\django\core\handlers\base.py", line 126, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "D:\AI\Python35\lib\site-packages\django\test\utils.py", line 371, in inner
return func(*args, **kwargs)
File "E:\PyCharmWork\PythonWebApp\ApiPicDjangoSite\PicWeb\views.py", line 25, in picAnalysis
predictBank(im, name) #对图片进行预测,并返回预测结果
File "E:\PyCharmWork\PythonWebApp\ApiPicDjangoSite\PicWeb\PreDictBankFunc.py", line 42, in predictBank
model = load_model('static\CnnBankUp.h5', compile=False)
File "D:\AI\Python35\lib\site-packages\keras\models.py", line 243, in load_model
topology.load_weights_from_hdf5_group(f['model_weights'], model.layers)
File "D:\AI\Python35\lib\site-packages\keras\engine\topology.py", line 3142, in load_weights_from_hdf5_group
K.batch_set_value(weight_value_tuples)
File "D:\AI\Python35\lib\site-packages\keras\backend\tensorflow_backend.py", line 2247, in batch_set_value
get_session().run(assign_ops, feed_dict=feed_dict)
File "D:\AI\Python35\lib\site-packages\tensorflow\python\client\session.py", line 889, in run
run_metadata_ptr)
File "D:\AI\Python35\lib\site-packages\tensorflow\python\client\session.py", line 1067, in _run
+ e.args[0])
TypeError: Cannot interpret feed_dict key as Tensor: Tensor Tensor("Placeholder:0", shape=(3, 3, 1, 32), dtype=float32) is not an element of t
his graph.
[08/Jan/2018 08:53:15] "POST /pic/analysis/ HTTP/1.1" 500 159200

个人理解:在初始化Django时,把keras中 model先初始化,免得后面不断调用,产生莫名其妙的问题

Django初始化代码写在__init__.py中:

from keras.models import load_model
import numpy as np #参考:https://zhuanlan.zhihu.com/p/27101000 print('load model...')
model = load_model('static\\CnnBankUp.h5', compile=False)
print('load done.') #一定要添加这段代码,先测试一下,可以避免ValueError: Tensor Tensor("Placeholder:0", shape=(3, 3, 1, 32), dtype=float32)
#is not an element of this graph.的错误
print('test model...')
#根据自己传入图片格式定义np.zeros()
print(model.predict(np.zeros((2, 200,200,1))))
print('test done.') # 使用模型,在得到用户输入时会调用以下两个函数进行实时文本分类
# 输入参数 comment 为经过了分词与向量化处理后的模型输入
def picType_class(comment):
global model
result_vec=None
result_vec = model.predict(comment)
return result_vec

很有用的参考,非常感谢

参考:https://zhuanlan.zhihu.com/p/27101000

Django整合Keras报错:ValueError: Tensor Tensor("Placeholder:0", shape=(3, 3, 1, 32), dtype=float32) is not an element of this graph.解决方法的更多相关文章

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

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

  3. memcached解压报错gzip: stdin: not in gzip format tar: Child returned status 1 tar: Error is not recoverable: exiting now的解决方法

    最近在部署环境,在安装memcached的过程中解压时, 解压命令:tar -zvxf memcached-1.4.34.tar.gz 遇到了一个问题, gzip: stdin: not in gzi ...

  4. 解压报错gzip: stdin: not in gzip format tar: Child returned status 1 tar: Error is not recoverable: exiting now的解决方法

    在部署tomcat的环境搞JDK的时候出现这个问题.分享一下. 解压命令:tar -zvxf memcached-1.4.34.tar.gz 遇到了一个问题, gzip: stdin: not in ...

  5. Django序列化时间报错

    一.前言 当利用models模块从数据库获取数据时,当获的取数据序列化时,如果获取的数据中有关于时间类型的字段,则会报错,错误如下: TypeError: datetime.datetime(2018 ...

  6. spring+hibernate整合:报错org.hibernate.HibernateException: No Session found for current thread

    spring+hibernate整合:报错信息如下 org.hibernate.HibernateException: No Session found for current thread at o ...

  7. weblogic 整合cxf 报错:cannot create a secure XmlInputFactory

    weblogic 整合cxf 报错:cannot create a secure XmlInputFactory ================================ ©Copyright ...

  8. django startproject xxx:报错UnicodeDecodeError: 'ascii' codec can't decode byte 0xe6 in position 13: ordinal not in range(128)

    django startproject xxx:报错UnicodeDecodeError: 'ascii' codec can't decode byte 0xe6 in position 13: o ...

  9. matplotlib.pyplot import报错: ValueError: _getfullpathname: embedded null character in path

    Environment: Windows 10, Anaconda 3.6 matplotlib 2.0 import matplotlib.pyplot 报错: ValueError: _getfu ...

随机推荐

  1. javascript 获取节点元素的封装

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. Spring session(redis存储方式)监听导致创建大量redisMessageListenerContailner-X线程

    待解决的问题 Spring session(redis存储方式)监听导致创建大量redisMessageListenerContailner-X线程 解决办法 为spring session添加spr ...

  3. WebStrom直接启动VUE项目

    点Run即可启动

  4. luogu P3565 [POI2014]HOT-Hotels

    传送门 无脑暴力+O2=AC 题目要统计距离两两相等的三个点的组数,这三个点之间显然有一个点,并且这三个点到这个点的距离都相同.所以枚举中间这个点作为根,然后bfs整棵树,对于每一层,把以根的某个儿子 ...

  5. luogu P4448 [AHOI2018初中组]球球的排列

    这道题我一上来只会80 还是要感谢题解区大佬题解的帮助 先考虑若\(xy,xz\)为完全平方数,则\(yz\)也为完全平方数,因为\(xy*xz=x^2yz\)为完全平方数,除掉\(x^2\)就行了 ...

  6. 手动创建binary log files和手动编辑binary log index file会有什么影响

    基本环境:官方社区版MySQL 5.7.19 一.了解Binary Log结构 1.1.High-Level Binary Log Structure and Contents • Binlog包括b ...

  7. ppt 制作圆角三角形

    制作圆角三角形: PART 01 :插入三角形与三个等大的圆形: PART 02 :利用[任意多边形]和[合并形状-剪除]获得缺三角: (先选中大三角形,然后再选中任意多边形,"格式&quo ...

  8. 【网络编程4】网络编程基础-ARP响应(ARP欺骗之中间人攻击)

    arp欺骗->arp响应 ARP 缓存中毒(ARP欺骗) arp传送原理在于主机发送信息时将包含目标IP地址的ARP请求广播到网络上的所有主机,并接收返回消息,以此确定目标的物理地址:收到返回消 ...

  9. Python中的__init__()和__call__()函数

    Python中的__init__()和__call__()函数 在Python的class中有一些函数往往具有特殊的意义.__init__()和__call__()就是class很有用的两类特殊的函数 ...

  10. git操作之冲突解决

    应用场景,任哥,我两个人共同修改了git项目上的一个文件.zsh命令行模式 准备工作 简写命令解释 gl=git pullgp=git pushgst=git statusgcmsg=git comm ...