block_expand_op.cc 4.6 KB
Newer Older
G
gongweibao 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */

#include "paddle/operators/block_expand_op.h"

namespace paddle {
namespace operators {

class BlockExpandOp : public framework::OperatorWithKernel {
 public:
  using framework::OperatorWithKernel::OperatorWithKernel;

 protected:
  void InferShape(framework::InferShapeContext* ctx) const override {
G
gongweibao 已提交
26
    using namespace framework;
G
gongweibao 已提交
27
    PADDLE_ENFORCE(ctx->HasInput("X"),
G
gongweibao 已提交
28 29
                   "Input of BlockExpandOp should not be null.");
    PADDLE_ENFORCE(ctx->HasOutput("Out"),
G
gongweibao 已提交
30
                   "Output of BlockExpandOp op should not be null.");
G
gongweibao 已提交
31

G
gongweibao 已提交
32
    auto in_dim = ctx->GetInputDim("X");
G
gongweibao 已提交
33 34 35
    PADDLE_ENFORCE_EQ(in_dim.size(), 4, "Input format  must be NCHW.");
    PADDLE_ENFORCE_GE(in_dim[0], 1, "Input batchsize must >= 1.");

G
gongweibao 已提交
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
    int blockHeight = ctx->Attrs().Get<int>("blockHeight");
    int blockWidth = ctx->Attrs().Get<int>("blockWidth");
    int strideHeight = ctx->Attrs().Get<int>("strideHeight");
    int strideWidth = ctx->Attrs().Get<int>("strideWidth");
    int paddingHeight = ctx->Attrs().Get<int>("paddingHeight");
    int paddingWidth = ctx->Attrs().Get<int>("paddingWidth");

    int N = in_dim[0];
    int C = in_dim[1];
    int imgHeight = in_dim[3];
    int imgWidth = in_dim[4];

    int outputHeight = 0;
    int outputWidth = 0;

    get_blockexpand_output_shape(imgHeight, imgWidth, blockHeight, blockWidth,
                                 strideHeight, strideWidth, paddingHeight,
                                 paddingWidth, outputHeight, outputWidth);

    // The result of im2col is [outputHeight, outputWidth,
    // inputChannels, filterHeight, filterWidth], and it is easy to
    // reshape into [seqLength, stepSize], where seqLength is equal
    // outputHeight * outputWidth, stepSize is equal
    // input_channels * blockHeight * blockWidth
    ctx->SetOutputDim(
        "Out", {N, outputHeight, outputWidth, C, blockHeight, blockWidth});

    // ctx->ShareLoD("X", /*->*/ "Out");
G
gongweibao 已提交
64 65 66 67 68 69 70 71
  }
};

class BlockExpandOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
  BlockExpandOpMaker(framework::OpProto* proto,
                     framework::OpAttrChecker* op_checker)
      : OpProtoAndCheckerMaker(proto, op_checker) {
G
gongweibao 已提交
72 73 74 75 76 77 78 79 80 81 82 83 84 85
    AddInput("X", R"DOC(
(Tensor)The input tensor has NCHW format.
    N: batch size
    C: channels
    H: height
    W: width
)DOC");
    AddOutput("Out", "(LodTensor)The output data of block_expand op,");
    AddAttr<int>("blockHeight", "(int)height of block.");
    AddAttr<int>("blockWidth", "(int)width of block.");
    AddAttr<int>("strideHeight", "(int)height of stride.");
    AddAttr<int>("strideWidth", "(int)width of stride.");
    AddAttr<int>("paddingHeight", "(int)height of padding.");
    AddAttr<int>("paddingWidth", "(int)width of padding.");
G
gongweibao 已提交
86 87
    AddComment(R"DOC(
Expand feature map to minibatch matrix.
G
gongweibao 已提交
88 89
- matirx height is: outputHeight * outputWidth
- matrix width is: blockHeight * blockWidth * channels
G
gongweibao 已提交
90

G
gongweibao 已提交
91 92 93 94 95 96
outputHeight = 
    1 + (2 * paddingHeight + imgHeight - blockHeight + strideHeight - 1) /
            strideHeight;
outputWidth = 
    1 + (2 * paddingWidth + imgWidth - blockWidth + strideWidth - 1) /
            strideWidth;
G
gongweibao 已提交
97 98

The expand method is the same with ExpandConvLayer, but saved the transposed
G
gongweibao 已提交
99 100 101
value. After expanding, The number of time steps are outputHeight * outputWidth
and the dimension of each time step is blockHeight * blockWidth * channels.
This layer can be used after convolution neural network, and before recurrent neural network.
G
gongweibao 已提交
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
)DOC");
  }
};

class BlockExpandGradOp : public framework::OperatorWithKernel {
 public:
  using framework::OperatorWithKernel::OperatorWithKernel;

 protected:
  void InferShape(framework::InferShapeContext* ctx) const override {}
};

}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
REGISTER_OP(block_expand, ops::BlockExpandOp, ops::BlockExpandOpMaker,
            block_expand_grad, ops::BlockExpandOpGrad);
REGISTER_OP_CPU_KERNEL(
G
gongweibao 已提交
121
    block_expand, ops::BlockExpandKernel<paddle::platform::CPUPlace, float>);
G
gongweibao 已提交
122 123 124
REGISTER_OP_CPU_KERNEL(
    block_expand_grad,
    ops::BlockExpandGradKernel<paddle::platform::CPUPlace, float>);