Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def on_Pad(ctx, node, name, args):
node.type = "Pad"
node.domain = 'com.microsoft'
mode = node.get_attr("mode")
if mode:
mode = mode.s.decode("utf-8").lower()
node.set_attr("mode", mode)
if mode not in [None, "constant", "reflect"]:
raise ValueError(mode + " pad mode is not supported")
origin_dtype = ctx.get_dtype(node.output[0])
cast_node = ctx.insert_new_node_on_input(node, "Cast", node.input[1])
cast_node.set_attr("to", onnx_pb.TensorProto.INT64)
ctx.set_dtype(cast_node.output[0], onnx_pb.TensorProto.INT64)
ctx.copy_shape(node.name, cast_node.output[0])
attrs = {'perm': [1, 0]}
transpose_node = ctx.make_node("Transpose", [cast_node.output[0]], name=tf2onnx.utils.make_name(node.name),
attr=attrs)
const_name = tf2onnx.utils.make_name(node.name)
const_array = ctx.make_const(const_name, np.array([-1], dtype=np.int64))
reshape = ctx.make_node("Reshape", [transpose_node.output[0], const_array.output[0]])
ctx.replace_input(node, node.input[1], reshape.output[0])
if origin_dtype not in [onnx_pb.TensorProto.FLOAT16, onnx_pb.TensorProto.FLOAT,
onnx_pb.TensorProto.DOUBLE]:
def make_range_non_const(ctx, start, limit, delta, output, scope_name, shape, dtype):
"""make Range subgraph."""
# T range = Range(T start, T limit, T delta)
# V v_final_and_scan_outputs = Loop(int64 M, B cond, V v_initial)
base_name = utils.make_name(scope_name)
# trip_count
diff_node = ctx.make_node("Sub",
[limit, start],
op_name_scope=base_name,
name=utils.make_name("diff"))
diff_output = diff_node.output[0]
delta_cast = delta
if dtype in [TensorProto.INT32, TensorProto.INT64]:
cast_node = ctx.make_node("Cast", [diff_output], op_name_scope=base_name,
name="cast_diff", attr={"to": TensorProto.FLOAT})
diff_output = cast_node.output[0]
cast_node = ctx.make_node("Cast", [delta], op_name_scope=base_name, name="cast_delta",
attr={"to": TensorProto.FLOAT})
delta_cast = cast_node.output[0]
div_node = ctx.make_node("Div", [diff_output, delta_cast], op_name_scope=base_name, name="div")
ceil_node = ctx.make_node("Ceil", [div_node.output[0]], op_name_scope=base_name, name="ceil")
trip_count_node = ctx.make_node("Cast", [ceil_node.output[0]], op_name_scope=base_name, name="trip_cnt",
attr={"to": TensorProto.INT64})
# cond
# Use initializer here since Constant OP before opset 9 does not support bool type
cond_name = "{}_cond".format(base_name)
ctx.make_const(cond_name, np.ones((), dtype=bool))
:param tensor: TensorProto object
:return tensor_float16: converted TensorProto object
Example:
::
from onnxmltools.utils.float16_converter import convert_tensor_float_to_float16
new_tensor = convert_tensor_float_to_float16(tensor)
'''
if not isinstance(tensor, onnx_proto.TensorProto):
raise ValueError('Expected input type is an ONNX TensorProto but got %s' % type(tensor))
if tensor.data_type == onnx_proto.TensorProto.FLOAT:
tensor.data_type = onnx_proto.TensorProto.FLOAT16
# convert float_data (float type) to float16 and write to int32_data
if tensor.float_data:
int_list = _npfloat16_to_int(np.float16(tensor.float_data))
tensor.int32_data[:] = int_list
tensor.float_data[:] = []
# convert raw_data (bytes type)
if tensor.raw_data:
# convert n.raw_data to float
float32_list = np.fromstring(tensor.raw_data, dtype='float32')
# convert float to float16
float16_list = np.float16(float32_list)
# convert float16 to bytes and write back to raw_data
tensor.raw_data = float16_list.tostring()
return tensor
def make_min_or_max_op(ctx, op_type, inputs, outputs,
output_shapes=None, output_dtypes=None):
# support more dtype
supported_dtypes = [
onnx_pb.TensorProto.FLOAT,
onnx_pb.TensorProto.FLOAT16,
onnx_pb.TensorProto.DOUBLE
]
target_dtype = onnx_pb.TensorProto.FLOAT
need_cast = False
cast_inputs = []
for inp in inputs:
dtype = ctx.get_dtype(inp)
utils.make_sure(dtype is not None, "dtype of {} is None".format(inp))
if dtype not in supported_dtypes:
cast_inp = ctx.make_node("Cast", [inp], attr={"to": target_dtype})
cast_inputs.append(cast_inp.output[0])
need_cast = True
else:
cast_inputs.append(inp)
node = ctx.make_node(op_type, cast_inputs, shapes=output_shapes)
actual_outputs = node.output
if need_cast:
origin_dtype = ctx.get_dtype(inputs[0])
if output_dtypes is not None:
def _wrap_concat_with_cast(ctx, node):
"""wrap concat in casts for opset < 8 since it only supports."""
supported_types = [onnx_pb.TensorProto.FLOAT, onnx_pb.TensorProto.FLOAT16]
dtype = ctx.get_dtype(node.output[0])
need_casting = dtype not in supported_types
if need_casting:
output_name = node.output[0]
# cast each inputs to float
for i, inp in enumerate(node.inputs):
input_cast = ctx.insert_new_node_on_input(node, "Cast", node.input[i])
input_cast.set_attr("to", onnx_pb.TensorProto.FLOAT)
ctx.set_dtype(input_cast.output[0], onnx_pb.TensorProto.FLOAT)
next_nodes = ctx.find_output_consumers(node.output[0])
# cast output back to dtype unless the next op is a cast
if next_nodes[0].type != "Cast":
op_name = utils.make_name(node.name)
output_cast = ctx.insert_new_node_on_output("Cast", output_name, name=op_name)
output_cast.set_attr("to", dtype)
ctx.set_dtype(output_cast.output[0], dtype)
target_shape = node.inputs[1].get_tensor_value()
n, h, w, c = shape
nh, nw = target_shape
# scales is nchw
# the reason not storing data at raw field is because of the bug: https://github.com/onnx/onnx/issues/1852
scale_val = np.array([1.0, 1.0, float(nh) / h, float(nw) / w]).astype(np.float32)
scales = ctx.make_const(utils.make_name("scales"), scale_val, raw=False)
else:
ori_shape = ctx.make_node("Shape", [node.input[0]])
attr = {"axes": [0], "starts": [1], "ends": [3]}
inputs_map = {"data": ori_shape.output[0], **attr}
ori_shape_hw = GraphBuilder(ctx).make_slice(inputs_map)
ori_shape_hw_float = ctx.make_node("Cast", [ori_shape_hw], attr={"to": onnx_pb.TensorProto.FLOAT})
target_hw = node.inputs[1]
target_hw_float = ctx.make_node("Cast", target_hw.output, attr={"to": onnx_pb.TensorProto.FLOAT})
scales_hw = ctx.make_node("Div", [target_hw_float.output[0], ori_shape_hw_float.output[0]])
const_one_array = ctx.make_const(utils.make_name("one"), np.array([1.0, 1.0]).astype(np.float32))
# scales is nchw
scales = ctx.make_node("Concat", [const_one_array.output[0], scales_hw.output[0]], {"axis": 0})
# because onnxruntime only supports to scale the last two dims so transpose is inserted
input_nchw = ctx.make_node("Transpose", [node.input[0]], {"perm": constants.NHWC_TO_NCHW})
upsample = ctx.make_node(op_type, [input_nchw.output[0], scales.output[0]], attr={"mode": mode})
shapes = node.output_shapes
dtypes = node.output_dtypes
ctx.remove_node(node.name)
ctx.make_node("Transpose", upsample.output, {"perm": constants.NCHW_TO_NHWC},
name=node.name, outputs=node.output, shapes=shapes, dtypes=dtypes)
def _make_range_non_const(ctx, start, limit, delta, output, scope_name, shape, dtype):
utils.make_sure(
dtype in [TensorProto.FLOAT, TensorProto.DOUBLE, TensorProto.INT16, TensorProto.INT32, TensorProto.INT64],
"dtype %s is not supported", dtype)
ctx.make_node("Range", [start, limit, delta], outputs=[output], name=scope_name, shapes=[shape], dtypes=[dtype],
domain=constants.MICROSOFT_DOMAIN)
output_name, params[1]))
zero_point_values = [params[0].item()]
zero_point_shape = []
zero_point_name = param_name + "_zero_point"
zero_point_type = onnx.mapping.NP_TYPE_TO_TENSOR_TYPE[params[0].dtype]
scale_values = [params[1].item()]
scale_shape = []
scale_name = param_name + "_scale"
# Add initializers
_add_initializer_if_not_present(self.model.graph, zero_point_name, zero_point_values, zero_point_shape,
zero_point_type)
_add_initializer_if_not_present(self.model.graph, scale_name, scale_values, scale_shape,
onnx_proto.TensorProto.FLOAT)
return True, scale_name, zero_point_name, scale_shape, zero_point_shape
def apply_affine(scope, input_name, output_name, container, operator_name=None, alpha=1., beta=0.):
if container.target_opset < 9:
op_type = 'Affine'
name = _create_name_or_use_existing_one(scope, 'Affine', operator_name)
attrs = {'name': name, 'alpha': alpha, 'beta': beta}
container.add_node(op_type, input_name, output_name, **attrs)
else:
name = _create_name_or_use_existing_one(scope, 'Affine', operator_name)
# Define a and b.
aName = scope.get_unique_variable_name(name + '_alpha')
container.add_initializer(aName, onnx_proto.TensorProto.FLOAT, [1], [alpha])
bName = scope.get_unique_variable_name(name + '_beta')
container.add_initializer(bName, onnx_proto.TensorProto.FLOAT, [1], [beta])
# Compute Z = a * X, where X is the original input.
zName = scope.get_unique_variable_name(name + '_scaled')
apply_mul(scope, [aName, input_name], zName, container)
# Compute Y = Z + b, where Y is the final output.
apply_add(scope, [zName, bName], output_name, container)