Export/Import assets with ibm-watsonx-ai¶

This notebook demonstrates an example for exporting/importing assets using Watson Machine Learning service. It contains steps and code to work with ibm-watsonx-ai library available in PyPI repository.

Learning goals¶

The learning goals of this notebook are:

  • Download an externally trained Keras model.
  • Persist an external model in Watson Machine Learning repository.
  • Export the model from the space
  • Import the model to another space and deploy

Contents¶

This notebook contains the following parts:

  1. Setup
  2. Download externally created Keras model
  3. Persist externally created Keras model
  4. Export the model
  5. Import the model
  6. Deploy and score the imported model
  7. Clean up
  8. Summary and next steps

1. Set up the environment¶

Before you use the sample code in this notebook, you must perform the following setup tasks:

  • Create a Watson Machine Learning (WML) Service instance (a free plan is offered and information about how to create the instance can be found here).

Install and import the ibm-watsonx-ai and dependecies¶

Note: ibm-watsonx-ai documentation can be found here.

In [ ]:
!pip install wget | tail -n 1
!pip install -U ibm-watsonx-ai | tail -n 1

Connection to WML¶

Authenticate the Watson Machine Learning service on IBM Cloud. You need to provide platform api_key and instance location.

You can use IBM Cloud CLI to retrieve platform API Key and instance location.

API Key can be generated in the following way:

ibmcloud login
ibmcloud iam api-key-create API_KEY_NAME

In result, get the value of api_key from the output.

Location of your WML instance can be retrieved in the following way:

ibmcloud login --apikey API_KEY -a https://cloud.ibm.com
ibmcloud resource service-instance WML_INSTANCE_NAME

In result, get the value of location from the output.

Tip: Your Cloud API key can be generated by going to the Users section of the Cloud console. From that page, click your name, scroll down to the API Keys section, and click Create an IBM Cloud API key. Give your key a name and click Create, then copy the created key and paste it below. You can also get a service specific url by going to the Endpoint URLs section of the Watson Machine Learning docs. You can check your instance location in your Watson Machine Learning (WML) Service instance details.

You can also get service specific apikey by going to the Service IDs section of the Cloud Console. From that page, click Create, then copy the created key and paste it below.

Action: Enter your api_key and location in the following cell.

In [35]:
api_key = 'PASTE YOUR PLATFORM API KEY HERE'
location = 'PASTE YOUR INSTANCE LOCATION HERE'
In [1]:
from ibm_watsonx_ai import Credentials

credentials = Credentials(
    api_key=api_key,
    url='https://' + location + '.ml.cloud.ibm.com'
)
In [2]:
from ibm_watsonx_ai import APIClient

client = APIClient(credentials)

Create two spaces. One for export and one for import¶

Tip: You can refer to details and example for space management apis here

In [3]:
# Refer to the link in above Tip how to find the cos and instance crns
cos_resource_crn = "PUT_YOUR_COS_CRN"
instance_crn = "PUT_YOUR_WML_INSTANCE_CRN"
name = "PUT_YOUR_WML_INSTANCE_NAME_HERE"
In [4]:
import uuid

space_name = str(uuid.uuid4())


export_space_metadata = {
              client.spaces.ConfigurationMetaNames.NAME: "client_space_export_" + space_name,
              client.spaces.ConfigurationMetaNames.DESCRIPTION: space_name + " description",
              client.spaces.ConfigurationMetaNames.STORAGE: { "resource_crn": cos_resource_crn},
              client.spaces.ConfigurationMetaNames.COMPUTE: {
                         "name": name,
                         "crn": instance_crn
              }
}

space = client.spaces.store(meta_props=export_space_metadata)

export_space_id = client.spaces.get_id(space)
print("{}export space_id: {}{}".format('\n', export_space_id, '\n'))


import_space_metadata = {
              client.spaces.ConfigurationMetaNames.NAME: "client_space_import_" + space_name,
              client.spaces.ConfigurationMetaNames.DESCRIPTION: space_name +  "description",
              client.spaces.ConfigurationMetaNames.STORAGE: {"resource_crn": cos_resource_crn},
              client.spaces.ConfigurationMetaNames.COMPUTE: {
                         "name": name,
                         "crn": instance_crn
              }
}

