block_expand_op.cc 5.4 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
    PADDLE_ENFORCE_EQ(in_dim.size(), 4,
                      "Input(X) format  must be 4D tensor, eg., NCHW.");
G
gongweibao 已提交
35

W
wanghaoshuang 已提交
36 37 38 39 40 41
    int block_height = ctx->Attrs().Get<int>("block_height");
    int block_width = ctx->Attrs().Get<int>("block_width");
    int stride_height = ctx->Attrs().Get<int>("stride_height");
    int stride_width = ctx->Attrs().Get<int>("stride_width");
    int padding_height = ctx->Attrs().Get<int>("padding_height");
    int padding_width = ctx->Attrs().Get<int>("padding_width");
G
gongweibao 已提交
42

W
wanghaoshuang 已提交
43 44
    int batch_size = in_dim[0];
    int img_channels = in_dim[1];
G
gongweibao 已提交
45 46
    int img_height = in_dim[2];
    int img_width = in_dim[3];
G
gongweibao 已提交
47

W
wanghaoshuang 已提交
48 49 50 51
    int output_height = get_output_size(img_height, block_height, stride_height,
                                        padding_height);
    int output_width =
        get_output_size(img_width, block_width, stride_width, padding_width);
G
gongweibao 已提交
52

W
wanghaoshuang 已提交
53 54 55
    ctx->SetOutputDim("Out", {batch_size * output_height * output_width,
                              img_channels * block_height * block_width});
    // TODO(wanghaoshuang): cal lod in complie time
G
gongweibao 已提交
56 57 58 59 60 61 62 63
  }
};

class BlockExpandOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
  BlockExpandOpMaker(framework::OpProto* proto,
                     framework::OpAttrChecker* op_checker)
      : OpProtoAndCheckerMaker(proto, op_checker) {
G
gongweibao 已提交
64 65 66 67 68 69 70 71
    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,");
W
wanghaoshuang 已提交
72 73 74 75 76 77
    AddAttr<int>("block_height", "(int)height of block.");
    AddAttr<int>("block_width", "(int)width of block.");
    AddAttr<int>("stride_height", "(int)height of stride.");
    AddAttr<int>("stride_width", "(int)width of stride.");
    AddAttr<int>("padding_height", "(int)height of padding.");
    AddAttr<int>("padding_width", "(int)width of padding.");
G
gongweibao 已提交
78 79
    AddComment(R"DOC(
Expand feature map to minibatch matrix.
G
gongweibao 已提交
80
- matirx height is: output_height * output_width
W
wanghaoshuang 已提交
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
- matrix width is: block_height * block_width * channels

output_height =
    1 + (2 * padding_height + img_height - block_height + stride_height - 1) /
            stride_height;
output_width =
    1 + (2 * padding_width + img_width - block_width + stride_width - 1) /
            stride_width;

After expanding, The number of time steps are output_height * output_width
and the dimension of each time step is block_height * block_width * channels.
This op can be used after convolution neural network, and before recurrent neural network.

Given:

x = [[[[ 6.  2.  1.]
       [ 8.  3.  5.]
       [ 0.  2.  6.]]

      [[ 2.  4.  4.]
       [ 6.  3.  0.]
       [ 6.  4.  7.]]]

     [[[ 6.  7.  1.]
       [ 5.  7.  9.]
       [ 2.  4.  8.]]

      [[ 1.  2.  1.]
       [ 1.  3.  5.]
       [ 9.  0.  8.]]]]
x.dims = {2, 2, 3, 3}

And:

block_height = 2
block_width = 2
stride_height = 1
stride_width = 1
padding_height = 0
padding_width = 0

Then:

output.data = [[ 6.  2.  8.  3.  2.  4.  6.  3.]
               [ 2.  1.  3.  5.  4.  4.  3.  0.]
               [ 8.  3.  0.  2.  6.  3.  6.  4.]
               [ 3.  5.  2.  6.  3.  0.  4.  7.]
               [ 6.  7.  5.  7.  1.  2.  1.  3.]
               [ 7.  1.  7.  9.  2.  1.  3.  5.]
               [ 5.  7.  2.  4.  1.  3.  9.  0.]
               [ 7.  9.  4.  8.  3.  5.  0.  8.]]
output.dims = {8, 9}
output.lod = [[0, 4, 8]]

G
gongweibao 已提交
135 136 137 138 139 140 141 142 143
)DOC");
  }
};

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

 protected:
G
add gpu  
gongweibao 已提交
144 145 146 147
  void InferShape(framework::InferShapeContext* ctx) const override {
    using namespace framework;
    PADDLE_ENFORCE(ctx->HasInput("X"), "Input(X) should not be null");
    PADDLE_ENFORCE(ctx->HasInput(framework::GradVarName("Out")),
G
gongweibao 已提交
148 149
                   "Input(Out@GRAD) shouldn't be null.");
    ctx->SetOutputDim(framework::GradVarName("X"), ctx->GetInputDim("X"));
G
add gpu  
gongweibao 已提交
150
  }
G
gongweibao 已提交
151 152 153 154 155 156 157
};

}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
REGISTER_OP(block_expand, ops::BlockExpandOp, ops::BlockExpandOpMaker,
G
add gpu  
gongweibao 已提交
158
            block_expand_grad, ops::BlockExpandGradOp);
G
gongweibao 已提交
159
REGISTER_OP_CPU_KERNEL(
G
gongweibao 已提交
160
    block_expand, ops::BlockExpandKernel<paddle::platform::CPUPlace, float>);
G
gongweibao 已提交
161 162 163
REGISTER_OP_CPU_KERNEL(
    block_expand_grad,
    ops::BlockExpandGradKernel<paddle::platform::CPUPlace, float>);