def convert_transpose(name, tf_node, inputs, uff_graph, **kwargs): tf_permutation_node = kwargs["tf_nodes"][inputs[1]] if tf_permutation_node.op != "Const": raise UffException("Transpose permutation has op " + str(tf_permutation_node.op) + ", expected Const. Only constant permuations are supported in UFF.") permutation = tf2uff.convert_tf2numpy_const_node( tf_permutation_node).tolist() inputs = inputs[:1] uff_graph.transpose(inputs[0], permutation, name) return [tf2uff.split_node_name_and_output(inp)[0] for inp in inputs]
def convert_argmin(name, tf_node, inputs, uff_graph, **kwargs): # Retrieve and remove the axis node. tf_axis_input_node = kwargs["tf_nodes"][inputs[-1]] if tf_axis_input_node.op != "Const": raise UffException("ArgMin Axis node has op " + str(tf_axis_input_node.op) + ", expected Const. The axis must be specified as a Const node.") axis = int(tf2uff.convert_tf2numpy_const_node(tf_axis_input_node)) inputs.pop(-1) # Add the op. uff_graph.argmin(inputs[0], axis, name) return [tf2uff.split_node_name_and_output(inp)[0] for inp in inputs]
def convert_resource_gather(name, tf_node, inputs, uff_graph, **kwargs): if len(inputs) > 2: tf_axis_node = kwargs["tf_nodes"][inputs[-1]] axis = int(tf2uff.convert_tf2numpy_const_node(tf_axis_node)) inputs = inputs[:-1] else: axis = 0 indices_dtype = tf2uff.convert_tf2numpy_dtype(tf_node.attr['Tindices'].type) params_dtype = tf2uff.convert_tf2numpy_dtype(tf_node.attr['dtype'].type) uff_graph.gather_v2(inputs, name, axis, indices_dtype, params_dtype) return [tf2uff.split_node_name_and_output(inp)[0] for inp in inputs]
def convert_concatv2(name, tf_node, inputs, uff_graph, **kwargs): if "axis" in tf_node.attr: # Handle cases where the axis is not a node, but an attribute instead. axis = tf_node.attr["axis"].i else: tf_axis_node = kwargs["tf_nodes"][inputs[-1]] if tf_axis_node.op != "Const": raise UffException("Concat Axis node has op " + str(tf_axis_node.op) + ", expected Const. The axis for a Concat op must be specified as either an attribute, or a Const node.") axis = int(tf2uff.convert_tf2numpy_const_node(tf_axis_node)) inputs = inputs[:-1] uff_graph.concat(inputs, axis, name) return [tf2uff.split_node_name_and_output(inp)[0] for inp in inputs]
def _reduce_helper(name, tf_node, inputs, uff_graph, **kwargs): func = kwargs.pop("func") tf_axes_node = kwargs["tf_nodes"][inputs[1]] array = tf2uff.convert_tf2numpy_const_node(tf_axes_node) axes = array.tolist() inputs = inputs[:1] keepdims = tf_node.attr['keep_dims'].b print("Warning: keepdims is ignored by the UFF Parser and defaults to True") uff_graph.reduce(inputs[0], func, axes, keepdims, name) return [tf2uff.split_node_name_and_output(inp)[0] for inp in inputs]
def convert_softmax(name, tf_node, inputs, uff_graph, **kwargs): # Some Softmax ops don't have an axis node. if len(inputs) > 1: tf_axis_node = kwargs["tf_nodes"][inputs[-1]] axis = int(tf2uff.convert_tf2numpy_const_node(tf_axis_node)) inputs = inputs[:-1] else: axis = 0 fmt = convert_to_str(tf_node.attr['data_format'].s) fmt = fmt if fmt else "NHWC" data_fmt = tf2uff.convert_tf2uff_data_format(fmt) uff_graph.softmax(inputs[0], axis, data_fmt, name) return [tf2uff.split_node_name_and_output(inp)[0] for inp in inputs]
def convert_bias_add(name, tf_node, inputs, uff_graph, **kwargs): fmt = convert_to_str(tf_node.attr['data_format'].s) fmt = fmt if fmt else "NHWC" biases_name = inputs[1] biases_array = tf2uff.convert_tf2numpy_const_node( kwargs["tf_nodes"][biases_name]) inputs = inputs[:1] if fmt == 'NCHW': ndim = 4 new_shape = [-1] + [1] * (ndim - 2) biases_array = biases_array.reshape(new_shape) uff_graph.const(biases_array, biases_name) uff_graph.binary(inputs[0], biases_name, 'add', name) return [tf2uff.split_node_name_and_output(inp)[0] for inp in inputs]
def convert_const(name, tf_node, inputs, uff_graph, **kwargs): array = tf2uff.convert_tf2numpy_const_node(tf_node) uff_node = uff_graph.const(array, name) uff_node.array = array return [tf2uff.split_node_name_and_output(inp)[0] for inp in inputs]