[Yandex Cloud documentation](../../index.md) > [Yandex DataSphere](../index.md) > [Tutorials](index.md) > Usage > Deploying a service based on an ONNX model

# Deploying a service based on an ONNX model

DataSphere allows you to deploy and run services based on a [model](../concepts/models/index.md) trained in the project.

In this tutorial, you will deploy a service based on 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](https://github.com/onnx/models/).

1. [Set up your infrastructure](#infra).
1. [Create a model](#create-model).
1. [Create a node](#create-node).
1. [Run a health check for the service you deployed](#check-node).

If you no longer need the resources you created, [delete them](#clear-out).

## Getting started {#before-you-begin}

Before getting started, register in Yandex Cloud, set up a [community](../concepts/community.md), and link your [billing account](../../billing/concepts/billing-account.md) to it.
1. [On the DataSphere home page](https://datasphere.yandex.cloud), click **Try for free** and select an account to log in with: Yandex ID or your working account with the identity federation (SSO).
1. Select the [Yandex Identity Hub organization](../../organization/index.md) you are going to use in Yandex Cloud.
1. [Create a community](../operations/community/create.md).
1. [Link your billing account](../operations/community/link-ba.md) to the DataSphere community you are going to work in. Make sure you have a linked billing account and its [status](../../billing/concepts/billing-account-statuses.md) is `ACTIVE` or `TRIAL_ACTIVE`. If you do not have a billing account yet, create one in the DataSphere interface.

### Required paid resources {#paid-resources}

The cost of deploying a service based on a model includes:

* Fee for continuously running node instances (see [DataSphere pricing](../pricing.md)).
* Fee for running code cells for health checks of the deployed service.

## Set up your infrastructure {#infra}

Log in to the Yandex Cloud [management console](https://console.yandex.cloud) and select the organization you use to access DataSphere. On the [**Yandex Cloud Billing**](https://center.yandex.cloud/billing/accounts) page, make sure you have a billing account linked.

If you have an active billing account, you can go to the [cloud page](https://console.yandex.cloud/cloud) to create or select a folder to run your infrastructure.

{% note info %}

If you are using an [identity federation](../../organization/concepts/add-federation.md) to work with Yandex Cloud, you might not have access to billing details. In this case, contact your Yandex Cloud organization administrator.

{% endnote %}

### Create a folder {#create-folder}

Create a folder for you to deploy your infrastructure and for your service to store the logs.

{% note info %}

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.

{% endnote %}

{% list tabs group=instructions %}

- Management console {#console}

   1. In the [management console](https://console.yandex.cloud), select a cloud and click ![create](../../_assets/console-icons/plus.svg)**Create folder**.
   1. Name your folder, e.g., `data-folder`.
   1. Click **Create**.

{% endlist %}

### Create a service account for the DataSphere project {#create-sa}

{% list tabs group=instructions %}

- Management console {#console}

   1. Navigate to `data-folder`.
   1. [Go](../../console/operations/select-service.md#select-service) to **Identity and Access Management**.
   1. Click **Create service account**.
   1. Name the [service account](../../iam/concepts/users/service-accounts.md), e.g., `datasphere-sa`.
   1. Click **Add role** and assign the following [roles](../../iam/concepts/access-control/roles.md) to the service account:
      * `vpc.user` to use the DataSphere network.
      * `datasphere.user` to send requests to the node.

   1. Click **Create**.

{% endlist %}

## Create a model {#create-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.

1. Open the DataSphere project:
   
   1. Select the project in your community or on the DataSphere [home page](https://datasphere.yandex.cloud) in the **Recent projects** tab.
   1. Click **Open project in JupyterLab** and wait for the loading to complete.
   1. Open the notebook tab.

1. Install the `onnx` module:

   ```python
   %pip install onnx
   ```

1. Download the `mosaic.onnx` file:

   ```python
   !wget -O "mosaic.onnx" "https://github.com/onnx/models/raw/main/validated/vision/style_transfer/fast_neural_style/model/mosaic-8.onnx"
   ```

1. Load the model into a variable and check it for consistency:

   ```python
   import onnx

   onnx_model = onnx.load("mosaic.onnx")
   onnx.checker.check_model(onnx_model)
   ```

1. In the right-hand panel, select ![models](../../_assets/console-icons/nodes-right.svg). In the window that opens, click **Create model**.
1. Select the name of the variable to build your model from.
1. Name the model, e.g., `onnx-model`.
1. Click **Create**.

## Create a node {#create-node}

1. Select the project in your community or on the DataSphere [home page](https://datasphere.yandex.cloud) in the **Recent projects** tab.
1. In the top-right corner, click **Create resource**. In the pop-up window, select **Node**.
1. Enter a name for the node in the **Name** field.
1. Under **Type**, specify the resource type: **Model**.
1. In the **Models** field, select `onnx-model`.
1. Under **Folder**, select `data-folder`.
1. Under **Provisioning**, select the [configuration](../concepts/configurations.md) of [instance](../concepts/deploy/index.md) computing resources, [availability zone](../../overview/concepts/geo-scope.md), and the ID of the [subnet](../../vpc/concepts/network.md#subnet) to host the instance.
1. Under **Access control list (ACL)**, click ![Add](../../_assets/console-icons/plus.svg) **Add ACL** and specify the [IDs of the folders](../../resource-manager/operations/folder/get-id.md) to allow connections to the node from. The folder ID of the user creating the node is specified by default.
1. Click **Create**.

## Run a health check for the service you deployed {#check-node}

1. [Create secrets](../operations/data/secrets.md#create) to test your node:

   * `IAM_SECRET`: [IAM token](../../iam/concepts/authorization/iam-token.md) value.
   * `NODE_ID`: Node ID.
   * `FOLDER_ID`: [Folder ID](../../resource-manager/operations/folder/get-id.md).

1. Open the DataSphere project:
   
   1. Select the project in your community or on the DataSphere [home page](https://datasphere.yandex.cloud) in the **Recent projects** tab.
   1. Click **Open project in JupyterLab** and wait for the loading to complete.
   1. Open the notebook tab.

1. Install the `tritonclient` module:

   ```python
   %pip install tritonclient[http]
   ```

1. Import the required libraries:

   ```python
   import numpy as np
   import matplotlib.pyplot as plt
   from PIL import Image
   import requests
   import urllib
   import os
   %matplotlib inline
   ```

1. Prepare your image:

   ```python
   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()
   ```

1. Get authenticated using the secrets created earlier:

   ```python
   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
   }
   ```

1. Send test requests with the model ID substituted as follows:

   ```python
   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""")
   ```

1. Prepare your image and send a request to the node:

   ```python
   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)
   ```

1. Get the result of image transformation:

   ```python
   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 {#clear-out}

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.

1. [Delete the node](../operations/deploy/node-delete.md).
1. [Delete the secrets](../operations/data/secrets.md#delete).