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

_get_onnx_function

Returns the list of functions defined in ONNX package.

_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 …

copy_value_info

Makes a copy of onnx.ValueInfoProto.

from_array

Converts an array into an ONNX tensor.

from_bytes

Retrieves an array from bytes then protobuf.

from_pb

Extracts tensor description from a protobuf.

get_dtype_shape

Returns the shape of a tensor.

get_onnx_schema

Returns the operator schema for a specific operator.

get_tensor_elem_type

Returns the element type if that makes sense for this object.

get_tensor_shape

Returns the shape if that makes sense for this object.

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.

make_value_info

Converts a variable defined by its name, type and shape into onnx.ValueInfoProto.

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.

numpy_type_prototype

Converts a numpy dtyp into a TensorProto dtype.

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._get_onnx_function()#

Returns the list of functions defined in ONNX package.

source on GitHub

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.copy_value_info(info, name=None)#

Makes a copy of onnx.ValueInfoProto.

Parameters:

name – if defined, changed the name

Returns:

instance of onnx.ValueInfoProto

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.from_pb(obj)#

Extracts tensor description from a protobuf.

Parameters:

obj – initializer, tensor

Returns:

(name, type, shape)

source on GitHub

mlprodict.onnx_tools.onnx2py_helper.get_dtype_shape(obj)#

Returns the shape of a tensor.

Parameters:

obj – onnx object

Returns:

(dtype, shape) or (None, None) if not applicable

source on GitHub

mlprodict.onnx_tools.onnx2py_helper.get_onnx_schema(opname, domain='', opset=None, load_function=False)#

Returns the operator schema for a specific operator.

Parameters:
  • domain – operator domain

  • opname – operator name

  • opset – opset or version, None for the latest

  • load_function – loads the function, if True, the function looks into the list of function if one of them has the same name, opset must be None in that case

Returns:

:epkg:`OpSchema`

source on GitHub

mlprodict.onnx_tools.onnx2py_helper.get_tensor_elem_type(obj)#

Returns the element type if that makes sense for this object.

source on GitHub

mlprodict.onnx_tools.onnx2py_helper.get_tensor_shape(obj)#

Returns the shape if that makes sense for this object.

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.make_value_info(name, dtype, shape)#

Converts a variable defined by its name, type and shape into onnx.ValueInfoProto.

Parameters:
  • name – name

  • dtype – numpy element type

  • shape – shape

Returns:

instance of onnx.ValueInfoProto

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.numpy_type_prototype(dtype)#

Converts a numpy dtyp into a TensorProto dtype.

Parameters:

dtype – dtype

Returns:

proto dtype

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