简体   繁体   English

使用Django服务Keras模型

[英]Serving Keras Model Using Django

I have a pre-trained keras model (in .hdf5 form) that I am planning to deploy using a Django Web-App. 我有一个预先训练的keras模型(.hdf5格式),我打算使用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. 在我的视图文件中,当传入某个POST请求时会调用它。我已将.hdf5模型存储在与Web应用程序关联的静态文件夹中。 However, when i call keras.models.load_model to try and load the model from the static directory the app returns the following error: 但是,当我调用keras.models.load_model尝试从静态目录中加载模型时,该应用会返回以下错误:

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. 但是,模型显然位于我的静态文件夹中,因此我不确定为什么load_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. 我也愿意切换到张量流服务模型,或者如果这是一个keras问题,那将是有益的。

What is the reason I am not able to load my keras model from my Django app and how can I remedy the situation? 我无法从Django应用程序加载keras模型的原因是什么,我该如何解决这种情况?

The static directory in Django is for serving media and other static content to users, like images or stylesheets. Django中的static目录用于向用户提供媒体和其他静态内容,例如图像或样式表。 If you want to access a file using an internal script, then the normal Python rules for accessing files apply. 如果要使用内部脚本访问文件,则适用用于访问文件的常规Python规则。 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: 例如,如果模型与您的views.py位于同一位置,则可以使用:

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. 这可能导致任何人都可以访问模型本身。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM