jrosebr1 / simple-keras-rest-api Goto Github PK
View Code? Open in Web Editor NEWA simple Keras REST API using Flask
License: MIT License
A simple Keras REST API using Flask
License: MIT License
Predict gives this error when i try on web app. But it works fine on console.
You can find related issue here:
keras-team/keras#2397
And also worked solution here:
keras-team/keras#2397 (comment)
When the run_keras_server.py
is deployed as a webservice to which you tunnel through SSH, I experienced this:
ERROR
raise ValueError("Tensor %s is not an element of this graph." % obj)
ValueError: Tensor Tensor("fc1000/Softmax:0", shape=(?, 1000), dtype=float32) is not an element of this graph.
(I tunnelled through SSH like this: ssh -N -f -L localhost:9999:localhost:5000 USERNAME@HOST
)
SOLUTION
And after some search I found out that many people are experiencing the same issue at - keras-team/keras#2397
What helped was:
1.) adding graph = tf.get_default_graph()
after we initialize the model, such as this:
def load_model():
global model
model = ResNet50(weights="imagenet")
global graph
graph = tf.get_default_graph()
2.) when working with the model, surrounding it by with graph.as_default():
, such as
with graph.as_default():
preds = model.predict(image)
#... etc
Not exactly sure what caused this problem, but perhaps it was something with different threads initializing and later using the model (as someone was saying in the referenced discussion).
Hope this helps someone who gets stuck on this.
Note that this ran perfectly all right on local machine, but started misbehaving when running Flask on remote server to which I tunnelled through SSH.
Ps: Thanks for the tutorial :)
I'm having some trouble using requests
to programatically call the REST API. Submitting the request via cURL works without issues, but when I attempt to run the same image through simple_request.py, I receive the following error:
File "C:\ProgramData\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 2881, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-19-0c3919566f71>", line 1, in <module>
r = requests.post(KERAS_REST_API_URL, files=payload).json()
File "C:\ProgramData\Anaconda3\lib\site-packages\requests\models.py", line 885, in json
return complexjson.loads(self.text, **kwargs)
File "C:\ProgramData\Anaconda3\lib\json\__init__.py", line 354, in loads
return _default_decoder.decode(s)
File "C:\ProgramData\Anaconda3\lib\json\decoder.py", line 339, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\ProgramData\Anaconda3\lib\json\decoder.py", line 357, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
When I run the script without .json()
at the end of the request, it returns a very strange HTML response that I can't make heads or tails of.
I am on a Windows 10 machine (corporate environment), Python 3.6.1, Keras v2.0.8, TensorFlow v1.3.0, requests v2.14.2
Any help with this would be greatly appreciated!
I cloned this code with
Keras==2.1.5
tensorflow==1.7.0
When I send a request I am getting
File "/home/root1/.virtualenv/MyDir/local/lib/python2.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)
Below is complete screen shot of the error
Any suggestions will be of great help
As title. I think #7 may be due to changes in newer version of tensorflow
this error occurs at model.predict() line. Without flask server, it is working fine but with flask server, it is not working.
I've cloned the repository and tried to run it with no changes. I've ran python run_keras_server.py
to start the server (no issues) and then run python simple_request.py
, which results in an error. I get the following.
From the run_keras_server.py
command window:
[2018-08-16 14:46:10,048] ERROR in app: Exception on /predict [POST]
Traceback (most recent call last):
File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site-packages\flask\app.py", line 2292, in wsgi_app
response = self.full_dispatch_request()
File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site-packages\flask\app.py", line 1815, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site-packages\flask\app.py", line 1718, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site-packages\flask\_compat.py", line 35, in reraise
raise value
File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site-packages\flask\app.py", line 1813, in full_dispatch_request
rv = self.dispatch_request()
File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site-packages\flask\app.py", line 1799, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "run_keras_server.py", line 61, in predict
preds = model.predict(image)
File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site-packages\keras\engine\training.py", line 1162, in predict
self._make_predict_function()
File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site-packages\keras\engine\training.py", line 543, in _make_predict_function
**kwargs)
File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site-packages\keras\backend\tensorflow_backend.py", line 2695, in function
return Function(inputs, outputs, updates=updates, **kwargs)
File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site-packages\keras\backend\tensorflow_backend.py", line 2516, in __init__
with tf.control_dependencies(self.outputs):
File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\framework\ops.py", line 5060, in control_dependencies
return get_default_graph().control_dependencies(control_inputs)
File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\framework\ops.py", line 4664, in control_dependencies
c = self.as_graph_element(c)
File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\framework\ops.py", line 3613, in as_graph_element
return self._as_graph_element_locked(obj, allow_tensor, allow_operation)
File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\framework\ops.py", line 3692, in _as_graph_element_locked
raise ValueError("Tensor %s is not an element of this graph." % obj)
ValueError: Tensor Tensor("fc1000/Softmax:0", shape=(?, 1000), dtype=float32) is not an element of this graph.
127.0.0.1 - - [16/Aug/2018 14:46:10] "�[1m�[35mPOST /predict HTTP/1.1�[0m" 500 -
From the simple_request.py
command window:
Traceback (most recent call last):
File "simple_request.py", line 17, in <module>
r = requests.post(KERAS_REST_API_URL, files=payload).json()
File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site-packages\requests\models.py", line 896, in json
return complexjson.loads(self.text, **kwargs)
File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\json\__init__.py", line 354, in loads
return _default_decoder.decode(s)
File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\json\decoder.py", line 339, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\json\decoder.py", line 357, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
Thanks for the interersting guide and repo. I have cloned it and setup a virtualenv with required packages. I am able to run the flask server, however not able to make requests.
When using curl request: curl -X POST -F [email protected] 'http://localhost:5000/predict'
, I get the following error:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>500 Internal Server Error</title>
<h1>Internal Server Error</h1>
<p>The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.</p>
And subsequently when running the python simple_request.py
I get the following error as an output:
Traceback (most recent call last):
File "simple_request.py", line 17, in <module>
r = requests.post(KERAS_REST_API_URL, files=payload).json()
File "/home/bloks/Projects/Sentriq/simple-keras-rest-api/skra_venv/lib/python3.5/site-packages/requests/models.py", line 896, in json
return complexjson.loads(self.text, **kwargs)
File "/usr/lib/python3.5/json/__init__.py", line 319, in loads
return _default_decoder.decode(s)
File "/usr/lib/python3.5/json/decoder.py", line 339, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib/python3.5/json/decoder.py", line 357, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
As far as I could find, it may have something to do with a version conflict of some of the packages.
Maybe include a requirements.txt
file solves the problem?
after this cmd ( curl -X POST -F [email protected] 'http://localhost:5000/predict')
i;m getting the following error
how to overcome with this..???
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.