distribute_fpn_proposals_op.h 7.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/* Copyright (c) 2019 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 <algorithm>
#include <cmath>
#include <cstring>
#include <string>
#include <vector>
22

23
#include "paddle/fluid/framework/op_registry.h"
24
#include "paddle/phi/kernels/funcs/math_function.h"
25 26 27 28 29 30

namespace paddle {
namespace operators {

const int kBoxDim = 4;

31 32
inline std::vector<size_t> GetLodFromRoisNum(
    const framework::Tensor* rois_num) {
33 34
  std::vector<size_t> rois_lod;
  auto* rois_num_data = rois_num->data<int>();
35
  framework::Tensor cpu_tensor;
36
  if (platform::is_gpu_place(rois_num->place())) {
37 38
    paddle::framework::TensorCopySync(
        *rois_num, platform::CPUPlace(), &cpu_tensor);
39 40 41 42 43 44 45 46 47
    rois_num_data = cpu_tensor.data<int>();
  }
  rois_lod.push_back(static_cast<size_t>(0));
  for (int i = 0; i < rois_num->numel(); ++i) {
    rois_lod.push_back(rois_lod.back() + static_cast<size_t>(rois_num_data[i]));
  }
  return rois_lod;
}

48
template <typename T>
49
static inline T BBoxArea(const T* box, bool pixel_offset) {
50 51 52 53 54 55 56
  if (box[2] < box[0] || box[3] < box[1]) {
    // If coordinate values are is invalid
    // (e.g. xmax < xmin or ymax < ymin), return 0.
    return static_cast<T>(0.);
  } else {
    const T w = box[2] - box[0];
    const T h = box[3] - box[1];
57
    if (pixel_offset) {
58 59
      // If coordinate values are not within range [0, 1].
      return (w + 1) * (h + 1);
60 61
    } else {
      return w * h;
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
    }
  }
}

template <typename T>
class DistributeFpnProposalsOpKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& context) const override {
    auto* fpn_rois = context.Input<paddle::framework::LoDTensor>("FpnRois");

    auto multi_fpn_rois =
        context.MultiOutput<paddle::framework::LoDTensor>("MultiFpnRois");

    auto* restore_index =
        context.Output<paddle::framework::Tensor>("RestoreIndex");

    const int min_level = context.Attr<int>("min_level");
    const int max_level = context.Attr<int>("max_level");
    const int refer_level = context.Attr<int>("refer_level");
    const int refer_scale = context.Attr<int>("refer_scale");
82
    const bool pixel_offset = context.Attr<bool>("pixel_offset");
83 84 85
    const int num_level = max_level - min_level + 1;

    // check that the fpn_rois is not empty
86
    if (!context.HasInput("RoisNum")) {
87 88
      PADDLE_ENFORCE_EQ(fpn_rois->lod().size(),
                        1UL,
89 90
                        platform::errors::InvalidArgument(
                            "DistributeFpnProposalsOp needs LoD "
91 92
                            "with one level. But received level is %d",
                            fpn_rois->lod().size()));
93
    }
94

95 96 97
    std::vector<size_t> fpn_rois_lod;
    int fpn_rois_num;
    if (context.HasInput("RoisNum")) {
98
      auto* rois_num = context.Input<framework::Tensor>("RoisNum");
99 100 101 102 103
      fpn_rois_lod = GetLodFromRoisNum(rois_num);
    } else {
      fpn_rois_lod = fpn_rois->lod().back();
    }
    fpn_rois_num = fpn_rois_lod[fpn_rois_lod.size() - 1];
104 105 106 107 108
    std::vector<int> target_level;
    // std::vector<int> target_level(fpn_rois_num, -1);
    // record the number of rois in each level
    std::vector<int> num_rois_level(num_level, 0);
    std::vector<int> num_rois_level_integral(num_level + 1, 0);
109
    for (size_t i = 0; i < fpn_rois_lod.size() - 1; ++i) {
110
      auto fpn_rois_slice =
111 112 113 114
          fpn_rois->Slice(fpn_rois_lod[i], fpn_rois_lod[i + 1]);
      const T* rois_data = fpn_rois_slice.data<T>();
      for (int j = 0; j < fpn_rois_slice.dims()[0]; ++j) {
        // get the target level of current rois
115
        T roi_scale = std::sqrt(BBoxArea(rois_data, pixel_offset));
116 117
        int tgt_lvl = std::floor(std::log2(roi_scale / refer_scale + (T)1e-6) +
                                 refer_level);
118 119 120 121 122 123 124 125
        tgt_lvl = std::min(max_level, std::max(tgt_lvl, min_level));
        target_level.push_back(tgt_lvl);
        num_rois_level[tgt_lvl - min_level]++;
        rois_data += kBoxDim;
      }
    }
    // define the output rois
    // pointer which point to each level fpn rois
J
jerrywgz 已提交
126
    std::vector<T*> multi_fpn_rois_data(num_level);
127 128 129 130 131 132 133 134 135 136 137 138 139
    // lod0 which will record the offset information of each level rois
    std::vector<std::vector<size_t>> multi_fpn_rois_lod0;
    for (int i = 0; i < num_level; ++i) {
      // allocate memory for each level rois
      multi_fpn_rois[i]->mutable_data<T>({num_rois_level[i], kBoxDim},
                                         context.GetPlace());
      multi_fpn_rois_data[i] = multi_fpn_rois[i]->data<T>();
      std::vector<size_t> lod0(1, 0);
      multi_fpn_rois_lod0.push_back(lod0);
      // statistic start point for each level rois
      num_rois_level_integral[i + 1] =
          num_rois_level_integral[i] + num_rois_level[i];
    }
140
    restore_index->mutable_data<int>({fpn_rois_num, 1}, context.GetPlace());
141 142 143
    int* restore_index_data = restore_index->data<int>();
    std::vector<int> restore_index_inter(fpn_rois_num, -1);
    // distribute the rois into different fpn level by target level
144
    for (size_t i = 0; i < fpn_rois_lod.size() - 1; ++i) {
145
      auto fpn_rois_slice =
146 147 148 149 150 151 152 153 154
          fpn_rois->Slice(fpn_rois_lod[i], fpn_rois_lod[i + 1]);
      const T* rois_data = fpn_rois_slice.data<T>();
      size_t cur_offset = fpn_rois_lod[i];
      // std::vector<size_t > lod_offset[num_level];
      for (int j = 0; j < num_level; j++) {
        multi_fpn_rois_lod0[j].push_back(multi_fpn_rois_lod0[j][i]);
      }
      for (int j = 0; j < fpn_rois_slice.dims()[0]; ++j) {
        int lvl = target_level[cur_offset + j];
155 156
        memcpy(multi_fpn_rois_data[lvl - min_level],
               rois_data,
157 158 159 160 161 162 163 164 165 166 167 168
               kBoxDim * sizeof(T));
        multi_fpn_rois_data[lvl - min_level] += kBoxDim;
        int index_in_shuffle = num_rois_level_integral[lvl - min_level] +
                               multi_fpn_rois_lod0[lvl - min_level][i + 1];
        restore_index_inter[index_in_shuffle] = cur_offset + j;
        multi_fpn_rois_lod0[lvl - min_level][i + 1]++;
        rois_data += kBoxDim;
      }
    }
    for (int i = 0; i < fpn_rois_num; ++i) {
      restore_index_data[restore_index_inter[i]] = i;
    }
169 170
    auto multi_rois_num =
        context.MultiOutput<framework::Tensor>("MultiLevelRoIsNum");
171 172 173 174 175 176 177 178 179 180 181
    if (multi_rois_num.size() > 0) {
      int batch_size = fpn_rois_lod.size() - 1;
      for (int i = 0; i < num_level; ++i) {
        int* rois_num_data = multi_rois_num[i]->mutable_data<int>(
            {batch_size}, context.GetPlace());
        for (int j = 0; j < batch_size; ++j) {
          rois_num_data[j] = static_cast<int>(multi_fpn_rois_lod0[i][j + 1] -
                                              multi_fpn_rois_lod0[i][j]);
        }
      }
    }
182 183 184 185 186 187 188 189 190 191
    // merge lod information into LoDTensor
    for (int i = 0; i < num_level; ++i) {
      framework::LoD lod;
      lod.emplace_back(multi_fpn_rois_lod0[i]);
      multi_fpn_rois[i]->set_lod(lod);
    }
  }
};
}  // namespace operators
}  // namespace paddle