interpolate_op.cc 5.8 KB
Newer Older
1
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserve.
2 3 4 5 6 7 8 9 10 11
   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. */

12 13
#include "paddle/fluid/operators/interpolate_op.h"
#include <string>
14 15 16 17 18 19 20 21
#include <vector>
#include "paddle/fluid/framework/op_registry.h"

namespace paddle {
namespace operators {

using framework::Tensor;

22
class InterpolateOp : public framework::OperatorWithKernel {
23 24 25 26 27 28
 public:
  using framework::OperatorWithKernel::OperatorWithKernel;

 protected:
  void InferShape(framework::InferShapeContext* ctx) const override {
    PADDLE_ENFORCE(ctx->HasInput("X"),
29
                   "Input(X) of InterpolateOp should not be null.");
30
    PADDLE_ENFORCE(ctx->HasOutput("Out"),
31 32 33 34 35 36
                   "Output(Out) of InterpolationOp should not be null.");

    auto interp_method = ctx->Attrs().Get<std::string>("interp_method");
    PADDLE_ENFORCE(
        "bilinear" == interp_method || "nearest" == interp_method,
        "Interpolation method can only be \"bilinear\" or \"nearest\".");
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60

    auto dim_x = ctx->GetInputDim("X");  // NCHW format
    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");

    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");
    }
    std::vector<int64_t> dim_out({dim_x[0], dim_x[1], out_h, out_w});
    ctx->SetOutputDim("Out", framework::make_ddim(dim_out));
  }

 protected:
  framework::OpKernelType GetExpectedKernelType(
      const framework::ExecutionContext& ctx) const override {
    return framework::OpKernelType(
        framework::ToDataType(ctx.Input<Tensor>("X")->type()), ctx.GetPlace());
  }
};

61
class InterpolateOpMaker : public framework::OpProtoAndCheckerMaker {
62 63 64
 public:
  void Make() override {
    AddInput("X",
65 66
             "The input tensor of interpolate operator, "
             "This is a 4-D tensor with shape of [N,  C, H, w].");
67
    AddInput("OutSize",
68
             "This is a 1-D tensor with two numbers to specify output size. "
69 70
             "The first number is height and the second number is width.")
        .AsDispensable();
71 72 73
    AddOutput("Out",
              "The output tensor of interpolate operator, "
              "This is a 4-D tensor with shape of [N, C, H, W].");
74

75 76 77 78 79 80 81
    AddAttr<int>("out_h", "output height of interpolate op.");
    AddAttr<int>("out_w", "output width of interpolate op.");
    AddAttr<std::string>(
        "interp_method",
        "(string), interpolation method, can be \"bilinear\" for "
        "bilinear interpolation and \"nearest\" for nearest "
        "neighbor interpolation.");
82
    AddComment(R"DOC(
83 84 85 86 87
          This operator samples input X to given output shape by using specified
          interpolation method, the interpolation methods can be \"nearest\"
          for nearest neighbor interpolation and \"bilinear\" for bilinear 
          interpolation.

88 89 90 91
          Nearest neighbor interpolation is to perform nearest neighbor interpolation
          in bot the 3rd dimention(in height direction) and the 4th dimention(in width 
          direction) on input tensor.
            
92 93 94 95 96 97 98
          Bilinear interpolation is an extension of linear interpolation for 
          interpolating functions of two variables (e.g. H-direction and 
          W-direction in this op) on a rectilinear 2D grid. The key idea is 
          to perform linear interpolation first in one direction, and then 
          again in the other direction.

          For details of nearest neighbor interpolation, please refer to Wikipedia: 
99
          https://en.wikipedia.org/wiki/Nearest-neighbor_interpolation
100 101 102

          For details of bilinear interpolation, please refer to Wikipedia: 
          https://en.wikipedia.org/wiki/Bilinear_interpolation
103 104 105 106
         )DOC");
  }
};

107
class InterpolateOpGrad : public framework::OperatorWithKernel {
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
 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);
    }
  }

  framework::OpKernelType GetExpectedKernelType(
      const framework::ExecutionContext& ctx) const override {
    return framework::OpKernelType(
        framework::ToDataType(ctx.Input<Tensor>("X")->type()), ctx.GetPlace());
  }
};

}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
133
REGISTER_OPERATOR(interpolate, ops::InterpolateOp, ops::InterpolateOpMaker,
134
                  paddle::framework::DefaultGradOpDescMaker<true>);
135 136 137 138 139 140
REGISTER_OPERATOR(interpolate_grad, ops::InterpolateOpGrad);
REGISTER_OP_CPU_KERNEL(interpolate, ops::InterpolateKernel<float>,
                       ops::InterpolateKernel<double>,
                       ops::InterpolateKernel<uint8_t>);
REGISTER_OP_CPU_KERNEL(interpolate_grad, ops::InterpolateGradKernel<float>,
                       ops::InterpolateGradKernel<double>);