target_assign_op.h 5.4 KB
Newer Older
1 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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 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 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.

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_;

    size_t off = lod_[row];

    int id = match_indices_[row * num_prior_box_ + col];
    T* obox = out_box_ + (row * num_prior_box_ + col) * 4;
    int* olabel = out_label_ + row * num_prior_box_ + col;
    T* obox_wt = out_box_wt_ + row * num_prior_box_ + col;
    T* olabel_wt = out_label_wt_ + row * num_prior_box_ + col;

    if (id > -1) {
      const T* gtbox = gt_box_ + ((off + id) * num_prior_box_ + col) * 4;

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

      olabel[0] = gt_label_[off + id];
      obox_wt[0] = 1.;
      olabel_wt[0] = 1.;
    } else {
      obox[0] = 0.;
      obox[1] = 0.;
      obox[2] = 0.;
      obox[3] = 0.;

      olabel[0] = background_label_;
      obox_wt[0] = 0.;
      olabel_wt[0] = 0.;
    }
  }
};

template <typename DeviceContext, typename T>
struct UpdateTargetLabelFunctor {
  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();
    auto neg_lod = neg_indices->lod().back();

    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);

    UpdateTargetLabelFunctor<DeviceContext, T> update_functor;
    update_functor(device_ctx, neg_idx_data, neg_lod_data, num, num_prior_box,
                   background_label, olabel_data, olabel_wt_data);
  }
};

}  // namespace operators
}  // namespace paddle