|
|
|
@ -12,44 +12,44 @@ 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/fluid/operators/reorg_op.h"
|
|
|
|
|
#include "paddle/fluid/operators/space_to_depth_op.h"
|
|
|
|
|
#include <string>
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
|
|
namespace paddle {
|
|
|
|
|
namespace operators {
|
|
|
|
|
|
|
|
|
|
class ReorgOp : public framework::OperatorWithKernel {
|
|
|
|
|
class SpaceToDepthOp : public framework::OperatorWithKernel {
|
|
|
|
|
public:
|
|
|
|
|
using framework::OperatorWithKernel::OperatorWithKernel;
|
|
|
|
|
|
|
|
|
|
void InferShape(framework::InferShapeContext* ctx) const override {
|
|
|
|
|
PADDLE_ENFORCE(ctx->HasInput("X"),
|
|
|
|
|
"Input(X) of reorgOp should not be null.");
|
|
|
|
|
"Input(X) of SpaceToDepthOp should not be null.");
|
|
|
|
|
PADDLE_ENFORCE(ctx->HasOutput("Out"),
|
|
|
|
|
"Output(Out) of reorgOp should not be null.");
|
|
|
|
|
"Output(Out) of SpaceToDepthOp should not be null.");
|
|
|
|
|
|
|
|
|
|
auto x_dims = ctx->GetInputDim("X");
|
|
|
|
|
PADDLE_ENFORCE_EQ(x_dims.size(), 4, "input should be a 4D tensor");
|
|
|
|
|
auto stride = ctx->Attrs().Get<int64_t>("stride");
|
|
|
|
|
|
|
|
|
|
PADDLE_ENFORCE_GT(stride, 0, "The stride should be Greater than 0");
|
|
|
|
|
PADDLE_ENFORCE_GT(stride, 1, "The stride should be Greater than 1");
|
|
|
|
|
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");
|
|
|
|
|
|
|
|
|
|
PADDLE_ENFORCE_EQ(
|
|
|
|
|
x_dims[1] % (stride * stride), 0,
|
|
|
|
|
"input channel should be dvisible of the square of reorg stride");
|
|
|
|
|
PADDLE_ENFORCE_EQ(
|
|
|
|
|
x_dims[2] % (stride), 0,
|
|
|
|
|
"input Height should be dvisible of the square of reorg stride");
|
|
|
|
|
PADDLE_ENFORCE_EQ(
|
|
|
|
|
x_dims[3] % (stride), 0,
|
|
|
|
|
"input Width should be dvisible of the square of reorg stride");
|
|
|
|
|
PADDLE_ENFORCE_EQ(x_dims[1] % (stride * stride), 0,
|
|
|
|
|
"input channel should be divisible of the square of "
|
|
|
|
|
"SpaceToDepthOp stride");
|
|
|
|
|
PADDLE_ENFORCE_EQ(x_dims[2] % (stride), 0,
|
|
|
|
|
"input Height should be divisible of the square of "
|
|
|
|
|
"SpaceToDepthOp stride");
|
|
|
|
|
PADDLE_ENFORCE_EQ(x_dims[3] % (stride), 0,
|
|
|
|
|
"input Width should be divisible of the square of "
|
|
|
|
|
"SpaceToDepthOp stride");
|
|
|
|
|
|
|
|
|
|
VLOG(3) << "reorg operator x.shape=" << x_dims << "Attribute stride"
|
|
|
|
|
<< stride << std::endl;
|
|
|
|
|
VLOG(3) << "SpaceToDepthOp operator x.shape=" << x_dims
|
|
|
|
|
<< "Attribute stride" << stride << std::endl;
|
|
|
|
|
|
|
|
|
|
std::vector<int64_t> output_shape(4, 0); // [B,C,H,W]
|
|
|
|
|
output_shape[0] = x_dims[0];
|
|
|
|
@ -69,19 +69,21 @@ class ReorgOp : public framework::OperatorWithKernel {
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class ReorgOpMaker : public framework::OpProtoAndCheckerMaker {
|
|
|
|
|
class SpaceToDepthOpMaker : public framework::OpProtoAndCheckerMaker {
|
|
|
|
|
public:
|
|
|
|
|
void Make() override {
|
|
|
|
|
AddInput("X",
|
|
|
|
|
"(Tensor). The input should be a 4D tensor B * C * W * H of reorg "
|
|
|
|
|
"(Tensor). The input should be a 4D tensor B * C * W * H of "
|
|
|
|
|
"SpaceToDepthOp "
|
|
|
|
|
"operator.");
|
|
|
|
|
AddOutput("Out",
|
|
|
|
|
"(Tensor), The output should be a 4D tensor B * C2 * W2 * H2 of "
|
|
|
|
|
"reorg operator.");
|
|
|
|
|
AddAttr<int64_t>("stride",
|
|
|
|
|
"(int64_t, default 1) stride used to do reorgnization.")
|
|
|
|
|
.SetDefault(1)
|
|
|
|
|
.EqualGreaterThan(1);
|
|
|
|
|
"SpaceToDepthOp operator.");
|
|
|
|
|
AddAttr<int64_t>(
|
|
|
|
|
"stride",
|
|
|
|
|
"(int64_t, default 2) stride used to do change Space To Depth.")
|
|
|
|
|
.SetDefault(2)
|
|
|
|
|
.GreaterThan(1);
|
|
|
|
|
AddComment(R"DOC(
|
|
|
|
|
reorg operator used in Yolo v2.
|
|
|
|
|
The equation is: C2 = C1/stride * stride, W2 = W1 ∗ stride + offset % stride, H2 = H1 ∗ stride + offset / stride,
|
|
|
|
@ -98,7 +100,7 @@ class ReorgOpMaker : public framework::OpProtoAndCheckerMaker {
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class ReorgGradOp : public framework::OperatorWithKernel {
|
|
|
|
|
class SpaceToDepthGradOp : public framework::OperatorWithKernel {
|
|
|
|
|
public:
|
|
|
|
|
using framework::OperatorWithKernel::OperatorWithKernel;
|
|
|
|
|
|
|
|
|
@ -114,14 +116,16 @@ class ReorgGradOp : public framework::OperatorWithKernel {
|
|
|
|
|
|
|
|
|
|
namespace ops = paddle::operators;
|
|
|
|
|
|
|
|
|
|
REGISTER_OPERATOR(reorg, ops::ReorgOp, ops::ReorgOpMaker,
|
|
|
|
|
REGISTER_OPERATOR(space_to_depth, ops::SpaceToDepthOp, ops::SpaceToDepthOpMaker,
|
|
|
|
|
paddle::framework::DefaultGradOpDescMaker<true>);
|
|
|
|
|
REGISTER_OPERATOR(reorg_grad, ops::ReorgGradOp);
|
|
|
|
|
REGISTER_OPERATOR(space_to_depth_grad, ops::SpaceToDepthGradOp);
|
|
|
|
|
REGISTER_OP_CPU_KERNEL(
|
|
|
|
|
reorg, ops::ReorgKernel<paddle::platform::CPUDeviceContext, float>,
|
|
|
|
|
ops::ReorgKernel<paddle::platform::CPUDeviceContext, double>,
|
|
|
|
|
ops::ReorgKernel<paddle::platform::CPUDeviceContext, int64_t>);
|
|
|
|
|
space_to_depth,
|
|
|
|
|
ops::SpaceToDepthKernel<paddle::platform::CPUDeviceContext, float>,
|
|
|
|
|
ops::SpaceToDepthKernel<paddle::platform::CPUDeviceContext, double>,
|
|
|
|
|
ops::SpaceToDepthKernel<paddle::platform::CPUDeviceContext, int64_t>);
|
|
|
|
|
REGISTER_OP_CPU_KERNEL(
|
|
|
|
|
reorg_grad, ops::ReorgGradKernel<paddle::platform::CPUDeviceContext, float>,
|
|
|
|
|
ops::ReorgGradKernel<paddle::platform::CPUDeviceContext, double>,
|
|
|
|
|
ops::ReorgGradKernel<paddle::platform::CPUDeviceContext, int64_t>);
|
|
|
|
|
space_to_depth_grad,
|
|
|
|
|
ops::SpaceToDepthGradKernel<paddle::platform::CPUDeviceContext, float>,
|
|
|
|
|
ops::SpaceToDepthGradKernel<paddle::platform::CPUDeviceContext, double>,
|
|
|
|
|
ops::SpaceToDepthGradKernel<paddle::platform::CPUDeviceContext, int64_t>);
|