推理脚本,单独的负载模型和预测



我有一个推理脚本,其中负载模型和预测在一个脚本中,如何将它们分开,这样我就不必每次都必须进行预测时加载模型

from keras.models import load_model
# load model
model = load_model('model.h5')
# prediction
result = model.predict(image)

将模型保留为全局变量,并在此模块中将预测作为函数。

from keras.models import load_model
model = None
# load model
def load_model_weights():
model = load_model('model.h5')
def predict(image):
if(model is None):
load_model_weights()
else:
# prediction
result = model.predict(image)
return result

从此模块导入函数 predict,并在其他模块中使用它。

最新更新