Creating a custom prediction routine with Keras

Colab logo Run this tutorial as a notebook in Colab GitHub logo View the notebook on GitHub

This tutorial shows how to deploy a trained Keras model to AI Platform and serve predictions using a custom prediction routine. This lets you customize how AI Platform responds to each prediction request.

In this example, you will use a custom prediction routine to preprocess prediction input by scaling it, and to postprocess prediction output by converting softmax probability outputs to label strings.

The tutorial walks through several steps:

  • Training a simple Keras model locally
  • Creating and deploy a custom prediction routine to AI Platform
  • Serving prediction requests from that deployment


This tutorial uses R.A. Fisher's Iris dataset, a small dataset that is popular for trying out machine learning techniques. Each instance has four numerical features, which are different measurements of a flower, and a target label that marks it as one of three types of iris: Iris setosa, Iris versicolour, or Iris virginica.

This tutorial uses the copy of the Iris dataset included in the scikit-learn library.


The goal is to train a model that uses a flower's measurements as input to predict what type of iris it is.

This tutorial focuses more on using this model with AI Platform than on the design of the model itself.


This tutorial uses billable components of Google Cloud Platform (GCP):

  • AI Platform
  • Cloud Storage

Learn about AI Platform pricing and Cloud Storage pricing, and use the Pricing Calculator to generate a cost estimate based on your projected usage.

Before you begin

You must do several things before you can train and deploy a model in AI Platform:

  • Set up your local development environment.
  • Set up a GCP project with billing and the necessary APIs enabled.
  • Create a Cloud Storage bucket to store your training package and your trained model.

Set up your local development environment

You need the following to complete this tutorial:

  • Python 3
  • virtualenv
  • The Cloud SDK

The Google Cloud guide to Setting up a Python development environment provides detailed instructions for meeting these requirements. The following steps provide a condensed set of instructions:

  1. Install Python 3.

  2. Install virtualenv and create a virtual environment that uses Python 3.

  3. Activate that environment.

  4. Complete the steps in the following section to install the Cloud SDK.

Set up your GCP project

  1. Sign in to your Google Account.

    If you don't already have one, sign up for a new account.

  2. Select or create a Google Cloud Platform project.

    Go to the Manage resources page

  3. Asegúrate de tener habilitada la facturación para tu proyecto.

    Aprende a habilitar la facturación

  4. Habilita las AI Platform ("Cloud Machine Learning Engine") and Compute Engine API necesarias.

    Habilita las API

  5. Install and initialize the Cloud SDK.

Authenticate your GCP account

To set up authentication, you need to create a service account key and set an environment variable for the file path to the service account key.

  1. Create a service account key for authentication:
    1. In the GCP Console, go to the Create service account key page.

      Go to the Create Service Account Key page
    2. From the Service account drop-down list, select New service account.
    3. In the Service account name field, enter a name .
    4. From the Role drop-down list, select Machine Learning Engine > ML Engine Admin and Storage > Storage Object Admin.

      Note: The Role field authorizes your service account to access resources. You can view and change this field later by using GCP Console. If you are developing a production app, you may need to specify more granular permissions than Machine Learning Engine > ML Engine Admin and Storage > Storage Object Admin. For more information, see access control for AI Platform.
    5. Click Create. A JSON file that contains your key downloads to your computer.
  2. Set the environment variable GOOGLE_APPLICATION_CREDENTIALS to the file path of the JSON file that contains your service account key. This variable only applies to your current shell session, so if you open a new session, set the variable again.

Create a Cloud Storage bucket

To deploy a custom prediction routine, you must upload your trained model artifacts and your custom code to Cloud Storage.

Set the name of your Cloud Storage bucket as an environment variable. It must be unique across all Cloud Storage buckets:


Select a region where AI Platform training and prediction are available and create another environment variable.


Create your Cloud Storage bucket in this region and, later, use the same region for training and prediction. Run the following command to create the bucket if it doesn't already exist:

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

Building and training a Keras model

