box_clip_op.h 2.2 KB
Newer Older
J
jerrywgz 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
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. */

#pragma once
#include <string>
14

J
jerrywgz 已提交
15 16
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/operators/detection/bbox_util.h"
17
#include "paddle/phi/kernels/funcs/math_function.h"
J
jerrywgz 已提交
18 19 20 21

namespace paddle {
namespace operators {

22
using Tensor = phi::DenseTensor;
23
using LoDTensor = phi::DenseTensor;
J
jerrywgz 已提交
24 25 26 27 28

template <typename DeviceContext, typename T>
class BoxClipKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& context) const override {
29
    auto* input_box = context.Input<LoDTensor>("Input");
J
jerrywgz 已提交
30
    auto* im_info = context.Input<LoDTensor>("ImInfo");
31
    auto* output_box = context.Output<LoDTensor>("Output");
L
Leo Chen 已提交
32
    auto& dev_ctx = context.template device_context<phi::CPUContext>();
J
jerrywgz 已提交
33 34
    output_box->mutable_data<T>(context.GetPlace());
    if (input_box->lod().size()) {
35 36
      PADDLE_ENFORCE_EQ(input_box->lod().size(),
                        1UL,
37
                        platform::errors::InvalidArgument(
38 39 40 41
                            "Input(Input) of BoxClip only supports 1 level "
                            "of LoD. But received the "
                            "level = %d",
                            input_box->lod().size()));
J
jerrywgz 已提交
42 43 44 45 46 47 48 49 50 51 52 53 54 55
    }
    auto box_lod = input_box->lod().back();
    int64_t n = static_cast<int64_t>(box_lod.size() - 1);
    for (int i = 0; i < n; ++i) {
      Tensor im_info_slice = im_info->Slice(i, i + 1);
      Tensor box_slice = input_box->Slice(box_lod[i], box_lod[i + 1]);
      Tensor output_slice = output_box->Slice(box_lod[i], box_lod[i + 1]);
      ClipTiledBoxes<T>(dev_ctx, im_info_slice, box_slice, &output_slice);
    }
  }
};

}  // namespace operators
}  // namespace paddle