crop_op.cc 4.7 KB
Newer Older
W
wanghaoshuang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/* 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/crop_op.h"
W
wanghaoshuang 已提交
16
#include <boost/lexical_cast.hpp>
W
wanghaoshuang 已提交
17 18 19 20 21 22 23 24 25 26 27 28

namespace paddle {
namespace operators {

using framework::Tensor;

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

 protected:
  void InferShape(const framework::InferShapeContext &ctx) const override {
W
wanghaoshuang 已提交
29 30 31 32
    PADDLE_ENFORCE_NOT_NULL(ctx.InputVar("X"),
                            "Input(X) of CropOp should not be null.");
    PADDLE_ENFORCE_NOT_NULL(ctx.OutputVar("Out"),
                            "Output(Out) of CropOp should not be null.");
D
dangqingqing 已提交
33 34 35
    auto x_dim = ctx.Input<Tensor>("X")->dims();
    auto *y = ctx.Input<Tensor>("Y");
    auto *out = ctx.Output<Tensor>("Out");
W
wanghaoshuang 已提交
36
    if (y == nullptr) {
37
      auto shape = Attr<std::vector<int>>("shape");
W
wanghaoshuang 已提交
38
      PADDLE_ENFORCE_EQ(
39
          int64_t(shape.size()), x_dim.size(),
W
wanghaoshuang 已提交
40
          "Shape size should be equal to dimention size of input tensor.");
W
wanghaoshuang 已提交
41
      std::vector<int64_t> tensor_shape(shape.size());
42
      for (size_t i = 0; i < shape.size(); ++i) {
43
        tensor_shape[i] = static_cast<int64_t>(shape[i]);
W
wanghaoshuang 已提交
44
      }
W
wanghaoshuang 已提交
45
      out->Resize(framework::make_ddim(tensor_shape));
W
wanghaoshuang 已提交
46
    } else {
W
wanghaoshuang 已提交
47
      PADDLE_ENFORCE_EQ(framework::arity(x_dim), framework::arity(y->dims()),
W
wanghaoshuang 已提交
48 49
                        "Tensor rank of both CropOp's "
                        "inputs must be same.");
W
wanghaoshuang 已提交
50
      out->Resize(y->dims());
W
wanghaoshuang 已提交
51 52 53 54 55 56 57 58
    }
  }
};

class CropOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
  CropOpMaker(framework::OpProto *proto, framework::OpAttrChecker *op_checker)
      : OpProtoAndCheckerMaker(proto, op_checker) {
59 60 61 62 63 64 65 66 67
    AddInput("X",
             "The input of pad op. "
             "The input should be a k-D tensor(k > 0 and k < 7)");
    AddInput("Y",
             "The input used as reference for cropping"
             " with the same dimension as X. ");
    AddOutput("Out",
              "The output of crop op "
              "with the same dimension as X.");
68 69 70 71 72 73 74 75 76
    AddAttr<std::vector<int>>("offsets",
                              "A list<int> describing offsets to be cropped."
                              "The size of offsets list should be as same as "
                              "dimension size of  input X.");
    AddAttr<std::vector<int>>("shape",
                              "A list<int> describing the shape of output."
                              "The size of shape list should be as same as "
                              "dimension size of  input X.")
        .SetDefault(std::vector<int>());
W
wanghaoshuang 已提交
77 78
    AddComment(R"DOC(
Crop Operator.
79 80 81 82 83 84 85 86 87 88 89 90 91 92
Crop input into output, as specified by offsets and shape.

There are two ways to set shape: 
1. referenc input: crop input X as shape as reference input.
                    The dimension of reference input should 
                    be as same as input X.
2. shape list: crop input X by shape described by a list<int>.
               The size of shape list should be as same as 
               dimension size of  input X.

The input should be a k-D tensor(k > 0 and k < 7). As an example:

Given:

93 94 95
    X = [[0, 1, 2, 0, 0]
         [0, 3, 4, 0, 0]
         [0, 0, 0, 0, 0]]
96 97 98

and 

99
    offsets = [0, 1]
100 101 102

and
 
103
    shape = [2, 2]
104 105 106

then we get 

107 108
    Out = [[1, 2],
           [3, 4]]
109

W
wanghaoshuang 已提交
110 111 112 113 114 115 116 117 118 119 120 121 122
)DOC");
  }
};

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

 protected:
  void InferShape(const framework::InferShapeContext &ctx) const override {
    PADDLE_ENFORCE_NOT_NULL(ctx.InputVar("X"), "Input(X) should not be null");
    PADDLE_ENFORCE_NOT_NULL(ctx.InputVar(framework::GradVarName("Out")),
                            "Input(Out@GRAD) should not be null");
D
dangqingqing 已提交
123 124
    auto x_dims = ctx.Input<Tensor>("X")->dims();
    auto *x_grad = ctx.Output<Tensor>(framework::GradVarName("X"));
125 126 127
    if (x_grad != nullptr) {
      x_grad->Resize(x_dims);
    }
W
wanghaoshuang 已提交
128 129 130 131 132 133 134 135
  }
};

}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
REGISTER_OP(crop, ops::CropOp, ops::CropOpMaker, crop_grad, ops::CropOpGrad);
136
REGISTER_OP_CPU_KERNEL(crop, ops::CropKernel<float>);
W
wanghaoshuang 已提交
137 138
REGISTER_OP_CPU_KERNEL(crop_grad,
                       ops::CropGradKernel<paddle::platform::CPUPlace, float>);