module onnxrt.onnx2py_helper

Short summary

module mlprodict.onnxrt.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_bytes

Retrieves an array from bytes then protobuf.

guess_proto_dtype

Guesses the ONNX dtype given a numpy 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.

to_bytes

Converts an array into protobuf and then into bytes.

Documentation

Functions which converts ONNX object into readable python objects.

source on GitHub

mlprodict.onnxrt.onnx2py_helper._elem_type_as_str(elem_type)
mlprodict.onnxrt.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.onnxrt.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.onnxrt.onnx2py_helper._to_array(var)
mlprodict.onnxrt.onnx2py_helper._type_to_string(dtype)

Converts a type into a readable string.

source on GitHub

mlprodict.onnxrt.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.onnxrt.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.onnxrt.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.onnxrt.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.onnxrt.onnx2py_helper.numpy_max(x)

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

source on GitHub

mlprodict.onnxrt.onnx2py_helper.numpy_min(x)

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

source on GitHub

mlprodict.onnxrt.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.onnxrt.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