crop_op.cc 4.9 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
W
wanghaoshuang 已提交
2

L
Luo Tao 已提交
3 4 5
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
W
wanghaoshuang 已提交
6

L
Luo Tao 已提交
7
    http://www.apache.org/licenses/LICENSE-2.0
W
wanghaoshuang 已提交
8

L
Luo Tao 已提交
9 10 11 12 13
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. */
W
wanghaoshuang 已提交
14

Y
Yi Wang 已提交
15
#include "paddle/fluid/operators/crop_op.h"
W
wanghaoshuang 已提交
16
#include <boost/lexical_cast.hpp>
W
wanghaoshuang 已提交
17 18 19 20 21 22 23 24 25 26

namespace paddle {
namespace operators {

using framework::Tensor;

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

27
  void InferShape(framework::InferShapeContext* ctx) const override {
Q
Qiao Longfei 已提交
28 29 30 31 32 33 34
    PADDLE_ENFORCE(ctx->HasInput("X"),
                   "Input(X) of CropOp should not be null.");
    PADDLE_ENFORCE(ctx->HasOutput("Out"),
                   "Output(Out) of CropOp should not be null.");
    auto x_dim = ctx->GetInputDim("X");
    if (!ctx->HasInput("Y")) {
      auto shape = ctx->Attrs().Get<std::vector<int>>("shape");
W
wanghaoshuang 已提交
35
      PADDLE_ENFORCE_EQ(
36
          int64_t(shape.size()), x_dim.size(),
W
wanghaoshuang 已提交
37
          "Shape size should be equal to dimention size of input tensor.");
W
wanghaoshuang 已提交
38
      std::vector<int64_t> tensor_shape(shape.size());
39
      for (size_t i = 0; i < shape.size(); ++i) {
40
        tensor_shape[i] = static_cast<int64_t>(shape[i]);
W
wanghaoshuang 已提交
41
      }
Q
Qiao Longfei 已提交
42
      ctx->SetOutputDim("Out", framework::make_ddim(tensor_shape));
W
wanghaoshuang 已提交
43
    } else {
Q
Qiao Longfei 已提交
44 45
      auto y_dim = ctx->GetInputDim("Y");
      PADDLE_ENFORCE_EQ(framework::arity(x_dim), framework::arity(y_dim),
W
wanghaoshuang 已提交
46 47
                        "Tensor rank of both CropOp's "
                        "inputs must be same.");
Q
Qiao Longfei 已提交
48
      ctx->SetOutputDim("Out", y_dim);
W
wanghaoshuang 已提交
49 50 51 52 53 54
    }
  }
};

class CropOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
Y
Yu Yang 已提交
55
  void Make() override {
56 57
    AddInput("X",
             "The input of pad op. "
K
Kexin Zhao 已提交
58
             "The input should be a k-D tensor(k > 0 and k < 7).");
59
    AddInput("Y",
K
Kexin Zhao 已提交
60 61
             "The input used as reference for cropping, "
             "which is of the same dimensions as X.")
Y
Yang Yang(Tony) 已提交
62
        .AsDispensable();
63
    AddOutput("Out",
K
Kexin Zhao 已提交
64 65
              "The output of crop op, "
              "which is of the same dimensions as X.");
66
    AddAttr<std::vector<int>>("offsets",
K
Kexin Zhao 已提交
67 68 69
                              "A list<int> describing offsets to be cropped. "
                              "The size of offsets list should be the same as "
                              "the dimension size of input X.");
70
    AddAttr<std::vector<int>>("shape",
K
Kexin Zhao 已提交
71 72 73
                              "A list<int> describing the shape of output. "
                              "The size of shape list should be the same as "
                              "the dimension size of input X.")
74
        .SetDefault(std::vector<int>());
W
wanghaoshuang 已提交
75 76
    AddComment(R"DOC(
Crop Operator.
K
Kexin Zhao 已提交
77

78 79
Crop input into output, as specified by offsets and shape.

Q
Qiao Longfei 已提交
80
There are two ways to set shape:
K
Kexin Zhao 已提交
81
1. reference input: crop input X into the same shape as reference input.
Q
Qiao Longfei 已提交
82
                    The dimension of reference input should
K
Kexin Zhao 已提交
83 84 85 86
                    be the same as the dimension of input X.
2. shape list: crop input X into the shape described by a list<int>.
               The size of shape list should be the same as
               the dimension size of input X.
87 88 89

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

W
wanghaoshuang 已提交
90 91
Case 1:
Given
92

93 94
    X = [[0, 1, 2, 0, 0]
         [0, 3, 4, 0, 0]
K
Kexin Zhao 已提交
95
         [0, 0, 0, 0, 0]],
96

Q
Qiao Longfei 已提交
97
and
98

K
Kexin Zhao 已提交
99
    offsets = [0, 1],
100 101

and
Q
Qiao Longfei 已提交
102

K
Kexin Zhao 已提交
103
    shape = [2, 2],
104

K
Kexin Zhao 已提交
105
we get:
106

107
    Out = [[1, 2],
K
Kexin Zhao 已提交
108
           [3, 4]].
109

W
wanghaoshuang 已提交
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130

Case 2:
Given

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

and

    offsets = [0, 1],

and

    Y = [[0, 0, 0]
         [0, 0, 0]],

we get:

    Out = [[1, 2, 5],
           [3, 4, 6]].
W
wanghaoshuang 已提交
131 132 133 134 135 136 137 138
)DOC");
  }
};

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

139
  void InferShape(framework::InferShapeContext* ctx) const override {
Q
Qiao Longfei 已提交
140 141 142 143 144 145 146
    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 x_dims = ctx->GetInputDim("X");
    auto x_grad_name = framework::GradVarName("X");
    if (ctx->HasOutput(x_grad_name)) {
      ctx->SetOutputDim(x_grad_name, x_dims);
147
    }
W
wanghaoshuang 已提交
148 149 150 151 152 153 154
  }
};

}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
Y
Yang Yang 已提交
155 156 157
REGISTER_OPERATOR(crop, ops::CropOp, ops::CropOpMaker,
                  paddle::framework::DefaultGradOpDescMaker<true>);
REGISTER_OPERATOR(crop_grad, ops::CropOpGrad);
158
REGISTER_OP_CPU_KERNEL(crop, ops::CropKernel<float>);
Q
QI JUN 已提交
159 160
REGISTER_OP_CPU_KERNEL(
    crop_grad, ops::CropGradKernel<paddle::platform::CPUDeviceContext, float>);