crop_op.cc 6.2 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 {
29
    auto x_dim = ctx.Input<Tensor>("X")->dims();
W
wanghaoshuang 已提交
30 31
    auto Y = ctx.Input<Tensor>("Y");
    if (Y == nullptr) {
32
      auto shape = Attr<std::vector<int>>("shape");
W
wanghaoshuang 已提交
33
      PADDLE_ENFORCE_EQ(
34
          int64_t(shape.size()), x_dim.size(),
W
wanghaoshuang 已提交
35
          "Shape size should be equal to dimention size of input tensor.");
W
wanghaoshuang 已提交
36
      std::vector<int64_t> tensor_shape(shape.size());
37
      for (size_t i = 0; i < shape.size(); ++i) {
W
wanghaoshuang 已提交
38 39
        tensor_shape[i] = (int64_t)shape[i];
      }
40
      ctx.Output<Tensor>("Out")->Resize(framework::make_ddim(tensor_shape));
W
wanghaoshuang 已提交
41 42 43 44 45 46 47 48 49 50
    } else {
      ctx.Output<Tensor>("Out")->Resize(Y->dims());
    }
  }
};

class CropOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
  CropOpMaker(framework::OpProto *proto, framework::OpAttrChecker *op_checker)
      : OpProtoAndCheckerMaker(proto, op_checker) {
51 52 53 54 55 56 57 58 59
    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.");
W
wanghaoshuang 已提交
60 61
    AddComment(R"DOC(
Crop Operator.
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 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:

X = [[0, 1, 2, 0, 0]
       [0, 3, 4, 0, 0]
       [0, 0, 0, 0, 0]]

and 

offsets = [0, 1]

and
 
shape = [2, 2]

then we get 

Out = [[1, 2],
   [3, 4]]

W
wanghaoshuang 已提交
93
)DOC");
94 95 96 97 98 99 100
    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 "
101 102
                              "dimension size of  input X.")
        .SetDefault(std::vector<int>());
W
wanghaoshuang 已提交
103 104 105 106 107 108 109 110 111 112 113 114 115 116
  }
};

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");
    auto x_dims = ctx.Input<Tensor>("X")->dims();
    auto *x_grad = ctx.Output<Tensor>(framework::GradVarName("X"));
117 118 119
    if (x_grad != nullptr) {
      x_grad->Resize(x_dims);
    }
W
wanghaoshuang 已提交
120 121 122
  }
};

123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
int64_t transIndex(std::vector<int64_t> out_shape, std::vector<int64_t> x_shape,
                   std::vector<std::pair<int, int>> crop_rules, size_t index) {
  int64_t dim_size = out_shape.size();
  std::vector<int64_t> pos(dim_size);

  for (int64_t i = out_shape.size() - 1; i >= 0; --i) {
    pos[i] = (index % out_shape[i]) + crop_rules[i].first;
    index = index / out_shape[i];
  }

  size_t result = pos[0];
  for (size_t i = 1; i < x_shape.size(); ++i) {
    result = result * x_shape[i] + pos[i];
  }
  return result;
}

140 141 142 143
template <typename T>
class CropCPUKernel : public framework::OpKernel {
 public:
  void Compute(const framework::ExecutionContext &context) const override {
144
    LOG(INFO) << "CropCPUKernel step1";
145
    auto *x = context.Input<Tensor>("X");
146
    LOG(INFO) << "CropCPUKernel step2";
147
    auto *out = context.Output<Tensor>("Out");
148
    LOG(INFO) << "CropCPUKernel step3";
149 150
    auto x_data = x->data<T>();
    T *out_data = out->mutable_data<T>(paddle::platform::CPUPlace());
151
    LOG(INFO) << "CropCPUKernel step4";
152 153
    auto x_dims = x->dims();
    auto out_dims = out->dims();
154
    LOG(INFO) << "CropCPUKernel step5";
155 156 157 158 159
    int64_t out_count = framework::product(out_dims);
    std::vector<int64_t> x_shape = framework::vectorize(x_dims);
    std::vector<int64_t> out_shape = framework::vectorize(out_dims);

    auto offsets = context.op().Attr<std::vector<int>>("offsets");
160
    LOG(INFO) << "CropCPUKernel step6";
161 162 163 164 165 166 167 168 169 170 171 172 173
    PADDLE_ENFORCE_EQ(
        x_dims.size(), offsets.size(),
        "Offsets size should be equal to dimension size of input tensor.");

    std::vector<std::pair<int, int>> crop_rules(x_dims.size());
    for (size_t i = 0; i < crop_rules.size(); ++i) {
      crop_rules[i].first = offsets[i];
      crop_rules[i].second = x_dims[i] - out_dims[i] - offsets[i];
    }

    for (int64_t i = 0; i < out_count; ++i) {
      out_data[i] = x_data[transIndex(out_shape, x_shape, crop_rules, i)];
    }
174
    LOG(INFO) << "CropCPUKernel step7";
175 176 177
  }
};

W
wanghaoshuang 已提交
178 179 180 181 182
}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
REGISTER_OP(crop, ops::CropOp, ops::CropOpMaker, crop_grad, ops::CropOpGrad);
183
REGISTER_OP_CPU_KERNEL(crop, ops::CropCPUKernel<float>);
W
wanghaoshuang 已提交
184 185
REGISTER_OP_CPU_KERNEL(crop_grad,
                       ops::CropGradKernel<paddle::platform::CPUPlace, float>);