Serving Keras Model Using Django

Viewed 1215

I have a pre-trained keras model (in .hdf5 form) that I am planning to deploy using a Django Web-App. In my views file it is called upon when a certain POST request is passed in. I have stored in .hdf5 model in the static folder associated with my web-app. However, when i call keras.models.load_model to try and load the model from the static directory the app returns the following error:

Unable to open file (unable to open file: name = '/static/model.hdf5', errno = 2, error message = 'No such file or directory', flags = 0, o_flags = 0)

However, the model is clearly in my static folder so I'm not sure why the load_model function is not working to load the model.

If there is some reason for this or a workaround I am not sure of it so any information regarding a solution would be helpful. I am also willing to switch to a tensorflow serving model or otherwise if this is a keras issue/it would be beneficial.

What is the reason I am not able to load my keras model from my Django app and how can I remedy the situation?

1 Answers

The static directory in Django is for serving media and other static content to users, like images or stylesheets. If you want to access a file using an internal script, then the normal Python rules for accessing files apply. Just put the model somewhere in your source tree that will be accessible to you. For example, if the model were in the same place as your views.py, this would work:

def my_view(request):
    if request.POST.get['foo', ''] == 'do_something':
        keras.models.load_model('model.hdf5')

Incidentally, unless I'm missing something, you probably don't want to put your model inside the static folder at all; this could lead the model itself being accessible to anyone.

Related