Deploying models

This page explains how to deploy your model to AI Platform Prediction to get predictions.

In order to deploy your trained model on AI Platform Prediction, you must:

  • Upload your saved model to a Cloud Storage bucket.
  • Create an AI Platform Prediction model resource.
  • Create an AI Platform Prediction version resource, specifying the Cloud Storage path to your saved model.

Before you begin

Train your machine learning model and follow the guide to exporting models for prediction to create model artifacts that can be deployed to AI Platform Prediction.

Store your model in Cloud Storage

Generally, it is easiest to use a dedicated Cloud Storage bucket in the same project you're using for AI Platform Prediction.

If you're using a bucket in a different project, you must ensure that your AI Platform Prediction service account can access your model in Cloud Storage. Without the appropriate permissions, your request to create an AI Platform Prediction model version fails. See more about granting permissions for storage.

Set up your Cloud Storage bucket

This section shows you how to create a new bucket. You can use an existing bucket, but it must be in the same region where you plan on running AI Platform jobs. Additionally, if it is not part of the project you are using to run AI Platform Prediction, you must explicitly grant access to the AI Platform Prediction service accounts.

  1. Specify a name for your new bucket. The name must be unique across all buckets in Cloud Storage.

    BUCKET_NAME="your_bucket_name"

    For example, use your project name with -aiplatform appended:

    PROJECT_ID=$(gcloud config list project --format "value(core.project)")
    BUCKET_NAME=${PROJECT_ID}-aiplatform
  2. Check the bucket name that you created.

    echo $BUCKET_NAME
  3. Select a region for your bucket and set a REGION environment variable.

    Use the same region where you plan on running AI Platform Prediction jobs. See the available regions for AI Platform Prediction services.

    For example, the following code creates REGION and sets it to us-central1:

    REGION=us-central1
  4. Create the new bucket:

    gsutil mb -l $REGION gs://$BUCKET_NAME

Upload the exported model to Cloud Storage

The following examples show how to upload different types of model artifacts to a model directory in Cloud Storage:

TensorFlow SavedModel

SAVED_MODEL_DIR=$(ls ./your-export-dir-base | tail -1)
gsutil cp -r $SAVED_MODEL_DIR gs://your-bucket

When you export a SavedModel from tf.keras or from a TensorFlow estimator, it gets saved as a timestamped subdirectory of a base export directory that you choose, like your-export-dir-base/1487877383942. This example shows how to upload the directory with the most recent timestamp. If you created your SavedModel in a different way, it may be in a different location on your local filesystem.

scikit-learn or XGBoost model file

Depending on how you exported your trained model, upload your model.joblib, model.pkl, or model.bst file.

The following example shows how to upload a file exported by sklearn.externals.joblib:

gsutil cp ./model.joblib gs://your-bucket/model.joblib

The following example shows how to upload a file exported by Python's pickle module:

gsutil cp ./model.pkl gs://your-bucket/model.pkl

The following example shows how to upload a file exported by xgboost.Booster's save_model method:

gsutil cp ./model.bst gs://your-bucket/model.bst

If you are deploying a custom prediction routine (beta), upload any additional model artifacts to your model directory as well.

The total file size of your model directory must be 500 MB or less if you use a legacy (MLS1) machine type or 2 GB or less if you use a Compute Engine (N1) machine type (beta). Learn more about machine types for online prediction.

When you create subsequent versions of your model, organize them by placing each one into its own separate directory within your Cloud Storage bucket.

Upload custom code

If you are deploying a scikit-learn pipeline with custom code or a custom prediction routine, you must also upload the source distribution package containing your custom code. For example:

gsutil cp dist/my_custom_code-0.1.tar.gz gs://your-bucket/my_custom_code-0.1.tar.gz

You may upload this tarball to the same directory in Cloud Storage as your model file, but you don't have to. In fact, keeping them separate may provide better organization, especially if you deploy many versions of your model and code.

Test your model with local predictions

You can use the gcloud ai-platform local predict command to test how your model serves predictions before you deploy it to AI Platform Prediction. The command uses dependencies in your local environment to perform prediction and returns results in the same format that gcloud ai-platform predict uses when it performs online predictions. Testing predictions locally can help you discover errors before you incur costs for online prediction requests.

