Deploying Azure autoML model locally

120 views Asked by At

I've successfully trained some promising models using Azure AutoML and now I want to deploy them locally.

I used simple CSV files as datasets (using Azure ML v1 APIs) to train the model. Afterward, I downloaded the model and inserted it using the scoring script provided by Microsoft in Conda:

# ---------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# ---------------------------------------------------------
import json
import logging
import os
import pickle
import numpy as np
import pandas as pd
import joblib

import azureml.automl.core
from azureml.automl.core.shared import logging_utilities, log_server
from azureml.telemetry import INSTRUMENTATION_KEY

from inference_schema.schema_decorators import input_schema, output_schema
from inference_schema.parameter_types.numpy_parameter_type import NumpyParameterType
from inference_schema.parameter_types.pandas_parameter_type import PandasParameterType
from inference_schema.parameter_types.standard_py_parameter_type import StandardPythonParameterType

data_sample = PandasParameterType(pd.DataFrame({"SIO2": pd.Series([0.0], dtype="float32"), "AL2O3": pd.Series([0.0], dtype="float32"), "B2O3": pd.Series([0.0], dtype="float32"), "CAO": pd.Series([0.0], dtype="float32"), "K2O": pd.Series([0.0], dtype="float32"), "NA2O": pd.Series([0.0], dtype="float32"), "PBO": pd.Series([0.0], dtype="float32"), "Li2O": pd.Series([0.0], dtype="float32"), "MgO": pd.Series([0.0], dtype="float32"), "SRO": pd.Series([0.0], dtype="float32"), "BAO": pd.Series([0.0], dtype="float32"), "ZNO": pd.Series([0.0], dtype="float32"), "P2O5": pd.Series([0.0], dtype="float32"), "ZRO2": pd.Series([0.0], dtype="float32"), "TIO2": pd.Series([0.0], dtype="float32"), "Bi2O3": pd.Series([0.0], dtype="float32")}))
input_sample = StandardPythonParameterType({'data': data_sample})

result_sample = NumpyParameterType(np.array([0.0]))
output_sample = StandardPythonParameterType({'Results':result_sample})
sample_global_parameters = StandardPythonParameterType(1.0)

try:
    log_server.enable_telemetry(INSTRUMENTATION_KEY)
    log_server.set_verbosity('INFO')
    logger = logging.getLogger('azureml.automl.core.scoring_script_v2')
except:
    pass


def init():
    global model
    # This name is model.id of model that we want to deploy deserialize the model file back
    # into a sklearn model
    model_path = os.path.join(os.getenv('AZUREML_MODEL_DIR'), 'model.pkl')
    path = os.path.normpath(model_path)
    path_split = path.split(os.sep)
    log_server.update_custom_dimensions({'model_name': path_split[-3], 'model_version': path_split[-2]})
    try:
        logger.info("Loading model from path.")
        model = joblib.load(model_path)
        logger.info("Loading successful.")
    except Exception as e:
        logging_utilities.log_traceback(e, logger)
        raise

@input_schema('Inputs', input_sample)
@input_schema('GlobalParameters', sample_global_parameters, convert_to_provided_type=False)
@output_schema(output_sample)
def run(Inputs, GlobalParameters=1.0):
    data = Inputs['data']
    result = model.predict(data)
    return {'Results':result.tolist()}

The environment is set up using the .yml file, but I always get the error message:

---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
Cell In[1], line 12
      9 import pandas as pd
     10 import joblib
---> 12 import azureml.automl.core
     13 from azureml.automl.core.shared import logging_utilities, log_server
     14 from azureml.telemetry import INSTRUMENTATION_KEY

ModuleNotFoundError: No module named 'azureml'

However, I don't believe it's related to packages. When I attempt to deploy the models via a real-time endpoint in Azure and test them using the "Test" tab after successful implementation, I receive the following error message:

Interestingly, I don't get this error message in VS. Instead, when executing the run function there, I only receive the following error message:

Invalid input data type to parse. Expected: <class 'dict'> but got <class 'inference_schema.parameter_types.pandas_parameter_type.PandasParameterType'>
Stapelüberwachung:
 >  File "C:\Users\weightedfinalmodel\scoring_file_v_2_0_0.py", line 59, in <module> (Current frame)
 >    run(data_sample)
"inference_schema.schema_decorators" geladen
"__main__" geladen
"runpy" geladen
Das Programm "python.exe" wurde mit Code 4294967295 (0xffffffff) beendet.

That looks better already, but I seem to be overlooking something.

1

There are 1 answers

0
Emanuel On

Found the solution for following error message. azureml-sdk only supports Python 3.8 and not Python 3.9.

Invalid input data type to parse. Expected: <class 'dict'> but got <class 'inference_schema.parameter_types.pandas_parameter_type.PandasParameterType'>
Stapelüberwachung:
 >  File "C:\Users\weightedfinalmodel\scoring_file_v_2_0_0.py", line 59, in <module> (Current frame)
 >    run(data_sample)
"inference_schema.schema_decorators" geladen
"__main__" geladen
"runpy" geladen
Das Programm "python.exe" wurde mit Code 4294967295 (0xffffffff) beendet.