This notebook provides an example of serving a model built in PyTorch with ONNX Runtime, a cross-platform, high performance scoring engine for machine learning models.
The Open Neural Network Exchange (ONNX) format is supported by a number of deep learning frameworks, including PyTorch, CNTK and MXNet.
import os from urllib.request import urlretrieve import sys import numpy as np fromPILimport Image import onnxfrom onnx import optimizerfrom konduit.utils import default_python_path
This page documents two ways to create Konduit Serving configurations with the Python SDK:
Using Python to create a configuration, and
Writing the configuration as a YAML file, then serving it using the Python SDK.
These approaches are documented in separate tabs throughout this page. For example, the following code block shows the imports for each approach in separate tabs:
from konduit.load import server_from_file, client_from_file
Download file
For the purposes of this example, we use ONNX model files from Ultra-Light-Fast-Generic-Face-Detector-1MB by Linzaer, a lightweight facedetection model designed for edge computing devices.
Here we use the python_code argument instead of python_code_path, since the code is defined as a string.
Define the inputs and outputs as dictionaries, where the keys represent objects in the server's Python environment, and the values represent data types (Python data structures), defined as strings. See https://serving.oss.konduit.ai/python for supported data types.
Define a pipeline step with the PythonStep class.
In the .step() method, define a name for this step (input1) and the respective configuration (python_config).
We define a single python_step of type PYTHON.
python_path specifies the location of Python modules.
python_code specifies the Python code to be run. Here, we use a YAML literal block scalar.
python_inputs and python_outputsspecifies the data type of the objects in the Python script to be used as input(s) and output(s) respectively.
Models loaded from a YAML configuration do not currently support input and output names for Python steps. To construct configurations with custom input and output names, use the Python SDK.
The default Python path includes NumPy and a basic set of modules. However, for this example, we also require the Pillow, PyTorch and ONNX Runtime modules. See the Python pipeline steps page for additional documentation on Python paths, and refer to the PyTorch quickstart for recommended installation steps.
To locate your Python path, run the following:
Configure the server
Start the server
Configure the client
Make sure to configure the client after starting the server, so that the Client object can inherit the Server's attributes.
Since the image is passed to the Server as a NumPy array, specify the input and output data format as NUMPY.
Add the following to your YAML configuration file:
Use client_from_file to create a Client object:
Inference
Load a sample image using PIL/Pillow and send the image to the server for prediction using the predict() method of the Client class.