For the --model-dir argument, specify a directory containing your exported machine learning model, either on your local machine or in Cloud Storage. For the --framework argument, specify tensorflow, scikit-learn, or xgboost. You cannot use the gcloud ai-platform local predict command with a custom prediction routine.

The following example shows how to perform local prediction:

gcloud ai-platform local predict --model-dir local-or-cloud-storage-path-to-model-directory/ \
  --json-instances local-path-to-prediction-input.json \
  --framework name-of-framework

Deploy models and versions

AI Platform Prediction organizes your trained models using model and version resources. An AI Platform Prediction model is a container for the versions of your machine learning model.

To deploy a model, you create a model resource in AI Platform Prediction, create a version of that model, then link the model version to the model file stored in Cloud Storage.

Create a model resource

AI Platform Prediction uses model resources to organize different versions of your model.

console

  1. Open the AI Platform Prediction models page in the Cloud Console:

    Open models in the Cloud Console

  2. Click the New Model button at the top of the Models page. This brings you to the Create model page.

  3. Enter a unique name for your model in the Model name field. Use the Region drop-down list to select a location for your prediction nodes.

  4. Click Create.

  5. Verify that you have returned to the Models page, and that your new model appears in the list.

gcloud

In the following command, replace MODEL_NAME with the desired name for your model. Replace REGION with the region where you want prediction nodes to run. You must choose a region that supports online prediction.

gcloud ai-platform models create MODEL_NAME \
  --regions REGION

REST API

  1. Format your request by placing the model object in the request body. At minimum, specify a name for your model by replacing MODEL_NAME in the following samples, and specify a region by replacing REGION with a region that supports online prediction.

    {
      "name": "MODEL_NAME",
      "regions": ["REGION"]
    }
    
  2. Make your REST API call to the following path, replacing PROJECT_ID with your Google Cloud project ID:

    POST https://ml.googleapis.com/v1/projects/PROJECT_ID/models/
    

    For example, you can make the following request using cURL:

    curl -X POST -H "Content-Type: application/json" \
      -d '{"name": "MODEL_NAME", "regions": ["REGION"]}' \
      -H "Authorization: Bearer `gcloud auth print-access-token`" \
      "https://ml.googleapis.com/v1/projects/PROJECT_ID/models"
    

    The API returns a response similar to the following:

    {
      "name": "projects/PROJECT_ID/models/MODEL_NAME",
      "regions": [
        "REGION"
      ]
    }
    

See the AI Platform Prediction model API for more details.

Create a model version

Now you are ready to create a model version with the trained model you previously uploaded to Cloud Storage. When you create a version, specify the following parameters:

  • name: must be unique within the AI Platform Prediction model.
  • deploymentUri: the path to your model directory in Cloud Storage.

    • If you're deploying a TensorFlow model, this is a SavedModel directory.
    • If you're deploying a scikit-learn or XGBoost model, this is the directory containing your model.joblib, model.pkl, or model.bst file.
    • If you're deploying a custom prediction routine, this is the directory containing all your model artifacts. The total size of this directory must be 500 MB or less.
  • framework: TENSORFLOW, SCIKIT_LEARN, or XGBOOST. Omit this parameter if you're deploying a custom prediction routine.

  • runtimeVersion: a runtime version based on the dependencies your model needs. If you're deploying a scikit-learn model, an XGBoost model, or a custom prediction routine, this must be at least 1.4.

  • packageUris (optional): a list of paths to your custom code distribution packages (.tar.gz files) in Cloud Storage. Only provide this parameter if you are deploying a scikit-learn pipeline with custom code (beta) or a custom prediction routine (beta).

  • predictionClass (optional): the name of your Predictor class in module_name.class_name format. Only provide this parameter if you are deploying a custom prediction routine (beta).

  • serviceAccount (optional): You may specify a service account for your model version to use if it accesses Google Cloud resources while serving predictions. Learn more about specifying a service account. Only provide this parameter if you are deploying a custom prediction routine (beta).

  • pythonVersion: must be set to "3.5" (for runtime versions 1.4 through 1.14) or "3.7" (for runtime versions 1.15 and later) to be compatible with model files exported using Python 3. Can also be set to "2.7" if used with runtime version 1.15 or earlier.

  • machineType (optional): the type of virtual machine that AI Platform Prediction uses for the nodes that serve predictions. Learn more about machine types. If not set, this defaults to mls1-c1-m2.

