detection_map_op.h 18.4 KB
Newer Older
1
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
W
wanghaox 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15

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
16 17 18 19 20
#include <algorithm>
#include <map>
#include <string>
#include <utility>
#include <vector>
W
wanghaox 已提交
21 22
#include "paddle/fluid/framework/eigen.h"
#include "paddle/fluid/framework/op_registry.h"
W
wanghaox 已提交
23 24 25 26

namespace paddle {
namespace operators {

W
wanghaox 已提交
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
enum APType { kNone = 0, kIntegral, k11point };

APType GetAPType(std::string str) {
  if (str == "integral") {
    return APType::kIntegral;
  } else if (str == "11point") {
    return APType::k11point;
  } else {
    return APType::kNone;
  }
}

template <typename T>
inline bool SortScorePairDescend(const std::pair<float, T>& pair1,
                                 const std::pair<float, T>& pair2) {
  return pair1.first > pair2.first;
}

W
wanghaox 已提交
45 46 47
template <typename T>
inline void GetAccumulation(std::vector<std::pair<T, int>> in_pairs,
                            std::vector<int>* accu_vec) {
W
wanghaox 已提交
48
  std::stable_sort(in_pairs.begin(), in_pairs.end(), SortScorePairDescend<int>);
W
wanghaox 已提交
49 50 51 52 53 54 55 56 57 58 59 60 61
  accu_vec->clear();
  size_t sum = 0;
  for (size_t i = 0; i < in_pairs.size(); ++i) {
    auto count = in_pairs[i].second;
    sum += count;
    accu_vec->push_back(sum);
  }
}

template <typename Place, typename T>
class DetectionMAPOpKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
W
wanghaox 已提交
62
    auto* in_detect = ctx.Input<framework::LoDTensor>("DetectRes");
W
wanghaox 已提交
63 64
    auto* in_label = ctx.Input<framework::LoDTensor>("Label");
    auto* out_map = ctx.Output<framework::Tensor>("MAP");
W
wanghaox 已提交
65

W
wanghaox 已提交
66 67 68 69
    auto* in_pos_count = ctx.Input<framework::Tensor>("PosCount");
    auto* in_true_pos = ctx.Input<framework::LoDTensor>("TruePos");
    auto* in_false_pos = ctx.Input<framework::LoDTensor>("FalsePos");

W
wanghaox 已提交
70 71 72
    auto* out_pos_count = ctx.Output<framework::Tensor>("AccumPosCount");
    auto* out_true_pos = ctx.Output<framework::LoDTensor>("AccumTruePos");
    auto* out_false_pos = ctx.Output<framework::LoDTensor>("AccumFalsePos");
W
wanghaox 已提交
73

W
wanghaox 已提交
74
    float overlap_threshold = ctx.Attr<float>("overlap_threshold");
75
    bool evaluate_difficult = ctx.Attr<bool>("evaluate_difficult");
W
wanghaox 已提交
76
    auto ap_type = GetAPType(ctx.Attr<std::string>("ap_type"));
77
    int class_num = ctx.Attr<int>("class_num");
W
wanghaox 已提交
78

79 80
    auto& label_lod = in_label->lod();
    auto& detect_lod = in_detect->lod();
81 82 83 84 85
    PADDLE_ENFORCE_EQ(
        label_lod.size(), 1UL,
        platform::errors::InvalidArgument("Only support LodTensor of lod_level "
                                          "with 1 in label, but received %d.",
                                          label_lod.size()));
W
wanghaox 已提交
86
    PADDLE_ENFORCE_EQ(label_lod[0].size(), detect_lod[0].size(),
87 88 89 90
                      platform::errors::InvalidArgument(
                          "The batch_size of input(Label) and input(Detection) "
                          "must be the same, but received %d:%d",
                          label_lod[0].size(), detect_lod[0].size()));
W
wanghaox 已提交
91 92 93 94

    std::vector<std::map<int, std::vector<Box>>> gt_boxes;
    std::vector<std::map<int, std::vector<std::pair<T, Box>>>> detect_boxes;

95
    GetBoxes(*in_label, *in_detect, &gt_boxes, detect_boxes);
W
wanghaox 已提交
96 97 98 99 100

