question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

model error in web service using Flask

See original GitHub issue

Check List

Environment

  • OS [Linux ubuntu 16.04]:

Issue Description

What

When I deploy kashgari.tasks.classification model on the web service using Flask, I encounter an error showing below: tensorflow.python.framework.errors_impl.InvalidArgumentError: Tensor Input-Token:0, specified in either feed_devices or fetch_devices was not found in the Graph

and source code:

@app.route('/api/deep_classify/multi', methods=['GET', 'POST'])
def deep_classify_all():
      text = unquote(request.values.get("text"))
      categories = kashgari.tasks.classification.BiGRU_Model.predict_top_k_class([segment(text)])
        
        return categories

Other Comment

After research a while, I think the reason may be this: []https://kobkrit.com/tensor-something-is-not-an-element-of-this-graph-error-in-keras-on-flask-web-server-4173a8fe15e1

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

9reactions
Kircheis17commented, Aug 7, 2019

I also encountered this error using flask. The way that solved this problem in my case is keeping the graph & session to be the same for loading the model and doing the prediction, like this:

graph = tf.Graph()
with graph.as_default():
    session = tf.Session()
    with session.as_default():
        # load the model

...

def deep_classify_all():
      text = unquote(request.values.get("text"))
      with graph.as_default():
          with session.as_default():
              categories = model.predict_top_k_class([segment(text)])
      
      return categories

It can’t possibly be the best way. But it works for me. @alexwwang 's point sounds quite convincing, I think that’s why specifying the graph & session helps.

0reactions
hhxx2015commented, May 14, 2020
init = tf.global_variables_initializer()
graph = tf.get_default_graph()
session = tf.Session()
session.run(init)
set_session(session)


def predict_prob(self, corpus):
       x = [tokenize(i) for i in corpus]
       logger.info(x)

       global kmodel
       global graph
       global session
       with graph.as_default():
            set_session(session)
            with session.as_default():
                pre = kmodel.predict_top_k_class(x, batch_size=1)
                prelabel = [p["label"] for p in pre]
                return prelabel


Read more comments on GitHub >

github_iconTop Results From Across the Web

Handling Application Errors — Flask Documentation (2.2.x)
When Flask catches an exception while handling a request, it is first looked up by code. If no handler is registered for the...
Read more >
How To Handle Errors in a Flask Application - DigitalOcean
In this tutorial, you'll build a small web application that demonstrates how to handle common errors one encounters when developing a web ......
Read more >
Can't connect to Flask web service, connection refused
In order to get access from your laptop open up the terminal on your raspi and try instead the ip from ifconfig should...
Read more >
Flask API Error Handling. Build and Deploy a ... - YouTube
Flask API Error Handling. Build and Deploy a Python Flask REST API with JWT #18Source code.https://github.com/CryceTruly/bookmarker-apiFull ...
Read more >
How To Fix an Internal Server Error in Flask - YouTube
In this beginner level video I explain what steps you need to take when you get an Internal Server Error in your Flask...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found