|
|
|
@ -18,16 +18,28 @@ limitations under the License. */
|
|
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
|
inline HOSTDEVICE T IOUSimilarity(T xmin1, T ymin1, T xmax1, T ymax1, T xmin2,
|
|
|
|
|
T ymin2, T xmax2, T ymax2) {
|
|
|
|
|
T ymin2, T xmax2, T ymax2, bool normalized) {
|
|
|
|
|
constexpr T zero = static_cast<T>(0);
|
|
|
|
|
T area1 = (ymax1 - ymin1) * (xmax1 - xmin1);
|
|
|
|
|
T area2 = (ymax2 - ymin2) * (xmax2 - xmin2);
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
if (!normalized) {
|
|
|
|
|
inter_height = inter_height + 1;
|
|
|
|
|
inter_width = inter_width + 1;
|
|
|
|
|
}
|
|
|
|
|
inter_height = inter_height > zero ? inter_height : zero;
|
|
|
|
|
inter_width = inter_width > zero ? inter_width : zero;
|
|
|
|
|
T inter_area = inter_width * inter_height;
|
|
|
|
@ -38,8 +50,12 @@ inline HOSTDEVICE T IOUSimilarity(T xmin1, T ymin1, T xmax1, T ymax1, T xmin2,
|
|
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
|
struct IOUSimilarityFunctor {
|
|
|
|
|
IOUSimilarityFunctor(const T* x, const T* y, T* z, int cols)
|
|
|
|
|
: x_(x), y_(y), z_(z), cols_(static_cast<size_t>(cols)) {}
|
|
|
|
|
IOUSimilarityFunctor(const T* x, const T* y, T* z, int cols, bool normalized)
|
|
|
|
|
: x_(x),
|
|
|
|
|
y_(y),
|
|
|
|
|
z_(z),
|
|
|
|
|
cols_(static_cast<size_t>(cols)),
|
|
|
|
|
normalized_(normalized) {}
|
|
|
|
|
|
|
|
|
|
inline HOSTDEVICE void operator()(size_t tid) const {
|
|
|
|
|
size_t row_id = tid / cols_;
|
|
|
|
@ -56,7 +72,7 @@ struct IOUSimilarityFunctor {
|
|
|
|
|
T y_max2 = y_[col_id * 4 + 3];
|
|
|
|
|
|
|
|
|
|
T sim = IOUSimilarity(x_min1, y_min1, x_max1, y_max1, x_min2, y_min2,
|
|
|
|
|
x_max2, y_max2);
|
|
|
|
|
x_max2, y_max2, normalized_);
|
|
|
|
|
|
|
|
|
|
z_[row_id * cols_ + col_id] = sim;
|
|
|
|
|
}
|
|
|
|
@ -64,6 +80,7 @@ struct IOUSimilarityFunctor {
|
|
|
|
|
const T* y_;
|
|
|
|
|
T* z_;
|
|
|
|
|
const size_t cols_;
|
|
|
|
|
bool normalized_;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
namespace paddle {
|
|
|
|
@ -75,12 +92,14 @@ class IOUSimilarityKernel : public framework::OpKernel<T> {
|
|
|
|
|
void Compute(const framework::ExecutionContext& ctx) const override {
|
|
|
|
|
const framework::LoDTensor* in_x = ctx.Input<framework::LoDTensor>("X");
|
|
|
|
|
const framework::Tensor* in_y = ctx.Input<framework::Tensor>("Y");
|
|
|
|
|
bool normalized = ctx.Attr<bool>("box_normalized");
|
|
|
|
|
framework::LoDTensor* out = ctx.Output<framework::LoDTensor>("Out");
|
|
|
|
|
|
|
|
|
|
int x_n = in_x->dims()[0];
|
|
|
|
|
int y_n = in_y->dims()[0];
|
|
|
|
|
IOUSimilarityFunctor<T> functor(in_x->data<T>(), in_y->data<T>(),
|
|
|
|
|
out->mutable_data<T>(ctx.GetPlace()), y_n);
|
|
|
|
|
out->mutable_data<T>(ctx.GetPlace()), y_n,
|
|
|
|
|
normalized);
|
|
|
|
|
|
|
|
|
|
platform::ForRange<DeviceContext> for_range(
|
|
|
|
|
static_cast<const DeviceContext&>(ctx.device_context()), x_n * y_n);
|
|
|
|
|