module onnx_tools.onnx2py_helper
#
Short summary#
module mlprodict.onnx_tools.onnx2py_helper
Functions which converts ONNX object into readable python objects.
Functions#
function |
truncated documentation |
---|---|
Returns the list of functions defined in ONNX package. |
|
Single function to create an array. |
|
Single function to create an sparse array (coo_matrix). |
|
Converts a type into a readable string. |
|
Converts a protobuf object into something readable. The current implementation relies on json. That’s not … |
|
Makes a copy of onnx.ValueInfoProto. |
|
Converts an array into an ONNX tensor. |
|
Retrieves an array from bytes then protobuf. |
|
Extracts tensor description from a protobuf. |
|
Returns the shape of a tensor. |
|
Returns the operator schema for a specific operator. |
|
Returns the element type if that makes sense for this object. |
|
Returns the shape if that makes sense for this object. |
|
Converts a proto type into a numpy type. |
|
Converts a string (such as ‘dtype(float32)’) into a numpy dtype. |
|
Converts a string (such as ‘float’) into a numpy dtype. |
|
Guesses the ONNX dtype given a numpy dtype. |
|
Returns a string equivalent to onnx_dtype. |
|
Converts a variable defined by its name, type and shape into onnx.ValueInfoProto. |
|
Returns the maximum of an array. Deals with text as well. |
|
Returns the minimum of an array. Deals with text as well. |
|
Converts a numpy dtyp into a TensorProto dtype. |
|
Extracts opsets in a dictionary. |
|
Converts an array into protobuf and then into bytes. |
|
Converts name, elem_type, shape into a sklearn-onnx type. |
Documentation#
Functions which converts ONNX object into readable python objects.
- 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.
- 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
- 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:
- mlprodict.onnx_tools.onnx2py_helper._to_array(var)#
- mlprodict.onnx_tools.onnx2py_helper._type_to_string(dtype)#
Converts a type into a readable string.
- 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.
- 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
- mlprodict.onnx_tools.onnx2py_helper.from_array(value, name=None)#
Converts an array into an ONNX tensor.
- Parameters:
value – numpy array
- Returns:
ONNX tensor
- 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.]]
- mlprodict.onnx_tools.onnx2py_helper.from_pb(obj)#
Extracts tensor description from a protobuf.
- Parameters:
obj – initializer, tensor
- Returns:
(name, type, shape)
- 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
- 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:
- mlprodict.onnx_tools.onnx2py_helper.get_tensor_elem_type(obj)#
Returns the element type if that makes sense for this object.
- mlprodict.onnx_tools.onnx2py_helper.get_tensor_shape(obj)#
Returns the shape if that makes sense for this object.
- 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
- mlprodict.onnx_tools.onnx2py_helper.guess_numpy_type_from_dtype(dt)#
Converts a string (such as ‘dtype(float32)’) into a numpy dtype.
- mlprodict.onnx_tools.onnx2py_helper.guess_numpy_type_from_string(name)#
Converts a string (such as ‘float’) into a numpy dtype.
- mlprodict.onnx_tools.onnx2py_helper.guess_proto_dtype(dtype)#
Guesses the ONNX dtype given a numpy dtype.
- Parameters:
dtype – numpy dtype
- Returns:
proto type
- 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
- 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
- mlprodict.onnx_tools.onnx2py_helper.numpy_max(x)#
Returns the maximum of an array. Deals with text as well.
- mlprodict.onnx_tools.onnx2py_helper.numpy_min(x)#
Returns the minimum of an array. Deals with text as well.
- mlprodict.onnx_tools.onnx2py_helper.numpy_type_prototype(dtype)#
Converts a numpy dtyp into a TensorProto dtype.
- Parameters:
dtype – dtype
- Returns:
proto dtype
- mlprodict.onnx_tools.onnx2py_helper.onnx_model_opsets(onnx_model)#
Extracts opsets in a dictionary.
- Parameters:
onnx_model – ONNX graph
- Returns:
dictionary {domain: version}
- 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'
- 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