Giter VIP home page Giter VIP logo

simple-keras-rest-api's Introduction

A Simple Keras + deep learning REST API

This repository contains the code for Building a simple Keras + deep learning REST API, published on the Keras.io blog.

The method covered here is intended to be instructional. It is not meant to be production-level and capable of scaling under heavy load. If you're interested in a more advanced Keras REST API that leverages message queues and batching, please refer to this tutorial.

For an even more advanced version that includes deploying a model to production, refer to this blog post.

Getting started

I assume you already have Keras (and a supported backend) installed on your system. From there you need to install Flask and requests:

$ pip install flask gevent requests

Next, clone the repo:

$ git clone https://github.com/jrosebr1/simple-keras-rest-api.git

Starting the Keras server

Below you can see the image we wish to classify, a dog, but more specifically a beagle:

dog

The Flask + Keras server can be started by running:

$ python run_keras_server.py 
Using TensorFlow backend.
 * Loading Keras model and Flask starting server...please wait until server has fully started
...
 * Running on http://127.0.0.1:5000

You can now access the REST API via http://127.0.0.1:5000.

Submitting requests to the Keras server

Requests can be submitted via cURL:

$ curl -X POST -F [email protected] 'http://localhost:5000/predict'
{
  "predictions": [
    {
      "label": "beagle", 
      "probability": 0.9901360869407654
    }, 
    {
      "label": "Walker_hound", 
      "probability": 0.002396771451458335
    }, 
    {
      "label": "pot", 
      "probability": 0.0013951235450804234
    }, 
    {
      "label": "Brittany_spaniel", 
      "probability": 0.001283277408219874
    }, 
    {
      "label": "bluetick", 
      "probability": 0.0010894243605434895
    }
  ], 
  "success": true
}

Or programmatically:

$ python simple_request.py 
1. beagle: 0.9901
2. Walker_hound: 0.0024
3. pot: 0.0014
4. Brittany_spaniel: 0.0013
5. bluetick: 0.0011

simple-keras-rest-api's People

Contributors

jrosebr1 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

simple-keras-rest-api's Issues

Getting Value() error

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

bug1

Any suggestions will be of great help

"Tensor %s is not an element of this graph." Error

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)

JSON decode error with simple_request.py

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!

JSONDecodeError

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?

Error particular to a case when I tunnel SSH to server on a remote server

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 :)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.