Skip to main content
Version: v1.3.0

Tutorial 2B: Model deployment with a model's H2O MLOps pipeline

Overview

This tutorial explores one of the options available in H2O Hydrogen Torch to deploy a built model. In particular, this tutorial builds an image regression model to explore how it can be deployed to H2O MLOps using the model's H2O MLOps pipeline.

Prerequisites

Step 1: Import dataset

For this tutorial, we are using the preprocessed (demo) coins image regression dataset. The dataset contains a collection of 6,028 images with one or more coins. Each image has been labeled to indicate the sum of its coins. The currency of the coins is the Brazilian Real (R$). Let's import the dataset:

  1. In the H2O Hydrogen Torch navigation menu, click Import dataset.
  2. In the S3 file name list, select coins_image_regression.zip.
  3. Click Continue.
  4. Again, click Continue.
  5. Again, click Continue.

Four Brazilian Real coins image called 150_1479430290.jpg

Note

As a requirement, H2O Hydrogen Torch requires the dataset for an experiment to be preprocessed to follow a certain dataset format for the problem type the experiment aims to solve. The coins image regression dataset was preprocessed to follow a dataset format for an image regression model. To learn more, see Dataset formats.

Step 2: Build model

Let's quickly build an image regression model capable of predicting the sum of Brazilian Real (R$) coins in images. After creating the model, we will use the model's H2O MLOps pipeline to generate predictions (deploy the model).

  1. In the H2O Hydrogen Torch navigation menu, click Create experiment.
  2. In the Dataset list, select coins_image_regression.
  3. In the Experiment name box, enter tutorial-2b.
  4. Click Run experiment.

Step 3: Download model's H2O MLOps pipeline

When H2O Hydrogen Torch completes the experiment (model), download the model's H2O MLOps pipeline to deploy to H2O MLOps. Let's download the pipeline.

  1. In the experiments table, click tutorial-2b.
    Note

    In the experiments table, H2O Hydrogen Torch marks an experiment as completed when its status changes to finished.

  2. Click Download MLOps.
    note

    H2O Hydrogen Torch downloads a file with the following naming convention: mlops_tutorial-2b_*.

Step 4: Deploy H2O MLOps pipeline

The downloaded H2O MLOps pipeline contains, in particular, the following files:

  • api_pipeline.py: an example Python script demonstrating how to score new data using an MLOps API endpoint
  • model.mlflow.zip: a .zip file container (model) ready to be uploaded to H2O MLOps for deployment

To deploy our model to H2O MLOps, we need to upload the model.mlflow.zip file to H2O MLOps. Right after, we will use the api_pipeline.py file to score new data.

Depending on whether you use the legacy H2O MLOps user interance (UI) or the Wave H2O MLOps application, proceed with the following steps:

  1. Upload the MLFlow model (model.mlflow.zip) to H2O MLOps
  2. Deploy the MLFLow model in H2O MLOps
  3. Copy the model's endpoint URL
note
  • To learn more about deploying models to H2O MLOps, see Deploy a model.
  • To learn more about MLFlow models, see MLflow.

Step 5: Score new data

Now that you have deployed the model to H2O MLOps, we can use the endpoint URL of the deployed model to score new data. For instance, let's score on an image using the api_pipeline.py file, but first, let's modify the file for our own purposes:

  1. On line 8 of the api_pipeline.py file, paste the copied endpoint URL of the deployed model
  2. In line 11, specify the path to the image you want to score.
  3. The other lines display how to use the H2O MLOps endpoint URL for other problem-type models (commented).
api_pipeline.py
import base64
import json

import cv2
import requests

# fill in the endpoint URL from MLOps
URL = "endpoint_url"

# if you want to score an image, please base64 encode it and send it as string
img = cv2.imread("image.jpg")
input = base64.b64encode(cv2.imencode(".png", img)[1]).decode()

# in case of a multi-channel numpy array, please json encode it and send it as string
# img = np.load("image.npy")
# input = json.dumps(img.tolist())

# json data to be sent to API
data = {"fields": ["input"], "rows": [[input]]}

# post request
r = requests.post(url=URL, json=data)

# extracting data in json format
ret = r.json()

# read output, output is a dictionary
ret = json.loads(ret["score"][0][0])

After modifying and running the api_pipeline.py file, the response obtained is in a JSON format following the format of the .pkl files discussed in the follwing page: Download a prediction.

For example, scoring the image 150_1479430290.jpg, we receive the following response:

{'predictions': [[143.6985321044922]], 'labels': [['label']]}

Four Brazilian Real coins image called 150_1479430290.jpg

150_1479430290.jpg

Summary

In this tutorial, we learned how to deploy a built model to H2O MLOps. Mainly, we learned how to use a model's H2O MLOps pipeline to obtain an endpoint URL to score new data. We also learned that a call to the endpoint REST API returns a JSON response.

Next

Now that you know how to deploy a built model using a model's H2O MLOps pipeline, consider the following two tutorials to learn how to deploy a built model using the H2O Hydrogen Torch (HT) user interface (UI) or a model's external Python scoring pipeline:

To learn how to improve a built model, consider the following tutorial:


Feedback