Correctly loading Keras model in Django that supports multi-tenancy

扶醉桌前 提交于 2019-12-03 15:23:18

Look here please https://github.com/keras-team/keras/issues/2397#issuecomment-254919212

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()

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)

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!