space = client.spaces.store(meta_props=import_space_metadata)

import_space_id = client.spaces.get_id(space)
print("{}import space_id: {}".format('\n', import_space_id))
Space has been created. However some background setup activities might still be on-going. Check for 'status' field in the response. It has to show 'active' before space can be used. If it's not 'active', you can monitor the state with a call to spaces.get_details(space_id). Alternatively, use background_mode=False when calling client.spaces.store().

export space_id: 63882722-7f0c-4774-a6db-5abd60db879b

Space has been created. However some background setup activities might still be on-going. Check for 'status' field in the response. It has to show 'active' before space can be used. If it's not 'active', you can monitor the state with a call to spaces.get_details(space_id). Alternatively, use background_mode=False when calling client.spaces.store().

import space_id: 5d7af2cc-4f8a-4262-ad3c-82916aa2d147

2. Download externally created Keras model and data¶

In this section, you will download externally created Keras models and data used for training it.

In [5]:
import os
import wget

data_dir = 'MNIST_DATA'
if not os.path.isdir(data_dir):
    os.mkdir(data_dir)
    
model_path = os.path.join(data_dir, 'mnist_keras.h5.tgz')
if not os.path.isfile(model_path):
    wget.download("https://github.com/IBM/watson-machine-learning-samples/raw/master/cloud/models/keras/mnist_keras.h5.tgz", out=data_dir)
In [6]:
import os
import wget

data_dir = 'MNIST_DATA'
if not os.path.isdir(data_dir):
    os.mkdir(data_dir)
    
filename = os.path.join(data_dir, 'mnist.npz')
if not os.path.isfile(filename):
    wget.download('https://s3.amazonaws.com/img-datasets/mnist.npz', out=data_dir)  
In [7]:
import numpy as np

dataset = np.load(filename)
x_test = dataset['x_test']

3. Persist externally created Keras model¶

In this section, you will learn how to store your model in Watson Machine Learning repository by using the Watson Machine Learning Client.

3.1: Publish model¶

Define model name, type and software specification needed to deploy model later.

In [8]:
sofware_spec_id = client.software_specifications.get_id_by_name("tensorflow_rt24.1-py3.11")
In [10]:
client.set.default_space(export_space_id)

metadata = {
            client.repository.ModelMetaNames.NAME: 'External Keras model',
            client.repository.ModelMetaNames.TYPE: 'tensorflow_2.14',
            client.repository.ModelMetaNames.SOFTWARE_SPEC_ID: sofware_spec_id
}

published_model = client.repository.store_model(
    model=model_path,
    meta_props=metadata)

3.2: Get model details¶

In [12]:
import json

published_model_id = client.repository.get_model_id(published_model)
model_details = client.repository.get_details(published_model_id)
print(json.dumps(model_details, indent=2))
{
  "entity": {
    "hybrid_pipeline_software_specs": [],
    "software_spec": {
      "id": "079a91e0-245f-5269-8926-3c20b28f37dc",
      "name": "tensorflow_rt23.1-py3.10"
    },
    "type": "tensorflow_2.12"
  },
  "metadata": {
    "created_at": "2024-03-07T09:23:04.528Z",
    "id": "6f7f77ef-30fd-47cf-bbe3-b868b83e01b3",
    "modified_at": "2024-03-07T09:23:20.169Z",
    "name": "External Keras model",
    "owner": "IBMid-55000091VC",
    "resource_key": "c05a75c7-c730-4907-b9b3-e2e530da1f8a",
    "space_id": "63882722-7f0c-4774-a6db-5abd60db879b"
  },
  "system": {
    "warnings": []
  }
}

3.3 Get all models in the space¶

space_id is automatically picked up from client.set.default_space() api call before

