Skip to content

Update conv_transpose api #26427

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

Merged
merged 8 commits into from
Aug 22, 2020
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
40 changes: 39 additions & 1 deletion paddle/fluid/operators/conv_transpose_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ void ConvTransposeOp::InferShape(framework::InferShapeContext* ctx) const {
auto filter_dims = ctx->GetInputDim("Filter");
std::vector<int> output_size =
ctx->Attrs().Get<std::vector<int>>("output_size");
std::vector<int> output_padding =
ctx->Attrs().Get<std::vector<int>>("output_padding");
std::vector<int> strides = ctx->Attrs().Get<std::vector<int>>("strides");
std::vector<int> paddings = ctx->Attrs().Get<std::vector<int>>("paddings");
std::vector<int> dilations = ctx->Attrs().Get<std::vector<int>>("dilations");
Expand Down Expand Up @@ -78,6 +80,12 @@ void ConvTransposeOp::InferShape(framework::InferShapeContext* ctx) const {
platform::errors::InvalidArgument(
"The Attr(output_size) and Attr(stride) of Op(conv_transpose) "
"should be the same."));
if (output_padding.size())
PADDLE_ENFORCE_EQ(
output_padding.size(), strides.size(),
platform::errors::InvalidArgument(
"The Attr(output_padding) and Attr(stride) of Op(conv_transpose) "
"should be the same."));

const int64_t C =
(data_layout != DataLayout::kNHWC ? in_dims[1]
Expand Down Expand Up @@ -136,6 +144,27 @@ void ConvTransposeOp::InferShape(framework::InferShapeContext* ctx) const {
infer_shape + strides[i]));
}
output_shape.push_back(output_size[i]);
} else if (output_padding.size()) {
if (ctx->IsRuntime()) {
PADDLE_ENFORCE_GE(
output_padding[i], 0,
platform::errors::InvalidArgument(
"output_padding of Op(ConvTransposeOp) should not be "
"less than the 0. But received output_padding = "
"[%s], whose dim %d is less than 0",
framework::make_ddim(output_padding), i));
PADDLE_ENFORCE_LT(
output_padding[i], std::max(strides[i], dilations[i]),
platform::errors::InvalidArgument(
"output_padding of Op(ConvTransposeOp) should be less "
"than either stride or dilation. But received output_size = "
"[%s], "
"whose dim %d is not less than either stride (%d) or "
"dilation (%d)",
framework::make_ddim(output_size), i, strides[i],
dilations[i]));
}
output_shape.push_back((infer_shape + output_padding[i]));
} else {
output_shape.push_back(infer_shape);
}
Expand Down Expand Up @@ -223,10 +252,14 @@ void Conv2DTransposeOpMaker::Make() {
"The format of output tensor is X (one-dimensional) of size equal"
"to the number of output channels. Only used with MKL-DNN.")
.AsDispensable();