Often, you can't use your data in its raw form to train a machine learning model. Even when you can, preprocessing the data before using it for training can sometimes improve your model.

Assuming that you expect the input for prediction to have the same format as your training data, you must apply identical preprocessing during training and prediction to ensure that your model makes consistent predictions.

In this section, create a preprocessing module and use it as part of training. Then export a preprocessor with characteristics learned during training to use later in your custom prediction routine.

Install dependencies for local training

Training locally requires several dependencies:

pip install numpy scikit-learn 'tensorflow>=1.13,<2'

Write your preprocessor

Scaling training data so each numerical feature column has a mean of 0 and a standard deviation of 1 can improve your model.

Create, which contains a class to do this scaling:

import numpy as np

class MySimpleScaler(object):
  def __init__(self):
    self._means = None
    self._stds = None

  def preprocess(self, data):
    if self._means is None: # during training only
      self._means = np.mean(data, axis=0)

    if self._stds is None: # during training only
      self._stds = np.std(data, axis=0)
      if not self._stds.all():
        raise ValueError('At least one column has standard deviation of 0.')

    return (data - self._means) / self._stds

Notice that an instance of MySimpleScaler saves the means and standard deviations of each feature column on first use. Then it uses these summary statistics to scale data it encounters afterward.

This lets you store characteristics of the training distribution and use them for identical preprocessing at prediction time.

Train your model

Next, use preprocess.MySimpleScaler to preprocess the iris data, then train a simple neural network using Keras.

At the end, export your trained Keras model as an HDF5 (.h5) file and export your MySimpleScaler instance as a pickle (.pkl) file:

import pickle

from sklearn.datasets import load_iris
import tensorflow as tf

from preprocess import MySimpleScaler

iris = load_iris()
scaler = MySimpleScaler()
num_classes = len(iris.target_names)
X = scaler.preprocess(
y = tf.keras.utils.to_categorical(, num_classes=num_classes)

model = tf.keras.Sequential()
model.add(tf.keras.layers.Dense(25, activation=tf.nn.relu))
model.add(tf.keras.layers.Dense(25, activation=tf.nn.relu))
model.add(tf.keras.layers.Dense(num_classes, activation=tf.nn.softmax))
  optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy']), y, epochs=10, batch_size=1)'model.h5')
with open ('preprocessor.pkl', 'wb') as f:
  pickle.dump(scaler, f)

Deploying a custom prediction routine

To deploy a custom prediction routine to serve predictions from your trained model, do the following:

  • Create a custom predictor to handle requests
  • Package your predictor and your preprocessing module
  • Upload your model artifacts and your custom code to Cloud Storage
  • Deploy your custom prediction routine to AI Platform

Create a custom predictor

To deploy a custom prediction routine, you must create a class that implements the Predictor interface. This tells AI Platform how to load your model and how to handle prediction requests.

Write the following code to

import os
import pickle

import numpy as np
from sklearn.datasets import load_iris
import tensorflow as tf

class MyPredictor(object):
  def __init__(self, model, preprocessor):
    self._model = model
    self._preprocessor = preprocessor
    self._class_names = load_iris().target_names

  def predict(self, instances, **kwargs):
    inputs = np.asarray(instances)
    preprocessed_inputs = self._preprocessor.preprocess(inputs)
    outputs = self._model.predict(preprocessed_inputs)
    if kwargs.get('probabilities'):
      return outputs.tolist()
      return [self._class_names[index] for index in np.argmax(outputs, axis=1)]

  def from_path(cls, model_dir):
    model_path = os.path.join(model_dir, 'model.h5')
    model = tf.keras.models.load_model(model_path)

    preprocessor_path = os.path.join(model_dir, 'preprocessor.pkl')
    with open(preprocessor_path, 'rb') as f:
      preprocessor = pickle.load(f)

    return cls(model, preprocessor)

Notice that, in addition to using the preprocessor that you defined during training, this predictor performs a postprocessing step that converts the neural network's softmax output (an array denoting the probability of each label being the correct one) into the label with the highest probability.

However, if the predictor receives a probabilities keyword argument with the value True, it returns the probability array instead. The last part of this tutorial shows how to provide this keyword argument.

Package your custom code

You must package and as a .tar.gz source distribution package and provide the package to AI Platform so it can use your custom code to serve predictions.

Write the following to define your package:

from setuptools import setup

    scripts=['', ''])

