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: /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.解决方法的更多相关文章
- 将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 ...
- 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 ...
- 解压报错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 ...
- Django序列化时间报错
一.前言 当利用models模块从数据库获取数据时,当获的取数据序列化时,如果获取的数据中有关于时间类型的字段,则会报错,错误如下: TypeError: datetime.datetime(2018 ...
- spring+hibernate整合:报错org.hibernate.HibernateException: No Session found for current thread
spring+hibernate整合:报错信息如下 org.hibernate.HibernateException: No Session found for current thread at o ...
- weblogic 整合cxf 报错:cannot create a secure XmlInputFactory
weblogic 整合cxf 报错:cannot create a secure XmlInputFactory ================================ ©Copyright ...
- 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 ...
- matplotlib.pyplot import报错: ValueError: _getfullpathname: embedded null character in path
Environment: Windows 10, Anaconda 3.6 matplotlib 2.0 import matplotlib.pyplot 报错: ValueError: _getfu ...
随机推荐
- 关于连接linux被拒
一.scp被拒 在服务器A命令行使用scp往服务器B传输,如: scp -i id_rsa_125 file1 user01@IP:/home #ssh_exchange_identification ...
- Swift学习笔记7--访问控制
在Swift语言中,访问修饰符有三种,分别为private,internal和public.同时,Swift对于访问权限的控制,不是基于类的,而是基于文件的.其区别如下: 1,private priv ...
- JS 将字符串数组用 | 或其他符号分割
var arr = ["吕超","赵云","典韦","关羽","马超","张飞" ...
- css postion 属性区别【原】
CSS样式中的postion元素有四个属性,即static | absolute | fixed | relative. static: 默认值.无特殊定位,遵循HTML基本定位规则 . fixed: ...
- git 分支branch
转:https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000/00137584003893 ...
- HDU - 4333 Revolving Digits(扩展KMP)
http://acm.hdu.edu.cn/showproblem.php?pid=4333 题意 一个数字,依次将第一位放到最后一位,问小于本身的数的个数及等于本身的个数和大于本身的个数,但是要注意 ...
- JAVA中equals方法与hashCode方法学习
首先参考文章:http://www.oschina.net/translate/working-with-hashcode-and-equals-methods-in-java 1,equals方法的 ...
- Objects源码解析
Objects类解析 JDK7新增Objects类介绍(以下程序以1.8来说明) 简介: JDK7里面新增的Objects类,本人学习HashMap源码偶遇此类,所以研究一下,本类将对象常用的 ...
- NOIP2018TG题解
应该不会被禁赛吧qwq Day1 T1 铺设道路 €€£:我 抄 我 自 己 我考场上写的是一个类似于差分的做法,就是开个数组表示某个位置是否贡献答案,从小到大枚举高度,那么一个位置能贡献答案当且仅当 ...
- img格式镜像转ISO格式
在做汇编学习时,需要用比较老的Windows XP来进行调试学习,因此找了最老的Windows XP(CN_WINXP_PRO_ISO,无SP版本 ),下载后发现镜像文件格式是img的,而virtua ...