    std::map<int, int> label_pos_count;
    std::map<int, std::vector<std::pair<T, int>>> true_pos;
    std::map<int, std::vector<std::pair<T, int>>> false_pos;

101 102 103 104 105 106 107
    auto* has_state = ctx.Input<framework::LoDTensor>("HasState");
    int state = 0;
    if (has_state) {
      state = has_state->data<int>()[0];
    }

    if (in_pos_count != nullptr && state) {
108 109
      GetInputPos(*in_pos_count, *in_true_pos, *in_false_pos, &label_pos_count,
                  &true_pos, &false_pos, class_num);
W
wanghaox 已提交
110 111
    }

W
wanghaox 已提交
112
    CalcTrueAndFalsePositive(gt_boxes, detect_boxes, evaluate_difficult,
113 114
                             overlap_threshold, &label_pos_count, &true_pos,
                             &false_pos);
W
wanghaox 已提交
115

116 117 118
    int background_label = ctx.Attr<int>("background_label");
    T map = CalcMAP(ap_type, label_pos_count, true_pos, false_pos,
                    background_label);
W
wanghaox 已提交
119

120 121
    GetOutputPos(ctx, label_pos_count, true_pos, false_pos, out_pos_count,
                 out_true_pos, out_false_pos, class_num);
W
wanghaox 已提交
122

W
wanghaox 已提交
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
    T* map_data = out_map->mutable_data<T>(ctx.GetPlace());
    map_data[0] = map;
  }

 protected:
  struct Box {
    Box(T xmin, T ymin, T xmax, T ymax)
        : xmin(xmin), ymin(ymin), xmax(xmax), ymax(ymax), is_difficult(false) {}

    T xmin, ymin, xmax, ymax;
    bool is_difficult;
  };

