bilinear_interp_op.cc 4.6 KB
Newer Older
W
wangyang59 已提交
1 2 3 4 5 6 7 8 9 10 11 12
/* 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/fluid/operators/bilinear_interp_op.h"
13
#include <vector>
W
wangyang59 已提交
14
#include "paddle/fluid/framework/op_registry.h"
W
wangyang59 已提交
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31

namespace paddle {
namespace operators {

using framework::Tensor;

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

 protected:
  void InferShape(framework::InferShapeContext* ctx) const override {
    PADDLE_ENFORCE(ctx->HasInput("X"),
                   "Input(X) of BilinearInterOp should not be null.");
    PADDLE_ENFORCE(ctx->HasOutput("Out"),
                   "Output(Out) of BilinearInterOp should not be null.");

32
    auto dim_x = ctx->GetInputDim("X");  // NCHW format
W
wangyang59 已提交
33 34 35 36
    int out_h = ctx->Attrs().Get<int>("out_h");
    int out_w = ctx->Attrs().Get<int>("out_w");
    PADDLE_ENFORCE_EQ(dim_x.size(), 4, "X's dimension must be 4");

37 38 39 40 41 42
    if (ctx->HasInput("OutSize")) {
      auto out_size_dim = ctx->GetInputDim("OutSize");
      PADDLE_ENFORCE_EQ(out_size_dim.size(), 1,
                        "OutSize's dimension size must be 1");
      PADDLE_ENFORCE_EQ(out_size_dim[0], 2, "OutSize's dim[0] must be 2");
    }
W
wangyang59 已提交
43
    std::vector<int64_t> dim_out({dim_x[0], dim_x[1], out_h, out_w});
44
    ctx->SetOutputDim("Out", framework::make_ddim(dim_out));
W
wangyang59 已提交
45
  }
46 47 48 49 50 51 52

 protected:
  framework::OpKernelType GetExpectedKernelType(
      const framework::ExecutionContext& ctx) const override {
    return framework::OpKernelType(
        framework::ToDataType(ctx.Input<Tensor>("X")->type()), ctx.GetPlace());
  }
W
wangyang59 已提交
53 54 55 56
};

class BilinearInterpOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
Y
Yu Yang 已提交
57
  void Make() override {
W
wangyang59 已提交
58
    AddInput("X",
Y
yuyang18 已提交
59
             "The input tensor of bilinear interpolation, "
W
wangyang59 已提交
60
             "This is a 4-D tensor with shape of (N x C x h x w)");
61
    AddInput("OutSize",
Y
yuyang18 已提交
62
             "This is a 1-D tensor with two number. "
63 64
             "The first number is height and the second number is width.")
        .AsDispensable();
Y
yuyang18 已提交
65
    AddOutput("Out", "The dimension of output is (N x C x out_h x out_w)");
W
wangyang59 已提交
66

Y
yuyang18 已提交
67 68
    AddAttr<int>("out_h", "output height of bilinear interpolation op.");
    AddAttr<int>("out_w", "output width of bilinear interpolation op.");
W
wangyang59 已提交
69 70
    AddComment(R"DOC(
          Bilinear interpolation is an extension of linear interpolation for 
W
wangyang59 已提交
71 72
          interpolating functions of two variables (e.g. H-direction and 
          W-direction in this op) on a rectilinear 2D grid. 
W
wangyang59 已提交
73
          
W
wangyang59 已提交
74 75
          The key idea is to perform linear interpolation first in one 
          direction, and then again in the other direction.
W
wangyang59 已提交
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
            
          For details, please refer to Wikipedia: 
          https://en.wikipedia.org/wiki/Bilinear_interpolation
         )DOC");
  }
};

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

 protected:
  void InferShape(framework::InferShapeContext* ctx) const override {
    PADDLE_ENFORCE(ctx->HasInput("X"), "Input(X) should not be null");
    PADDLE_ENFORCE(ctx->HasInput(framework::GradVarName("Out")),
                   "Input(Out@GRAD) should not be null");
    auto dim_x = ctx->GetInputDim("X");
    if (ctx->HasOutput(framework::GradVarName("X"))) {
      ctx->SetOutputDim(framework::GradVarName("X"), dim_x);
    }
  }
97 98 99 100 101 102

  framework::OpKernelType GetExpectedKernelType(
      const framework::ExecutionContext& ctx) const override {
    return framework::OpKernelType(
        framework::ToDataType(ctx.Input<Tensor>("X")->type()), ctx.GetPlace());
  }
W
wangyang59 已提交
103 104 105 106 107 108
};

}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
109 110 111 112
REGISTER_OPERATOR(bilinear_interp, ops::BilinearInterpOp,
                  ops::BilinearInterpOpMaker,
                  paddle::framework::DefaultGradOpDescMaker<true>);
REGISTER_OPERATOR(bilinear_interp_grad, ops::BilinearInterpOpGrad);
113 114
REGISTER_OP_CPU_KERNEL(bilinear_interp, ops::BilinearInterpKernel<float>,
                       ops::BilinearInterpKernel<uint8_t>);
115
REGISTER_OP_CPU_KERNEL(bilinear_interp_grad,
F
fengjiayi 已提交
116
                       ops::BilinearInterpGradKernel<float>);