Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serve trained Tensorflow model with REST API using Flask?

I have got a trained Tensorflow model and I want to serve the prediction method with REST API. What I can think of is to use Flask to build a simple REST API that receive JSON as input and then call the predict method in Tensorflow and then return the predicted result to the client side.

I would like to know is there any concern to do it this way especially in production environment?

Many thanks!

like image 558
user1515940 Avatar asked Apr 08 '16 06:04

user1515940


People also ask

Can we deploy a machine learning model with Flask?

Machine learning is a process that is widely used for prediction. N number of algorithms are available in various libraries which can be used for prediction.


2 Answers

The first concern which comes into my mind is the performance.

TensorFlow team seems to have worked out server/client usage. You may want to look into tensorflow serving. As a default, it uses gRPC for communication protocol.

like image 120
Tosh Avatar answered Oct 17 '22 09:10

Tosh


We use Flask + TensorFlow serving at work. Our setup might not be the most optimal way to serve models, but it gets the job done and it works fine for us so far.

The setup is the following:

  1. Because tfserving takes forever to build, we built a docker image (not GPU support or anything, but it works for just serving a model and it's faster and better than serving it directly from within a huge Python/Flask monolite). The model server image can be found here: https://hub.docker.com/r/epigramai/model-server/
  2. Then Flask is used to setup an API. In order to send requests to the model server we need a grcp prediction client, so we built one in Python that we can import directly into the flask API, https://github.com/epigramai/tfserving_predict_client/.

The good thing here is that the model is not served by the Flask API application. The docker image model server can easily be replaced with a model server running on a GPU compiled for the machines hardware instead of the docker container.

like image 2
stianlp Avatar answered Oct 17 '22 09:10

stianlp