See more information about each of these parameters in the AI Platform Training and Prediction API for a version resource.

See the full details for each runtime version.

console

  1. On the Models page, select the name of the model resource you would like to use to create your version. This brings you to the Model Details page.

    Open models in the Cloud Console

  2. Click the New Version button at the top of the Model Details page. This brings you to the Create version page.

  3. Enter your version name in the Name field. Optionally, enter a description for your version in the Description field.

  4. Enter the following information about how you trained your model in the corresponding dropdown boxes:

  5. Optionally, select a Machine type to run online prediction. This field defaults to "Single core CPU".

  6. In the Model URI field, enter the Cloud Storage bucket location where you uploaded your model file. You may use the Browse button to find the correct path.

    Make sure to specify the path to the directory containing the file, not the path to the model file itself. For example, use gs://your_bucket_name/model-dir/ instead of gs://your_bucket_name/model-dir/saved_model.pb or gs://your_bucket_name/model-dir/model.pkl.

  7. If you are deploying a scikit-learn pipeline with custom code (beta) or a custom prediction routine (beta), provide the Cloud Storage path to any custom code packages (.tar.gz) under Custom code and dependencies. If you are are deploying a custom prediction routine, enter the name of your Predictor class in the Prediction class field.

  8. Select a Scaling option for online prediction deployment:

    • If you select "Auto scaling", the optional Minimum number of nodes field displays. You can enter the minimum number of nodes to keep running at all times, when the service has scaled down. This field defaults to 0.

    • If you select "Manual scaling", you must enter the Number of nodes you want to keep running at all times.

      Learn more about pricing for prediction costs.

  9. To finish creating your model version, click Save.

gcloud

  1. Set environment variables to store the path to the Cloud Storage directory where your model binary is located, your model name, your version name and your framework choice.

    When you create a version with the gcloud tool, you may provide the framework name in capital letters with underscores (for example, SCIKIT_LEARN) or in lowercase letters with hyphens (for example, scikit-learn). Both options lead to identical behavior.

    Replace [VALUES_IN_BRACKETS] with the appropriate values:

    MODEL_DIR="gs://your_bucket_name/"
    VERSION_NAME="[YOUR-VERSION-NAME]"
    MODEL_NAME="[YOUR-MODEL-NAME]"
    FRAMEWORK="[YOUR-FRAMEWORK_NAME]"
    

    For a scikit-learn pipeline with custom code (beta), set an additional variable with the path to your custom code tarball:

    MODEL_DIR="gs://your_bucket_name/"
    VERSION_NAME="[YOUR-VERSION-NAME]"
    MODEL_NAME="[YOUR-MODEL-NAME]"
    FRAMEWORK="scikit-learn"
    CUSTOM_CODE_PATH="gs://your_bucket_name/my_custom_code-0.1.tar.gz"
    

    For a custom prediction routine (beta), omit the FRAMEWORK variable and set additional variables with the path to your custom code tarball and the name of your predictor class:

    MODEL_DIR="gs://your_bucket_name/"
    VERSION_NAME="[YOUR-VERSION-NAME]"
    MODEL_NAME="[YOUR-MODEL-NAME]"
    CUSTOM_CODE_PATH="gs://your_bucket_name/my_custom_code-0.1.tar.gz"
    PREDICTOR_CLASS="[MODULE_NAME].[CLASS_NAME]"
    
  2. Create the version:

    gcloud ai-platform versions create $VERSION_NAME \
      --model $MODEL_NAME \
      --origin $MODEL_DIR \
      --runtime-version=1.15 \
      --framework $FRAMEWORK \
      --python-version=3.7
    

    For a scikit-learn pipeline with custom code (beta), use the gcloud beta component and make sure to set the --package-uris flag:

    gcloud components install beta
    
    gcloud beta ai-platform versions create $VERSION_NAME \
      --model $MODEL_NAME \
      --origin $MODEL_DIR \
      --runtime-version=1.15 \
      --framework $FRAMEWORK \
      --python-version=3.7 \
      --package-uris=$CUSTOM_CODE_PATH
    

    For a custom prediction routine (beta), use the gcloud beta component, omit the --framework flag, and set the --package-uris and --prediction-class flags:

    gcloud components install beta
    
    gcloud beta ai-platform versions create $VERSION_NAME \
      --model $MODEL_NAME \
      --origin $MODEL_DIR \
      --runtime-version=1.15 \
      --python-version=3.7
      --package-uris=$CUSTOM_CODE_PATH \
      --prediction-class=$PREDICTOR_CLASS
    

    Creating the version takes a few minutes. When it is ready, you should see the following output:

    Creating version (this might take a few minutes)......done.

  3. Get information about your new version:

    gcloud ai-platform versions describe $VERSION_NAME \
      --model $MODEL_NAME
    

    You should see output similar to this:

    createTime: '2018-02-28T16:30:45Z'
    deploymentUri: gs://your_bucket_name
    framework: [YOUR-FRAMEWORK-NAME]
    machineType: mls1-c1-m2
    name: projects/[YOUR-PROJECT-ID]/models/[YOUR-MODEL-NAME]/versions/[YOUR-VERSION-NAME]
    pythonVersion: '3.7'
    runtimeVersion: '1.15'
    state: READY

