DjangoでKerasを動作させるとAttributeError(詳細は以下のエラー参照)となります。
Djangoではなく、以下のプログラムの最後に2行を追加しPythonコマンド「python ファイル名」で実行するとエラーとならず、正常に動作します。
#最後に追加した二行 testData="0,2,1106,0657,196,196,173,183,0,0,0,0,-26,-26,-41,-34,0,2,1106,0658,183,191,174,187,0,0,0,0,-35,-25,-43,-35,(省略),0,2,1106,0935,8,24,8,12,0,0,0,0,-3,1,-3,0,0,2,1106,0936,9,26,-37,0,0,0,0,0,1,2,-1,0" ai_predict(testData)
Djangoは同じユーザから「python manage.py runserver」で起動しているのでPython,keras,tensorflowなどのバージョンに差分はないと考えています。
また、Djangoで「predicted = model.predict(X, verbose=0)」の前にリターンで強制的に終了すると問題は発生しないので予測させているこの1行で問題が発生していると考えています。
(modelのLoadをPredict直前で実施してもエラーは同様でした。)
解決方法がわからないので、ご教授をお願いします。
error
1Traceback (most recent call last): 2 File "/home/user1/.pyenv/versions/3.6.8/lib/python3.6/site-packages/django/core/handlers/exception.py", line 34, in inner 3 response = get_response(request) 4 File "/home/user1/.pyenv/versions/3.6.8/lib/python3.6/site-packages/django/core/handlers/base.py", line 115, in _get_response 5 response = self.process_exception_by_middleware(e, request) 6 File "/home/user1/.pyenv/versions/3.6.8/lib/python3.6/site-packages/django/core/handlers/base.py", line 113, in _get_response 7 response = wrapped_callback(request, *callback_args, **callback_kwargs) 8 File "/home/user1/.pyenv/versions/3.6.8/lib/python3.6/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view 9 return view_func(*args, **kwargs) 10 File "/home/user1/.pyenv/versions/3.6.8/lib/python3.6/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view 11 return view_func(*args, **kwargs) 12 File "/home/user1/.pyenv/versions/3.6.8/lib/python3.6/site-packages/django/views/generic/base.py", line 71, in view 13 return self.dispatch(request, *args, **kwargs) 14 File "/home/user1/.pyenv/versions/3.6.8/lib/python3.6/site-packages/rest_framework/views.py", line 505, in dispatch 15 response = self.handle_exception(exc) 16 File "/home/user1/.pyenv/versions/3.6.8/lib/python3.6/site-packages/rest_framework/views.py", line 465, in handle_exception 17 self.raise_uncaught_exception(exc) 18 File "/home/user1/.pyenv/versions/3.6.8/lib/python3.6/site-packages/rest_framework/views.py", line 476, in raise_uncaught_exception 19 raise exc 20 File "/home/user1/.pyenv/versions/3.6.8/lib/python3.6/site-packages/rest_framework/views.py", line 502, in dispatch 21 response = handler(request, *args, **kwargs) 22 File "/home/user1/.pyenv/versions/3.6.8/lib/python3.6/site-packages/rest_framework/decorators.py", line 50, in handler 23 return func(*args, **kwargs) 24 File "/home/user1/zeus_cloud/app_zeus/views.py", line 25, in ai_job 25 aiJudge = ai_predict(getData) 26 File "/home/user1/zeus_cloud/app_zeus/ai_predict.py", line 32, in ai_predict 27 predicted = model.predict(X, verbose=0) 28 File "/home/user1/.pyenv/versions/3.6.8/lib/python3.6/site-packages/keras/engine/training.py", line 1452, in predict 29 if self._uses_dynamic_learning_phase(): 30 File "/home/user1/.pyenv/versions/3.6.8/lib/python3.6/site-packages/keras/engine/training.py", line 382, in _uses_dynamic_learning_phase 31 not isinstance(K.learning_phase(), int)) 32 File "/home/user1/.pyenv/versions/3.6.8/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py", line 73, in symbolic_fn_wrapper 33 if _SYMBOLIC_SCOPE.value: 34AttributeError: '_thread._local' object has no attribute 'value'
python
1import keras 2from keras import backend as K 3import numpy as np 4import logging 5 6logger = logging.getLogger("file") 7 8savefile = '/home/user1/zeus_cloud/fx_ai.h5' 9 10n = 160 11n_classes = 3 12# 学習済ファイルを読み込んでmodelを作成 13model = keras.models.load_model(savefile) 14 15def ai_predict(getData): 16 npData = np.array(getData.split(","), dtype = np.int) 17 logger.info('recv num' + str(npData.shape[0])) 18 #logger.info(npData.shape[1]) 19 if npData.shape[0] != n*4*4: 20 logger.info('err num' + str(npData.shape[0])) 21 return (1) 22 23 X = npData.reshape(1, 1, n, 4*4) 24 25 # 予測結果の取得 26 #model = keras.models.load_model(savefile) 27 predicted = model.predict(X, verbose=0) 28 predicted_label = np.argmax(predicted[0]) 29 30 logger.info('aiJudge:' + str(predicted_label)) 31 32 return (predicted_label)
回答1件
あなたの回答
tips
プレビュー