Deploying a service from an ONNX model
DataSphere allows you to deploy and run services based on a model trained in the project.
In this tutorial, you will deploy a service from an ONNX model. The fast-neural-style-mosaic-onnx
model transforms an image as per the specified style. The model is taken from the ONNX model repository
- Prepare your infrastructure.
- Create a model.
- Create a node.
- Run a health check for the service you deployed.
If you no longer need the resources you created, delete them.
Getting started
Before getting started, register in Yandex Cloud, set up a community, and link your billing account to it.
- On the DataSphere home page
, click Try for free and select an account to log in with: Yandex ID or your working account in the identity federation (SSO). - Select the Yandex Cloud Organization organization you are going to use in Yandex Cloud.
- Create a community.
- Link your billing account to the DataSphere community you are going to work in. Make sure that you have a billing account linked and its status is
ACTIVE
orTRIAL_ACTIVE
. If you do not have a billing account yet, create one in the DataSphere interface.
Required paid resources
The cost of deploying a service from a model includes:
- Fee for continuously running node instances (see DataSphere pricing).
- Fee for running code cells for health checks of the deployed service.
Prepare the infrastructure
Log in to the Yandex Cloud management console
If you have an active billing account, you can create or select a folder to deploy your infrastructure in, on the cloud page
Note
If you use an identity federation to access Yandex Cloud, billing details might be unavailable to you. In this case, contact your Yandex Cloud organization administrator.
Create a folder
Create a folder for you to deploy your infrastructure and for your service to store the logs.
Note
In our example, both the Yandex Cloud infrastructure and the deployed service operate from the same Yandex Cloud folder; however, this is not a requirement.
- In the management console
, select a cloud and click Create folder. - Name your folder, e.g.,
data-folder
. - Click Create.
Create a service account for the DataSphere project
-
Go to
data-folder
. -
In the list of services, select Identity and Access Management.
-
Click Create service account.
-
Enter a name for the service account, e.g.,
datasphere-sa
. -
Click Add role and assign the following roles to the service account:
vpc.user
: To use the DataSphere network.datasphere.user
: To send requests to the node.
-
Click Create.
Create a model
The fast-neural-style-mosaic-onnx
model is one of the style transfer models designed to convert an image to the style of another image.
-
Open the DataSphere project:
-
Select the relevant project in your community or on the DataSphere homepage
in the Recent projects tab. - Click Open project in JupyterLab and wait for the loading to complete.
- Open the notebook tab.
-
-
Install the
onnx
module:%pip install onnx
-
Download the
mosaic.onnx
file:!wget -O "mosaic.onnx" "https://github.com/onnx/models/raw/main/validated/vision/style_transfer/fast_neural_style/model/mosaic-8.onnx"
-
Load the model into a variable and check it for consistency:
import onnx onnx_model = onnx.load("mosaic.onnx") onnx.checker.check_model(onnx_model)
-
In the right-hand panel, select
. In the window that opens, click Create model. -
Select the name of the variable to create your model from.
-
Enter a name for the model, e.g.,
onnx-model
. -
Click Create.
Create a node
-
Select the relevant project in your community or on the DataSphere homepage
in the Recent projects tab. - In the top-right corner, click Create resource. In the pop-up window, select Node.
- Enter a name for the node in the Name field.
- Under Type, specify the resource type: Model.
- In the Models field, select
onnx-model
. - Under Folder, select
data-folder
. - Under Provisioning, select the configuration of instance computing resources, the availability zone, and the ID of the subnet to host the instance in.
- Under Access control list (ACL), click
Add ACL and specify the IDs of the folders to allow connections to the node from. By default, the ID of the folder owned by the user creating the node is specified. - Click Create.
Run a health check for the service you deployed
-
Create secrets to test your node:
-
Open the DataSphere project:
-
Select the relevant project in your community or on the DataSphere homepage
in the Recent projects tab. - Click Open project in JupyterLab and wait for the loading to complete.
- Open the notebook tab.
-
-
Install the
tritonclient
module:%pip install tritonclient[http]
-
Import the required libraries:
import numpy as np import matplotlib.pyplot as plt from PIL import Image import requests import urllib import os %matplotlib inline
-
Prepare your image:
url, filename = ("https://github.com/pytorch/hub/raw/master/images/dog.jpg", "dog.jpg") try: urllib.URLopener().retrieve(url, filename) except: urllib.request.urlretrieve(url, filename) # loading input and resize if needed onnx_image = Image.open(filename) size_reduction_factor = 5 onnx_image = onnx_image.resize((224, 224)) # Preprocess image x = np.array(onnx_image).astype('float32') x = np.transpose(x, [2, 0, 1]) onnx_input = np.expand_dims(x, axis=0) plt.figure(figsize=(15, 15)) plt.imshow(onnx_image) plt.show()
-
Authenticate using the secrets created earlier:
IAM_SECRET = os.environ['IAM_SECRET'] NODE_ID = os.environ['NODE_ID'] FOLDER_ID = os.environ['FOLDER_ID'] headers = { "Authorization": f"Bearer {IAM_SECRET}", # get IAM token from secrets "x-node-id": f"{NODE_ID}", # sample node "x-folder-id": f"{FOLDER_ID}" # node folder ID }
-
Send test requests with the model ID substituted as follows:
import tritonclient.http as httpclient model="<model_ID_in_DataSphere>" # request model config with model ready status print(f"""model_name: {model},\n model_ready: {triton_client.is_model_ready(model_name=model, headers=headers)},\n model_config: {triton_client.get_model_config(model_name=model, headers=headers)}\n""")
-
Prepare your image and send a request to the node:
payload = httpclient.InferInput("input1", list(onnx_input.shape), "FP32") payload.set_data_from_numpy(onnx_input, binary_data=False) results = triton_client.infer(model, inputs=[payload], headers=headers)
-
Receive the result of image transformation:
output = results.as_numpy("output1")[0] output = np.clip(output, 0, 255) output = output.transpose(1,2,0).astype("uint8") img = Image.fromarray(output) img
You will get the transformed image in the response.
How to delete the resources you created
When deploying and using models, you pay for the uptime of each node instance: from its start to deletion.
If you no longer need the service you deployed, delete the node.