module onnx_tools.onnx2py_helper

Short summary

module mlprodict.onnx_tools.onnx2py_helper

Functions which converts ONNX object into readable python objects.

source on GitHub

Functions

function

truncated documentation

_elem_type_as_str

_numpy_array

Single function to create an array.

_sparse_array

Single function to create an sparse array (coo_matrix).

_to_array

_type_to_string

Converts a type into a readable string.

_var_as_dict

Converts a protobuf object into something readable. The current implementation relies on json. That’s not …

from_array

Converts an array into an ONNX tensor.

from_bytes

Retrieves an array from bytes then protobuf.

guess_dtype

Converts a proto type into a numpy type.

guess_numpy_type_from_dtype

Converts a string (such as ‘dtype(float32)’) into a numpy dtype.

guess_numpy_type_from_string

Converts a string (such as ‘float’) into a numpy dtype.

guess_proto_dtype

Guesses the ONNX dtype given a numpy dtype.

guess_proto_dtype_name

Returns a string equivalent to onnx_dtype.

numpy_max

Returns the maximum of an array. Deals with text as well.

numpy_min

Returns the minimum of an array. Deals with text as well.

onnx_model_opsets

Extracts opsets in a dictionary.

to_bytes

Converts an array into protobuf and then into bytes.

to_skl2onnx_type

Converts name, elem_type, shape into a sklearn-onnx type.

Documentation

Functions which converts ONNX object into readable python objects.

source on GitHub

mlprodict.onnx_tools.onnx2py_helper._elem_type_as_str(elem_type)
mlprodict.onnx_tools.onnx2py_helper._numpy_array(data, dtype=None, copy=True)

Single function to create an array.

Parameters
  • data – data

  • dtype – dtype

  • copy – copy

Returns

numpy array

source on GitHub

mlprodict.onnx_tools.onnx2py_helper._sparse_array(shape, data, indices, dtype=None, copy=True)

Single function to create an sparse array (coo_matrix).

Parameters
  • shape – shape

  • data – data

  • indices – indices

  • dtype – dtype

  • copy – copy

Returns

coo_matrix

source on GitHub

mlprodict.onnx_tools.onnx2py_helper._to_array(var)
mlprodict.onnx_tools.onnx2py_helper._type_to_string(dtype)

Converts a type into a readable string.

source on GitHub

mlprodict.onnx_tools.onnx2py_helper._var_as_dict(var)

Converts a protobuf object into something readable. The current implementation relies on json. That’s not the most efficient way.

source on GitHub

mlprodict.onnx_tools.onnx2py_helper.from_array(value, name=None)

Converts an array into an ONNX tensor.

Parameters

value – numpy array

Returns

ONNX tensor

source on GitHub

mlprodict.onnx_tools.onnx2py_helper.from_bytes(b)

Retrieves an array from bytes then protobuf.

Parameters

b – bytes

Returns

array

Converts bytes into an array (serialization)

Useful to deserialize.

<<<

import numpy
from mlprodict.onnx_tools.onnx2py_helper import to_bytes, from_bytes

data = numpy.array([[0, 1], [2, 3], [4, 5]], dtype=numpy.float32)
pb = to_bytes(data)
data2 = from_bytes(pb)
print(data2)

>>>

    [[0. 1.]
     [2. 3.]
     [4. 5.]]

source on GitHub

mlprodict.onnx_tools.onnx2py_helper.guess_dtype(proto_type)

Converts a proto type into a numpy type.

Parameters

proto_type – example onnx.TensorProto.FLOAT

Returns

numpy dtype

source on GitHub

mlprodict.onnx_tools.onnx2py_helper.guess_numpy_type_from_dtype(dt)

Converts a string (such as ‘dtype(float32)’) into a numpy dtype.

source on GitHub

mlprodict.onnx_tools.onnx2py_helper.guess_numpy_type_from_string(name)

Converts a string (such as ‘float’) into a numpy dtype.

source on GitHub

mlprodict.onnx_tools.onnx2py_helper.guess_proto_dtype(dtype)

Guesses the ONNX dtype given a numpy dtype.

Parameters

dtype – numpy dtype

Returns

proto type

source on GitHub

mlprodict.onnx_tools.onnx2py_helper.guess_proto_dtype_name(onnx_dtype)

Returns a string equivalent to onnx_dtype.

Parameters

dtype – onnx dtype

Returns

proto type

source on GitHub

mlprodict.onnx_tools.onnx2py_helper.numpy_max(x)

Returns the maximum of an array. Deals with text as well.

source on GitHub

mlprodict.onnx_tools.onnx2py_helper.numpy_min(x)

Returns the minimum of an array. Deals with text as well.

source on GitHub

mlprodict.onnx_tools.onnx2py_helper.onnx_model_opsets(onnx_model)

Extracts opsets in a dictionary.

Parameters

onnx_model – ONNX graph

Returns

dictionary {domain: version}

source on GitHub

mlprodict.onnx_tools.onnx2py_helper.to_bytes(val)

Converts an array into protobuf and then into bytes.

Parameters

val – array

Returns

bytes

Converts an array into bytes (serialization)

Useful to serialize.

<<<

import numpy
from mlprodict.onnx_tools.onnx2py_helper import to_bytes

data = numpy.array([[0, 1], [2, 3], [4, 5]], dtype=numpy.float32)
pb = to_bytes(data)
print(len(pb), data.size * data.itemsize, pb[:10])

>>>

    32 24 b'\x08\x03\x08\x02\x10\x01J\x18\x00\x00'

source on GitHub

mlprodict.onnx_tools.onnx2py_helper.to_skl2onnx_type(name, elem_type, shape)

Converts name, elem_type, shape into a sklearn-onnx type.

Parameters
  • name – string

  • elem_type – tensor of elements of this type

  • shape – expected shape

Returns

data type

source on GitHub