Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access all registered models when deploying a machine learning model in an Azure Container Instance?

I have built a continuous integration/deployment pipeline in Azure DevOps to train and deploy a machine learning model into a production environment. It uses Azure Machine Learning Services in Python to set everything up i.e. train the model, register it in a machine learning workspace and deploy it as a webservice. One requirement is that I need to use multiple models in the deployed webservice. There is no problem to include the models in the deployed webservice when looking at the workspace from the Azure portal. My problem lies in that I don't know how to access them without knowing the names of the models.

What normally happens looks like this: score.py

from azureml.core.model import Model
from sklearn.externals import joblib
import pandas
def init():
   global model
   model_path = Model.get_model_path('model_name')
   model = joblib.load(model_path)
def run(raw_data):
   data = pandas.DataFrame(json.loads(raw_data)['Inputs'])
   return do_prediction(data) # Use the model to make prediction

Then I also have a python script that creates an image with all needed models and deploys it as a webservice in Azure.

What I would like to use would look something like this (but it gives an error since I can't list the models). score.py

from azureml.core.model import Model
from sklearn.externals import joblib
import pandas
def init():
   model_list = []
   models = Model.list() # Gives an error since no workspace is provided.
   for model in models:
      model_list.append(joblib.load(model.name))
def run(raw_data):
   data = pandas.DataFrame(json.loads(raw_data)['Inputs'])
   return do_prediction(data) # Use the model to make prediction

like image 544
Tomas Bergvall Avatar asked Oct 18 '25 18:10

Tomas Bergvall


1 Answers

My current approach is to navigate the directory structure provided by Azure in the Docker image create by the release pipeline.

    root_dir = './azureml-models'
    for model_name in os.listdir(root_dir):
        for model_version in os.listdir(os.path.join(root_dir, model_name) ):
            model_path = Model.get_model_path(model_name, version = int(model_version))
            model = joblib.load(model_path)

Let me know if you find a better solution.

like image 194
Tomas Bergvall Avatar answered Oct 20 '25 06:10

Tomas Bergvall



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!