In [13]:
models_details = client.repository.list_models()
------------------------------------  --------------------  ------------------------  ---------------  ----------  ----------------
ID                                    NAME                  CREATED                   TYPE             SPEC_STATE  SPEC_REPLACEMENT
6f7f77ef-30fd-47cf-bbe3-b868b83e01b3  External Keras model  2024-03-07T09:23:04.002Z  tensorflow_2.12  supported
------------------------------------  --------------------  ------------------------  ---------------  ----------  ----------------

4. Export¶

In [14]:
help(client.export_assets.start)
Help on method start in module ibm_watson_machine_learning.export_assets:

start(meta_props, space_id=None, project_id=None) method of ibm_watson_machine_learning.export_assets.Export instance
    Start the export. Either space_id or project_id has to be provided and is mandatory.
    ALL_ASSETS is by default False. No need to provide explicitly unless it has to be set to True.
    Either ALL_ASSETS or ASSET_TYPES or ASSET_IDS has to be given in the meta_props. Only one of these can be
    provided.
    
    In the `meta_props`:
    
    ALL_ASSETS is a boolean. When set to True, it exports all assets in the given space.
    ASSET_IDS is an array containing the list of assets ids to be exported.
    ASSET_TYPES is for providing the asset types to be exported. All assets of that asset type will be exported.
    
            Eg: wml_model, wml_model_definition, wml_pipeline, wml_function, wml_experiment,
            software_specification, hardware_specification, package_extension, script
    
    :param meta_props: meta data,
        to see available meta names use ``client.export_assets.ConfigurationMetaNames.get()``
    :type meta_props: dict
    :param space_id: space identifier
    :type space_id: str, optional
    :param project_id: project identifier
    :type project: str, optional
    
    :return: Response json
    :rtype: dict
    
    **Example**
    
    .. code-block:: python
    
        metadata = {
            client.export_assets.ConfigurationMetaNames.NAME: "export_model",
            client.export_assets.ConfigurationMetaNames.ASSET_IDS: ["13a53931-a8c0-4c2f-8319-c793155e7517",
                                                                    "13a53931-a8c0-4c2f-8319-c793155e7518"]}
    
        details = client.export_assets.start(meta_props=metadata, space_id="98a53931-a8c0-4c2f-8319-c793155e4598")
    
    .. code-block:: python
    
        metadata = {
            client.export_assets.ConfigurationMetaNames.NAME: "export_model",
            client.export_assets.ConfigurationMetaNames.ASSET_TYPES: ["wml_model"]}
    
        details = client.export_assets.start(meta_props=metadata, space_id="98a53931-a8c0-4c2f-8319-c793155e4598")
    
    .. code-block:: python
    
        metadata = {
            client.export_assets.ConfigurationMetaNames.NAME: "export_model",
            client.export_assets.ConfigurationMetaNames.ALL_ASSETS: True}
    
        details = client.export_assets.start(meta_props=metadata, space_id="98a53931-a8c0-4c2f-8319-c793155e4598")

client.export_assets has these apis. For any help on these apis, type 'help(api_name)' in your notebook Example: help(client.export_assets.start), help(client.export_assets.get_details)

  1. client.export_assets.start: This starts the export job. export job is asynchronously executed
  2. client.export_assets.get_details: Given export_id and corresponding space_id/project_id, this gives the export job details. Usually used for monitoring the export job submitted with start api
  3. client.export_assets.list: Prints summary of all the export jobs
  4. client.export_assets.get_exported_content: Downloads the exported content. This information will be used by the import process
  5. client.export_assets.delete: Deletes the given export job
  6. client.export_assets.cancel: Cancels the given export job if running

4.1: Start the export process¶

Start the export process for the model created. Either ASSET_IDS or ASSET_TYPES or ALL_ASSETS can be provided. If you have more than one model ids, you need to provide them as array like client.export_assets.ConfigurationMetaNames.ASSET_IDS: [model_id1, model_id2] Refer to the help api above to see different usages and details

In [15]:
metadata = { client.export_assets.ConfigurationMetaNames.NAME: "export_model",
             client.export_assets.ConfigurationMetaNames.ASSET_IDS: [published_model_id]                                                                         
           }

details = client.export_assets.start(meta_props=metadata, space_id=export_space_id)
print(json.dumps(details, indent=2))

