在Django中正确加载支持多租户的Keras模型 [英] Correctly loading Keras model in Django that supports multi-tenancy

查看:217
本文介绍了在Django中正确加载支持多租户的Keras模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在Django中编写一个REST API,该API使用Keras模型返回预测.但是load_model()函数需要一些时间来加载模型,并且我不希望我的用户等待这么长时间(每次初始化模型).初始化模型以便一次加载并使用同一模型进行预测的正确方法是什么?

I am try to write a REST api in django that uses a Keras model to return a prediction. However the load_model() function takes some time to load the model and I don't want my users to have to wait so long (each time the model is initialized). What would be the correct way to initialize the model so that is is loaded once and the predictions are done using that same model?

顺便说一句,我认为可以冷的一种方法是在settings.py中初始化模型,如下所示:

On a side note one method that I thought cold be possible was to initialize the model in settings.py as below :

settings.py

json_file=open("model.json","r")
loaded_json=json_file.read()
json_file.close()

model=model_from_json(loaded_json)
model.load_weights("model.h5")
MODEL=model

然后在我的views.py中,我将此变量MODEL用作:

And then in my views.py I use this variable MODEL as :

views.py

from django.conf import settings
model=settings.MODEL
def index():
    print "Predicting"
    res=model.predict(numpy.stack([test_img]))
    print res

如果一次仅一个用户处于活动状态(模型初始化一次,并且使用该模型完成所有后续预测),则此方法非常有用.但是,如果一次有多个用户处于活动状态,则对于先到的呼叫来说效果很好,但后一个呼叫会出现错误

This works great if only one user is active at a time ( model is initialized once and all subsequent predictions are done using that model). However if multiple users are active at a time then it works good for the call that came first but the latter call gives the error

'NoneType' object has no attribute 'shape'
Apply node that caused the error: ConvOp{('imshp', (31, 31, 32)),('kshp', (3, 3)),('nkern', 64),('bsize', None),('dx', 1),('dy', 1),('out_mode', 'valid'),('unroll_batch', None),('unroll_kern', None),('unroll_patch', True),('imshp_logical', (31, 31, 32)),('kshp_logical', (3, 3)),('kshp_logical_top_aligned', True)}(InplaceDimShuffle{0,2,3,1}.0, InplaceDimShuffle{3,2,0,1}.0)
Toposort index: 13
Inputs types: [TensorType(float32, 4D), TensorType(float32, 4D)]
Inputs shapes: [(1L, 31L, 31L, 32L), 'No shapes']
Inputs strides: [(123008L, 124L, 4L, 3844L), 'No strides']
Inputs values: ['not shown', None]
Outputs clients: [[Elemwise{Composite{(i0 * ((i1 + i2) + Abs((i1 + i2))))}}[(0, 1)](TensorConstant{(1L, 1L, 1..1L) of 0.5}, ConvOp{('imshp', (31, 31, 32)),('kshp', (3, 3)),('nkern', 64),('bsize', None),('dx', 1),('dy', 1),('out_mode', 'valid'),('unroll_batch', None),('unroll_kern', None),('unroll_patch', True),('imshp_logical', (31, 31, 32)),('kshp_logical', (3, 3)),('kshp_logical_top_aligned', True)}.0, InplaceDimShuffle{x,0,x,x}.0)]]

我应该如何正确加载模型,以便可以同时访问它?

How should i load the model properly so that it can be accessed simultaneously?

谢谢您的时间.

推荐答案

请在此处查看例如在Django设置中构建模型...

eg. in Django settings construct the model...

modelFile = 'path_to_my_model.h5'    
pipe = joblib.load(modelFile.replace('.h5','.pkl'))
model = models.load_model(modelFile)
pipe.steps.append(('nn', model))    
graph = tensorflow.get_default_graph()

,然后在Django REST方法中像这样重用:

and then reuse like this in Django REST method:

import myDjango.settings as sett
# ...

@csrf_exempt
def evaluate(request):
    """
    Do the evaluation.
    """
    if request.method == 'POST':
        data = JSONParser().parse(request)
        i = data['inputs']

        outputs = MyMlClass.PredictArray( sett.graph, sett.pipe , i, 'model.h5' )

        return JsonResponse(outputs, status=201, safe=False)

非常适合我(VisualStudio Django项目,Python 3.6).不建议在REST处理程序中构建模型,并且实际上是行不通的-它仅在第一次调用时就可以工作.

Works for me very well (VisualStudio Django project, Python 3.6). Construction of the model in REST handler is not recommended and in fact won't work - it will work just in the very first invocation.

这篇关于在Django中正确加载支持多租户的Keras模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