From 32db8db51c5384f213a0b1402d2632519da5416a Mon Sep 17 00:00:00 2001 From: gongweibao Date: Tue, 17 Oct 2017 08:12:56 +0000 Subject: [PATCH] fix bugs --- paddle/operators/block_expand_op.cc | 9 +- paddle/operators/block_expand_op.h | 9 +- .../framework/tests/test_block_expand_op.py | 176 +++++++++++------- 3 files changed, 120 insertions(+), 74 deletions(-) diff --git a/paddle/operators/block_expand_op.cc b/paddle/operators/block_expand_op.cc index 49c7011fe..37ea57f39 100644 --- a/paddle/operators/block_expand_op.cc +++ b/paddle/operators/block_expand_op.cc @@ -23,6 +23,7 @@ class BlockExpandOp : public framework::OperatorWithKernel { protected: void InferShape(framework::InferShapeContext* ctx) const override { + printf("op infershape\n"); using namespace framework; PADDLE_ENFORCE(ctx->HasInput("X"), "Input of BlockExpandOp should not be null."); @@ -33,6 +34,7 @@ class BlockExpandOp : public framework::OperatorWithKernel { PADDLE_ENFORCE_EQ(in_dim.size(), 4, "Input format must be NCHW."); PADDLE_ENFORCE_GE(in_dim[0], 1, "Input batchsize must >= 1."); + printf("op infershape2\n"); int block_height = ctx->Attrs().Get("blockHeight"); int block_width = ctx->Attrs().Get("blockWidth"); int stride_height = ctx->Attrs().Get("strideHeight"); @@ -42,8 +44,8 @@ class BlockExpandOp : public framework::OperatorWithKernel { int N = in_dim[0]; int C = in_dim[1]; - int img_height = in_dim[3]; - int img_width = in_dim[4]; + int img_height = in_dim[2]; + int img_width = in_dim[3]; int output_height = 0; int output_width = 0; @@ -58,6 +60,8 @@ class BlockExpandOp : public framework::OperatorWithKernel { // reshape into [seqLength, stepSize], where seqLength is equal // output_height * output_width, stepSize is equal // input_channels * blockHeight * blockWidth + printf("N:%d, o_h:%d o_w:%d C:%d b_h:%d b_w:%d\n", N, output_height, + output_width, C, block_height, block_width); ctx->SetOutputDim( "Out", {N, output_height, output_width, C, block_height, block_width}); @@ -77,6 +81,7 @@ class BlockExpandOpMaker : public framework::OpProtoAndCheckerMaker { H: height W: width )DOC"); + printf("opmakeer\n"); AddOutput("Out", "(LodTensor)The output data of block_expand op,"); AddAttr("blockHeight", "(int)height of block."); AddAttr("blockWidth", "(int)width of block."); diff --git a/paddle/operators/block_expand_op.h b/paddle/operators/block_expand_op.h index b27258288..69bd7d698 100644 --- a/paddle/operators/block_expand_op.h +++ b/paddle/operators/block_expand_op.h @@ -44,7 +44,7 @@ class BlockExpandKernel : public framework::OpKernel { public: void Compute(const framework::ExecutionContext& ctx) const override { using namespace framework; - const Tensor* in = ctx.Input("input"); + const Tensor* in = ctx.Input("X"); Tensor* out = ctx.Output("Out"); out->mutable_data(ctx.GetPlace()); @@ -68,7 +68,11 @@ class BlockExpandKernel : public framework::OpKernel { img_height, img_width, block_height, block_width, stride_height, stride_width, padding_height, padding_width, outputHeight, outputWidth); + printf("N:%d, o_h:%d o_w:%d C:%d b_h:%d b_w:%d\n", N, outputHeight, + outputWidth, C, block_height, block_width); + for (int i = 0; i < N; i++) { + printf("i:%d\n", i); Tensor src = in->Slice(i, i + 1).Resize({C, img_height, img_width}); Tensor dst = out->Slice(i, i + 1).Resize( {outputHeight, outputWidth, C, block_height, block_width}); @@ -109,6 +113,9 @@ class BlockExpandGradKernel : public framework::OpKernel { img_height, img_width, block_height, block_width, stride_height, stride_width, padding_height, padding_width, outputHeight, outputWidth); + printf("N:%d, o_h:%d o_w:%d C:%d b_h:%d b_w:%d\n", N, outputHeight, + outputWidth, C, block_height, block_width); + for (int i = 0; i < N; i++) { Tensor dst = out_grad->Slice(i, i + 1).Resize({C, img_height, img_width}); diff --git a/python/paddle/v2/framework/tests/test_block_expand_op.py b/python/paddle/v2/framework/tests/test_block_expand_op.py index aa4fa479a..f8f4afc88 100644 --- a/python/paddle/v2/framework/tests/test_block_expand_op.py +++ b/python/paddle/v2/framework/tests/test_block_expand_op.py @@ -3,119 +3,153 @@ import numpy as np from op_test import OpTest -def get_output_shape(attrs, X): - img_height = X.shape[2] - img_width = X.shpe[3] - padding_height = attrs['padding_height'] - padding_width = attrs['padding_width'] - block_height = attrs['block_height'] - block_width = attrs['block_width'] - stride_height = attrs['stride_height'] - stride_width = attrs['stride_width'] - output_height = \ +def get_output_shape(attrs, x): + imgHeight = x.shape[1] + imgWidth = x.shape[2] + + paddingHeight = attrs['paddingHeight'] + paddingWidth = attrs['paddingWidth'] + blockHeight = attrs['blockHeight'] + blockWidth = attrs['blockWidth'] + strideHeight = attrs['strideHeight'] + strideWidth = attrs['strideWidth'] + + outputHeight = \ 1 + \ - (img_height + 2 * padding_height - block_height + stride_height - 1) / \ - stride_height + (imgHeight + 2 * paddingHeight - blockHeight + strideHeight - 1) / \ + strideHeight - output_width = \ + outputWidth = \ 1 + \ - (img_width + 2 * padding_width - block_width + stride_width - 1) / \ - stride_width + (imgWidth + 2 * paddingWidth - blockWidth + strideWidth - 1) / \ + strideWidth - return output_height, output_width + return outputHeight, outputWidth """ -img: {CHW} +im: {CHW} col: - {output_height, output_width, inputChannels, filterHeight, filterWidth} + {outputHeight, outputWidth, inputChannels, filterHeight, filterWidth} """ -def img2col(attrs, im, col): - input_channels = im.shape.dims[0] - input_height = im.shape.dims[1] - input_width = im.shape.dims[2] - filter_height = col.shape.dims[3] - filter_width = col.shape.dims[4] - output_height = col.shape.dims[0] - output_width = col.shape.dims[1] +def im2col(attrs, im, col): + input_channels = im.shape[0] + inputHeight = im.shape[1] + inputWidth = im.shape[2] + + outputHeight = col.shape[0] + outputWidth = col.shape[1] + filterHeight = col.shape[3] + filterWidth = col.shape[4] - for col_row_idx in range(0, output_height): - for col_col_idx in range(0, output_width): + strideHeight = attrs['strideHeight'] + strideWidth = attrs['strideWidth'] + paddingHeight = attrs['paddingHeight'] + paddingWidth = attrs['paddingWidth'] + + for col_row_idx in range(0, outputHeight): + for col_col_idx in range(0, outputWidth): for channel in range(0, input_channels): - for filter_row_idx in range(0, filter_height): - for filter_col_idx in range(0, filter_width): - im_row_offset = col_row_idx * stride_height \ - + filter_row_idx - padding_height - im_col_offset = col_col_idx * stride_width \ - + filter_col_idx - padding_width - if (im_row_offset < 0 or - im_row_offset >= input_height or + for filter_row_idx in range(0, filterHeight): + for filter_col_idx in range(0, filterWidth): + im_row_offset = col_row_idx * strideHeight \ + + filter_row_idx - paddingHeight + + im_col_offset = col_col_idx * strideWidth \ + + filter_col_idx - paddingWidth + + if (im_row_offset < 0 or im_row_offset >= inputHeight or im_col_offset < 0 or - im_col_offset >= input_width): - col[col_row_idx][col_col_idx][channel][ + im_col_offset >= inputWidth): + col[col_row_idx][col_col_idx][channel][\ filter_row_idx][filter_col_idx] = 0.0 else: - im_offset = (channel * input_height + im_row_offset - ) * input_width + im_col_offset - col[col_row_idx][col_col_idx][channel][ - filter_row_idx][filter_col_idx] = im[channel][ + im_offset = (channel * inputHeight + im_row_offset \ + ) * inputWidth + im_col_offset + + col[col_row_idx][col_col_idx][channel][\ + filter_row_idx][filter_col_idx] = im[channel][ \ im_row_offset][im_col_offset] """ img: {CHW} col: - {output_height, output_width, inputChannels, filterHeight, filterWidth} + {outputHeight, outputWidth, inputChannels, filterHeight, filterWidth} """ def col2img(attrs, col, img): - input_channels = im.shape.dims[0] - input_height = im.shape.dims[1] - input_width = im.shape.dims[2] - filter_height = col.shape.dims[3] - filter_width = col.shape.dims[4] - output_height = col.shape.dims[0] - output_width = col.shape.dims[1] - - for col_row_idx in range(0, output_height): - for col_col_idx in range(0, output_width): + input_channels = im.shape[0] + inputHeight = im.shape[1] + inputWidth = im.shape[2] + + outputHeight = col.shape[0] + outputWidth = col.shape[1] + filterHeight = col.shape[3] + filterWidth = col.shape[4] + + strideHeight = attrs['strideHeight'] + strideWidth = attrs['strideWidth'] + paddingHeight = attrs['paddingHeight'] + paddingWidth = attrs['paddingWidth'] + + for col_row_idx in range(0, outputHeight): + for col_col_idx in range(0, outputWidth): for channel in range(0, input_channels): - for filter_row_idx in range(0, filter_height): - for filter_col_idx in range(0, filter_width): + for filter_row_idx in range(0, filterHeight): + for filter_col_idx in range(0, filterWidth): im_row_offset = \ - col_row_idx * stride_height + filter_row_idx - padding_height + col_row_idx * strideHeight + filter_row_idx - paddingHeight im_col_offset = \ - col_col_idx * stride_width + filter_col_idx - padding_width + col_col_idx * strideWidth + filter_col_idx - paddingWidth if (im_row_offset >= 0 and - im_row_offset < input_height and + im_row_offset < inputHeight and im_col_offset >= 0 and - im_col_offset < input_width): + im_col_offset < inputWidth): im[channel][im_row_offset][im_col_offset] = \ col[col_row_idx][col_col_idx][channel][filter_row_idx][filter_col_idx] class TestBlockExpandMulOp(OpTest): def setUp(self): - self.op_type = "block_expand" - self.inputs = { - 'X': np.random.uniform(0.1, 1, [2, 3, 9, 9]).astype("float64"), - } - self.attrs = { - 'block_height': 3, - 'block_width': 3, - 'stride_height': 2, - 'stride_width': 2, - 'padding_height': 3, - 'padding_width': 3, + x = np.random.uniform(0.1, 1, [3, 9, 9]).astype("float32") + attrs = { + 'blockHeight': 3, + 'blockWidth': 3, + 'strideHeight': 2, + 'strideWidth': 2, + 'paddingHeight': 3, + 'paddingWidth': 3, } - self.outputs = {'Out': np.multiply(self.inputs['X'], self.inputs['Y'])} + outputHeight, outputWidth = get_output_shape(attrs, x) + out = np.random.uniform(0.1, 1,\ + [outputHeight, outputWidth, x.shape[0], \ + attrs['blockHeight'], attrs['blockWidth']]).astype("float32") + + self.op_type = "block_expand" + self.inputs = {'X': x.reshape(1, 3, 9, 9)} + self.attrs = attrs + + im2col(attrs, x, out) + self.outputs = { + 'Out':out.reshape(1, outputHeight, outputWidth, x.shape[0], \ + attrs['blockHeight'], attrs['blockWidth']) + } + #print out def test_check_output(self): self.check_output() + print 1 + """ def test_check_grad_normal(self): self.check_grad(['X'], 'Out') + """ + + +if __name__ == '__main__': + unittest.main() -- GitLab