You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Paddle/paddle/fluid/operators/space_to_depth_op.cc

132 lines
5.2 KiB

6 years ago
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
6 years ago
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. */
6 years ago
#include "paddle/fluid/operators/space_to_depth_op.h"
6 years ago
#include <string>
#include <vector>
namespace paddle {
namespace operators {
6 years ago
class SpaceToDepthOp : public framework::OperatorWithKernel {
6 years ago
public:
using framework::OperatorWithKernel::OperatorWithKernel;
void InferShape(framework::InferShapeContext* ctx) const override {
PADDLE_ENFORCE(ctx->HasInput("X"),
6 years ago
"Input(X) of SpaceToDepthOp should not be null.");
6 years ago
PADDLE_ENFORCE(ctx->HasOutput("Out"),
6 years ago
"Output(Out) of SpaceToDepthOp should not be null.");
6 years ago
auto x_dims = ctx->GetInputDim("X");
PADDLE_ENFORCE_EQ(x_dims.size(), 4, "input should be a 4D tensor");
6 years ago
auto blocksize = ctx->Attrs().Get<int64_t>("blocksize");
6 years ago
6 years ago
PADDLE_ENFORCE_GT(blocksize, 1, "The blocksize should be Greater than 1");
6 years ago
PADDLE_ENFORCE_GT(x_dims[1], 0, "input channel should be Greater than 0");
PADDLE_ENFORCE_GT(x_dims[2], 0, "input Height should be Greater than 0");
PADDLE_ENFORCE_GT(x_dims[3], 0, "input Width should be Greater than 0");
6 years ago
PADDLE_ENFORCE_EQ(x_dims[1] % (blocksize * blocksize), 0,
6 years ago
"input channel should be divisible of the square of "
6 years ago
"SpaceToDepthOp blocksize");
PADDLE_ENFORCE_EQ(x_dims[2] % (blocksize), 0,
6 years ago
"input Height should be divisible of the square of "
6 years ago
"SpaceToDepthOp blocksize");
PADDLE_ENFORCE_EQ(x_dims[3] % (blocksize), 0,
6 years ago
"input Width should be divisible of the square of "
6 years ago
"SpaceToDepthOp blocksize");
6 years ago
6 years ago
VLOG(3) << "SpaceToDepthOp operator x.shape=" << x_dims
6 years ago
<< "Attribute blocksize" << blocksize << std::endl;
6 years ago
std::vector<int64_t> output_shape(4, 0); // [B,C,H,W]
output_shape[0] = x_dims[0];
6 years ago
output_shape[1] = x_dims[1] * blocksize * blocksize;
output_shape[2] = x_dims[2] / blocksize;
output_shape[3] = x_dims[3] / blocksize;
6 years ago
auto out_dims = framework::make_ddim(output_shape);
ctx->SetOutputDim("Out", out_dims);
if (x_dims[0] == out_dims[0]) {
// Only pass LoD when the first dimension of output and Input(X)
// are the same.
ctx->ShareLoD("X", /*->*/ "Out");
}
}
};
6 years ago
class SpaceToDepthOpMaker : public framework::OpProtoAndCheckerMaker {
6 years ago
public:
void Make() override {
AddInput("X",
6 years ago
"(Tensor). The input should be a 4D tensor B * C * W * H of "
"SpaceToDepthOp "
6 years ago
"operator.");
AddOutput("Out",
"(Tensor), The output should be a 4D tensor B * C2 * W2 * H2 of "
6 years ago
"SpaceToDepthOp operator.");
AddAttr<int64_t>(
6 years ago
"blocksize",
"(int64_t, default 2) blocksize used to do change Space To Depth.")
6 years ago
.SetDefault(2)
.GreaterThan(1);
6 years ago
AddComment(R"DOC(
reorg operator used in Yolo v2.
The equation is: C2 = C1/blocksize * blocksize, W2 = W1 * blocksize + offset % blocksize, H2 = H1 * blocksize + offset / blocksize,
6 years ago
6 years ago
Reshape Input(X) into the shape according to Attr(blocksize). The
6 years ago
data in Input(X) are unchanged.
Examples:
6 years ago
1. Given a 4-D tensor Input(X) with a shape [128, 2048, 26, 26], and the blocksize is 2, the reorg operator will transform Input(X)
6 years ago
into a 4-D tensor with shape [128, 2048, 13, 13] and leaving Input(X)'s data unchanged.
6 years ago
)DOC");
}
};
6 years ago
class SpaceToDepthGradOp : public framework::OperatorWithKernel {
6 years ago
public:
using framework::OperatorWithKernel::OperatorWithKernel;
void InferShape(framework::InferShapeContext* ctx) const override {
PADDLE_ENFORCE(ctx->HasInput("X"), "Input(X) shouldn't be null.");
PADDLE_ENFORCE(ctx->HasInput(framework::GradVarName("Out")),
"Input(Out@GRAD) shouldn't be null.");
ctx->SetOutputDim(framework::GradVarName("X"), ctx->GetInputDim("X"));
}
};
} // namespace operators
} // namespace paddle
namespace ops = paddle::operators;
6 years ago
REGISTER_OPERATOR(space_to_depth, ops::SpaceToDepthOp, ops::SpaceToDepthOpMaker,
6 years ago
paddle::framework::DefaultGradOpDescMaker<true>);
6 years ago
REGISTER_OPERATOR(space_to_depth_grad, ops::SpaceToDepthGradOp);
6 years ago
REGISTER_OP_CPU_KERNEL(
6 years ago
space_to_depth,
ops::SpaceToDepthKernel<paddle::platform::CPUDeviceContext, float>,
ops::SpaceToDepthKernel<paddle::platform::CPUDeviceContext, double>,
ops::SpaceToDepthKernel<paddle::platform::CPUDeviceContext, int64_t>);
6 years ago
REGISTER_OP_CPU_KERNEL(
6 years ago
space_to_depth_grad,
ops::SpaceToDepthGradKernel<paddle::platform::CPUDeviceContext, float>,
ops::SpaceToDepthGradKernel<paddle::platform::CPUDeviceContext, double>,
ops::SpaceToDepthGradKernel<paddle::platform::CPUDeviceContext, int64_t>);