How to keep a Keras model loaded into memory and use it when needed?

Viewed 2744

I was reading a Keras blog teaching how to create a simple image classifier restful API with Flask. I was wondering how to achieve the same approach of loading model in other web frameworks that do not use python. In the code below the model is loaded into memory just before the server starts and it runs until the server is alive:

# if this is the main thread of execution first load the model and
# then start the server
if __name__ == "__main__":
    print(("* Loading Keras model and Flask starting server..."
        "please wait until server has fully started"))
    load_model()
    app.run()

I'm familiar with Pickle and I know how to run python code in other web frameworks (such as python-shell of Node.js). pickled models are built once and can be loaded each time they're needed. but I'm looking to achieve the same thing as the tutorial suggests which is loading it only once, and using it multiple times. Is creating a separate python server app that serves the loaded model to Node.js requests a good idea?

2 Answers

I managed to develop a python "model" server in which the ML model is loaded into memory and is shared through sockets. the consumer app, on the other hand, is a simple Node.js web app that forwards the requests to the python server and retrieves the reply. you can find the code sample here: Keras deep api this is an image classifier app that uses ResNet50 to classify images. images can be uploaded through the Node.js app and are then passed to the python server for classification, the result is then sent back to the Node.js app.

Related