target_assign_op.h 5.7 KB
Newer Older
D
dangqingqing 已提交
1
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserve.
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58

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 "paddle/framework/op_registry.h"
#include "paddle/platform/assert.h"
#include "paddle/platform/for_range.h"

namespace paddle {
namespace operators {

template <typename T>
struct TargetAssignFunctor {
  const T* gt_box_;
  const int* gt_label_;
  const int* match_indices_;
  const size_t* lod_;
  const int background_label_;
  const int64_t num_;
  const int64_t num_prior_box_;

  T* out_box_;
  T* out_box_wt_;
  int* out_label_;
  T* out_label_wt_;

  TargetAssignFunctor(const T* gt_box, const int* gt_label,
                      const int* match_indices, const size_t* lod,
                      const int background_label, const int64_t num,
                      const int64_t np, T* out_box, T* out_box_wt,
                      int* out_label, T* out_label_wt)
      : gt_box_(gt_box),
        gt_label_(gt_label),
        match_indices_(match_indices),
        lod_(lod),
        background_label_(background_label),
        num_(num),
        num_prior_box_(np),
        out_box_(out_box),
        out_box_wt_(out_box_wt),
        out_label_(out_label),
        out_label_wt_(out_label_wt) {}

  HOSTDEVICE void operator()(size_t i) const {
    int row = i / num_prior_box_;
    int col = i - row * num_prior_box_;

D
dangqingqing 已提交
59 60
    size_t row_off = lod_[row];
    int offset = row * num_prior_box_ + col;
61

D
dangqingqing 已提交
62 63 64 65 66
    int id = match_indices_[offset];
    T* obox = out_box_ + offset * 4;
    int* olabel = out_label_ + offset;
    T* obox_wt = out_box_wt_ + offset;
    T* olabel_wt = out_label_wt_ + offset;
67 68

    if (id > -1) {
D
dangqingqing 已提交
69
      const T* gtbox = gt_box_ + ((row_off + id) * num_prior_box_ + col) * 4;
70 71 72 73 74 75

      obox[0] = gtbox[0];
      obox[1] = gtbox[1];
      obox[2] = gtbox[2];
      obox[3] = gtbox[3];

D
dangqingqing 已提交
76 77 78
      olabel[0] = gt_label_[row_off + id];
      obox_wt[0] = static_cast<T>(1.);
      olabel_wt[0] = static_cast<T>(1.);
79
    } else {
D
dangqingqing 已提交
80 81 82 83
      obox[0] = static_cast<T>(0.);
      obox[1] = static_cast<T>(0.);
      obox[2] = static_cast<T>(0.);
      obox[3] = static_cast<T>(0.);
84 85

      olabel[0] = background_label_;
D
dangqingqing 已提交
86 87
      obox_wt[0] = static_cast<T>(0.);
      olabel_wt[0] = static_cast<T>(0.);
88 89 90 91 92
    }
  }
};

template <typename DeviceContext, typename T>
D
dangqingqing 已提交
93
struct NegTargetAssignFunctor {
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
  void operator()(const platform::DeviceContext& ctx, const int* neg_indices,
                  const size_t* lod, const int num, const int num_prior_box,
                  const int background_label, int* out_label,
                  T* out_label_wt) const;
};

template <typename DeviceContext, typename T>
class TargetAssignKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
    auto* enc_gt_box = ctx.Input<framework::LoDTensor>("EncodedGTBBox");
    auto* gt_label = ctx.Input<framework::LoDTensor>("GTScoreLabel");
    auto* match_indices = ctx.Input<framework::Tensor>("MatchIndices");
    auto* neg_indices = ctx.Input<framework::LoDTensor>("NegIndices");

    auto* out_box = ctx.Output<framework::Tensor>("PredBBoxLabel");
    auto* out_box_wt = ctx.Output<framework::Tensor>("PredBBoxWeight");
    auto* out_label = ctx.Output<framework::Tensor>("PredScoreLabel");
    auto* out_label_wt = ctx.Output<framework::Tensor>("PredScoreWeight");

    PADDLE_ENFORCE_EQ(enc_gt_box->lod().size(), 1UL);
    PADDLE_ENFORCE_EQ(gt_label->lod().size(), 1UL);
    PADDLE_ENFORCE_EQ(neg_indices->lod().size(), 1UL);

    int background_label = ctx.Attr<int>("background_label");

    const T* box_data = enc_gt_box->data<T>();
    const int* label_data = gt_label->data<int>();
    const int* match_idx_data = match_indices->data<int>();
    const int* neg_idx_data = neg_indices->data<int>();

    T* obox_data = out_box->mutable_data<T>(ctx.GetPlace());
    T* obox_wt_data = out_box_wt->mutable_data<T>(ctx.GetPlace());
    int* olabel_data = out_label->mutable_data<int>(ctx.GetPlace());
    T* olabel_wt_data = out_label_wt->mutable_data<T>(ctx.GetPlace());

    int64_t num = match_indices->dims()[0];
    int64_t num_prior_box = match_indices->dims()[1];

    auto gt_lod = enc_gt_box->lod().back();
D
dangqingqing 已提交
134
    auto gt_label_lod = gt_label->lod().back();
135
    auto neg_lod = neg_indices->lod().back();
D
dangqingqing 已提交
136 137 138
    for (size_t i = 0; i < gt_lod.size(); ++i) {
      PADDLE_ENFORCE_EQ(gt_lod.data()[i], gt_label_lod.data()[i]);
    }
139 140 141 142 143 144 145 146 147 148 149 150 151 152

    size_t* gt_lod_data = gt_lod.data(ctx.GetPlace());
    size_t* neg_lod_data = neg_lod.data(ctx.GetPlace());

    TargetAssignFunctor<T> functor(box_data, label_data, match_idx_data,
                                   gt_lod_data, background_label, num,
                                   num_prior_box, obox_data, obox_wt_data,
                                   olabel_data, olabel_wt_data);

    auto& device_ctx = ctx.template device_context<DeviceContext>();
    platform::ForRange<DeviceContext> for_range(device_ctx,
                                                num * num_prior_box);
    for_range(functor);

D
dangqingqing 已提交
153 154 155
    NegTargetAssignFunctor<DeviceContext, T> neg_trg_functor;
    neg_trg_functor(device_ctx, neg_idx_data, neg_lod_data, num, num_prior_box,
                    background_label, olabel_data, olabel_wt_data);
156 157 158 159 160
  }
};

}  // namespace operators
}  // namespace paddle