How to save and access pickle/hdf5 files in azure machine learning studio

Viewed 130

I have a pickle file parameters.pkl containing some parameters and their values of a model. The pickle file has been created through the following process:

dict={'scaler': scaler,
'features': z_tags,
'Z_reconstruction_loss': Z_reconstruction_loss} 
pickle.dump(dict, open('parameters.pkl', 'wb'))

model_V2.hdf5

I am new to azure machine learning studio.It will be helpful to know, how the pickle file and hdf5 files can be stored in Azure machine Learning Studio and an API endpoint be created, so that the the pickle file can be accessed through API. Objective is to access the pickle file and its contents through API.. I have tried the following:

pip install azureml , azureml-core
from azureml.core import Workspace
from azureml.core.webservice import AciWebservice
from azureml.core.webservice import Webservice
from azureml.core.model import InferenceConfig
from azureml.core.environment import Environment
from azureml.core import Workspace
from azureml.core.model import Model
from azureml.core.conda_dependencies import CondaDependencies


ws = Workspace.create(
               name='myworkspace',            
               subscription_id='<azure-subscription-id>',           
               resource_group='myresourcegroup',                 
               create_resource_group=True,                 
               location='eastus2'                
               )

ws.write_config()

ws = Workspace.from_config()

model = Model.register(workspace = ws,
              model_path ="model/parameters.pkl",
              model_name = "parameters",
              tags = {"version": "1"},
              description = "parameters",
              )


# to install required packages
env = Environment('env')
cd = CondaDependencies.create(pip_packages=['pandas==1.1.5', 'azureml-defaults','joblib==0.17.0'], conda_packages = ['scikit-learn==0.23.2'])
env.python.conda_dependencies = cd
# Register environment to re-use later
env.register(workspace = ws)
print("Registered Environment")

myenv = Environment.get(workspace=ws, name="env")

myenv.save_to_directory('./environ', overwrite=True)

aciconfig = AciWebservice.deploy_configuration(
            cpu_cores=1,
            memory_gb=1,
            tags={"data":"parameters"},
            description='parameters MODEL',
            )

inference_config = InferenceConfig(entry_script="score.py", environment=myenv)

What to modify in following score script, as I don't want to predict anything but access the parameter values stored in the pickle file.

def init():
    global modelmodel_path = Model.get_model_path("parameters")
    print("Model Path is  ", model_path)
    model = joblib.load(model_path)

def run(data):
   try:
     data = json.loads(data)
     result = model.predict(data['data'])
     return {'data' : result.tolist() , 'message' : "Successfully 
            accessed"}
   except Exception as e:
      error = str(e)
      return {'data' : error , 'message' : 'Failed to access'}

Deploy the Model
service = Model.deploy(workspace=ws,
                name='iris-model',
                models=[model],
                inference_config=inference_config,
                deployment_config=aciconfig, 
                overwrite = True)
service.wait_for_deployment(show_output=True)
url = service.scoring_uri
print(url)
1 Answers

We need to deploy the model as the external service and as a web service application to azure machine learning studio.

Follow the below steps to reproduce the work to deploy the model as the web service application to perform API calls.

Note: Before proceeding with the steps, make the models pickle file handy

Open https://studio.azureml.net/

Click on New

enter image description here

Then click on upload from local files

enter image description here

Select the zip file to upload

enter image description here

Under experiments upload the dataset.

enter image description here

Use the following canvas steps to create the model enter image description here enter image description here enter image description here enter image description here enter image description here After mapping all the elements and also executing the python script. Click on set up web service

enter image description here

Click on deploy web service.

enter image description here enter image description here

Related