AddOutput("Output",
"(Tensor) The output tensor of convolution transpose operator. "
"The format of output tensor is the same as input tensor.");
AddAttr<std::vector<int>>("output_padding",
"(vector<int> default: []), Additional size added "
"to one side of each dimension in the output "
"shape")
.SetDefault({});
AddAttr<std::vector<int>>("output_size",
"(vector<int> default: []), the "
"size of the output tensor")
Expand Down Expand Up @@ -338,6 +371,11 @@ void Conv3DTransposeOpMaker::Make() {
"Where N is batch size, C is "
"the number of channels, D is the depth of the feature, H is the "
"height of the feature, and W is the width of the feature.");
AddAttr<std::vector<int>>("output_padding",
"(vector<int> default: []), Additional size added "
"to one side of each dimension in the output "
"shape")
.SetDefault({});
AddAttr<std::vector<int>>("output_size",
"(vector<int> default: []), the "
"size of the output tensor")
Expand Down
64 changes: 38 additions & 26 deletions python/paddle/fluid/tests/unittests/test_conv2d_transpose_layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,12 @@ def __init__(self,
num_filters=8,
filter_size=3,
output_size=None,
output_padding=0,
padding=0,
stride=1,
dilation=1,
groups=1,
act=None,
no_bias=False,
use_cudnn=True,
data_format="NCHW",
dtype="float32"):
super(Conv2DTransposeTestCase, self).__init__(methodName)
Expand All @@ -45,14 +44,13 @@ def __init__(self,
self.spartial_shape = spartial_shape
self.filter_size = filter_size
self.output_size = output_size
self.output_padding = output_padding

self.padding = padding
self.stride = stride
self.dilation = dilation
self.groups = groups
self.act = act
self.no_bias = no_bias
self.use_cudnn = use_cudnn
self.data_format = data_format
self.dtype = dtype

Expand Down Expand Up @@ -93,6 +91,7 @@ def fluid_layer(self, place):
bias_attr = False
else:
bias_attr = I.NumpyArrayInitializer(self.bias)

y_var = fluid.layers.conv2d_transpose(
x_var,
self.num_filters,
Expand All @@ -104,8 +103,6 @@ def fluid_layer(self, place):
groups=self.groups,
param_attr=weight_attr,
bias_attr=bias_attr,
use_cudnn=self.use_cudnn,
act=self.act,
data_format=self.data_format)
feed_dict = {"input": self.input}
exe = fluid.Executor(place)
Expand All @@ -125,17 +122,22 @@ def functional(self, place):
"weight", self.weight_shape, dtype=self.dtype)
b_var = fluid.data(
"bias", (self.num_filters, ), dtype=self.dtype)
y_var = F.conv2d_transpose(

if self.output_padding != 0:
output_size = None
else:
output_size = self.output_size

y_var = F.conv_transpose2d(
x_var,
w_var,
None if self.no_bias else b_var,
output_size=self.output_size,
output_size=output_size,
padding=self.padding,
output_padding=self.output_padding,
stride=self.stride,
dilation=self.dilation,
groups=self.groups,
act=self.act,
use_cudnn=self.use_cudnn,
data_format=self.data_format)
feed_dict = {"input": self.input, "weight": self.weight}
if self.bias is not None:
Expand All @@ -147,32 +149,38 @@ def functional(self, place):

def paddle_nn_layer(self):
x_var = dg.to_variable(self.input)
conv = nn.Conv2DTranspose(

if self.output_padding != 0:
output_size = None
else:
output_size = self.output_size

conv = nn.ConvTranspose2d(
self.num_channels,
self.num_filters,
self.filter_size,
output_size=self.output_size,
padding=self.padding,
output_padding=self.output_padding,
stride=self.stride,
dilation=self.dilation,
groups=self.groups,
act=self.act,
use_cudnn=self.use_cudnn,
data_format=self.data_format,
dtype=self.dtype)
data_format=self.data_format)
conv.weight.set_value(self.weight)
if not self.no_bias:
conv.bias.set_value(self.bias)
y_var = conv(x_var)
y_var = conv(x_var, output_size)
y_np = y_var.numpy()
return y_np

def _test_equivalence(self, place):
place = fluid.CPUPlace()

result1 = self.fluid_layer(place)
result2 = self.functional(place)

with dg.guard(place):
result3 = self.paddle_nn_layer()

np.testing.assert_array_almost_equal(result1, result2)
np.testing.assert_array_almost_equal(result2, result3)

Expand All @@ -194,7 +202,7 @@ def runTest(self):


def add_cases(suite):
suite.addTest(Conv2DTransposeTestCase(methodName='runTest', act="relu"))
suite.addTest(Conv2DTransposeTestCase(methodName='runTest'))
suite.addTest(
Conv2DTransposeTestCase(
methodName='runTest', stride=[1, 2], no_bias=True, dilation=2))
Expand All @@ -211,9 +219,6 @@ def add_cases(suite):
suite.addTest(
Conv2DTransposeTestCase(
methodName='runTest', padding="valid"))
suite.addTest(
Conv2DTransposeTestCase(
methodName='runTest', padding='valid'))
suite.addTest(
Conv2DTransposeTestCase(
methodName='runTest', filter_size=1, padding=(2, 3)))
Expand All @@ -240,15 +245,22 @@ def add_cases(suite):
num_filters=6,
num_channels=3,
groups=3,
use_cudnn=False,
act="sigmoid",
padding="valid"))
suite.addTest(
Conv2DTransposeTestCase(
methodName='runTest',
num_filters=6,
num_channels=3,
spartial_shape=(7, 7),
filter_size=[5, 5],
groups=1,
padding=2,
stride=2,
output_size=[14, 14],
output_padding=[1, 1], ))


