jrosebr1 / simple-keras-rest-api

A simple Keras REST API using Flask
MIT License
374 stars 197 forks source link

Getting Value() error #3

Closed anujgupta82 closed 6 years ago

anujgupta82 commented 6 years ago

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

anujgupta82 commented 6 years ago

This is resolved. This is a very well researched problem. The solution is given in Issue #1

def load_model():
    global model
    model = ResNet50(weights="imagenet")
            # this is key : save the graph after loading the model
    global graph
    graph = tf.get_default_graph()

While predicting, use the same graph

    with graph.as_default():
    preds = model.predict(image)
    #... etc
jrosebr1 commented 6 years ago

Congrats on resolving the issue @anujgupta82 and thank you for sharing the relevant links just in case others have this problem.

johncleveland commented 6 years ago

This works for the developmental server, but it does not work if you want to use a standalone wsgi container e.g. gunicorn, gevent, uwsgi.

Also there is another solution if you just want to get the model to work. Essentially just get rid of the load_model() function. run_keras_server.txt

The above works on the standalone wsgi containers but not on the Flask developmental server ??? Is there a solution that works for all cases?

jrosebr1 commented 6 years ago

Hey @johncleveland, please don't copy and paste the same question into multiple threads. Your question has already been addressed and answered in #1. Thanks.