Importing models to a deployment space
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: 1. Store the trained model in your Watson Machine Learning repository 2. Optional: Deploy the stored model in your Watson Machine Learning service 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: * [Directly through the UI](https://dataplatform.cloud.ibm.com/docs/content/wsj/analyze-data/ml-importing-model.html?context=cdpaas&locale=enui-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=enpath-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=enpath-dir-import) * [Import a model object](https://dataplatform.cloud.ibm.com/docs/content/wsj/analyze-data/ml-importing-model.html?context=cdpaas&locale=enobject-import) 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=ensupported-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=enmodel-import-considerations). For an example of how to add a model programmatically by using the Python client, refer to this notebook: * [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) For an example of how to add a model programmatically by using the REST API, refer to this notebook: * [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) Available ways to import models, per framework type This table lists the available ways to import models to Watson Machine Learning, per framework type. 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=enobject-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=enpath-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=enpath-dir-import) ✓ ✓ ✓ ✓ 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: 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. 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=ensupported-formats). To import a model object: 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=enmodel-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=enstore-in-repo). 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=ensupported-formats). To import a model by using a path to a file: 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=enmodel-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=enstore-in-repo). 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=ensupported-formats). To import a model by using a path to a directory: 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=enmodel-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=enstore-in-repo). 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: