当多次加载keras“加载模型”时,时间会增加

2024-09-30 08:18:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个pythonrestapi(keras),它有5个模型文件供5个用户使用。当一个请求出现时,会发生以下情况 1检查用户 2加载该特定用户的相关模型 三。执行并输出结果。在

但我的问题是,当请求计数增加时,执行时间也会增加。 debug output in the console

post请求的代码如下(flask app):

my_path = os.path.abspath(os.path.dirname(__file__))
    model_name = "./"+userName+"/model.h5"
    scaler_name = "./"+userName+"/scaler.sc"
    modelPath = os.path.join(my_path, model_name)
    scalerPath = os.path.join(my_path, scaler_name)

    start_time = time.time()
    # load the model
    model = load_model(modelPath)
    scaler = joblib.load(scalerPath)
    print("--- %s seconds ---" % (time.time() - start_time))
    ............

Tags: thepath用户name模型modeltimeos
1条回答
网友
1楼 · 发布于 2024-09-30 08:18:07

Keras不会删除您不再使用的模型,因此仍在使用内存。在

您可以尝试clear_session(),如here所示,或者如果模型相同,但具有不同的权重,则可以尝试重用模型,但替换权重。在

相关问题 更多 >

    热门问题