# 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 available for importing models\.

Here, *to import a trained model* means:

<!-- <ol> -->

1.  Store the trained model in your Watson Machine Learning repository
2.  Optional: Deploy the stored model in your Watson Machine Learning service

<!-- </ol> -->

and *repository* means a Cloud Object Storage bucket\. For more information, see [Creating deployment spaces](https://dataplatform.cloud.ibm.com/docs/content/wsj/analyze-data/ml-space-create.html)\.

You can import a model in these ways:

<!-- <ul> -->

 *  [Directly through the UI](https://dataplatform.cloud.ibm.com/docs/content/wsj/analyze-data/ml-importing-model.html?context=cdpaas&locale=en#ui-import)
 *  [By using a path to a file](https://dataplatform.cloud.ibm.com/docs/content/wsj/analyze-data/ml-importing-model.html?context=cdpaas&locale=en#path-file-import)
 *  [By using a path to a directory](https://dataplatform.cloud.ibm.com/docs/content/wsj/analyze-data/ml-importing-model.html?context=cdpaas&locale=en#path-dir-import)
 *  [Import a model object](https://dataplatform.cloud.ibm.com/docs/content/wsj/analyze-data/ml-importing-model.html?context=cdpaas&locale=en#object-import)

<!-- </ul> -->

For more information, see [Importing models by ML framework](https://dataplatform.cloud.ibm.com/docs/content/wsj/analyze-data/ml-importing-model.html?context=cdpaas&locale=en#supported-formats)\.

For more information, see [Things to consider when you import models](https://dataplatform.cloud.ibm.com/docs/content/wsj/analyze-data/ml-importing-model.html?context=cdpaas&locale=en#model-import-considerations)\.

For an example of how to add a model programmatically by using the Python client, refer to this notebook:

<!-- <ul> -->

 *  [Use PMML to predict iris species\.](https://github.com/IBM/watson-machine-learning-samples/blob/df8e5122a521638cb37245254fe35d3a18cd3f59/cloud/notebooks/python_sdk/deployments/pmml/Use%20PMML%20to%20predict%20iris%20species.ipynb)

<!-- </ul> -->

For an example of how to add a model programmatically by using the REST API, refer to this notebook:

<!-- <ul> -->

 *  [Use scikit\-learn to predict diabetes progression](https://github.com/IBM/watson-machine-learning-samples/blob/be84bcd25d17211f41fb34ec262b418f6cd6c87b/cloud/notebooks/rest_api/curl/deployments/scikit/Use%20scikit-learn%20to%20predict%20diabetes%20progression.ipynb)

<!-- </ul> -->

## Available ways to import models, per framework type ##

This table lists the available ways to import models to Watson Machine Learning, per framework type\.

<!-- <table> -->

Import options for models, per framework type

| Import option                                                                      | Spark MLlib | Scikit\-learn | XGBoost | TensorFlow | PyTorch |
| ---------------------------------------------------------------------------------- | ----------- | ------------- | ------- | ---------- | ------- |
| [Importing a model object](https://dataplatform.cloud.ibm.com/docs/content/wsj/analyze-data/ml-importing-model.html?context=cdpaas&locale=en#object-import)                                  | ✓           | ✓             | ✓       |            |         |
| [Importing a model by using a path to a file](https://dataplatform.cloud.ibm.com/docs/content/wsj/analyze-data/ml-importing-model.html?context=cdpaas&locale=en#path-file-import)           |             | ✓             | ✓       | ✓          | ✓       |
| [Importing a model by using a path to a directory](https://dataplatform.cloud.ibm.com/docs/content/wsj/analyze-data/ml-importing-model.html?context=cdpaas&locale=en#path-dir-import) |             | ✓             | ✓       | ✓          | ✓       |

<!-- </table ""> -->

### Adding a model by using UI ###

Note:If you want to import a model in the PMML format, you can directly import the model `.xml` file\.

To import a model by using UI:

<!-- <ol> -->

1.  From the **Assets** tab of your space in Watson Machine Learning, click **Import assets**\.
2.  Select `Local file` and then select **Model**\.
3.  Select the model file that you want to import and click **Import**\.

<!-- </ol> -->

The importing mechanism automatically selects a matching model type and software specification based on the version string in the `.xml` file\.

### Importing a model object ###

Note:This import method is supported by a limited number of ML frameworks\. For more information, see [Available ways to import models, per framework type](https://dataplatform.cloud.ibm.com/docs/content/wsj/analyze-data/ml-importing-model.html?context=cdpaas&locale=en#supported-formats)\.

To import a model object:

<!-- <ol> -->

1.  If your model is located in a remote location, follow [Downloading a model that is stored in a remote location](https://dataplatform.cloud.ibm.com/docs/content/wsj/analyze-data/ml-importing-model.html?context=cdpaas&locale=en#model-download)\.
2.  Store the model object in your Watson Machine Learning repository\. For more information, see [Storing model in Watson Machine Learning repository](https://dataplatform.cloud.ibm.com/docs/content/wsj/analyze-data/ml-importing-model.html?context=cdpaas&locale=en#store-in-repo)\.

<!-- </ol> -->

### Importing a model by using a path to a file ###

Note:This import method is supported by a limited number of ML frameworks\. For more information, see [Available ways to import models, per framework type](https://dataplatform.cloud.ibm.com/docs/content/wsj/analyze-data/ml-importing-model.html?context=cdpaas&locale=en#supported-formats)\.

To import a model by using a path to a file:

<!-- <ol> -->

1.  If your model is located in a remote location, follow [Downloading a model that is stored in a remote location](https://dataplatform.cloud.ibm.com/docs/content/wsj/analyze-data/ml-importing-model.html?context=cdpaas&locale=en#model-download) to download it\.
2.  If your model is located locally, place it in a specific directory:
    
        !cp <saved model> <target directory>
           !cd <target directory>
3.  For **Scikit\-learn**, **XGBoost**, **Tensorflow**, and **PyTorch** models, if the downloaded file is not a `.tar.gz` archive, make an archive:
    
        !tar -zcvf <saved model>.tar.gz <saved model>
    
    The model file must be at the top-level folder of the directory, for example:
    
        assets/
         <saved model>
         variables/
         variables/variables.data-00000-of-00001
         variables/variables.index
4.  Use the path to the saved file to store the model file in your Watson Machine Learning repository\. For more information, see [Storing model in Watson Machine Learning repository](https://dataplatform.cloud.ibm.com/docs/content/wsj/analyze-data/ml-importing-model.html?context=cdpaas&locale=en#store-in-repo)\.

<!-- </ol> -->

### Importing a model by using a path to a directory ###

Note:This import method is supported by a limited number of ML frameworks\. For more information, see [Available ways to import models, per framework type](https://dataplatform.cloud.ibm.com/docs/content/wsj/analyze-data/ml-importing-model.html?context=cdpaas&locale=en#supported-formats)\.

To import a model by using a path to a directory:

<!-- <ol> -->

1.  If your model is located in a remote location, refer to [Downloading a model stored in a remote location](https://dataplatform.cloud.ibm.com/docs/content/wsj/analyze-data/ml-importing-model.html?context=cdpaas&locale=en#model-download)\.
2.  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 folder of the directory, for example:
    
        assets/
         <saved model>
         variables/
         variables/variables.data-00000-of-00001
         variables/variables.index
3.  Use the directory path to store the model file in your Watson Machine Learning repository\. For more information, see [Storing model in Watson Machine Learning repository](https://dataplatform.cloud.ibm.com/docs/content/wsj/analyze-data/ml-importing-model.html?context=cdpaas&locale=en#store-in-repo)\.

<!-- </ol> -->

### 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 you import models ##

To learn more about importing a specific model type, see:

<!-- <ul> -->

 *  [Models saved in PMML format](https://dataplatform.cloud.ibm.com/docs/content/wsj/analyze-data/ml-importing-model.html?context=cdpaas&locale=en#pmml-import)
 *  [Spark MLlib models](https://dataplatform.cloud.ibm.com/docs/content/wsj/analyze-data/ml-importing-model.html?context=cdpaas&locale=en#spark-ml-lib-import)
 *  [Scikit\-learn models](https://dataplatform.cloud.ibm.com/docs/content/wsj/analyze-data/ml-importing-model.html?context=cdpaas&locale=en#scikit-learn-import)
 *  [XGBoost models](https://dataplatform.cloud.ibm.com/docs/content/wsj/analyze-data/ml-importing-model.html?context=cdpaas&locale=en#xgboost-import)
 *  [TensorFlow models](https://dataplatform.cloud.ibm.com/docs/content/wsj/analyze-data/ml-importing-model.html?context=cdpaas&locale=en#tf-import)
 *  [PyTorch models](https://dataplatform.cloud.ibm.com/docs/content/wsj/analyze-data/ml-importing-model.html?context=cdpaas&locale=en#pt-import)

<!-- </ul> -->

To learn more about frameworks that you can use with Watson Machine Learning, see [Supported frameworks](https://dataplatform.cloud.ibm.com/docs/content/wsj/analyze-data/pm_service_supported_frameworks.html)\.

### Models saved in PMML format ###

<!-- <ul> -->

 *  The only available deployment type for models that are imported from PMML is online deployment\.
 *  The PMML file must have the `.xml` file 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\.

<!-- </ul> -->

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 ###

<!-- <ul> -->

 *  Only classification and regression models are available\.
 *  Custom transformers, user\-defined functions, and classes are not available\.

<!-- </ul> -->

### Scikit\-learn models ###

<!-- <ul> -->

 *  `.pkl` and `.pickle` are the available import formats\.
 *  To serialize or pickle the model, use the `joblib` package\.
 *  Only classification and regression models are available\.
 *  Pandas Dataframe input type for `predict()` API is not available\.
 *  The only available deployment type for scikit\-learn models is online deployment\.

<!-- </ul> -->

### XGBoost models ###

<!-- <ul> -->

 *  `.pkl` and `.pickle` are the available import formats\.
 *  To serialize or pickle the model, use the `joblib` package\.
 *  Only classification and regression models are available\.
 *  Pandas Dataframe input type for `predict()` API is not available\.
 *  The only available deployment type for XGBoost models is online deployment\.

<!-- </ul> -->

### TensorFlow models ###

<!-- <ul> -->

 *  `.pb`, `.h5`, and `.hdf5` are the available import formats\.
 *  To save or serialize a TensorFlow model, use the `tf.saved_model.save()` method\.
 *  `tf.estimator` is not available\.
 *  The only available deployment types for TensorFlow models are: online deployment and batch deployment\.

<!-- </ul> -->

### PyTorch models ###

<!-- <ul> -->

 *  The only available deployment type for PyTorch models is online deployment\.
 *  For a Pytorch model to be importable to Watson Machine Learning, it must be previously exported to `.onnx` format\. 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>)

<!-- </ul> -->

## 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**:

<!-- <ul> -->

 *  Depending on the model framework used, `<your model>` can be the actual model object, a full path to a saved model file, or a path to a directory where the model file is located\. For more information, see [Available ways to import models, per framework type](https://dataplatform.cloud.ibm.com/docs/content/wsj/analyze-data/ml-importing-model.html?context=cdpaas&locale=en#supported-formats)\.
 *  For a list of available software specifications to use as `<software specification name>`, use the `client.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](https://dataplatform.cloud.ibm.com/docs/content/wsj/analyze-data/pm_service_supported_frameworks.html)\.
 *  When you export a Pytorch model to the `.onnx` format, specify the `keep_initializers_as_inputs=True` flag and set `opset_version` to 9 (Watson Machine Learning deployments use the `caffe2` ONNX 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)
 *  To learn more about how to create the `<your credentials>` dictionary, refer to [Watson Machine Learning authentication](https://dataplatform.cloud.ibm.com/docs/content/wsj/analyze-data/ml-authentication.html)\.

<!-- </ul> -->

**Parent topic:**[Assets in deployment spaces](https://dataplatform.cloud.ibm.com/docs/content/wsj/analyze-data/ml-space-add-assets-all.html)

<!-- </article "role="article" "> -->