From 7bbeeb59aac054aa0cfd10443a2c3ed726260b71 Mon Sep 17 00:00:00 2001 From: wangguanzhong Date: Thu, 17 Jun 2021 08:26:46 +0800 Subject: [PATCH] cherry-pick fix output padding conv (#33587) * cherry-pick fix_output_padding_conv * add repr unittest for conv --- .../tests/unittests/test_conv2d_transpose_op.py | 13 +++++++++++++ python/paddle/nn/layer/conv.py | 12 ++++++------ 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/python/paddle/fluid/tests/unittests/test_conv2d_transpose_op.py b/python/paddle/fluid/tests/unittests/test_conv2d_transpose_op.py index 4e582d74c24..b106f7aa9c1 100644 --- a/python/paddle/fluid/tests/unittests/test_conv2d_transpose_op.py +++ b/python/paddle/fluid/tests/unittests/test_conv2d_transpose_op.py @@ -18,6 +18,7 @@ import unittest import numpy as np import paddle +import paddle.nn as nn paddle.enable_static() import paddle.fluid.core as core import paddle.fluid as fluid @@ -898,5 +899,17 @@ class TestConv2DTransposeOpException(unittest.TestCase): self.assertRaises(ValueError, attr_padding_with_data_format) +class TestConv2DTransposeRepr(unittest.TestCase): + def test_case(self): + paddle.disable_static() + x_var = paddle.uniform((2, 4, 8, 8), dtype='float32', min=-1., max=1.) + conv = nn.Conv2DTranspose(4, 6, (3, 3), output_padding=1, stride=2) + print(conv) + y_var = conv(x_var) + y_np = y_var.numpy() + self.assertIsNotNone(y_np) + paddle.enable_static() + + if __name__ == '__main__': unittest.main() diff --git a/python/paddle/nn/layer/conv.py b/python/paddle/nn/layer/conv.py index 51eab0d1838..12700035e5e 100644 --- a/python/paddle/nn/layer/conv.py +++ b/python/paddle/nn/layer/conv.py @@ -98,7 +98,7 @@ class _ConvNd(layers.Layer): 'kernel_size') self._padding = padding self._padding_mode = padding_mode - self._output_padding = output_padding + self.output_padding = output_padding if dims != 1: self._updated_padding, self._padding_algorithm = _update_padding_nd( padding, channel_last, dims) @@ -163,8 +163,8 @@ class _ConvNd(layers.Layer): main_str += ', padding={_padding}' if self._padding_mode is not 'zeros': main_str += ', padding_mode={_padding_mode}' - if self._output_padding != 0: - main_str += ', output_padding={_output_padding}' + if self.output_padding != 0: + main_str += ', output_padding={output_padding}' if self._dilation != [1] * len(self._dilation): main_str += ', dilation={_dilation}' if self._groups != 1: @@ -502,7 +502,7 @@ class Conv1DTranspose(_ConvNd): self.weight, bias=self.bias, output_size=output_size, - output_padding=self._output_padding, + output_padding=self.output_padding, padding=self._padding, stride=self._stride, dilation=self._dilation, @@ -810,7 +810,7 @@ class Conv2DTranspose(_ConvNd): def forward(self, x, output_size=None): if output_size is None: - output_padding = self._output_padding + output_padding = self.output_padding else: output_padding = 0 @@ -1139,7 +1139,7 @@ class Conv3DTranspose(_ConvNd): def forward(self, x, output_size=None): if output_size is None: - output_padding = self._output_padding + output_padding = self.output_padding else: output_padding = 0 -- GitLab