Is there any other way to save and load keras models?

Viewed 643

I was deploying a tf.keras model to a django app on shared hosting. I have saved it as a .h5 file and it works. Here is the problem, when it loads on the browser for the fist time it takes too much time to import tensorflow and gives a 403 error. It only works after that. Is there any other way to load the model faster? Thanks in advance for your help.

1 Answers

I have also encountered the same issue before. One thing you can do to mitigate this issue is to import tensorflow and load the saved models globally instead of within your view function. By doing so tensorflow and the model gets loaded initially when you start your server, instead of every time you hit the route, which had significantly reduced the reponse time for me previously. Within your view function you can just use the model to get the predictions.

Example

Instead of importing tensorflow and loading the model within the view function

def my_view(request):
    import tensorflow as tf

    model = tf.keras.models.load_model("my_h5_model.h5")

    predictions = model.predict(test_data)

    return HTTPResponse(
        json.dumps({"predictions": predictions}),
        content_type="application/json"
        )

Move them globally

import tensorflow as tf

# Loading the model
model = tf.keras.models.load_model("my_h5_model.h5")

def my_view(request):

    predictions = model.predict(test_data)

    return HTTPResponse(
        json.dumps({"predictions": predictions}),
        content_type="application/json"
        )
Related