IBM Cloud Pak® for Data Version 4.6 will reach end of support (EOS) on 31 July, 2025. For more information, see the Discontinuance of service announcement for IBM Cloud Pak for Data Version 4.X.
Upgrade to IBM Software Hub Version 5.1 before IBM Cloud Pak for Data Version 4.6 reaches end of support. For more information, see Upgrading IBM Software Hub in the IBM Software Hub Version 5.1 documentation.
Importing models to a deployment space
Import machine learning models trained outside of IBM Watson Machine Learning so that you can deploy and test the models. Review the model frameworks that are supported for importing models.
Here, to import a trained model means:
- Store the trained model in your Watson Machine Learning repository
- Optional: Deploy the stored model in your Watson Machine Learning service
and repository means a directory in your cluster. For more information on space storage, refer to Creating deployment spaces.
For information on the available ways to import the model formats supported by Watson Machine Learning, refer to Importing models by import format.
For additional information on importing specific model types, refer to Things to consider when importing models.
For an example of how to add a model programmatically by using the Python client, refer to this notebook:
For an example of how to add a model programmatically by using the REST API, refer to this notebook:
Importing models by import format
This table lists the available ways to import the model formats supported by Watson Machine Learning.
| Import option | PMML | Spark MLlib | scikit-learn | XGBoost | TensorFlow | PyTorch |
|---|---|---|---|---|---|---|
| Importing a model by using UI | ✓ | |||||
| Importing a model object | ✓ | ✓ | ✓ | |||
| Importing a model by using a path to a file | ✓ | ✓ | ✓ | ✓ | ✓ | |
| Importing a model by using a path to a directory | ✓ | ✓ | ✓ | ✓ |
Importing a model by using UI
To import a model by using UI:
- From the Assets tab of your space in Watson Studio, click Import assets.
- Select
Local fileand then select Model. - Select the model file that you want to import and click Import.
Importing a model object
To import a model object:
- If your model is located in a remote location, follow Downloading a model that is stored in a remote location, and then De-serializing models.
- Store the model object in your Watson Machine Learning repository. For details, refer to Storing model in Watson Machine Learning repository.
Importing a model by using a path to a file
To import a model by using a path to a file:
-
If your model is located in a remote location, follow Downloading a model that is stored in a remote location to download it.
-
If your model is located locally, place it in a specific directory:
!cp <saved model> <target directory> !cd <target directory> -
For Scikit-learn, XGBoost, Tensorflow, and PyTorch models, if the downloaded file is not a
.tar.gzarchive, make an archive:!tar -zcvf <saved model>.tar.gz <saved model>The model file must be at the top level of the directory, for example:
assets/ <saved model> variables/ variables/variables.data-00000-of-00001 variables/variables.index -
Use the path to the saved file to store the model file in your Watson Machine Learning repository. For details, refer to Storing model in Watson Machine Learning repository.
Importing a model by using a path to a directory
To import a model by using a path to a directory:
-
If your model is located in a remote location, refer to Downloading a model stored in a remote location.
-
If your model is located locally, place it in a specific directory:
!cp <saved model> <target directory> !cd <target directory>For scikit-learn, XGBoost, Tensorflow, and PyTorch models, the model file must be at the top level of the directory, for example:
assets/ <saved model> variables/ variables/variables.data-00000-of-00001 variables/variables.index -
Use the directory path to store the model file in your Watson Machine Learning repository. For details, refer to Storing model in Watson Machine Learning repository.
Downloading a model stored in a remote location
Follow this sample code to download your model from a remote location:
import os
from wget import download
target_dir = '<target directory name>'
if not os.path.isdir(target_dir):
os.mkdir(target_dir)
filename = os.path.join(target_dir, '<model name>')
if not os.path.isfile(filename):
filename = download('<url to model>', out = target_dir)
Things to consider when importing models
Refer to these sections for additional information on importing specific model types:
For more information on supported frameworks, refer to Supported frameworks
PMML models
- The only supported type for models that are imported from PMML is: web service.
- The PMML file must have the
.xmlfile extension. - PMML models cannot be used in an SPSS stream flow.
- The PMML file must not contain a prolog. Depending on the library that you are using when you save your model, a prolog might be added to the beginning of the file by default. For example, if your file contains a prolog string such as
spark-mllib-lr-model-pmml.xml, remove the string before you import the PMML file to the deployment space.
Depending on the library that you are using when you save your model, a prolog might be added to the beginning of the file by default, like in this example:
::::::::::::::
spark-mllib-lr-model-pmml.xml
::::::::::::::
You must remove that prolog before you can import the PMML file to Watson Machine Learning.
Spark MLlib models
- Only classification and regression models are supported.
- Custom transformers, user-defined functions, and classes are not supported.
Scikit-learn models
.pklfile is the supported import format.- To serialize/pickle the model, use the
joblibpackage. - Only classification and regression models are supported.
- Pandas Dataframe input type for
predict()API is not supported. - Supported deployment types for scikit-learn models are: web service and virtual deployment.
XGBoost models
.pklfile is the supported import format.- To serialize/pickle the model, use the
joblibpackage. - Only classification and regression models are supported.
- Pandas Dataframe input type for
predict()API is not supported. - Supported deployment types for XGBoost models are: web service and virtual deployment.
TensorFlow models
- To save/serialize a TensorFlow model, use the
tf.saved_model.save()method. tf.estimatoris not supported.- The only supported deployment types for TensorFlow models are: web service and batch.
- For more information on supported frameworks, see Supported frameworks.
PyTorch models
-
The only supported deployment type for PyTorch models is: web service.
-
For a Pytorch model to be importable to Watson Machine Learning, it must be previously exported to
.onnxformat. Refer to this code.torch.onnx.export(<model object>, <prediction/training input data>, "<serialized model>.onnx", verbose=True, input_names=<input tensor names>, output_names=<output tensor names>)
De-serializing models
To de-serialize models, follow these sections:
De-serializing scikit-learn and XGBoost models
Use this code to de-serialize your scikit-learn and XGBoost model:
import joblib
<your_model> = joblib.load("<saved model>")
De-serializing Spark MLlib models
Use this code to de-serialize your Spark MLlib model:
from pyspark.ml import PipelineModel
<your model> = PipelineModel.load("<saved model>")
Storing a model in your Watson Machine Learning repository
Use this code to store your model in your Watson Machine Learning repository:
from ibm_watson_machine_learning import APIClient
client = APIClient(<your credentials>)
sw_spec_uid = client.software_specifications.get_uid_by_name("<software specification name>")
meta_props = {
client.repository.ModelMetaNames.NAME: "<your model name>",
client.repository.ModelMetaNames.SOFTWARE_SPEC_UID: sw_spec_uid,
client.repository.ModelMetaNames.TYPE: "<model type>"}
client.repository.store_model(model=<your model>, meta_props=meta_props)
Notes:
- Depending on the model framework used,
<your model>can be the actual model object, full path to a saved model file, or a path to a directory where the model file is located. For details, refer to Supported input formats. - For a list of available software specifications to use as
<software specification name>, use theclient.software_specifications.list()method. - For a list of available model types to use as
model_type, refer to Software specifications and hardware specifications for deployments. - When you export a Pytorch model to the onnx format, specify the
keep_initializers_as_inputs=Trueflag and setopset_versionto9(Watson Machine Learning deployments use thecaffe2ONNX runtime that doesn't support opset versions higher than 9).torch.onnx.export(net, x, 'lin_reg1.onnx', verbose=True, keep_initializers_as_inputs=True, opset_version=9) - For information on how to create the
<your credentials>dictionary, refer to Watson Machine Learning authentication.
Learn more
-
For details on adding data assets to a space, refer to Adding data assets to a deployment space.
-
For details on promoting data assets to a space, refer to Promoting assets to a deployment space.
-
For details on importing whole spaces and projects into an existing space, refer to Importing spaces and projects into existing deployment spaces.
Parent topic: Adding assets to a deployment space