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

fix dynamic shape inference in DepthToSpace #880

Merged
merged 4 commits into from
Mar 17, 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
24 changes: 15 additions & 9 deletions onnx_tf/handlers/backend/depth_to_space.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import tensorflow as tf

from onnx_tf.common import get_data_format
from onnx_tf.common.tf_helper import tf_shape
from onnx_tf.handlers.backend_handler import BackendHandler
from onnx_tf.handlers.handler import onnx_op
from onnx_tf.handlers.handler import tf_func
Expand All @@ -24,23 +25,27 @@ def version_1(cls, node, **kwargs):
attrs = copy.deepcopy(node.attrs)
attrs["data_format"] = storage_format
return [
cls.make_tensor_from_onnx_node(
node, attrs=attrs, c_first_cuda_only=True, **kwargs)
cls.make_tensor_from_onnx_node(node,
attrs=attrs,
c_first_cuda_only=True,
**kwargs)
]

@classmethod
def _common(cls, node, **kwargs):
x = kwargs["tensor_dict"][node.inputs[0]]
x_rank = len(x.get_shape())
storage_format, compute_format = get_data_format(x_rank)
storage_format, _ = get_data_format(x_rank)
attrs = copy.deepcopy(node.attrs)
attrs["data_format"] = storage_format
mode = attrs.get("mode", "DCR")

if mode == "CRD":
# need native computation
bsize = attrs.get("blocksize")
batch, channel, height, width = x.shape
x_shape = tf_shape(x)
batch, channel = x_shape[0], x_shape[1]
height, width = x_shape[2], x_shape[3]
csize = channel // (bsize**2)

reshape_node = tf.reshape(x, [batch, csize, bsize, bsize, height, width])
Expand All @@ -50,11 +55,12 @@ def _common(cls, node, **kwargs):
[batch, csize, height * bsize, width * bsize])
]

else:
return [
cls.make_tensor_from_onnx_node(
node, attrs=attrs, c_first_cuda_only=True, **kwargs)
]
return [
cls.make_tensor_from_onnx_node(node,
attrs=attrs,
c_first_cuda_only=True,
**kwargs)
]

@classmethod
def version_11(cls, node, **kwargs):
Expand Down
31 changes: 31 additions & 0 deletions test/backend/test_dynamic_shape.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,37 @@ def test_conv_transpose(self):

np.testing.assert_almost_equal(tf_model_output[0], test_output, decimal=5)

def test_depth_to_space(self):
b, c, h, w = shape = [2, 48, 5, 6]
blocksize = 4
x = self._get_rnd_float32(shape=shape)
node_def = helper.make_node("DepthToSpace", ["X"], ["Y"],
blocksize=blocksize,
mode="DCR")
graph_def = helper.make_graph(
[node_def],
name="test_unknown_shape",
inputs=[
helper.make_tensor_value_info("X", TensorProto.FLOAT,
[None, None, None, None])
],
outputs=[
helper.make_tensor_value_info("Y", TensorProto.FLOAT,
[None, None, None, None])
])
tf_rep = onnx_graph_to_tensorflow_rep(graph_def)
# export to tf.saved_model
model_path = 'test_dynamic_shape/depth_to_space'
tf_rep.export_graph(model_path)
# load the saved_model back
tf_model = tf.saved_model.load(model_path)
# run the model
tf_model_output = tf_model(X=x)
tmp = np.reshape(x, [b, blocksize, blocksize, c // (blocksize**2), h, w])
tmp = np.transpose(tmp, [0, 3, 4, 1, 5, 2])
y = np.reshape(tmp, [b, c // (blocksize**2), h * blocksize, w * blocksize])
np.testing.assert_almost_equal(tf_model_output[0], y)

def test_eye_like(self):
if legacy_opset_pre_ver(9):
raise unittest.SkipTest("ONNX version {} doesn't support EyeLike.".format(
Expand Down