9.16 ONNX

The oml.onnx class allows you to import your own ONNX-format model, load it in the database, and score data using the prediction operators of Oracle Machine Learning.

You can provide an onnx file (.onnx) along with its associated metadata to load your ONNX-format model in the database and score using the existing data in the database. You can also access your already existing ONNX model in the database. The python module, onnx, accepts ONNX-format models with the following characteristics:

  • Techniques: The pretrained models must use one of the following machine learning techniques:
    • Regression
    • Classification
    • Clustering
    • Embedding
  • Data types: The model inputs must be one of the following Python types:
    • Numerical (float, int8, int16, int32, int64, uint8, uint16, uint32, uint64)
    • String
  • Input types: The model's inputs must be two-dimensional vectors of the shape [batch_size, n]. In this structure, the first dimension specifies the size of each batch, while the second dimension defines the size of each individual input.

For information on the oml.onnx class attributes and methods, invoke help(oml.onnx) or see Oracle Machine Learning for Python API Reference.

ONNX-format Model Examples

  1. Load an ONNX-format model into the database:

    Use the onnx module to load your ONNX-format models into the database. You need to provide the onnx file (.onnx) along with its associated metadata to build the model in OML4Py.

    Note:

    If the model's name is not provided when loading it to the database, a system-generated model name will be used. For example model.load2db()

    from oml.algo import onnx
    model = onnx(onnx_file="path_to_model.onnx",
             mining_function='CLASSIFICATION',
             classification_prob_output="output", 
             model_input={"tensor_of_dim_4":['Sepal_Length','Sepal_Width','Petal_Length','Petal_Width']},
             apply_softmax=True,
             labels=['setosa','versicolor','virginica'],
             description={"description":"This model is useful.","author":"John Doe"},
             default_on_null={'Sepal_Length':3.2,'Sepal_Width':2.5,'Petal_Length':46,'Petal_Width':1.8})
    model.load2db("model1")
  2. Access an existing model:

    An existing ONNX model in the database can be accessed using the onnx module. In the following example, a model named classif_auto owned by omluser is accessed.

    from oml.algo import onnx
    model = onnx(model_name="classif_auto", model_owner="omluser")
    

    Once the model is loaded, you can view the model information by entering model.

  3. Score data using an ONNX-format models:

    In this example, an ONNX-format model loaded in the database is used for scoring:

    model.predict(oml_iris.drop('Species'),
                      supplemental_cols=oml_iris[:,['Sepal_Length', 'Sepal_Width', 'Species']], proba=True)

Final Example: Putting it All Together

import oml
import pandas as pd
from sklearn import datasets
 
# Load the iris data set and create a pandas.DataFrame for it.
iris = datasets.load_iris()
 
x = pd.DataFrame(iris.data,
                  columns = ['Sepal_Length','Sepal_Width',
                             'Petal_Length','Petal_Width'])
 
y = pd.DataFrame(list(map(lambda x:
                            {0: 'setosa', 1: 'versicolor',
                             2:'virginica'}[x], iris.target)),
                  columns = ['Species'])
 
 try:
     oml.drop('IRIS')
 except:
     pass
 
# Create the IRIS database table and the proxy object for the table.
oml_iris = oml.create(pd.concat([x, y], axis=1), table = 'IRIS')
model = oml.algo.onnx(onnx_file="model.onnx", mining_function='CLASSIFICATION', classification_prob_output="output", 
         model_input={"tensor_of_dim_4":['Sepal_Length','Sepal_Width','Petal_Length','Petal_Width']},
         apply_softmax=True, labels=['setosa','versicolor','virginica'],
         description={"description":"This model is useful.","author":"John Doe"},
         default_on_null={'Sepal_Length':3.2,'Sepal_Width':2.5,'Petal_Length':4.6,'Petal_Width':1.8})
 
model.load2db("model1")
model.predict(oml_iris.drop('Species'),
               supplemental_cols=oml_iris[:,['Sepal_Length', 'Sepal_Width', 'Species']], proba=True)

Sepal_Length  Sepal_Width     Species  PREDICTION  PROBABILITY
0            4.9          3.0      setosa      setosa     1.000000
1            4.9          3.1      setosa      setosa     1.000000
2            4.8          3.4      setosa      setosa     1.000000
3            5.8          4.0      setosa      setosa     1.000000
...          ...          ...         ...         ...          ...
44           6.7          3.3   virginica  versicolor     0.514706
45           6.7          3.0   virginica  versicolor     0.514706
46           6.5          3.0   virginica  versicolor     0.514706
47           5.9          3.0   virginica  versicolor     0.514706
 
model.predict_proba(oml_iris.drop('Species'),
                     supplemental_cols = oml_iris[:,['Sepal_Length', 'Species']])
Sepal_Length     Species  PROBABILITY_OF_SETOSA  \
0            4.4      setosa                    1.0  
1            4.4      setosa                    1.0  
2            4.5      setosa                    1.0  
3            4.8      setosa                    1.0  
...          ...         ...                    ...
42           6.7   virginica                    0.0  
43           6.9  versicolor                    0.0  
44           6.9   virginica                    0.0  
45           7.0  versicolor                    0.0  
 
    PROBABILITY_OF_VERSICOLOR  PROBABILITY_OF_VIRGINICA 
0                    0.000000                  0.000000 
1                    0.000000                  0.000000 
2                    0.000000                  0.000000 
3                    0.000000                  0.000000 
...                       ...                       ...
42                   0.514706                  0.485294 
43                   0.514706                  0.485294 
44                   0.514706                  0.485294 
45                   0.514706                  0.485294

Related Topics