def add_error_cases(suite):
suite.addTest(
Conv2DTransposeErrorTestCase(
methodName='runTest', use_cudnn="not_valid"))
suite.addTest(
Conv2DTransposeErrorTestCase(
methodName='runTest', num_channels=5, groups=2))
Expand Down
41 changes: 38 additions & 3 deletions python/paddle/fluid/tests/unittests/test_conv2d_transpose_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,13 @@ def _get_padding_with_SAME(input_shape, kernel_size, kernel_stride):
output_size = attrs['output_size']
out_h = output_size[0] + pad_h_0 + pad_h_1
out_w = output_size[1] + pad_w_0 + pad_w_1

out = np.zeros((in_n, out_c, out_h, out_w), dtype=input_.dtype)
out_pad_h = 0
out_pad_w = 0
if 'output_padding' in attrs:
out_pad_h = attrs['output_padding'][0]
out_pad_w = attrs['output_padding'][1]
out = np.zeros(
(in_n, out_c, out_h + out_pad_h, out_w + out_pad_w), dtype=input_.dtype)

for n in range(in_n):
for i in range(in_h):
Expand All @@ -99,7 +104,8 @@ def _get_padding_with_SAME(input_shape, kernel_size, kernel_stride):
out[n, g * f_out_c + k, i1:i2:dilations[0], j1:j2:
dilations[1]] += tmp_out

out = out[:, :, pad_h_0:out_h - pad_h_1, pad_w_0:out_w - pad_w_1]
out = out[:, :, pad_h_0:out_h - pad_h_1 + out_pad_h, pad_w_0:out_w - pad_w_1
+ out_pad_w]
if attrs['data_format'] == 'NHWC':
out = np.transpose(out, [0, 2, 3, 1])
return out
Expand All @@ -114,6 +120,7 @@ def setUp(self):
self.use_cudnn = False
self.use_mkldnn = False
self.output_size = None
self.output_padding = []
self.data_format = "NCHW"
self.pad = [0, 0]
self.padding_algorithm = "EXPLICIT"
Expand All @@ -138,6 +145,9 @@ def setUp(self):
if self.output_size is not None:
self.attrs['output_size'] = self.output_size

if len(self.output_padding) > 0:
self.attrs['output_padding'] = self.output_padding

output = conv2dtranspose_forward_naive(input_, filter_,
self.attrs).astype(self.dtype)

Expand Down Expand Up @@ -290,6 +300,18 @@ def init_test_case(self):
self.filter_size = [f_c, 6, 5, 5]


class TestWithEvenUpsampleOutputPadding(TestConv2dTransposeOp):
def init_test_case(self):
self.pad = [2, 2]
self.stride = [2, 2]
self.groups = 1
self.dilations = [1, 1]
self.output_padding = [1, 1]
self.input_size = [2, 3, 7, 7] # NCHW
f_c = self.input_size[1]
self.filter_size = [f_c, 6, 5, 5]


class Test_NHWC(TestConv2dTransposeOp):
def init_test_case(self):
self.pad = [0, 0]
Expand Down Expand Up @@ -375,6 +397,19 @@ def init_test_case(self):
self.data_format = 'NHWC'


class TestWithEvenUpsample_NHWC_output_padding(TestConv2dTransposeOp):
def init_test_case(self):
self.pad = [2, 2]
self.stride = [2, 2]
self.groups = 1
self.dilations = [1, 1]
self.output_padding = [1, 1]
self.input_size = [2, 7, 7, 3] # NHWC
f_c = self.input_size[-1]
self.filter_size = [f_c, 6, 5, 5]
self.data_format = 'NHWC'


# ------------ test_cudnn ------------
@unittest.skipIf(not core.is_compiled_with_cuda(),
"core is not compiled with CUDA")
Expand Down
Loading