Then run the following command to createdist/my_custom_code-0.1.tar.gz:

python sdist --formats=gztar

Upload model artifacts and custom code to Cloud Storage

Before you can deploy your model for serving, AI Platform needs access to the following files in Cloud Storage:

  • model.h5 (model artifact)
  • preprocessor.pkl (model artifact)
  • my_custom_code-0.1.tar.gz (custom code)

Model artifacts must be stored together in a model directory, which your Predictor can access as the model_dir argument in its from_path class method. The custom code does not need to be in the same directory. Run the following commands to upload your files:

gsutil cp ./dist/my_custom_code-0.1.tar.gz gs://$BUCKET_NAME/custom_prediction_routine_tutorial/my_custom_code-0.1.tar.gz
gsutil cp model.h5 preprocessor.pkl gs://$BUCKET_NAME/custom_prediction_routine_tutorial/model/

Deploy your custom prediction routine

Create a model resource and a version resource to deploy your custom prediction routine. First define environment variables with your resource names:


Then create your model:

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

Next, create a version. In this step, provide paths to the artifacts and custom code you uploaded to Cloud Storage:

gcloud components install beta

gcloud beta ai-platform versions create $VERSION_NAME \
  --model $MODEL_NAME \
  --runtime-version 1.13 \
  --python-version 3.5 \
  --origin gs://$BUCKET_NAME/custom_prediction_routine_tutorial/model/ \
  --package-uris gs://$BUCKET_NAME/custom_prediction_routine_tutorial/my_custom_code-0.1.tar.gz \
  --prediction-class predictor.MyPredictor

Learn more about the options you must specify when you deploy a custom prediction routine.

Serving online predictions

Try out your deployment by sending an online prediction request. First, install the Google APIs Client Library for Python:

pip install --upgrade google-api-python-client

Then send two instances of iris data to your deployed version by running the following Python code:

import googleapiclient.discovery

instances = [
  [6.7, 3.1, 4.7, 1.5],
  [4.6, 3.1, 1.5, 0.2],

service ='ml', 'v1')
name = 'projects/{}/models/{}/versions/{}'.format(PROJECT_ID, MODEL_NAME, VERSION_NAME)

response = service.projects().predict(
    body={'instances': instances}

if 'error' in response:
    raise RuntimeError(response['error'])
['versicolor', 'setosa']

Sending keyword arguments

When you send a prediction request to a custom prediction routine, you can provide additional fields on your request body. The Predictor's predict method receives these as fields of the **kwargs dictionary.

The following code sends the same request as before, but this time it adds a probabilities field to the request body:

response = service.projects().predict(
    body={'instances': instances, 'probabilities': True}

if 'error' in response:
    raise RuntimeError(response['error'])
[[0.0019204545533284545, 0.8623144626617432, 0.13576509058475494], [0.999488353729248, 0.000511515187099576, 1.293626752385535e-07]]

Cleaning up

To clean up all GCP resources used in this project, you can delete the GCP project you used for the tutorial.

Alternatively, you can clean up individual resources by running the following commands:

# Delete version resource
gcloud ai-platform versions delete $VERSION_NAME --quiet --model $MODEL_NAME

# Delete model resource
gcloud ai-platform models delete $MODEL_NAME --quiet

# Delete Cloud Storage objects that were created
gsutil -m rm -r gs://$BUCKET_NAME/custom_prediction_routine_tutorial

What's next

¿Te sirvió esta página? Envíanos tu opinión:

Enviar comentarios sobre…

AI Platform for TensorFlow