4

我有一个模型,我用管道注册了它:

register_step = PythonScriptStep(name = "Register Model",
                                source_directory = training_folder,
                                script_name = "register_model.py",
                                arguments = ['--model_folder', model_folder],
                                inputs=[model_folder],
                                compute_target = pipeline_cluster,
                                runconfig = pipeline_run_config,
                                allow_reuse = True)

这是我的 register_model.py:

import argparse
import joblib
from azureml.core import Workspace, Model, Run

# Get parameters
parser = argparse.ArgumentParser()
parser.add_argument('--model_folder', type=str, dest='model_folder', default="model", help='model location')
args = parser.parse_args()
model_folder = args.model_folder

# Get the experiment run context
run = Run.get_context()

# Load the model
print("Loading model from " + model_folder)
model_file = model_folder + "/model.pkl"
model = joblib.load(model_file)

Model.register(workspace=run.experiment.workspace,
               model_path = model_file,
               model_name = 'model',
               tags={'Training context':'Pipeline'})

run.complete() 

当我使用以下方法循环现有模型时,我可以看到模型已注册:

from azureml.core import Model

for model in Model.list(ws):
    print(model.name, 'version:', model.version)
    for tag_name in model.tags:
        tag = model.tags[tag_name]
        print ('\t',tag_name, ':', tag)
    for prop_name in model.properties:
        prop = model.properties[prop_name]
        print ('\t',prop_name, ':', prop)
    print('\n')

但是,当我尝试在我的 Score.py(如下)中加载模型以将模型部署为服务时,我收到以下错误:

我相信这是错误的来源:

model_path = Model.get_model_path(
        model_name="model", version=1)

错误:

ModelNotFoundException                    Traceback (most recent call last)
/anaconda/envs/azureml_py36/lib/python3.6/site-packages/azureml/core/model.py in get_model_path(model_name, version, _workspace)
    751             try:
--> 752                 return Model._get_model_path_local(model_name, version)
    753             except ModelNotFoundException as ee:

/anaconda/envs/azureml_py36/lib/python3.6/site-packages/azureml/core/model.py in _get_model_path_local(model_name, version)
    783         if not os.path.exists(candidate_model_path):
--> 784             return Model._get_model_path_local_from_root(model_name)
    785         else:

/anaconda/envs/azureml_py36/lib/python3.6/site-packages/azureml/core/model.py in _get_model_path_local_from_root(model_name)
    826         raise ModelNotFoundException("Model not found in cache or in root at ./{}. For more info,"
--> 827                                      "set logging level to DEBUG.".format(candidate_model_path))
    828 

ModelNotFoundException: ModelNotFoundException:
    Message: Model not found in cache or in root at ./model. For more info,set logging level to DEBUG.
    InnerException None
    ErrorResponse 
{
    "error": {
        "message": "Model not found in cache or in root at ./model. For more info,set logging level to DEBUG."
    }
}

During handling of the above exception, another exception occurred:

WebserviceException                       Traceback (most recent call last)
<ipython-input-6-27e8df94d66f> in <module>
      1 model_path = Model.get_model_path(
----> 2         model_name="model", version=1)

/anaconda/envs/azureml_py36/lib/python3.6/site-packages/azureml/core/model.py in get_model_path(model_name, version, _workspace)
    756                     module_logger.debug("Getting model from remote")
    757                     return Model._get_model_path_remote(model_name, version, active_workspace)
--> 758                 raise WebserviceException(ee.message, logger=module_logger)
    759         else:
    760             if active_workspace is not None:

WebserviceException: WebserviceException:
    Message: Model not found in cache or in root at ./model. For more info,set logging level to DEBUG.
    InnerException None
    ErrorResponse 
{
    "error": {
        "message": "Model not found in cache or in root at ./model. For more info,set logging level to DEBUG."
    }
}
4

2 回答 2

2
#Assume model_name is the variable containing name of your model
model_name = 'model'
ws = Run.get_context().experiment.workspace
model_obj = Model(ws, model_name )
model_path = model_obj.download(exist_ok = True)
model = joblib.load(model_path)
于 2021-05-26T05:38:15.560 回答
0

对不起这个麻烦。我不确定在您的具体情况下发生了什么,但您可以同时尝试使用 AZUREML_MODEL_DIR 吗?https://docs.microsoft.com/en-us/azure/machine-learning/how-to-deploy-advanced-entry-script#load-registered-models

于 2020-09-17T20:30:13.497 回答