如何使用MLlfow在Azure ML中加载已记录/已保存的模型?

iezvtpos  于 2023-05-29  发布在  其他
关注(0)|答案(1)|浏览(138)

我想通过AZURE ML在线端点部署经过训练的ML模型。
我已经在工作区中注册了我的模型。
现在,当我尝试使用cutome score.py为mlflow.pyfunc.load_model()加载模型时,出现以下错误
这是我的代码-

model_path = os.path.join(os.getenv("AZUREML_MODEL_DIR"), "use-case1-model")
model = mlflow.pyfunc.load_model(model_path)

score.py

import logging
import os
import json
import mlflow
from io import StringIO
from mlflow.pyfunc.scoring_server import infer_and_parse_json_input, predictions_to_json
import sys
from time import strftime, localtime
from collections import Counter
from pytorch_transformers import BertTokenizer
import random
import numpy as np 
import torch 
from tqdm import tqdm

def init():
    global model
    # "model" is the path of the mlflow artifacts when the model was registered. For automl
    # models, this is generally "mlflow-model".
    model_path = os.path.join(os.getenv("AZUREML_MODEL_DIR"), "use-case1-model")
    model = mlflow.pyfunc.load_model(model_path)
    logging.info("Init complete")

def run(raw_data):
    data = json.loads(raw_data)
    title = json.dumps(data["title"])
    att = json.dumps(data["attributes"])

    output = model.predict([tensor_t,tensor_a])

    predict_list = output.tolist()[0]
    
    result = StringIO()
    predictions_to_json(predict_list,result)
    return result.getvalue()

我得到的错误-

File "/azureml-envs/azureml_9a3b1e0a66d72d612aebc12b4a285f72/lib/python3.9/site-packages/azureml_inference_server_http/server/user_script.py", line 117, in invoke_init
    self._user_init()
  File "/var/azureml-app/dependencies/score.py", line 21, in init
    model = mlflow.pyfunc.load_model(model_path)
  File "/azureml-envs/azureml_9a3b1e0a66d72d612aebc12b4a285f72/lib/python3.9/site-packages/mlflow/pyfunc/__init__.py", line 735, in load_model
    model_impl = importlib.import_module(conf[MAIN])._load_pyfunc(data_path)
  File "/azureml-envs/azureml_9a3b1e0a66d72d612aebc12b4a285f72/lib/python3.9/site-packages/mlflow/pytorch/__init__.py", line 735, in _load_pyfunc
    return _PyTorchWrapper(_load_model(path, **kwargs))
  File "/azureml-envs/azureml_9a3b1e0a66d72d612aebc12b4a285f72/lib/python3.9/site-packages/mlflow/pytorch/__init__.py", line 643, in _load_model
    return torch.load(model_path, **kwargs)
  File "/azureml-envs/azureml_9a3b1e0a66d72d612aebc12b4a285f72/lib/python3.9/site-packages/torch/serialization.py", line 809, in load
    return _load(opened_zipfile, map_location, pickle_module, **pickle_load_args)
  File "/azureml-envs/azureml_9a3b1e0a66d72d612aebc12b4a285f72/lib/python3.9/site-packages/torch/serialization.py", line 1172, in _load
    result = unpickler.load()
  File "/azureml-envs/azureml_9a3b1e0a66d72d612aebc12b4a285f72/lib/python3.9/site-packages/torch/serialization.py", line 1142, in persistent_load
    typed_storage = load_tensor(dtype, nbytes, key, _maybe_decode_ascii(location))
  File "/azureml-envs/azureml_9a3b1e0a66d72d612aebc12b4a285f72/lib/python3.9/site-packages/torch/serialization.py", line 1116, in load_tensor
    wrap_storage=restore_location(storage, location),
  File "/azureml-envs/azureml_9a3b1e0a66d72d612aebc12b4a285f72/lib/python3.9/site-packages/torch/serialization.py", line 217, in default_restore_location
    result = fn(storage, location)
  File "/azureml-envs/azureml_9a3b1e0a66d72d612aebc12b4a285f72/lib/python3.9/site-packages/torch/serialization.py", line 182, in _cuda_deserialize
    device = validate_cuda_device(location)
  File "/azureml-envs/azureml_9a3b1e0a66d72d612aebc12b4a285f72/lib/python3.9/site-packages/torch/serialization.py", line 166, in validate_cuda_device
    raise RuntimeError('Attempting to deserialize object on a CUDA '
RuntimeError: Attempting to deserialize object on a CUDA device but torch.cuda.is_available() is False. If you are running on a CPU-only machine, please use torch.load with map_location=torch.device('cpu') to map your storages to the CPU.

如何以及在何处更新map_location=torch.device('cpu')?mlflow.pyfunc.load_model()没有访问map_location的参数,并且由于包安装在docker中,因此无法更改serilaization.py

ddarikpa

ddarikpa1#

根据错误日志,您试图在CUDA设备上反序列化对象,但torch.cuda.is_available()返回False,这是由于在仅CPU的机器上运行。要解决此问题,您需要更新torch.load函数,以指定map_location=torch.device('cpu')将存储Map到CPU。
由于mlflow.pyfunc.load_model()函数没有map_location参数,因此可以使用**kwargs参数,该参数可以将任何其他关键字参数传递给torch.load()函数。
要解决此问题,请在score.py文件中添加*{'map_location': torch.device('cpu')}

model = mlflow.pyfunc.load_model(model_path, *{'map_location': torch.device('cpu')})

示例:

import  mlflow
import  torch
path="./deploy/credit_defaults_model/"
model = mlflow.pyfunc.load_model(path, *{'map_location': torch.device('cpu')})

相关问题