Tutorial 2B: Model deployment with a model's H2O MLOps pipeline
This tutorial explores one of the three 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
- Intermediate understanding of H2O MLOps
- Complete Tutorial 1A: Default hyperparameter values (start your first experiment)
Step 1: Import dataset
For this tutorial, we will be using the preprocessed 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:
- In the H2O Hydrogen Torch navigation menu, click Import dataset.
- In the S3 file name list, select coins_image_regression.zip.
- Click Continue.
- Again, click Continue.
- Again, click Continue.
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).
- In the H2O Hydrogen Torch navigation menu, click Create experiment.
- In the Dataset list, select coins_image_regression.
- In the Experiment name box, enter
tutorial-2b
. - Click Run experiment. Note
After starting your experiment, H2O Hydrogen Torch will take you to the experiments table, where you can view running and completed experiments.
Step 3: Download model's H2O MLOps pipeline
Now that our experiment is complete, let's download the model's H2O MLOps pipeline to deploy the model using H2O MLOps.
- In the experiments table, click tutorial-2b. Note
In the experiments table, H2O Hydrogen Torch will mark an experiment as completed when its status changes to finished.
- Click Download MLOps.
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:
- Upload the MLFlow model to H2O MLOps (model.mlflow.zip)
- Deploy the MLFLow model in H2O MLOps
- Copy endpoint URL
Step 5: Score new data (H2O MLOps endpoint URL)
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:
- On line 8 of the api_pipeline.py file, paste the copied endpoint URL of the deployed model
- In line 11, specify the path to the image you want to score
- Lines 14 - 18 and 23 - 25 display how to use the H2O MLOps endpoint URL for other problem types. By default, they are commented out
import base64
import json
import cv2
import requests
# fill in the endpoint URL from MLOps
URL = "enpoint_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()
# # if you want to score an audio, please base64 encode it and send it as string
# input = base64.b64encode(open("audio.ogg", "rb").read()).decode()
# # in case of text, you can simply send the string
# input = "This is a test message!"
# json data to be sent to API
data = {"fields": ["input"], "rows": [[input]]}
# for text span prediction problem type, pass question and context texts
# input = ["Input question", "Input context"]
# data = {"fields": ["question", "context"], "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': [[145.16915893554688]], 'labels': [['label']]}
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:
- Tutorial 1B: Model deployment through the H2O Hydrogen Torch UI
- Tutorial 3B: Model deployment with a model's Python scoring pipeline
To learn how to improve a built model, consider the following tutorial:
- Submit and view feedback for this page
- Send feedback about H2O Hydrogen Torch to cloud-feedback@h2o.ai