REST API

  1. Format your request body to contain the version object. This example specifies the version name, deploymentUri, runtimeVersion and framework. Replace [VALUES_IN_BRACKETS] with the appropriate values:

      {
        "name": "[YOUR-VERSION-NAME]",
        "deploymentUri": "gs://your_bucket_name/"
        "runtimeVersion": "1.15"
        "framework": "[YOUR_FRAMEWORK_NAME]"
        "pythonVersion": "3.7"
      }
    
  2. Make your REST API call to the following path, replacing [VALUES_IN_BRACKETS] with the appropriate values:

      POST https://ml.googleapis.com/v1/projects/[YOUR-PROJECT-ID]/models/[YOUR-MODEL-NAME]/versions
    

    For example, you can make the following request using cURL:

        curl -X POST -H "Content-Type: application/json" \
          -d '{"name": "[YOUR-VERSION-NAME]", "deploymentUri": "gs://your_bucket_name/", "runtimeVersion": "1.15", "framework": "[YOUR_FRAMEWORK_NAME]", "pythonVersion": "3.7"}' \
          -H "Authorization: Bearer `gcloud auth print-access-token`" \
          "https://ml.googleapis.com/v1/projects/[YOUR-PROJECT-ID]/models/[YOUR-MODEL-NAME]/versions"
    

    Creating the version takes a few minutes. When it is ready, you should see output similar to this:

      {
        "name": "projects/[YOUR-PROJECT-ID]/operations/create_[YOUR-MODEL-NAME]_[YOUR-VERSION-NAME]-[TIMESTAMP]",
        "metadata": {
          "@type": "type.googleapis.com/google.cloud.ml.v1.OperationMetadata",
          "createTime": "2018-07-07T02:51:50Z",
          "operationType": "CREATE_VERSION",
          "modelName": "projects/[YOUR-PROJECT-ID]/models/[YOUR-MODEL-NAME]",
          "version": {
            "name": "projects/[YOUR-PROJECT-ID]/models/[YOUR-MODEL-NAME]/versions/[YOUR-VERSION-NAME]",
            "deploymentUri": "gs://your_bucket_name",
            "createTime": "2018-07-07T02:51:49Z",
            "runtimeVersion": "1.15",
            "framework": "[YOUR_FRAMEWORK_NAME]",
            "machineType": "mls1-c1-m2",
            "pythonVersion": "3.7"
          }
        }
      }
    

Specify a service account for your model version

By default, when you deploy a model version it has the permissions of a Google-managed service account with the Cloud ML Service Agent IAM role. This default service account is sufficient for most use cases. For example, it can read from Cloud Storage buckets in the same Google Cloud project.

However, if you are deploying a custom prediction routine and need your model version to have a different set of permissions, you can specify a different service account for it to use. For example, if your model version needs to access a Cloud Storage bucket from a different Google Cloud project during prediction, you can specify a service account with permission to read from that bucket.

  1. Create or select a service account for your model version. You (the user deploying the model version) must have the service account token creator role for the service account.

  2. Specify the service account name in the serviceAccount field when you create your model version. If you deploy your model using the gcloud tool, you can use the --service-account flag, as in the following example:

    gcloud components install beta
    
    gcloud beta ai-platform versions create your-version-name \
      --service-account your-service-account-name@your-project-id.iam.gserviceaccount.com
      ...