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 ...
随机推荐
- H3C常用命令详解
H3C常用命令详解 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 1.关闭后台日志输出 <yinzhengjie>sys [yinzhengjie]undo info- ...
- Hi3519v101-uboot-start.S分析
00032: #include <config.h>00033: #include <version.h> 1)#include <config.h>.config ...
- 在IIS上启用Gzip压缩(HTTP压缩)
一.摘要 本文总结了如何为使用IIS托管的网站启用Gzip压缩, 从而减少网页网络传输大小, 提高用户显示页面的速度. 二.前言. 本文的知识点是从互联网收集整理, 主要来源于中文wiki. 使用Y ...
- 部署flask
部署 当我们执行下面的hello.py时,使用的flask自带的服务器,完成了web服务的启动.在生产环境中,flask自带的服务器,无法满足性能要求,我们这里采用Gunicorn做wsgi容器,来部 ...
- C#复习正则表达式
由于前段时间为了写工具学的太J8粗糙 加上最近一段时间太浮躁 所以静下心来复习 一遍以前学的很弱的一些地方 1 委托 public delegate double weituo(double a, d ...
- 使用MedleyText与Syncthink自建云笔记
随着学习加深,做的笔记越来越多,而使用云笔记怕万一平台关闭(如360网盘啥的)还需要导出笔记费时费力,并且多平台兼容性未知.还是自己搭建放心省事. MedleyText介绍 MedleyText为ma ...
- bzoj千题计划305:bzoj2565: 最长双回文串(回文自动机)
https://www.lydsy.com/JudgeOnline/problem.php?id=2565 正着构造回文自动机 倒过来再构造一个回文自动机 分别求出以位置i开始的和结尾的最长回文串 # ...
- 《Maven实战》笔记
maven是什么 maven是 构建工具 依赖关系工具 项目信息管理工具 而JAVA世界的ant只是一个构建工具,不具备依赖管理的功能,需要配合使用ivy进行依赖管理. maven的安装 下载mave ...
- json 不能 dumps datetime 解决办法
backend.myviews.json_time.py from datetime import date import json from datetime import datetime cla ...
- dubbo集群服务下一台服务挂了对服务调用的影响
一.问题描述:项目中2台dubbo服务给移动端提供查询接口,移动端反应说查询时而很快(秒刷),时而很慢(4-5秒). 二.问题分析: 1.问题猜想:网络不稳定原因导致,但是切换公司wifi和手机4G, ...