Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support for AsString, StringToNumber #1648

Merged
merged 2 commits into from
Aug 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions tests/test_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -5541,6 +5541,36 @@ def func(x):
x_val = np.array([1, 5, 2, 0, 3, 4], dtype=np.int64)
self._run_test_case(func, [_OUTPUT], {_INPUT: x_val})

@check_tf_min_version("2.1")
@skip_tflite("TFlite errors on some attributes")
@check_opset_min_version(9, "string")
def test_asstring(self):
def func(x):
op_ = tf.strings.as_string(x)
return tf.identity(op_, name=_TFOUTPUT)

x_val = np.array([0, 1, 2, 3], dtype=np.int32)
self._run_test_case(func, [_OUTPUT], {_INPUT: x_val})

x_val = np.array([0, 1, 2, 3], dtype=np.float32)
# can't check the values because in onnx they are padded with 0, in tf they are not
self._run_test_case(func, [_OUTPUT], {_INPUT: x_val}, check_value=False)

@check_tf_min_version("2.1")
@skip_tflite("TFlite errors on some attributes")
@check_opset_min_version(9, "string")
def test_string_to_number(self):
def func(x):
op_ = tf.strings.to_number(x)
return tf.identity(op_, name=_TFOUTPUT)

# tf gets this wrong and returns fp32 instead of int
x_val = np.array("123", dtype=np.object)
self._run_test_case(func, [_OUTPUT], {_INPUT: x_val})

x_val = np.array("123.1", dtype=np.object)
# can't check the values because in onnx they are padded with 0, in tf they are not
self._run_test_case(func, [_OUTPUT], {_INPUT: x_val}, check_value=False)


if __name__ == '__main__':
Expand Down
24 changes: 24 additions & 0 deletions tf2onnx/onnx_opset/tensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -3799,3 +3799,27 @@ def version_8(cls, ctx, node, **kwargs):
# broadcast by expanding
node.type = "Expand"
ctx.insert_new_node_on_input(node, "Cast", node.input[1], to=TensorProto.INT64)


@tf_op("AsString")
class AsString:
@classmethod
def version_9(cls, ctx, node, **kwargs):
if (node.get_attr_value("precision") or node.get_attr_value("scientific") or node.get_attr_value("fill")):
logger.warning(
"ONNX does not support precision, scientific and fill attributes for AsString")
shapes = node.output_shapes
ctx.remove_node(node.name)
_ = ctx.make_node("Cast", node.input, name=node.name,
dtypes=[TensorProto.STRING], shapes=shapes, attr={"to": TensorProto.STRING})


@tf_op("StringToNumber")
class StringToNumber:
@classmethod
def version_9(cls, ctx, node, **kwargs):
shapes = node.output_shapes
dtypes = node.output_dtypes
ctx.remove_node(node.name)
_ = ctx.make_node("Cast", node.input, name=node.name,
dtypes=dtypes, shapes=shapes, attr={"to": dtypes[0]})