export_job_id = details[u'metadata'][u'id']
export job with id 93716065-c088-4132-b9cf-35e4c700fcb2 has started. Monitor status using client.export_assets.get_details api. Check 'help(client.export_assets.get_details)' for details on the api usage
{
  "entity": {
    "assets": {
      "all_assets": false,
      "asset_ids": [
        "6f7f77ef-30fd-47cf-bbe3-b868b83e01b3"
      ]
    },
    "format": "json",
    "status": {
      "state": "pending"
    }
  },
  "metadata": {
    "created_at": "2024-03-07T09:23:43.943Z",
    "creator_id": "IBMid-55000091VC",
    "id": "93716065-c088-4132-b9cf-35e4c700fcb2",
    "name": "export_model",
    "space_id": "63882722-7f0c-4774-a6db-5abd60db879b",
    "url": "/v2/asset_exports/93716065-c088-4132-b9cf-35e4c700fcb2"
  }
}

4.2: Monitor the export process¶

In [16]:
import time

start_time = time.time()
diff_time = start_time - start_time
while True and diff_time < 10 * 60:
    time.sleep(3)
    response = client.export_assets.get_details(export_job_id, space_id=export_space_id)
    state = response[u'entity'][u'status'][u'state']
    print(state)
    if state == 'completed' or state == 'error' or state == 'failed':
        break
    diff_time = time.time() - start_time

print(json.dumps(response, indent=2))
completed
{
  "entity": {
    "assets": {
      "all_assets": false,
      "asset_ids": [
        "6f7f77ef-30fd-47cf-bbe3-b868b83e01b3"
      ]
    },
    "format": "json",
    "status": {
      "state": "completed"
    }
  },
  "metadata": {
    "created_at": "2024-03-07T09:23:43.943Z",
    "creator_id": "IBMid-55000091VC",
    "id": "93716065-c088-4132-b9cf-35e4c700fcb2",
    "name": "export_model",
    "space_id": "63882722-7f0c-4774-a6db-5abd60db879b",
    "updated_at": "2024-03-07T09:23:45.384Z",
    "url": "/v2/asset_exports/93716065-c088-4132-b9cf-35e4c700fcb2"
  }
}

4.3: Get the exported content¶

In [17]:
export_dir = 'EXPORT_DATA'

if not os.path.isdir(export_dir):
    os.mkdir(export_dir)
    
export_file_name = 'exported_content_' + str(uuid.uuid4()) + '.zip'
    
export_file_path = os.path.join(export_dir, export_file_name)

details = client.export_assets.get_exported_content(export_job_id, 
                                                    space_id = export_space_id, 
                                                    file_path = export_file_path)

print(details)
Successfully saved export content to file: 'EXPORT_DATA/exported_content_14bc565c-ba87-4606-87c4-ccac7c26aeda.zip'
EXPORT_DATA/exported_content_14bc565c-ba87-4606-87c4-ccac7c26aeda.zip

5. Import¶

client.import_assets has these apis. For any help on these apis, type 'help(api_name)' in your notebook Example: help(client.import_assets.start), help(client.import_assets.get_details)

  1. client.import_assets.start: This starts the import job. import job is asynchronously executed
  2. client.import_assets.get_details: Given import_id and corresponding space_id/project_id, this gives the import job details. Usually used for monitoring the import job submitted with start api
  3. client.import_assets.list: Prints summary of all the import jobs
  4. client.import_assets.delete: Deletes the given import job
  5. client.import_assets.cancel: Cancels the given import job if running

5.1: Start the import process¶

In [18]:
details = client.import_assets.start(file_path=export_file_path,
                                     space_id=import_space_id)
print(json.dumps(details, indent=2))

