关键词

Django整合Keras报错:ValueError: Tensor Tensor(“Placeh

本人在写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

 

本文链接:http://task.lmcjl.com/news/1542.html

展开阅读全文