Open Neural Network Exchange (ONNX)
This page provides a Java example of inferencing a model, built in Python 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, MXNet, etc,.
import ai.konduit.serving.InferenceConfiguration;
import ai.konduit.serving.config.ServingConfig;
import ai.konduit.serving.configprovider.KonduitServingMain;
import ai.konduit.serving.model.PythonConfig;
import ai.konduit.serving.pipeline.step.ImageLoadingStep;
import ai.konduit.serving.pipeline.step.PythonStep;
import com.mashape.unirest.http.Unirest;
import com.mashape.unirest.http.exceptions.UnirestException;
import org.apache.commons.io.FileUtils;
import org.datavec.python.PythonVariables;
import org.nd4j.linalg.io.ClassPathResource;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.
Python script with PyTorch and ONNX Runtime
Now that we have an optimized ONNX file, we can serve our model.
The following is the python script onnxFacedetect.py :
transforms a PIL image into a 240 x 320 image,
casts it into a PyTorch Tensor,
adds an extra dimension with
unsqueeze,casts the Tensor into a NumPy array, then
returns the model's output with ONNX Runtime.
Configure the step
Defining a PythonConfig
PythonConfigHere we use the
pythonCodePathargument instead ofpythonCode, in order to specify the location of the Python script.Define the inputs and outputs name and type of the value as defined by the name() method of a PythonVariables.Type, here we use NDARRAY. See https://serving.oss.konduit.ai/python for supported data types.
To run this example please install (PIL 6.21,numpy,matplotlib 3.1.2,onnxruntime 1.1.0, torchvision 0.4.2)and set the python path as
pythonPath(pythonPath)in thepython_configto refer the required Python libraries.
Define a pipeline step with the PythonStep class
PythonStep classIn the .step() method, define the input configuration (python_config).
Define a pipeline step with ImageLoadingStep class
ImageLoadingStep classA Pipeline Step for loading and transforming an image
Configure the server
In the ServingConfig, specify any port number that is not reserved.
The ServingConfig has to be passed to Server in addition to the imageLoadingStep as a list. In this case, there is a single step: onnx_step.
The inferenceConfiguration is stored as a JSON File.
Inference
Load a sample image and send the image as NDARRAY to the server for prediction.
Accepted input and output data formats are as follows:
Input: JSON, ARROW, IMAGE, ND4J and NUMPY.
Output: NUMPY, JSON, ND4J and ARROW. {% endhint %}
The Client should be configured to match the Konduit Serving instance. As this example is run on a local computer, the server is located at host 'http://localhost' and port port. And Finally, we run the Konduit Serving instance with the saved config.json file path as configPath and other necessary server configuration arguments.
A Callback Function onSuccess is implemented in order to post the Client request and get the HttpResponse, only after the successful run of the KonduitServingMain Server.
Confirm the output
After executing the above, in order to confirm the successful start of the Server, check for the below output text:
The Output of the program is as follows:
The complete inference configuration in JSON format is as follows:
Last updated
Was this helpful?