iou_similarity_op.h 3.8 KB
Newer Older
1
/* Copyright (c) 2016 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
Y
Yi Wang 已提交
16 17
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/platform/for_range.h"
W
wanghaox 已提交
18 19

template <typename T>
W
wanghaox 已提交
20
inline HOSTDEVICE T IOUSimilarity(T xmin1, T ymin1, T xmax1, T ymax1, T xmin2,
21 22
                                  T ymin2, T xmax2, T ymax2, bool normalized,
                                  T eps) {
W
wanghaox 已提交
23
  constexpr T zero = static_cast<T>(0);
24 25 26 27 28 29 30 31 32 33
  T area1;
  T area2;
  if (!normalized) {
    area1 = (ymax1 - ymin1 + 1) * (xmax1 - xmin1 + 1);
    area2 = (ymax2 - ymin2 + 1) * (xmax2 - xmin2 + 1);
  } else {
    area1 = (ymax1 - ymin1) * (xmax1 - xmin1);
    area2 = (ymax2 - ymin2) * (xmax2 - xmin2);
  }

W
wanghaox 已提交
34 35 36 37 38 39
  T inter_xmax = xmax1 > xmax2 ? xmax2 : xmax1;
  T inter_ymax = ymax1 > ymax2 ? ymax2 : ymax1;
  T inter_xmin = xmin1 > xmin2 ? xmin1 : xmin2;
  T inter_ymin = ymin1 > ymin2 ? ymin1 : ymin2;
  T inter_height = inter_ymax - inter_ymin;
  T inter_width = inter_xmax - inter_xmin;
40 41 42 43
  if (!normalized) {
    inter_height = inter_height + 1;
    inter_width = inter_width + 1;
  }
W
wanghaox 已提交
44 45
  inter_height = inter_height > zero ? inter_height : zero;
  inter_width = inter_width > zero ? inter_width : zero;
W
wanghaox 已提交
46
  T inter_area = inter_width * inter_height;
47
  T union_area = area1 + area2 - inter_area + eps;
W
wanghaox 已提交
48 49 50 51 52 53
  T sim_score = inter_area / union_area;
  return sim_score;
}

template <typename T>
struct IOUSimilarityFunctor {
54 55
  IOUSimilarityFunctor(const T* x, const T* y, T* z, int cols, bool normalized,
                       T eps)
56 57 58 59
      : x_(x),
        y_(y),
        z_(z),
        cols_(static_cast<size_t>(cols)),
60 61
        normalized_(normalized),
        eps_(eps) {}
W
wanghaox 已提交
62

B
baiyf 已提交
63 64 65 66
  inline HOSTDEVICE void operator()(size_t tid) const {
    size_t row_id = tid / cols_;
    size_t col_id = tid % cols_;

W
wanghaox 已提交
67 68 69 70 71
    T x_min1 = x_[row_id * 4];
    T y_min1 = x_[row_id * 4 + 1];
    T x_max1 = x_[row_id * 4 + 2];
    T y_max1 = x_[row_id * 4 + 3];

B
baiyf 已提交
72 73 74 75 76 77
    T x_min2 = y_[col_id * 4];
    T y_min2 = y_[col_id * 4 + 1];
    T x_max2 = y_[col_id * 4 + 2];
    T y_max2 = y_[col_id * 4 + 3];

    T sim = IOUSimilarity(x_min1, y_min1, x_max1, y_max1, x_min2, y_min2,
78
                          x_max2, y_max2, normalized_, eps_);
W
wanghaox 已提交
79

B
baiyf 已提交
80
    z_[row_id * cols_ + col_id] = sim;
W
wanghaox 已提交
81 82 83 84 85
  }
  const T* x_;
  const T* y_;
  T* z_;
  const size_t cols_;
86
  bool normalized_;
87
  T eps_;
W
wanghaox 已提交
88 89 90 91 92 93 94 95 96
};

namespace paddle {
namespace operators {

template <typename DeviceContext, typename T>
class IOUSimilarityKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
97
    const framework::LoDTensor* in_x = ctx.Input<framework::LoDTensor>("X");
W
wanghaox 已提交
98
    const framework::Tensor* in_y = ctx.Input<framework::Tensor>("Y");
99
    bool normalized = ctx.Attr<bool>("box_normalized");
100
    framework::LoDTensor* out = ctx.Output<framework::LoDTensor>("Out");
W
wanghaox 已提交
101 102 103

    int x_n = in_x->dims()[0];
    int y_n = in_y->dims()[0];
104
    T eps = static_cast<T>(1e-10);
W
wanghaox 已提交
105
    IOUSimilarityFunctor<T> functor(in_x->data<T>(), in_y->data<T>(),
106
                                    out->mutable_data<T>(ctx.GetPlace()), y_n,
107
                                    normalized, eps);
W
wanghaox 已提交
108 109

    platform::ForRange<DeviceContext> for_range(
B
baiyf 已提交
110
        static_cast<const DeviceContext&>(ctx.device_context()), x_n * y_n);
W
wanghaox 已提交
111 112 113 114 115 116
    for_range(functor);
  }
};  // namespace operators

}  // namespace operators
}  // namespace paddle