crop_op.cc 7.0 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 {
Q
Qiao Longfei 已提交
30 31 32 33 34 35 36
    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 已提交
37
      PADDLE_ENFORCE_EQ(
38
          int64_t(shape.size()), x_dim.size(),
W
wanghaoshuang 已提交
39
          "Shape size should be equal to dimention size of input tensor.");
W
wanghaoshuang 已提交
40
      std::vector<int64_t> tensor_shape(shape.size());
41
      for (size_t i = 0; i < shape.size(); ++i) {
42
        tensor_shape[i] = static_cast<int64_t>(shape[i]);
W
wanghaoshuang 已提交
43
      }
Q
Qiao Longfei 已提交
44
      ctx->SetOutputDim("Out", framework::make_ddim(tensor_shape));
W
wanghaoshuang 已提交
45
    } else {
Q
Qiao Longfei 已提交
46 47
      auto y_dim = ctx->GetInputDim("Y");
      PADDLE_ENFORCE_EQ(framework::arity(x_dim), framework::arity(y_dim),
W
wanghaoshuang 已提交
48 49
                        "Tensor rank of both CropOp's "
                        "inputs must be same.");
Q
Qiao Longfei 已提交
50
      ctx->SetOutputDim("Out", y_dim);
W
wanghaoshuang 已提交
51 52
    }
  }
53 54 55

  framework::OpKernelType GetExpectedKernelType(
      const framework::ExecutionContext& ctx) const override {
Y
Yu Yang 已提交
56 57
    return framework::OpKernelType(ctx.Input<framework::LoDTensor>("X")->type(),
                                   ctx.device_context());
58
  }
W
wanghaoshuang 已提交
59 60 61 62
};

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

92 93
Crop input into output, as specified by offsets and shape.

F
stash  
fengjiayi 已提交
94 95 96 97 98 99 100 101 102 103 104
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 已提交
105
There are two ways to set shape:
K
Kexin Zhao 已提交
106
1. reference input: crop input X into the same shape as reference input.
Q
Qiao Longfei 已提交
107
                    The dimension of reference input should
K
Kexin Zhao 已提交
108 109 110 111
                    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.
112 113 114

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

W
wanghaoshuang 已提交
115 116
Case 1:
Given
117

118 119
    X = [[0, 1, 2, 0, 0]
         [0, 3, 4, 0, 0]
K
Kexin Zhao 已提交
120
         [0, 0, 0, 0, 0]],
121

Q
Qiao Longfei 已提交
122
and
123

K
Kexin Zhao 已提交
124
    offsets = [0, 1],
125 126

and
Q
Qiao Longfei 已提交
127

K
Kexin Zhao 已提交
128
    shape = [2, 2],
129

K
Kexin Zhao 已提交
130
we get:
131

132
    Out = [[1, 2],
K
Kexin Zhao 已提交
133
           [3, 4]].
134

W
wanghaoshuang 已提交
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155

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 已提交
156 157 158 159 160 161 162 163
)DOC");
  }
};

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

164
  void InferShape(framework::InferShapeContext* ctx) const override {
Q
Qiao Longfei 已提交
165 166 167 168 169 170 171
    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);
172
    }
W
wanghaoshuang 已提交
173
  }
174 175 176 177

  framework::OpKernelType GetExpectedKernelType(
      const framework::ExecutionContext& ctx) const override {
    return framework::OpKernelType(
Y
Yu Yang 已提交
178
        ctx.Input<framework::LoDTensor>(framework::GradVarName("Out"))->type(),
179 180
        ctx.device_context());
  }
W
wanghaoshuang 已提交
181 182
};

S
sneaxiy 已提交
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
class CropGradOpDescMaker : public framework::SingleGradOpDescMaker {
 public:
  using framework::SingleGradOpDescMaker::SingleGradOpDescMaker;

 protected:
  std::unique_ptr<framework::OpDesc> Apply() const override {
    std::unique_ptr<framework::OpDesc> op(new framework::OpDesc());
    op->SetType("crop_grad");
    op->SetInput(framework::GradVarName("Out"), OutputGrad("Out"));
    op->SetInput("X", Input("X"));
    if (ForwardOp().Inputs().count("Offsets") > 0) {
      op->SetInput("Offsets", Input("Offsets"));
    }
    op->SetOutput(framework::GradVarName("X"), InputGrad("X"));
    op->SetAttrMap(Attrs());
    return op;
  }
};

W
wanghaoshuang 已提交
202 203 204 205
}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
Y
Yang Yang 已提交
206
REGISTER_OPERATOR(crop, ops::CropOp, ops::CropOpMaker,
S
sneaxiy 已提交
207
                  ops::CropGradOpDescMaker);
Y
Yang Yang 已提交
208
REGISTER_OPERATOR(crop_grad, ops::CropOpGrad);
W
whs 已提交
209
REGISTER_OP_CPU_KERNEL(
S
SunGaofeng 已提交
210 211
    crop, ops::CropKernel<paddle::platform::CPUDeviceContext, float>,
    ops::CropKernel<paddle::platform::CPUDeviceContext, double>);
Q
QI JUN 已提交
212
REGISTER_OP_CPU_KERNEL(
S
SunGaofeng 已提交
213 214
    crop_grad, ops::CropGradKernel<paddle::platform::CPUDeviceContext, float>,
    ops::CropGradKernel<paddle::platform::CPUDeviceContext, double>);