import_job_id = details[u'metadata'][u'id']
import job with id 7bb20176-1566-41dd-bf5e-04212e4527be has started. Monitor status using client.import_assets.get_details api. Check 'help(client.import_assets.get_details)' for details on the api usage
{
  "entity": {
    "format": "json",
    "status": {
      "state": "pending"
    }
  },
  "metadata": {
    "created_at": "2024-03-07T09:24:01.357Z",
    "creator_id": "IBMid-55000091VC",
    "id": "7bb20176-1566-41dd-bf5e-04212e4527be",
    "space_id": "5d7af2cc-4f8a-4262-ad3c-82916aa2d147",
    "url": "/v2/asset_imports/7bb20176-1566-41dd-bf5e-04212e4527be"
  }
}

5.2: Monitor the import process¶

In [19]:
import time

start_time = time.time()
diff_time = start_time - start_time
while True and diff_time < 10 * 60:
    time.sleep(3)
    response = client.import_assets.get_details(import_job_id,
                                                space_id=import_space_id)
    state = response[u'entity'][u'status'][u'state']
    print(state)
    if state == 'completed' or state == 'error' or state == 'failed':
         break
    diff_time = time.time() - start_time

print(json.dumps(response, indent=2))

client.set.default_space(import_space_id)

print("{}List of models: {}".format('\n', '\n'))
client.repository.list_models()
details = client.repository.get_model_details()

for obj in details[u'resources']:
    if obj[u'metadata'][u'name'] == "External Keras model":
        model_id_for_deployment = obj[u'metadata'][u'id']

print("{}model id for deployment: {}".format('\n', model_id_for_deployment))
completed
{
  "entity": {
    "format": "json",
    "status": {
      "state": "completed"
    }
  },
  "metadata": {
    "created_at": "2024-03-07T09:24:01.357Z",
    "creator_id": "IBMid-55000091VC",
    "id": "7bb20176-1566-41dd-bf5e-04212e4527be",
    "space_id": "5d7af2cc-4f8a-4262-ad3c-82916aa2d147",
    "updated_at": "2024-03-07T09:24:04.658Z",
    "url": "/v2/asset_imports/7bb20176-1566-41dd-bf5e-04212e4527be"
  }
}

List of models: 

------------------------------------  --------------------  ------------------------  ---------------  ----------  ----------------
ID                                    NAME                  CREATED                   TYPE             SPEC_STATE  SPEC_REPLACEMENT
ca321e98-3bcc-4f3b-8619-110438da3c98  External Keras model  2024-03-07T09:24:03.002Z  tensorflow_2.12  supported
------------------------------------  --------------------  ------------------------  ---------------  ----------  ----------------

model id for deployment: ca321e98-3bcc-4f3b-8619-110438da3c98

List the import and export jobs

In [20]:
print("Export jobs: \n")
client.export_assets.list(space_id=export_space_id)
print("\nImport jobs:")
client.import_assets.list(space_id=import_space_id)
Export jobs: 

------------------------------------  ------------  ------------------------  ---------
ID                                    NAME          CREATED                   STATUS
93716065-c088-4132-b9cf-35e4c700fcb2  export_model  2024-03-07T09:23:43.943Z  completed
------------------------------------  ------------  ------------------------  ---------

Import jobs:
------------------------------------  ------------------------  ---------
ID                                    CREATED                   STATUS
7bb20176-1566-41dd-bf5e-04212e4527be  2024-03-07T09:24:01.357Z  completed
------------------------------------  ------------------------  ---------
Out[20]:
ID CREATED STATUS
0 7bb20176-1566-41dd-bf5e-04212e4527be 2024-03-07T09:24:01.357Z completed

6. Deploy and score the imported model¶

6.1: Create model deployment¶

Create online deployment for published model¶

In [21]:
metadata = {
    client.deployments.ConfigurationMetaNames.NAME: "Deployment of external Keras model",
    client.deployments.ConfigurationMetaNames.ONLINE: {}
}

created_deployment = client.deployments.create(model_id_for_deployment, meta_props=metadata)

#######################################################################################

Synchronous deployment creation for uid: 'ca321e98-3bcc-4f3b-8619-110438da3c98' started

#######################################################################################


initializing
Note: online_url and serving_urls are deprecated and will be removed in a future release. Use inference instead.
...
ready


