Skip to content

Commit 528bcac

Browse files
committed
update iou_sim code
1 parent 3b63815 commit 528bcac

File tree

4 files changed

+70
-33
lines changed

4 files changed

+70
-33
lines changed

paddle/operators/iou_similarity_op.cc

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,16 @@ class IOUSimilarityOp : public framework::OperatorWithKernel {
2323

2424
protected:
2525
void InferShape(framework::InferShapeContext *ctx) const override {
26+
PADDLE_ENFORCE(ctx->HasInput("X"),
27+
"Input(X) of IOUSimilarityOp should not be null.");
28+
PADDLE_ENFORCE(ctx->HasInput("Y"),
29+
"Input(Y) of IOUSimilarityOp should not be null.");
2630
auto x_dims = ctx->GetInputDim("X");
2731
auto y_dims = ctx->GetInputDim("Y");
2832

29-
PADDLE_ENFORCE_EQ(x_dims.size(), 2UL, "The shape of X is [N, 4]");
33+
PADDLE_ENFORCE_EQ(x_dims.size(), 2UL, "The rank of Input(X) must be 2.");
3034
PADDLE_ENFORCE_EQ(x_dims[1], 4UL, "The shape of X is [N, 4]");
31-
PADDLE_ENFORCE_EQ(y_dims.size(), 2UL, "The shape of Y is [M, 4]");
35+
PADDLE_ENFORCE_EQ(y_dims.size(), 2UL, "The rank of Input(Y) must be 2.");
3236
PADDLE_ENFORCE_EQ(y_dims[1], 4UL, "The shape of Y is [M, 4]");
3337

3438
ctx->SetOutputDim("Out", framework::make_ddim({x_dims[0], y_dims[0]}));
@@ -39,16 +43,18 @@ class IOUSimilarityOpMaker : public framework::OpProtoAndCheckerMaker {
3943
public:
4044
IOUSimilarityOpMaker(OpProto *proto, OpAttrChecker *op_checker)
4145
: OpProtoAndCheckerMaker(proto, op_checker) {
42-
AddInput(
43-
"X",
44-
"(Tensor, default Tensor<float>) "
45-
"BoxList X holding N boxes, each box is "
46-
"represented as [xmin, ymin, xmax, ymax], the shape of X is [N, 4].");
47-
AddInput(
48-
"Y",
49-
"(Tensor, default Tensor<float>) "
50-
"BoxList Y holding M boxes, each box is "
51-
"represented as [xmin, ymin, xmax, ymax], the shape of X is [N, 4].");
46+
AddInput("X",
47+
"(Tensor, default Tensor<float>) "
48+
"Box list X holds N boxes, each box is "
49+
"represented as [xmin, ymin, xmax, ymax], the shape of X is [N, "
50+
"4]. [xmin, ymin] is the lower left coordinate of the box, and "
51+
"[xmax, ymax] is the right upper coordinate of the box.");
52+
AddInput("Y",
53+
"(Tensor, default Tensor<float>) "
54+
"Box list Y holds M boxes, each box is "
55+
"represented as [xmin, ymin, xmax, ymax], the shape of X is [N, "
56+
"4]. [xmin, ymin] is the lower left coordinate of the box, and "
57+
"[xmax, ymax] is the right upper coordinate of the box.");
5258

5359
AddOutput(
5460
"Out",
@@ -57,7 +63,7 @@ class IOUSimilarityOpMaker : public framework::OpProtoAndCheckerMaker {
5763

5864
AddComment(R"DOC(
5965
IOU Similarity Operator.
60-
Computes pairwise intersection-over-union between box collections.
66+
Computes intersection-over-union (IOU) between two box lists.
6167
)DOC");
6268
}
6369
};

paddle/operators/iou_similarity_op.cu

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License. */
14+
15+
#define EIGEN_USE_GPU
16+
#include "paddle/operators/iou_similarity_op.h"
17+
18+
namespace ops = paddle::operators;
19+
REGISTER_OP_CUDA_KERNEL(
20+
iou_similarity,
21+
ops::IOUSimilarityKernel<paddle::platform::CUDADeviceContext, float>);

paddle/operators/iou_similarity_op.h

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,19 @@ limitations under the License. */
1717
#include "paddle/platform/for_range.h"
1818

1919
template <typename T>
20-
inline T IOUSimilarity(T xmin1, T ymin1, T xmax1, T ymax1, T xmin2, T ymin2,
21-
T xmax2, T ymax2) {
20+
inline HOSTDEVICE T IOUSimilarity(T xmin1, T ymin1, T xmax1, T ymax1, T xmin2,
21+
T ymin2, T xmax2, T ymax2) {
22+
constexpr T zero = static_cast<T>(0);
2223
T area1 = (ymax1 - ymin1) * (xmax1 - xmin1);
2324
T area2 = (ymax2 - ymin2) * (xmax2 - xmin2);
24-
T inter_xmax = std::min(xmax1, xmax2);
25-
T inter_ymax = std::min(ymax1, ymax2);
26-
T inter_xmin = std::max(xmin1, xmin2);
27-
T inter_ymin = std::max(ymin1, ymin2);
28-
T inter_height = std::max(inter_ymax - inter_ymin, static_cast<T>(0));
29-
T inter_width = std::max(inter_xmax - inter_xmin, static_cast<T>(0));
25+
T inter_xmax = xmax1 > xmax2 ? xmax2 : xmax1;
26+
T inter_ymax = ymax1 > ymax2 ? ymax2 : ymax1;
27+
T inter_xmin = xmin1 > xmin2 ? xmin1 : xmin2;
28+
T inter_ymin = ymin1 > ymin2 ? ymin1 : ymin2;
29+
T inter_height = inter_ymax - inter_ymin;
30+
T inter_width = inter_xmax - inter_xmin;
31+
inter_height = inter_height > zero ? inter_height : zero;
32+
inter_width = inter_width > zero ? inter_width : zero;
3033
T inter_area = inter_width * inter_height;
3134
T union_area = area1 + area2 - inter_area;
3235
T sim_score = inter_area / union_area;

python/paddle/v2/fluid/tests/test_iou_similarity_op.py

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserve.
2+
#
3+
#Licensed under the Apache License, Version 2.0 (the "License");
4+
#you may not use this file except in compliance with the License.
5+
#You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
#Unless required by applicable law or agreed to in writing, software
10+
#distributed under the License is distributed on an "AS IS" BASIS,
11+
#WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
#See the License for the specific language governing permissions and
13+
#limitations under the License.
114
import unittest
215
import numpy as np
316
import sys
@@ -6,30 +19,24 @@
619

720

821
class TestIOUSimilarityOp(OpTest):
9-
def set_data(self):
10-
self.init_test_data()
11-
self.inputs = {'X': self.boxes1, 'Y': self.boxes2}
12-
13-
self.outputs = {'Out': self.output}
14-
1522
def test_check_output(self):
1623
self.check_output()
1724

18-
def test_check_grad(self):
19-
return
20-
2125
def setUp(self):
2226
self.op_type = "iou_similarity"
23-
self.set_data()
24-
25-
def init_test_data(self):
2627
self.boxes1 = np.array(
2728
[[4.0, 3.0, 7.0, 5.0], [5.0, 6.0, 10.0, 7.0]]).astype('float32')
2829
self.boxes2 = np.array([[3.0, 4.0, 6.0, 8.0], [14.0, 14.0, 15.0, 15.0],
2930
[0.0, 0.0, 20.0, 20.0]]).astype('float32')
3031
self.output = np.array(
3132
[[2.0 / 16.0, 0, 6.0 / 400.0],
3233
[1.0 / 16.0, 0.0, 5.0 / 400.0]]).astype('float32')
34+
# self.output = np.array([[0, 0, 0],
35+
# [0, 0, 0]]).astype('float32')
36+
37+
self.inputs = {'X': self.boxes1, 'Y': self.boxes2}
38+
39+
self.outputs = {'Out': self.output}
3340

3441

3542
if __name__ == '__main__':

0 commit comments

Comments
 (0)