module onnx_tools.exports.tf2onnx_helper#

Inheritance diagram of mlprodict.onnx_tools.exports.tf2onnx_helper

Short summary#

module mlprodict.onnx_tools.exports.tf2onnx_helper

Helpers to run examples created with function export2tf2onnx().

source on GitHub

Classes#

class

truncated documentation

GraphBuilder

Helpers to build graph.

Tf2OnnxConvert

Applies the converter on an ONNX graph.

tf_op

Decorator to register any new converter.

Functions#

function

truncated documentation

get_max_value

Returns the maximum value for a specific type.

make_name

Creates a unique name.

make_sure

Raises an exception if cond is not verified.

make_tf2onnx_code

Converts an ONNX operators into tf2onnx code.

map_onnx_to_numpy_type

Converts ONNX type into numpy type.

Properties#

property

truncated documentation

graph

Returns the graph.

Methods#

method

truncated documentation

__call__

__init__

__init__

__init__

_add_node_name

Registers an object in in the graph by its name.

_convert_to_attribute

_convert_to_input

in ONNX, input shold come from node, so it must be a string

_register_handler

get_dtype

Returns the type of one node or None if unknown.

get_node_by_name

Retrieves a node by its name.

get_shape

Returns the type of one node or None if unknown.

make_const

Make a new constants in the graph.

make_model

Produces the new ONNX graph with the updated sets of nodes.

make_node

Adds a node to the list of nodes.

make_slice

slice changes its schema at opset 10: it treats some attributes as dynamic input so this function has to process …

make_squeeze

Squeeze changes its schema at opset 13: it treats axes as a dynamic input kwargs: key could be [“data”, “axes”]. …

make_unsqueeze

Unsqueeze changes its schema at opset 13: it treats axes as a dynamic input kwargs: key could be [“data”, “axes”]. …

remove_node

Removes a node name from the list.

replace_all_inputs

Every taking old_name as inputs will take new_name instead. Looks in the output as well but in that case, it …

run

Calls the registered converters on the graph held by this instance. Returns the new onnx graph.

Documentation#

Helpers to run examples created with function export2tf2onnx.

source on GitHub

class mlprodict.onnx_tools.exports.tf2onnx_helper.GraphBuilder(graph)#

Bases: object

Helpers to build graph. :param graph!

source on GitHub

__init__(graph)#
_convert_to_attribute(tensor, is_optional=False)#
_convert_to_input(tensor, const_name, is_optional=False, dtype=None)#

in ONNX, input shold come from node, so it must be a string

source on GitHub

property graph#

Returns the graph.

make_slice(kwargs, name=None, shapes=None, dtypes=None, return_node=False)#

slice changes its schema at opset 10: it treats some attributes as dynamic input so this function has to process inputs according to graph’s opset version to get “inputs” and “attr” to feed “make_node” kwargs: key could be [“data”, “starts”, “ends”, “axes”, “steps”, “outputs”].

source on GitHub

make_squeeze(kwargs, name=None, shapes=None, dtypes=None, return_node=False, op_name_scope=None)#

Squeeze changes its schema at opset 13: it treats axes as a dynamic input kwargs: key could be [“data”, “axes”].

source on GitHub

make_unsqueeze(kwargs, name=None, shapes=None, dtypes=None, return_node=False, op_name_scope=None)#

Unsqueeze changes its schema at opset 13: it treats axes as a dynamic input kwargs: key could be [“data”, “axes”].

source on GitHub

class mlprodict.onnx_tools.exports.tf2onnx_helper.Tf2OnnxConvert(onnx_model, _tf_op=None, verbose=None, target_opset=None, max_iter=5)#

Bases: object

Applies the converter on an ONNX graph.

Parameters:
  • onnx_model – ONNX graph

  • tf_op – class which register

  • verbose – verbosity

  • target_opset – targetted opsets

source on GitHub

__init__(onnx_model, _tf_op=None, verbose=None, target_opset=None, max_iter=5)#
_add_node_name(obj)#

Registers an object in in the graph by its name. :param name: node or initializer

source on GitHub

get_dtype(input_name)#

Returns the type of one node or None if unknown. :param input_name: result name :return: numpy dtype

source on GitHub

get_node_by_name(name)#

Retrieves a node by its name.

Parameters:

name – node name

Returns:

node name

source on GitHub

get_shape(input_name)#

Returns the type of one node or None if unknown. :param input_name: result name :return: numpy dtype

source on GitHub

make_const(name, np_val, skip_conversion=False, raw=True)#

Make a new constants in the graph. :param name: const node name, must be unique. :param np_val: value of type numpy ndarray. :param skip_conversion:

bool, indicate whether this created node would be mapped during conversion

Parameters:

raw – whether to store data at field of raw_data or the specific field according to its dtype

Returns:

create initializer

source on GitHub

make_model()#

Produces the new ONNX graph with the updated sets of nodes.

source on GitHub

make_node(op_type, inputs, attr=None, outputs=None, name=None, domain='', output_count=1, shapes=None, dtypes=None)#

Adds a node to the list of nodes.

Parameters:
  • op_type – operator type

  • inputs – list of strings

  • attr – dictionary of attributes

  • outputs – None or list of strings

  • output_count – used if outputs is None to guess the number of outputs of this node

  • name – name of the node

  • domain – domain

  • shapes – unused

  • dtypes – unused

Returns:

created node

source on GitHub

remove_node(name)#

Removes a node name from the list.

source on GitHub

replace_all_inputs(old_name, new_name)#

Every taking old_name as inputs will take new_name instead. Looks in the output as well but in that case, it creates an identity node to avoid changing an output name. :param old_name: name to replace :param new_name: new name :return: list of impacted nodes

source on GitHub

run()#

Calls the registered converters on the graph held by this instance. Returns the new onnx graph.

Returns:

ONNX graph

source on GitHub

mlprodict.onnx_tools.exports.tf2onnx_helper.get_max_value(np_dtype)#

Returns the maximum value for a specific type.

mlprodict.onnx_tools.exports.tf2onnx_helper.make_name(name)#

Creates a unique name.

mlprodict.onnx_tools.exports.tf2onnx_helper.make_sure(cond, msg, *args)#

Raises an exception if cond is not verified.

mlprodict.onnx_tools.exports.tf2onnx_helper.make_tf2onnx_code(opset, name=None, op_type=None, domain='', inputs=None, outputs=None, attributes=None, used=None, context=None, mark_inits=None, indent=8, **unused)#

Converts an ONNX operators into tf2onnx code.

Parameters:
  • opset – target opset for the conversion (usually unused)

  • name – node name

  • op_type – operator type

  • domain – domain

  • inputs – inputs

  • outputs – outputs

  • attributes – attributes

  • used – dictionary {k: v}, list of nodes taking k as input

  • context – whole context

  • mark_inits – marks initializer as replaced

  • indent – number of spaces to add on the second and following rows

Returns:

code as str

source on GitHub

mlprodict.onnx_tools.exports.tf2onnx_helper.map_onnx_to_numpy_type(onnx_dtype)#

Converts ONNX type into numpy type.

class mlprodict.onnx_tools.exports.tf2onnx_helper.tf_op(name, domain='', **kwargs)#

Bases: object

Decorator to register any new converter. :param name: type of the operator to rewrite :param domain: domain

source on GitHub

_OPSETS = {}#
__call__(func)#

Call self as a function.

__init__(name, domain='', **kwargs)#
_register_handler(func, version, names, domain, kwargs)#