From 6735bf9da29d13b78e427c3be3b96db9980144bb Mon Sep 17 00:00:00 2001 From: SunAhong1993 Date: Thu, 25 Mar 2021 15:50:14 +0800 Subject: [PATCH] add DepthToSpace --- .../dygraph/onnx2paddle/opset9/opset.py | 47 +++++++++++++++++++ .../static/onnx2paddle/opset9/opset.py | 47 +++++++++++++++++++ 2 files changed, 94 insertions(+) diff --git a/x2paddle/op_mapper/dygraph/onnx2paddle/opset9/opset.py b/x2paddle/op_mapper/dygraph/onnx2paddle/opset9/opset.py index fedb75f..2858915 100755 --- a/x2paddle/op_mapper/dygraph/onnx2paddle/opset9/opset.py +++ b/x2paddle/op_mapper/dygraph/onnx2paddle/opset9/opset.py @@ -2043,3 +2043,50 @@ class OpSet9(): inputs={"x": val_x.name}, outputs=layer_outputs, **layer_attrs) + + @print_mapping_info + def DepthToSpace(self, node): + val_x = self.graph.get_input_node(node, idx=0, copy=True) + blocksize = node.get_attr('blocksize') + mode = node.get_attr('mode', "DCR") + val_x_shape = val_x.out_shapes[0] + b, c, h, w = val_x_shape + if mode == "DCR": + self.paddle_graph.add_layer( + 'paddle.reshape', + inputs={"x": val_x.name}, + outputs=[node.name], + shape=[b, blocksize, blocksize, c // (blocksize**2), h, w] + ) + self.paddle_graph.add_layer( + 'paddle.transpose', + inputs={"x": node.name}, + outputs=[node.name], + perm=[0, 3, 4, 1, 5, 2] + ) + self.paddle_graph.add_layer( + 'paddle.reshape', + inputs={"x": node.name}, + outputs=[node.name], + shape=[b, c // (blocksize**2), h * blocksize, w * blocksize] + ) + else: + self.paddle_graph.add_layer( + 'paddle.reshape', + inputs={"x": val_x.name}, + outputs=[node.name], + shape=[b, c // (blocksize ** 2), blocksize, blocksize, h, w] + ) + self.paddle_graph.add_layer( + 'paddle.transpose', + inputs={"x": node.name}, + outputs=[node.name], + perm=[0, 1, 4, 2, 5, 3] + ) + self.paddle_graph.add_layer( + 'paddle.reshape', + inputs={"x": node.name}, + outputs=[node.name], + shape=[b, c // (blocksize ** 2), h * blocksize, w * blocksize] + ) + \ No newline at end of file diff --git a/x2paddle/op_mapper/static/onnx2paddle/opset9/opset.py b/x2paddle/op_mapper/static/onnx2paddle/opset9/opset.py index ef7e97f..220d561 100644 --- a/x2paddle/op_mapper/static/onnx2paddle/opset9/opset.py +++ b/x2paddle/op_mapper/static/onnx2paddle/opset9/opset.py @@ -1810,3 +1810,50 @@ class OpSet9(): inputs={"x": val_x.name}, outputs=[node.name], **layer_attrs) + + @print_mapping_info + def DepthToSpace(self, node): + val_x = self.graph.get_input_node(node, idx=0, copy=True) + blocksize = node.get_attr('blocksize') + mode = node.get_attr('mode', "DCR") + val_x_shape = val_x.out_shapes[0] + b, c, h, w = val_x_shape + if mode == "DCR": + self.paddle_graph.add_layer( + 'paddle.reshape', + inputs={"x": val_x.name}, + outputs=[node.name], + shape=[b, blocksize, blocksize, c // (blocksize**2), h, w] + ) + self.paddle_graph.add_layer( + 'paddle.transpose', + inputs={"x": node.name}, + outputs=[node.name], + perm=[0, 3, 4, 1, 5, 2] + ) + self.paddle_graph.add_layer( + 'paddle.reshape', + inputs={"x": node.name}, + outputs=[node.name], + shape=[b, c // (blocksize**2), h * blocksize, w * blocksize] + ) + else: + self.paddle_graph.add_layer( + 'paddle.reshape', + inputs={"x": val_x.name}, + outputs=[node.name], + shape=[b, c // (blocksize ** 2), blocksize, blocksize, h, w] + ) + self.paddle_graph.add_layer( + 'paddle.transpose', + inputs={"x": node.name}, + outputs=[node.name], + perm=[0, 1, 4, 2, 5, 3] + ) + self.paddle_graph.add_layer( + 'paddle.reshape', + inputs={"x": node.name}, + outputs=[node.name], + shape=[b, c // (blocksize ** 2), h * blocksize, w * blocksize] + ) + \ No newline at end of file -- GitLab