To create a model and version on Google Cloud Platform (GCP) after uploading a Scikit-learn model artifact (e.g., `model.joblib`) to a Cloud Storage bucket, you need to use Google Cloud’s Vertex AI (previously AI Platform) for model management and deployment. The process involves several structured steps: preparing your model and artifacts, setting up the environment, creating a model resource in Vertex AI, and then creating a version (endpoint) that points to your uploaded model artifact. Below is an exhaustive, step-by-step guide.
1. Preparing the Model Artifact and the Serving Code
Scikit-learn models such as `model.joblib` are binary files generated using `joblib.dump()`. These are not directly interpretable by GCP; you need to provide a custom prediction routine or container that knows how to load and serve the model. Vertex AI provides two primary options for serving Scikit-learn models:
– Using Prebuilt Containers: Vertex AI offers prebuilt containers for Scikit-learn.
– Custom Containers: For more control, you can build a Docker container.
a. Saving the Model
{{EJS16}}b. Uploading Model to Google Cloud Storage
Use the `gsutil` command to upload your model:
{{EJS17}}2. Enabling Required APIs and Setting Up GCP
Ensure the following APIs are enabled for your project:
- Vertex AI API
- Cloud Storage API
- Compute Engine API (for deployment)
Set up the environment variables and authentication (assuming you use Cloud Shell or authorized gcloud CLI):
bash
export PROJECT_ID=your-gcp-project-id
export REGION=us-central1
gcloud config set project $PROJECT_ID
gcloud config set ai/region $REGION
Authenticate if necessary:
{{EJS19}}3. Creating the Vertex AI Model Resource
a. Using Prebuilt Container for Scikit-learn
Vertex AI provides a prebuilt container for Scikit-learn. You only need to point to your model artifact on GCS and specify the correct container.
i. Create the Model Resource
bash
gcloud ai models upload \
--region=$REGION \
--display-name=sklearn-rf-model \
--artifact-uri=gs://your-bucket-name/path/to/model \
--container-image-uri=us-docker.pkg.dev/vertex-ai/prediction/sklearn-cpu.0-24:latest
- `--display-name`: The name for your model in Vertex AI.
- `--artifact-uri`: This should point to the directory containing `model.joblib`.
- `--container-image-uri`: Use the latest Scikit-learn prebuilt image. Find more at [Vertex AI prebuilt containers](https://cloud.google.com/vertex-ai/docs/predictions/pre-built-containers).
ii. Model Directory Structure
The artifact directory must contain the model file at the root:
{{EJS21}}b. Custom Prediction Routine (Optional)
If you need custom preprocessing or postprocessing, you should package your model with a `predictor.py` file as described in [Vertex AI custom prediction routines](https://cloud.google.com/vertex-ai/docs/predictions/custom-prediction-routines-overview).
c. Using Custom Containers (Advanced)
You can build a Docker container that loads your Scikit-learn model and serves predictions using Flask, FastAPI, or similar frameworks, push it to Artifact Registry, and use its URI.
4. Creating a Model Version (Endpoint) for Online Prediction
Vertex AI uses the concept of "endpoints" for serving models.
a. Create an Endpoint
bash
gcloud ai endpoints create \
--region=$REGION \
--display-name=sklearn-rf-endpoint
Capture the endpoint ID from the output (e.g., `1234567890`).
b. Deploy the Model to the Endpoint
You can now deploy the model to the endpoint:
bash
gcloud ai endpoints deploy-model ENDPOINT_ID \
--region=$REGION \
--model=MODEL_ID \
--display-name=sklearn-rf-deployment \
--machine-type=n1-standard-2 \
--traffic-split=0=100
- `ENDPOINT_ID`: The ID obtained from the previous step.
- `MODEL_ID`: The model resource ID from the upload step.
- You can adjust `--machine-type` as needed.
5. Testing Online Prediction
a. Prepare Input Instance
Create a JSON file `instance.json` that matches the expected input schema:
{{EJS24}}b. Send Prediction Request
{{EJS25}}6. Example: End-to-End Process
Assume you have trained an Iris classifier and saved it as `model.joblib`:
1. Upload to GCS:
bash
gsutil cp model.joblib gs://my-ml-bucket/iris-model/
2. Model Upload:
bash
gcloud ai models upload \
--region=us-central1 \
--display-name=iris-sklearn \
--artifact-uri=gs://my-ml-bucket/iris-model/ \
--container-image-uri=us-docker.pkg.dev/vertex-ai/prediction/sklearn-cpu.0-24:latest
Output provides the `MODEL_ID`.
3. Create Endpoint:
bash
gcloud ai endpoints create \
--region=us-central1 \
--display-name=iris-endpoint
Output provides the `ENDPOINT_ID`.
4. Deploy Model:
bash
gcloud ai endpoints deploy-model ENDPOINT_ID \
--region=us-central1 \
--model=MODEL_ID \
--display-name=iris-sklearn-deployment \
--machine-type=n1-standard-2 \
--traffic-split=0=100
5. Send Prediction:
Prepare `instance.json` as above and run:
{{EJS30}}
7. Batch Prediction (Optional)
For batch prediction jobs, you can submit a job that reads input instances from a file in Cloud Storage and writes results to another Cloud Storage location.
{{EJS31}}
8. Best Practices and Considerations
- Artifact Path: Ensure the artifact path points to the directory containing the model and not directly to the file.
- File Naming: The default filename expected by the Scikit-learn prebuilt container is `model.joblib`.
- Input Format: The JSON input should match the format expected by your model (e.g., key names and data types).
- Model Size: For large models, verify that memory on the selected machine type is sufficient.
- IAM Permissions: Ensure that the deploying user/service account has the necessary permissions (`Vertex AI Admin`, `Storage Object Viewer`).
- Versioning: Vertex AI manages versions via endpoint deployments. Multiple models can be deployed to a single endpoint with traffic-splitting, supporting A/B testing and model rollbacks.
9. Automation and Infrastructure-as-Code
For production workflows, consider using Terraform or Google Cloud Deployment Manager to manage these resources as code. For continuous deployment, integrate with CI/CD pipelines using Cloud Build.
10. Troubleshooting
- Prediction Errors: Check that the input instance schema matches the model’s expected input features.
- Deployment Failures: Validate the container image URI and that the model file is accessible from the specified GCS path.
- Permission Issues: Confirm that the appropriate IAM roles are assigned.
11. References
- [Vertex AI documentation](https://cloud.google.com/vertex-ai/docs)
- [Prebuilt Prediction Containers](https://cloud.google.com/vertex-ai/docs/predictions/pre-built-containers)
- [Custom Prediction Routines](https://cloud.google.com/vertex-ai/docs/predictions/custom-prediction-routines-overview)
- [gcloud AI Command Reference](https://cloud.google.com/sdk/gcloud/reference/ai/)
By following these steps, you can successfully register, version, and deploy your Scikit-learn model at scale on GCP using Vertex AI, taking advantage of robust infrastructure for serving and managing machine learning models in production environments.
Other recent questions and answers regarding Scikit-learn models at scale:
- What are the benefits of deploying scikit-learn models on Google Cloud ML Engine?
- How can you call predictions using a sample row of data on a deployed scikit-learn model on Cloud ML Engine?
- What are the requirements for creating a model and version on Cloud ML Engine for a scikit-learn model?
- What are the steps to upload a joblib file to Google Cloud Storage for deploying a scikit-learn model?
- How can you export a scikit-learn model using the joblib library from sklearn.externals?

