本人在寫Django RESful API時,碰到一個難題,老出現,整合Keras,報以下錯誤;很糾結,探索找資料近一個星期,皇天不負有心人,解決了python
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
Django初始化代碼寫在__init__.py中:session
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
頗有用的參考,很是感謝app
參考:https://zhuanlan.zhihu.com/p/27101000函數