Skip to content

Commit

Permalink
Update Cast handlers for version 9
Browse files Browse the repository at this point in the history
The Cast operator was updated in ONNX opset version 9, causning
frontend and backend conversion to fail.

This PR should fix the issue:
onnx#362
  • Loading branch information
chinhuang007 committed Feb 15, 2019
1 parent 5882701 commit c525605
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 6 deletions.
14 changes: 14 additions & 0 deletions doc/CLI.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ ONNX-Tensorflow Command Line Interface

More information: `onnx-tf -h`
```
WARNING: The TensorFlow contrib module will not be included in TensorFlow 2.0.
For more information, please see:
* https://github.com/tensorflow/community/blob/master/rfcs/20180907-contrib-sunset.md
* https://github.com/tensorflow/addons
If you depend on functionality not listed there, please file an issue.
usage: onnx-tf [-h] {convert,check,optimize}
ONNX-Tensorflow Command Line Interface
Expand Down Expand Up @@ -36,6 +43,13 @@ optional arguments:

More information: `onnx-tf convert -h`
```
WARNING: The TensorFlow contrib module will not be included in TensorFlow 2.0.
For more information, please see:
* https://github.com/tensorflow/community/blob/master/rfcs/20180907-contrib-sunset.md
* https://github.com/tensorflow/addons
If you depend on functionality not listed there, please file an issue.
usage: onnx-tf [-h] --infile INFILE --outfile OUTFILE --convert_to {onnx,tf}
[--graph GRAPH] [--device DEVICE] [--strict STRICT]
[--output OUTPUT] [--opset OPSET]
Expand Down
4 changes: 2 additions & 2 deletions doc/support_status.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ ______
|Atanh|9|
|AveragePool|1, 7|
|BatchNormalization|1, 6, 7, 9|
|Cast|1, 6|
|Cast|1, 6, 9|
|Ceil|1, 6|
|Clip|1, 6|
|Compress|9|
Expand Down Expand Up @@ -162,7 +162,7 @@ ______
|AvgPool|1, 7|
|BatchNorm|1, 6, 7, 9|
|BiasAdd|1, 6, 7|
|Cast|1, 6|
|Cast|1, 6, 9|
|Ceil|1, 6|
|ConcatV2|1, 4|
|Conv1D|1|
Expand Down
17 changes: 17 additions & 0 deletions onnx_tf/handlers/backend/cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,20 @@ def version_1(cls, node, **kwargs):
@classmethod
def version_6(cls, node, **kwargs):
return [cls.make_tensor_from_onnx_node(node, **kwargs)]

@classmethod
def version_9(cls, node, **kwargs):
inp = kwargs["tensor_dict"][node.inputs[0]]
to_type = node.attrs.get("to")

if to_type == tf.string:
return [tf.as_string(inp)]

if inp.dtype == tf.string:
if to_type not in [tf.float32, tf.float64, tf.int32, tf.int64]:
raise RuntimeError(
"Cast string to type {} is not supported in Tensorflow.".format(
to_type))
return [tf.strings.to_number(inp, to_type)]

return [cls.make_tensor_from_onnx_node(node, **kwargs)]
6 changes: 6 additions & 0 deletions onnx_tf/handlers/frontend/cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,9 @@ def version_6(cls, node, **kwargs):
dst_t = mapping.NP_TYPE_TO_TENSOR_TYPE[np.dtype(
tf.as_dtype(node.attr["DstT"]).as_numpy_dtype)]
return cls.make_node_from_tf_node(node, [node.inputs[0]], to=dst_t)

@classmethod
def version_9(cls, node, **kwargs):
dst_t = mapping.NP_TYPE_TO_TENSOR_TYPE[np.dtype(
tf.as_dtype(node.attr["DstT"]).as_numpy_dtype)]
return cls.make_node_from_tf_node(node, [node.inputs[0]], to=dst_t)
6 changes: 3 additions & 3 deletions onnx_tf/opset_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
'Atanh': [9],
'AveragePool': [1, 7],
'BatchNormalization': [1, 6, 7, 9],
'Cast': [1, 6],
'Cast': [1, 6, 9],
'Ceil': [1, 6],
'Clip': [1, 6],
'Compress': [9],
Expand Down Expand Up @@ -152,7 +152,7 @@
'Atanh': [9],
'AveragePool': [1, 7],
'BatchNormalization': [1, 6, 7, 9],
'Cast': [1, 6],
'Cast': [1, 6, 9],
'Ceil': [1, 6],
'Clip': [],
'Compress': [],
Expand Down Expand Up @@ -288,7 +288,7 @@
'AvgPool': [1, 7],
'BatchNorm': [1, 6, 7, 9],
'BiasAdd': [1, 6, 7],
'Cast': [1, 6],
'Cast': [1, 6, 9],
'Ceil': [1, 6],
'ConcatV2': [1, 4],
'Conv1D': [1],
Expand Down
13 changes: 12 additions & 1 deletion test/backend/test_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,13 +193,24 @@ def test_cast(self):
(TensorProto.DOUBLE,
tf.float64), (TensorProto.COMPLEX64,
tf.complex64), (TensorProto.COMPLEX128,
tf.complex128)]
tf.complex128),
(TensorProto.STRING, tf.string)]
for ty, tf_type in test_cases:
node_def = helper.make_node("Cast", ["input"], ["output"], to=ty)
vector = [2, 3]
output = run_node(node_def, [vector])
np.testing.assert_equal(output["output"].dtype, tf_type)

test_cases2 = [(TensorProto.FLOAT, tf.float32),
(TensorProto.INT32, tf.int32),
(TensorProto.INT64, tf.int64),
(TensorProto.DOUBLE, tf.float64)]
for ty, tf_type in test_cases2:
node_def = helper.make_node("Cast", ["input"], ["output"], to=ty)
vector = ['2', '3']
output = run_node(node_def, [vector])
np.testing.assert_equal(output["output"].dtype, tf_type)

def test_ceil(self):
node_def = helper.make_node("Ceil", ["X"], ["Y"])
x = self._get_rnd([1000])
Expand Down

0 comments on commit c525605

Please sign in to comment.