From 6ef2da2e89a388e3d7419fd61ef82e1fb33d349a Mon Sep 17 00:00:00 2001 From: chengduoZH Date: Thu, 12 Oct 2017 12:57:19 +0800 Subject: [PATCH] finetune conv2d navie func --- .../v2/framework/tests/test_conv2d_op.py | 21 +++++++++---------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/python/paddle/v2/framework/tests/test_conv2d_op.py b/python/paddle/v2/framework/tests/test_conv2d_op.py index 478579cca3..f5d32b1356 100644 --- a/python/paddle/v2/framework/tests/test_conv2d_op.py +++ b/python/paddle/v2/framework/tests/test_conv2d_op.py @@ -11,19 +11,19 @@ def conv2d_forward_naive(input, filter, group, conv_param): sub_out_c = out_c / group stride, pad = conv_param['stride'], conv_param['pad'] - out_h = 1 + (in_h + 2 * pad - f_h) / stride - out_w = 1 + (in_w + 2 * pad - f_w) / stride + out_h = 1 + (in_h + 2 * pad[0] - f_h) / stride[0] + out_w = 1 + (in_w + 2 * pad[1] - f_w) / stride[1] out = np.zeros((in_n, out_c, out_h, out_w)) - input_pad = np.pad(input, ((0, ), (0, ), (pad, ), (pad, )), + input_pad = np.pad(input, ((0, ), (0, ), (pad[0], ), (pad[1], )), mode='constant', constant_values=0) for i in range(out_h): for j in range(out_w): for g in range(group): input_pad_masked = input_pad[:, g * f_c:( - g + 1) * f_c, i * stride:i * stride + f_h, j * stride:j * - stride + f_w] + g + 1) * f_c, i * stride[0]:i * stride[0] + f_h, j * stride[ + 1]:j * stride[1] + f_w] f_sub = filter[g * sub_out_c:(g + 1) * sub_out_c, :, :, :] for k in range(sub_out_c): out[:, g * sub_out_c + k, i, j] = np.sum(input_pad_masked * @@ -37,11 +37,14 @@ class TestConv2dOp(OpTest): def setUp(self): self.init_groups() self.op_type = "conv2d" + pad = [0, 0] + stride = [1, 1] input_size = [2, 3, 5, 5] # NCHW assert np.mod(input_size[1], self.groups) == 0 f_c = input_size[1] / self.groups filter_size = [6, f_c, 3, 3] - conv2d_param = {'stride': 1, 'pad': 0} + + conv2d_param = {'stride': stride, 'pad': pad} input = np.random.random(input_size).astype("float32") filter = np.random.random(filter_size).astype("float32") @@ -49,11 +52,7 @@ class TestConv2dOp(OpTest): output = conv2d_forward_naive(input, filter, self.groups, conv2d_param) self.inputs = {'Input': input, 'Filter': filter} - self.attrs = { - 'strides': [1, 1], - 'paddings': [0, 0], - 'groups': self.groups - } + self.attrs = {'strides': stride, 'paddings': pad, 'groups': self.groups} self.outputs = {'Output': output} def test_check_output(self): -- GitLab