crop_op.cc 7.5 KB
Newer Older
W
whs 已提交
1
/* Copyright (c) 2018 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"
S
sneaxiy 已提交
16 17 18
#include <memory>
#include <string>
#include <vector>
W
wanghaoshuang 已提交
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;

29
  void InferShape(framework::InferShapeContext* ctx) const override {
30 31
    OP_INOUT_CHECK(ctx->HasInput("X"), "Input", "X", "Crop");
    OP_INOUT_CHECK(ctx->HasOutput("Out"), "Output", "Out", "Crop");
Q
Qiao Longfei 已提交
32 33 34
    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(),
37 38 39 40 41
          platform::errors::InvalidArgument(
              "The number of elements (%d) of CropOp's "
              "'shape' attribute should be equal to the number of dimensions "
              "(%d) of the Input(X).",
              shape.size(), x_dim.size()));
W
wanghaoshuang 已提交
42
      std::vector<int64_t> tensor_shape(shape.size());
43
      for (size_t i = 0; i < shape.size(); ++i) {
44
        tensor_shape[i] = static_cast<int64_t>(shape[i]);
W
wanghaoshuang 已提交
45
      }
Q
Qiao Longfei 已提交
46
      ctx->SetOutputDim("Out", framework::make_ddim(tensor_shape));
W
wanghaoshuang 已提交
47
    } else {
Q
Qiao Longfei 已提交
48 49
      auto y_dim = ctx->GetInputDim("Y");
      PADDLE_ENFORCE_EQ(framework::arity(x_dim), framework::arity(y_dim),
50 51 52 53
                        platform::errors::InvalidArgument(
                            "The number of dimensions (%d) of CropOp's input(X)"
                            " must be equal to that (%d) of input(Y).",
                            framework::arity(x_dim), framework::arity(y_dim)));
Q
Qiao Longfei 已提交
54
      ctx->SetOutputDim("Out", y_dim);
W
wanghaoshuang 已提交
55 56
    }
  }
57 58 59

  framework::OpKernelType GetExpectedKernelType(
      const framework::ExecutionContext& ctx) const override {
60 61 62
    return framework::OpKernelType(
        OperatorWithKernel::IndicateVarDataType(ctx, "X"),
        ctx.device_context());
63
  }
W
wanghaoshuang 已提交
64 65 66 67
};

class CropOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
Y
Yu Yang 已提交
68
  void Make() override {
69 70
    AddInput("X",
             "The input of pad op. "
K
Kexin Zhao 已提交
71
             "The input should be a k-D tensor(k > 0 and k < 7).");
72
    AddInput("Y",
K
Kexin Zhao 已提交
73 74
             "The input used as reference for cropping, "
             "which is of the same dimensions as X.")
Y
Yang Yang(Tony) 已提交
75
        .AsDispensable();
F
stash  
fengjiayi 已提交
76 77 78 79 80
    AddInput("Offsets",
             "The input used to describe offsets in runtime, which is a "
             "1-D vector whose size equals to the rank of input 'X'. The "
             "elements data type must be int.")
        .AsDispensable();
81
    AddOutput("Out",
K
Kexin Zhao 已提交
82 83
              "The output of crop op, "
              "which is of the same dimensions as X.");
84
    AddAttr<std::vector<int>>("offsets",
K
Kexin Zhao 已提交
85 86
                              "A list<int> describing offsets to be cropped. "
                              "The size of offsets list should be the same as "
F
stash  
fengjiayi 已提交
87 88
                              "the dimension size of input X.")
        .SetDefault(std::vector<int>());
89
    AddAttr<std::vector<int>>("shape",
K
Kexin Zhao 已提交
90 91 92
                              "A list<int> describing the shape of output. "
                              "The size of shape list should be the same as "
                              "the dimension size of input X.")
93
        .SetDefault(std::vector<int>());
W
wanghaoshuang 已提交
94 95
    AddComment(R"DOC(
Crop Operator.
K
Kexin Zhao 已提交
96

97 98
Crop input into output, as specified by offsets and shape.

F
stash  
fengjiayi 已提交
99 100 101 102 103 104 105 106 107 108 109
There are two ways to set the offsets:
1. In runtime: Using the input 'Offsets', which is a Vairbale and can be 
               output of other operators. This way is suitable for 
               dynamic offsets.
2. In network configuration: Using the attribute 'offsets', which will be 
                             set in Python configure script. This way is 
                             suitable for fixed offsets.
You CANNOT use these two ways at the same time. An exception will be raised 
if input 'Offset' is configured and meanwhile the attribute 'offsets' is 
not empty.

Q
Qiao Longfei 已提交
110
There are two ways to set shape:
K
Kexin Zhao 已提交
111
1. reference input: crop input X into the same shape as reference input.
Q
Qiao Longfei 已提交
112
                    The dimension of reference input should
K
Kexin Zhao 已提交
113 114 115 116
                    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.
117 118 119

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

W
wanghaoshuang 已提交
120 121
Case 1:
Given
122

123 124
    X = [[0, 1, 2, 0, 0]
         [0, 3, 4, 0, 0]
K
Kexin Zhao 已提交
125
         [0, 0, 0, 0, 0]],
126

Q
Qiao Longfei 已提交
127
and
128

K
Kexin Zhao 已提交
129
    offsets = [0, 1],
130 131

and
Q
Qiao Longfei 已提交
132

K
Kexin Zhao 已提交
133
    shape = [2, 2],
134

K
Kexin Zhao 已提交
135
we get:
136

137
    Out = [[1, 2],
K
Kexin Zhao 已提交
138
           [3, 4]].
139

W
wanghaoshuang 已提交
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160

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 已提交
161 162 163 164 165 166 167 168
)DOC");
  }
};

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

169
  void InferShape(framework::InferShapeContext* ctx) const override {
170 171 172
    OP_INOUT_CHECK(ctx->HasInput("X"), "Input", "X", "CropGrad");
    OP_INOUT_CHECK(ctx->HasInput(framework::GradVarName("Out")), "Input",
                   framework::GradVarName("Out"), "CropGrad");
Q
Qiao Longfei 已提交
173 174 175 176
    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);
177
    }
W
wanghaoshuang 已提交
178
  }
179 180 181

  framework::OpKernelType GetExpectedKernelType(
      const framework::ExecutionContext& ctx) const override {
182 183 184
    return framework::OpKernelType(OperatorWithKernel::IndicateVarDataType(
                                       ctx, framework::GradVarName("Out")),
                                   ctx.device_context());
185
  }
W
wanghaoshuang 已提交
186 187
};

H
hong 已提交
188 189
template <typename T>
class CropGradOpMaker : public framework::SingleGradOpMaker<T> {
S
sneaxiy 已提交
190
 public:
H
hong 已提交
191
  using framework::SingleGradOpMaker<T>::SingleGradOpMaker;
S
sneaxiy 已提交
192 193

 protected:
194
  void Apply(GradOpPtr<T> op) const override {
S
sneaxiy 已提交
195
    op->SetType("crop_grad");
H
hong 已提交
196 197 198 199
    op->SetInput(framework::GradVarName("Out"), this->OutputGrad("Out"));
    op->SetInput("X", this->Input("X"));
    if (this->HasInput("Offsets")) {
      op->SetInput("Offsets", this->Input("Offsets"));
S
sneaxiy 已提交
200
    }
H
hong 已提交
201 202
    op->SetOutput(framework::GradVarName("X"), this->InputGrad("X"));
    op->SetAttrMap(this->Attrs());
S
sneaxiy 已提交
203 204 205
  }
};

206
DECLARE_NO_NEED_BUFFER_VARS_INFERER(GropNoNeedBufferVarInference, "Y");
207

W
wanghaoshuang 已提交
208 209 210 211
}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
Y
Yang Yang 已提交
212
REGISTER_OPERATOR(crop, ops::CropOp, ops::CropOpMaker,
H
hong 已提交
213
                  ops::CropGradOpMaker<paddle::framework::OpDesc>,
214 215
                  ops::CropGradOpMaker<paddle::imperative::OpBase>,
                  ops::GropNoNeedBufferVarInference);
Y
Yang Yang 已提交
216
REGISTER_OPERATOR(crop_grad, ops::CropOpGrad);
W
whs 已提交
217
REGISTER_OP_CPU_KERNEL(
S
SunGaofeng 已提交
218 219
    crop, ops::CropKernel<paddle::platform::CPUDeviceContext, float>,
    ops::CropKernel<paddle::platform::CPUDeviceContext, double>);
Q
QI JUN 已提交
220
REGISTER_OP_CPU_KERNEL(
S
SunGaofeng 已提交
221 222
    crop_grad, ops::CropGradKernel<paddle::platform::CPUDeviceContext, float>,
    ops::CropGradKernel<paddle::platform::CPUDeviceContext, double>);