  inline T JaccardOverlap(const Box& box1, const Box& box2) const {
    if (box2.xmin > box1.xmax || box2.xmax < box1.xmin ||
        box2.ymin > box1.ymax || box2.ymax < box1.ymin) {
      return 0.0;
W
wanghaox 已提交
140
    } else {
W
wanghaox 已提交
141 142 143 144 145 146 147 148 149 150 151 152 153
      T inter_xmin = std::max(box1.xmin, box2.xmin);
      T inter_ymin = std::max(box1.ymin, box2.ymin);
      T inter_xmax = std::min(box1.xmax, box2.xmax);
      T inter_ymax = std::min(box1.ymax, box2.ymax);

      T inter_width = inter_xmax - inter_xmin;
      T inter_height = inter_ymax - inter_ymin;
      T inter_area = inter_width * inter_height;

      T bbox_area1 = (box1.xmax - box1.xmin) * (box1.ymax - box1.ymin);
      T bbox_area2 = (box2.xmax - box2.xmin) * (box2.ymax - box2.ymin);

      return inter_area / (bbox_area1 + bbox_area2 - inter_area);
W
wanghaox 已提交
154 155 156
    }
  }

157 158 159 160 161 162 163 164 165
  inline void ClipBBox(const Box& bbox, Box* clipped_bbox) const {
    T one = static_cast<T>(1.0);
    T zero = static_cast<T>(0.0);
    clipped_bbox->xmin = std::max(std::min(bbox.xmin, one), zero);
    clipped_bbox->ymin = std::max(std::min(bbox.ymin, one), zero);
    clipped_bbox->xmax = std::max(std::min(bbox.xmax, one), zero);
    clipped_bbox->ymax = std::max(std::min(bbox.ymax, one), zero);
  }

W
wanghaox 已提交
166 167
  void GetBoxes(const framework::LoDTensor& input_label,
                const framework::LoDTensor& input_detect,
168
                std::vector<std::map<int, std::vector<Box>>>* gt_boxes,
W
wanghaox 已提交
169 170 171 172
                std::vector<std::map<int, std::vector<std::pair<T, Box>>>>&
                    detect_boxes) const {
    auto labels = framework::EigenTensor<T, 2>::From(input_label);
    auto detect = framework::EigenTensor<T, 2>::From(input_detect);
W
wanghaox 已提交
173

174 175
    auto& label_lod = input_label.lod();
    auto& detect_lod = input_detect.lod();
W
wanghaox 已提交
176 177

    int batch_size = label_lod[0].size() - 1;
178
    auto& label_index = label_lod[0];
W
wanghaox 已提交
179

W
wanghaox 已提交
180 181
    for (int n = 0; n < batch_size; ++n) {
      std::map<int, std::vector<Box>> boxes;
Q
QI JUN 已提交
182
      for (size_t i = label_index[n]; i < label_index[n + 1]; ++i) {
W
wanghaox 已提交
183
        int label = labels(i, 0);
184 185 186 187 188 189 190 191 192
        if (input_label.dims()[1] == 6) {
          Box box(labels(i, 2), labels(i, 3), labels(i, 4), labels(i, 5));
          auto is_difficult = labels(i, 1);
          if (std::abs(is_difficult - 0.0) < 1e-6)
            box.is_difficult = false;
          else
            box.is_difficult = true;
          boxes[label].push_back(box);
        } else {
193 194 195 196 197 198
          PADDLE_ENFORCE_EQ(
              input_label.dims()[1], 5,
              platform::errors::InvalidArgument(
                  "The input label width"
                  " must be 5, but received %d, please check your input data",
                  input_label.dims()[1]));
199 200 201
          Box box(labels(i, 1), labels(i, 2), labels(i, 3), labels(i, 4));
          boxes[label].push_back(box);
        }
W
wanghaox 已提交
202
      }
203
      gt_boxes->push_back(boxes);
W
wanghaox 已提交
204 205
    }

W
wanghaox 已提交
206 207 208
    auto detect_index = detect_lod[0];
    for (int n = 0; n < batch_size; ++n) {
      std::map<int, std::vector<std::pair<T, Box>>> boxes;
Q
QI JUN 已提交
209
      for (size_t i = detect_index[n]; i < detect_index[n + 1]; ++i) {
W
wanghaox 已提交
210 211 212 213
        Box box(detect(i, 2), detect(i, 3), detect(i, 4), detect(i, 5));
        int label = detect(i, 0);
        auto score = detect(i, 1);
        boxes[label].push_back(std::make_pair(score, box));
W
wanghaox 已提交
214
      }
W
wanghaox 已提交
215
      detect_boxes.push_back(boxes);
W
wanghaox 已提交
216 217 218
    }
  }

W
wanghaox 已提交
219 220 221 222 223
  void GetOutputPos(
      const framework::ExecutionContext& ctx,
      const std::map<int, int>& label_pos_count,
      const std::map<int, std::vector<std::pair<T, int>>>& true_pos,
      const std::map<int, std::vector<std::pair<T, int>>>& false_pos,
224 225 226
      framework::Tensor* output_pos_count,
      framework::LoDTensor* output_true_pos,
      framework::LoDTensor* output_false_pos, const int class_num) const {
W
wanghaox 已提交
227 228
    int true_pos_count = 0;
    int false_pos_count = 0;
229 230 231 232 233 234 235
    for (auto it = true_pos.begin(); it != true_pos.end(); ++it) {
      auto tp = it->second;
      true_pos_count += tp.size();
    }
    for (auto it = false_pos.begin(); it != false_pos.end(); ++it) {
      auto fp = it->second;
      false_pos_count += fp.size();
W
wanghaox 已提交
236 237
    }

238
    int* pos_count_data = output_pos_count->mutable_data<int>(
239
        framework::make_ddim({class_num, 1}), ctx.GetPlace());
240

241
    T* true_pos_data = output_true_pos->mutable_data<T>(
W
wanghaox 已提交
242
        framework::make_ddim({true_pos_count, 2}), ctx.GetPlace());
243
    T* false_pos_data = output_false_pos->mutable_data<T>(
W
wanghaox 已提交
244 245 246 247 248
        framework::make_ddim({false_pos_count, 2}), ctx.GetPlace());
    true_pos_count = 0;
    false_pos_count = 0;
    std::vector<size_t> true_pos_starts = {0};
    std::vector<size_t> false_pos_starts = {0};
249
    for (int i = 0; i < class_num; ++i) {
W
wanghaox 已提交
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
      auto it_count = label_pos_count.find(i);
      pos_count_data[i] = 0;
      if (it_count != label_pos_count.end()) {
        pos_count_data[i] = it_count->second;
      }
      auto it_true_pos = true_pos.find(i);
      if (it_true_pos != true_pos.end()) {
        const std::vector<std::pair<T, int>>& true_pos_vec =
            it_true_pos->second;
        for (const std::pair<T, int>& tp : true_pos_vec) {
          true_pos_data[true_pos_count * 2] = tp.first;
          true_pos_data[true_pos_count * 2 + 1] = static_cast<T>(tp.second);
          true_pos_count++;
        }
      }
      true_pos_starts.push_back(true_pos_count);

      auto it_false_pos = false_pos.find(i);
      if (it_false_pos != false_pos.end()) {
        const std::vector<std::pair<T, int>>& false_pos_vec =
            it_false_pos->second;
        for (const std::pair<T, int>& fp : false_pos_vec) {
          false_pos_data[false_pos_count * 2] = fp.first;
          false_pos_data[false_pos_count * 2 + 1] = static_cast<T>(fp.second);
          false_pos_count++;
        }
      }
      false_pos_starts.push_back(false_pos_count);
    }

    framework::LoD true_pos_lod;
    true_pos_lod.emplace_back(true_pos_starts);
    framework::LoD false_pos_lod;
    false_pos_lod.emplace_back(false_pos_starts);

285 286
    output_true_pos->set_lod(true_pos_lod);
    output_false_pos->set_lod(false_pos_lod);
W
wanghaox 已提交
287 288
  }

289 290 291
  void GetInputPos(const framework::Tensor& input_pos_count,
                   const framework::LoDTensor& input_true_pos,
                   const framework::LoDTensor& input_false_pos,
292 293 294
                   std::map<int, int>* label_pos_count,
                   std::map<int, std::vector<std::pair<T, int>>>* true_pos,
                   std::map<int, std::vector<std::pair<T, int>>>* false_pos,
295
                   const int class_num) const {
W
wanghaox 已提交
296
    const int* pos_count_data = input_pos_count.data<int>();
297
    for (int i = 0; i < class_num; ++i) {
298
      (*label_pos_count)[i] = pos_count_data[i];
W
wanghaox 已提交
299 300
    }

W
wanghaox 已提交
301 302 303
    auto SetData = [](const framework::LoDTensor& pos_tensor,
                      std::map<int, std::vector<std::pair<T, int>>>& pos) {
      const T* pos_data = pos_tensor.data<T>();
304
      auto& pos_data_lod = pos_tensor.lod()[0];
305 306
      for (size_t i = 0; i < pos_data_lod.size() - 1; ++i) {
        for (size_t j = pos_data_lod[i]; j < pos_data_lod[i + 1]; ++j) {
W
wanghaox 已提交
307
          T score = pos_data[j * 2];
308
          int flag = pos_data[j * 2 + 1];
W
wanghaox 已提交
309 310
          pos[i].push_back(std::make_pair(score, flag));
        }
W
wanghaox 已提交
311
      }
W
wanghaox 已提交
312 313
    };

314 315
    SetData(input_true_pos, *true_pos);
    SetData(input_false_pos, *false_pos);
W
wanghaox 已提交
316 317 318
    return;
  }

W
wanghaox 已提交
319
  void CalcTrueAndFalsePositive(
W
wanghaox 已提交
320 321 322 323
      const std::vector<std::map<int, std::vector<Box>>>& gt_boxes,
      const std::vector<std::map<int, std::vector<std::pair<T, Box>>>>&
          detect_boxes,
      bool evaluate_difficult, float overlap_threshold,
324 325 326
      std::map<int, int>* label_pos_count,
      std::map<int, std::vector<std::pair<T, int>>>* true_pos,
      std::map<int, std::vector<std::pair<T, int>>>* false_pos) const {
W
wanghaox 已提交
327 328
    int batch_size = gt_boxes.size();
    for (int n = 0; n < batch_size; ++n) {
329 330
      auto& image_gt_boxes = gt_boxes[n];
      for (auto& image_gt_box : image_gt_boxes) {
W
wanghaox 已提交
331
        size_t count = 0;
332
        auto& labeled_bboxes = image_gt_box.second;
W
wanghaox 已提交
333 334 335
        if (evaluate_difficult) {
          count = labeled_bboxes.size();
        } else {
336 337 338 339 340
          for (auto& box : labeled_bboxes) {
            if (!box.is_difficult) {
              ++count;
            }
          }
W
wanghaox 已提交
341 342 343 344
        }
        if (count == 0) {
          continue;
        }
345
        int label = image_gt_box.first;
346 347
        if (label_pos_count->find(label) == label_pos_count->end()) {
          (*label_pos_count)[label] = count;
W
wanghaox 已提交
348
        } else {
349
          (*label_pos_count)[label] += count;
W
wanghaox 已提交
350 351 352 353
        }
      }
    }

W
wanghaox 已提交
354 355 356
    for (size_t n = 0; n < detect_boxes.size(); ++n) {
      auto image_gt_boxes = gt_boxes[n];
      auto detections = detect_boxes[n];
W
wanghaox 已提交
357

W
wanghaox 已提交
358
      if (image_gt_boxes.size() == 0) {
W
wanghaox 已提交
359
        for (auto it = detections.begin(); it != detections.end(); ++it) {
W
wanghaox 已提交
360
          auto pred_boxes = it->second;
W
wanghaox 已提交
361
          int label = it->first;
W
wanghaox 已提交
362 363
          for (size_t i = 0; i < pred_boxes.size(); ++i) {
            auto score = pred_boxes[i].first;
364 365
            (*true_pos)[label].push_back(std::make_pair(score, 0));
            (*false_pos)[label].push_back(std::make_pair(score, 1));
W
wanghaox 已提交
366 367 368 369 370 371 372
          }
        }
        continue;
      }

      for (auto it = detections.begin(); it != detections.end(); ++it) {
        int label = it->first;
W
wanghaox 已提交
373 374 375 376
        auto pred_boxes = it->second;
        if (image_gt_boxes.find(label) == image_gt_boxes.end()) {
          for (size_t i = 0; i < pred_boxes.size(); ++i) {
            auto score = pred_boxes[i].first;
377 378
            (*true_pos)[label].push_back(std::make_pair(score, 0));
            (*false_pos)[label].push_back(std::make_pair(score, 1));
W
wanghaox 已提交
379 380 381 382
          }
          continue;
        }

W
wanghaox 已提交
383
        auto matched_bboxes = image_gt_boxes.find(label)->second;
W
wanghaox 已提交
384 385
        std::vector<bool> visited(matched_bboxes.size(), false);
        // Sort detections in descend order based on scores
W
wanghaox 已提交
386 387 388 389
        std::sort(pred_boxes.begin(), pred_boxes.end(),
                  SortScorePairDescend<Box>);
        for (size_t i = 0; i < pred_boxes.size(); ++i) {
          T max_overlap = -1.0;
W
wanghaox 已提交
390
          size_t max_idx = 0;
W
wanghaox 已提交
391
          auto score = pred_boxes[i].first;
W
wanghaox 已提交
392
          for (size_t j = 0; j < matched_bboxes.size(); ++j) {
393 394 395
            Box& pred_box = pred_boxes[i].second;
            ClipBBox(pred_box, &pred_box);
            T overlap = JaccardOverlap(pred_box, matched_bboxes[j]);
W
wanghaox 已提交
396 397 398 399 400 401 402 403 404 405 406
            if (overlap > max_overlap) {
              max_overlap = overlap;
              max_idx = j;
            }
          }
          if (max_overlap > overlap_threshold) {
            bool match_evaluate_difficult =
                evaluate_difficult ||
                (!evaluate_difficult && !matched_bboxes[max_idx].is_difficult);
            if (match_evaluate_difficult) {
              if (!visited[max_idx]) {
407 408
                (*true_pos)[label].push_back(std::make_pair(score, 1));
                (*false_pos)[label].push_back(std::make_pair(score, 0));
W
wanghaox 已提交
409 410
                visited[max_idx] = true;
              } else {
411 412
                (*true_pos)[label].push_back(std::make_pair(score, 0));
                (*false_pos)[label].push_back(std::make_pair(score, 1));
W
wanghaox 已提交
413 414 415
              }
            }
          } else {
416 417
            (*true_pos)[label].push_back(std::make_pair(score, 0));
            (*false_pos)[label].push_back(std::make_pair(score, 1));
W
wanghaox 已提交
418 419 420 421 422 423
          }
        }
      }
    }
  }

424 425 426 427
  T CalcMAP(APType ap_type, const std::map<int, int>& label_pos_count,
            const std::map<int, std::vector<std::pair<T, int>>>& true_pos,
            const std::map<int, std::vector<std::pair<T, int>>>& false_pos,
            const int background_label) const {
W
wanghaox 已提交
428 429 430 431 432
    T mAP = 0.0;
    int count = 0;
    for (auto it = label_pos_count.begin(); it != label_pos_count.end(); ++it) {
      int label = it->first;
      int label_num_pos = it->second;
433 434 435 436 437
      if (label_num_pos == background_label) {
        continue;
      }
      if (true_pos.find(label) == true_pos.end()) {
        count++;
W
wanghaox 已提交
438
        continue;
439
      }
W
wanghaox 已提交
440 441 442 443 444 445 446
      auto label_true_pos = true_pos.find(label)->second;
      auto label_false_pos = false_pos.find(label)->second;
      // Compute average precision.
      std::vector<int> tp_sum;
      GetAccumulation<T>(label_true_pos, &tp_sum);
      std::vector<int> fp_sum;
      GetAccumulation<T>(label_false_pos, &fp_sum);
W
wanghaox 已提交
447
      std::vector<T> precision, recall;
W
wanghaox 已提交
448 449 450
      size_t num = tp_sum.size();
      // Compute Precision.
      for (size_t i = 0; i < num; ++i) {
W
wanghaox 已提交
451 452 453
        precision.push_back(static_cast<T>(tp_sum[i]) /
                            static_cast<T>(tp_sum[i] + fp_sum[i]));
        recall.push_back(static_cast<T>(tp_sum[i]) / label_num_pos);
W
wanghaox 已提交
454 455
      }
      // VOC2007 style
W
wanghaox 已提交
456 457
      if (ap_type == APType::k11point) {
        std::vector<T> max_precisions(11, 0.0);
W
wanghaox 已提交
458 459 460 461 462 463 464 465 466 467 468 469 470 471
        int start_idx = num - 1;
        for (int j = 10; j >= 0; --j)
          for (int i = start_idx; i >= 0; --i) {
            if (recall[i] < j / 10.) {
              start_idx = i;
              if (j > 0) max_precisions[j - 1] = max_precisions[j];
              break;
            } else {
              if (max_precisions[j] < precision[i])
                max_precisions[j] = precision[i];
            }
          }
        for (int j = 10; j >= 0; --j) mAP += max_precisions[j] / 11;
        ++count;
W
wanghaox 已提交
472
      } else if (ap_type == APType::kIntegral) {
W
wanghaox 已提交
473 474 475 476 477 478 479 480 481 482 483
        // Nature integral
        float average_precisions = 0.;
        float prev_recall = 0.;
        for (size_t i = 0; i < num; ++i) {
          if (fabs(recall[i] - prev_recall) > 1e-6)
            average_precisions += precision[i] * fabs(recall[i] - prev_recall);
          prev_recall = recall[i];
        }
        mAP += average_precisions;
        ++count;
      } else {
484 485 486
        PADDLE_THROW(platform::errors::Unimplemented(
            "Unkown ap version %s. Now only supports integral and l1point.",
            ap_type));
W
wanghaox 已提交
487 488 489
      }
    }
    if (count != 0) mAP /= count;
490
    return mAP;
W
wanghaox 已提交
491 492 493 494 495
  }
};  // namespace operators

}  // namespace operators
}  // namespace paddle