|
| 1 | +/* Copyright (c) 2017 PaddlePaddle Authors. All Rights Reserved. |
| 2 | +
|
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | +
|
| 7 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +
|
| 9 | +Unless required by applicable law or agreed to in writing, software |
| 10 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +See the License for the specific language governing permissions and |
| 13 | +limitations under the License. */ |
| 14 | + |
| 15 | +#include "paddle/fluid/framework/op_registry.h" |
| 16 | + |
| 17 | +namespace paddle { |
| 18 | +namespace operators { |
| 19 | + |
| 20 | +using Tensor = framework::Tensor; |
| 21 | + |
| 22 | +template <typename DeviceContext, typename T> |
| 23 | +class PolygonBoxTransformCPUKernel : public framework::OpKernel<T> { |
| 24 | + public: |
| 25 | + void Compute(const framework::ExecutionContext& ctx) const override { |
| 26 | + PADDLE_ENFORCE(platform::is_cpu_place(ctx.GetPlace()), |
| 27 | + "It must use CUDAPlace."); |
| 28 | + auto* in = ctx.Input<Tensor>("Input"); |
| 29 | + auto in_dims = in->dims(); |
| 30 | + const T* in_data = in->data<T>(); |
| 31 | + auto* out = ctx.Output<Tensor>("Output"); |
| 32 | + T* out_data = out->mutable_data<T>(ctx.GetPlace()); |
| 33 | + |
| 34 | + int batch_size = in_dims[0]; |
| 35 | + int geo_channel = in_dims[1]; |
| 36 | + int height = in_dims[2]; |
| 37 | + int width = in_dims[3]; |
| 38 | + int id = 0; |
| 39 | + for (int id_n = 0; id_n < batch_size * geo_channel; ++id_n) { |
| 40 | + for (int id_h = 0; id_h < height; ++id_h) { |
| 41 | + for (int id_w = 0; id_w < width; ++id_w) { |
| 42 | + id = id_n * height * width + width * id_h + id_w; |
| 43 | + if (id_n % 2 == 0) { |
| 44 | + out_data[id] = id_w - in_data[id]; |
| 45 | + } else { |
| 46 | + out_data[id] = id_h - in_data[id]; |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | +}; |
| 53 | + |
| 54 | +class PolygonBoxTransformOp : public framework::OperatorWithKernel { |
| 55 | + public: |
| 56 | + using framework::OperatorWithKernel::OperatorWithKernel; |
| 57 | + |
| 58 | + void InferShape(framework::InferShapeContext* ctx) const override { |
| 59 | + PADDLE_ENFORCE( |
| 60 | + ctx->HasInput("Input"), |
| 61 | + "Input (Input) of polygon_box transform op should not be null."); |
| 62 | + PADDLE_ENFORCE( |
| 63 | + ctx->HasOutput("Output"), |
| 64 | + "Output (Output) of polygon_box transform op should not be null."); |
| 65 | + |
| 66 | + auto in_dim = ctx->GetInputDim("Input"); |
| 67 | + |
| 68 | + PADDLE_ENFORCE_EQ(in_dim.size(), 4, "input's rank must be 4."); |
| 69 | + PADDLE_ENFORCE_EQ(in_dim[1] % 2, 0, |
| 70 | + "input's second dimension must be even."); |
| 71 | + |
| 72 | + ctx->SetOutputDim("Output", in_dim); |
| 73 | + } |
| 74 | +}; |
| 75 | + |
| 76 | +class PolygonBoxTransformOpMaker : public framework::OpProtoAndCheckerMaker { |
| 77 | + public: |
| 78 | + void Make() override { |
| 79 | + AddInput( |
| 80 | + "Input", |
| 81 | + "The input with shape [batch_size, geometry_channels, height, width]"); |
| 82 | + AddOutput("Output", "The output with the same shape as input"); |
| 83 | + |
| 84 | + AddComment(R"DOC( |
| 85 | +PolygonBoxTransform Operator. |
| 86 | +The input is the final geometry output in detection network. |
| 87 | +We use 2*n numbers to denote the coordinate shift from n corner vertices of |
| 88 | +the polygon_box to the pixel location. As each distance offset contains two numbers (xi, yi), |
| 89 | +the geometry output contains 2*n channels. |
| 90 | +PolygonBoxTransform Operator is used to transform the coordinate shift to the real coordinate. |
| 91 | +)DOC"); |
| 92 | + } |
| 93 | +}; |
| 94 | + |
| 95 | +} // namespace operators |
| 96 | +} // namespace paddle |
| 97 | + |
| 98 | +namespace ops = paddle::operators; |
| 99 | +REGISTER_OPERATOR(polygon_box_transform, ops::PolygonBoxTransformOp, |
| 100 | + ops::PolygonBoxTransformOpMaker, |
| 101 | + paddle::framework::EmptyGradOpMaker); |
| 102 | +REGISTER_OP_CPU_KERNEL( |
| 103 | + polygon_box_transform, |
| 104 | + ops::PolygonBoxTransformCPUKernel<paddle::platform::CPUPlace, float>, |
| 105 | + ops::PolygonBoxTransformCPUKernel<paddle::platform::CPUPlace, double>); |
0 commit comments