It Works! An Epic Debugging – Thesis Week 8

A quick note about the error I encountered and the hardship of solving it.

To classify doodle, I need a doodle predicting server.



After some research, I found this:

Greyscale MobileNet [LB=0.892]

I downloaded the final model, read the code, and removed the training part, then I tested it, it works as a charm.

Next, I will need to call the prediction function each time I request this api;

Doing this via Flask is quite easy, however, I encountered an error when I call this function inside Flask’s callback.


app = Flask(__name__) @app.route("/api/doodlePredict", methods=["POST"]) def predictAPI(): global model, graph print(request.get_data()) response = perpareDataAndPredict(model, testJson) print("this is the response: ", response) return jsonify(response.tolist()) Output__________________: The Session graph is empty. Add operations to the graph before calling run().

I’m new to tensorflow, what’s graph? After hours of Googling and Youtubing, I learnt a little about Session and Graph.

It said graph is empty, so what I need to do add with graph.as_default(): after I call graph = tf.get_default_graph().

Then a new error occurs:

Error while reading resource variable conv_preds/bias from Container: localhost. This could mean that the variable was uninitialized. Not found: Resource localhost/conv_preds/bias/N10tensorflow3VarE does not exist.
     [[{{node conv_preds/BiasAdd/ReadVariableOp}}]]

I think it’s something about session, but every example I find involves something like this:

with tf.Session() as sess:
  sess.run(...)

What should be in the run function? I only have model.predict() since I’m using Keras.

Then I tried to ask some school mates, and Gene Kogan.

Me: I think I can’t solve this is because I don’t understand Sessions.

Gene: I don’t understand it either.

Me: what?

I’m beaten by this error.

Then I Google and Google, I learnt some new facts:

  • graph.as_default(): is not from Tensorflow, it’s from Keras.
  • I didn’t write anything involving graph inside with graph.as_default():.

Interesting, can I just use this session, then don’t write anything about this session? Perhaps Keras can do somethin with it?

So I wrote:

@app.route("/api/doodlePredict", methods=["POST"])
def predictAPI():
    global model, graph
    print(request.get_data())
    with sess.as_default():
        with graph.as_default():
            response = perpareDataAndPredict(model, testJson)
    print("this is the response: ", response)
    return jsonify(response.tolist())

Boom! It works! I’m so happy!

Source Code Here

4 Comments

Add Yours →

Hi there, I’m running into the same issue as you! I’m trying to follow the same solution, but there’s one part I don’t understand. When you write:

with sess.as_default():

What is `sess`? It’s not defined. I’d appreciate if you can help me out!

Leave a Reply