skip to Main Content

Saving an sklearn model to blob storage from AzureML

Simply put, I have a sklearn model in Azure ML. I want to write the model to a specific blob storage container to save it. I’ve already authenticated the connection to the right blob container. Per docs :

# Create a blob client using the local file name as the name for the blob
blob_client = blob_service_client.get_blob_client(container=container_name, blob=file_name)

# Upload the created file
with open(file=upload_file_path, mode="rb") as data:
    blob_client.upload_blob(data)

However, this requires me to first save the model to a local file. Is there a local file store associated with AzureML?

Alternatively, could I write back to a datastore but point the datastore to a specific storage container?

2

Answers


  1. First, save your model in local as below.

    mlflow.sklearn.save_model(
    sk_model=clf,
    path=os.path.join(os.path.abspath('credit_defaults_model'), "trained_model")
    )
    

    enter image description here

    Here, os.path.abspath('credit_defaults_model') gives path to local filesystem.
    There you can save the model executing above ml code.

    Then you will get files highlighted in image.

    Later, you upload the model.pkl file to blob storage.

    from  azure.storage.blob  import  BlobServiceClient, BlobClient, ContainerClient
    connection_string = "<your connection string>"
    
    container_name = "data"
    local_file_path = os.path.join(os.path.abspath('credit_defaults_model'), "trained_model/model.pkl")
    file_name = "from_ml/sklearn.pkl"
    
    blob_service_client = BlobServiceClient.from_connection_string(connection_string)
    container_client = blob_service_client.get_container_client(container_name)
    blob_client = container_client.get_blob_client(file_name)
    
    
    with  open(local_file_path, "rb") as  data:
        blob_client.upload_blob(data)
    print("File uploaded successfully!")
    

    Output:

    enter image description here

    enter image description here

    Here, model is uploaded successfully to blob.

    Login or Signup to reply.
  2. There’s no need to use a temporary file to upload your model. You can simply pass it directly to the upload_blob function. Data can be a pickle, for example.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search