------------------------------------------------------------------------------------------------
Successfully finished deployment creation, deployment_uid='60545f0f-5d53-440c-b04f-3ff4538041ab'
------------------------------------------------------------------------------------------------


In [22]:
deployment_id = client.deployments.get_id(created_deployment)

Now you can print an online scoring endpoint.

In [ ]:
scoring_endpoint = client.deployments.get_scoring_href(created_deployment)
print(scoring_endpoint)

You can also list existing deployments.

In [ ]:
client.deployments.list()

6.2: Get deployment details¶

In [ ]:
details = client.deployments.get_details(deployment_id)
print(json.dumps(details, indent=2))

6.3: Score¶

You can use below method to do test scoring request against deployed model.

Let's first visualize two samples from dataset, we'll use for scoring. You must have matplotlib package installed

In [26]:
%matplotlib inline
import matplotlib.pyplot as plt
In [27]:
for i, image in enumerate([x_test[0], x_test[1]]):
    plt.subplot(2, 2, i + 1)
    plt.axis('off')
    plt.imshow(image, cmap=plt.cm.gray_r, interpolation='nearest')
No description has been provided for this image

Prepare scoring payload with records to score.

In [28]:
score_0 = (x_test[0].ravel() / 255).tolist()
score_1 = (x_test[1].ravel() / 255).tolist()
In [29]:
scoring_payload = {"input_data": [{"values": [score_0, score_1]}]}

Use client.deployments.score() method to run scoring.

In [30]:
predictions = client.deployments.score(deployment_id, scoring_payload)
In [31]:
print(json.dumps(predictions, indent=2))
{
  "predictions": [
    {
      "id": "dense_2",
      "fields": [
        "prediction",
        "prediction_classes",
        "probability"
      ],
      "values": [
        [
          [
            1.7066226903894588e-12,
            2.6338506375900295e-10,
            2.5274502490901796e-08,
            2.2722660730778443e-07,
            6.751365417244359e-13,
            1.0386839999210196e-10,
            4.863354148457343e-17,
            0.9999995231628418,
            1.871915816797909e-11,
            2.220721739831788e-07
          ],
          7,
          [
            1.7066226903894588e-12,
            2.6338506375900295e-10,
            2.5274502490901796e-08,
            2.2722660730778443e-07,
            6.751365417244359e-13,
            1.0386839999210196e-10,
            4.863354148457343e-17,
            0.9999995231628418,
            1.871915816797909e-11,
            2.220721739831788e-07
          ]
        ],
        [
          [
            9.035264723900038e-12,
            6.61216359443273e-10,
            1.0,
            4.201330394693059e-09,
            1.0897417679605476e-16,
            1.4480784626957899e-11,
            2.3426386698555124e-12,
            1.4434292303078244e-11,
            3.934309475428499e-12,
            1.1301396279513021e-15
          ],
          2,
          [
            9.035264723900038e-12,
            6.61216359443273e-10,
            1.0,
            4.201330394693059e-09,
            1.0897417679605476e-16,
            1.4480784626957899e-11,
            2.3426386698555124e-12,
            1.4434292303078244e-11,
            3.934309475428499e-12,
            1.1301396279513021e-15
          ]
        ]
      ]
    }
  ]
}

7. Clean up¶

In [32]:
client.export_assets.delete(export_job_id, space_id=export_space_id)
client.import_assets.delete(import_job_id, space_id=import_space_id)

client.spaces.delete(export_space_id)
client.spaces.delete(import_space_id)
Export job deleted
Import job deleted
DELETED
DELETED
Out[32]:
'SUCCESS'

If you want to clean up all created assets:

  • experiments
  • trainings
  • pipelines
  • model definitions
  • models
  • functions
  • deployments

please follow up this sample notebook.

8. Summary and next steps¶

You successfully completed this notebook! You learned how to use export/import assets client apis. Check out our Online Documentation for more samples, tutorials, documentation, how-tos, and blog posts.

Authors¶

Mithun - [email protected]**, Software Engineer

Mateusz Szewczyk, Software Engineer at Watson Machine Learning

Copyright © 2020-2024 IBM. This notebook and its source code are released under the terms of the MIT License.