我正在尝试运行此tutorial。我在build_model.py中没有任何错误。但是,当我尝试运行app.py时。我收到了ValueError。顺便说一下,我使用conda环境。
Keras版本:2.2.4
Tensorflow版本:1.13.0
OpenCV版本:4.0.0
烧瓶版本:1.0.2
终奌站
(base) tarik:webapp tarik$ sudo python app.py
Password:
Using TensorFlow backend.
Loading model....
WARNING:tensorflow:From /anaconda3/lib/python3.7/site-packages/tensorflow/python/framework/op_def_library.py:263: colocate_with (from tensorflow.python.framework.ops) is deprecated and will be removed in a future version.
Instructions for updating:
Colocations handled automatically by placer.
2019-02-24 18:06:55.261293: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
WARNING:tensorflow:From /anaconda3/lib/python3.7/site-packages/keras/backend/tensorflow_backend.py:3445: calling dropout (from tensorflow.python.ops.nn_ops) with keep_prob is deprecated and will be removed in a future version.
Instructions for updating:
Please use `rate` instead of `keep_prob`. Rate should be set to `rate = 1 - keep_prob`.
WARNING:tensorflow:From /anaconda3/lib/python3.7/site-packages/tensorflow/python/ops/math_ops.py:3066: to_int32 (from tensorflow.python.ops.math_ops) is deprecated and will be removed in a future version.
Instructions for updating:
Use tf.cast instead.
....Loaded model
* Serving Flask app "app" (lazy loading)
* Environment: production
WARNING: Do not use the development server in a production environment.
Use a production WSGI server instead.
* Debug mode: off
* Running on http://0.0.0.0:8080/ (Press CTRL+C to quit)
127.0.0.1 - - [24/Feb/2019 18:06:58] "GET / HTTP/1.1" 200 -
/anaconda3/lib/python3.7/site-packages/skimage/transform/_warps.py:105: UserWarning: The default mode, 'constant', will be changed to 'reflect' in skimage 0.15.
warn("The default mode, 'constant', will be changed to 'reflect' in "
/anaconda3/lib/python3.7/site-packages/skimage/transform/_warps.py:110: UserWarning: Anti-aliasing will be enabled by default in skimage 0.15 to avoid aliasing artifacts when down-sampling images.
warn("Anti-aliasing will be enabled by default in skimage 0.15 to "
[2019-02-24 18:07:03,391] ERROR in app: Exception on /api/v1 [POST]
Traceback (most recent call last):
File "/anaconda3/lib/python3.7/site-packages/flask/app.py", line 2292, in wsgi_app
response = self.full_dispatch_request()
File "/anaconda3/lib/python3.7/site-packages/flask/app.py", line 1815, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/anaconda3/lib/python3.7/site-packages/flask/app.py", line 1718, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/anaconda3/lib/python3.7/site-packages/flask/_compat.py", line 35, in reraise
raise value
File "/anaconda3/lib/python3.7/site-packages/flask/app.py", line 1813, in full_dispatch_request
rv = self.dispatch_request()
File "/anaconda3/lib/python3.7/site-packages/flask/app.py", line 1799, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "app.py", line 174, in traffic_light_prediction
response_num = traffic_prediction(f)
File "app.py", line 136, in traffic_prediction
y_pred = model.predict_classes(X_test)
File "/anaconda3/lib/python3.7/site-packages/keras/engine/sequential.py", line 267, in predict_classes
proba = self.predict(x, batch_size=batch_size, verbose=verbose)
File "/anaconda3/lib/python3.7/site-packages/keras/engine/training.py", line 1164, in predict
self._make_predict_function()
File "/anaconda3/lib/python3.7/site-packages/keras/engine/training.py", line 554, in _make_predict_function
**kwargs)
File "/anaconda3/lib/python3.7/site-packages/keras/backend/tensorflow_backend.py", line 2744, in function
return Function(inputs, outputs, updates=updates, **kwargs)
File "/anaconda3/lib/python3.7/site-packages/keras/backend/tensorflow_backend.py", line 2546, in __init__
with tf.control_dependencies(self.outputs):
File "/anaconda3/lib/python3.7/site-packages/tensorflow/python/framework/ops.py", line 5028, in control_dependencies
return get_default_graph().control_dependencies(control_inputs)
File "/anaconda3/lib/python3.7/site-packages/tensorflow/python/framework/ops.py", line 4528, in control_dependencies
c = self.as_graph_element(c)
File "/anaconda3/lib/python3.7/site-packages/tensorflow/python/framework/ops.py", line 3478, in as_graph_element
return self._as_graph_element_locked(obj, allow_tensor, allow_operation)
File "/anaconda3/lib/python3.7/site-packages/tensorflow/python/framework/ops.py", line 3557, in _as_graph_element_locked
raise ValueError("Tensor %s is not an element of this graph." % obj)
ValueError: Tensor Tensor("dense_2/Softmax:0", shape=(?, 43), dtype=float32) is not an element of this graph.
127.0.0.1 - - [24/Feb/2019 18:07:03] "POST /api/v1 HTTP/1.1" 500 -
最佳答案
通过在加载模型后在模型上调用_make_predict_function()为我解决了问题。
从https://github.com/keras-team/keras/issues/6462获得
关于python - python-ValueError:Tensor Tensor(“dense_2/Softmax:0”,shape =(?, 43),dtype = float32)不是此图的元素,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54853427/