test_iou_similarity_op.py 3.9 KB
Newer Older
1
#   Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
W
wanghaox 已提交
2
#
3 4 5
# 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
W
wanghaox 已提交
6
#
7
#     http://www.apache.org/licenses/LICENSE-2.0
W
wanghaox 已提交
8
#
9 10 11 12 13 14
# 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.

15 16
from __future__ import print_function

W
wanghaox 已提交
17 18
import unittest
import numpy as np
B
baiyf 已提交
19
import numpy.random as random
W
wanghaox 已提交
20 21
import sys
import math
22
from op_test import OpTest
W
wanghaox 已提交
23 24 25 26


class TestIOUSimilarityOp(OpTest):
    def test_check_output(self):
H
hong 已提交
27
        self.check_output(check_dygraph=False)
W
wanghaox 已提交
28 29 30

    def setUp(self):
        self.op_type = "iou_similarity"
B
baiyf 已提交
31 32 33
        self.boxes1 = random.rand(2, 4).astype('float32')
        self.boxes2 = random.rand(3, 4).astype('float32')
        self.output = random.rand(2, 3).astype('float32')
34 35 36 37 38 39 40 41
        self.box_normalized = False
        # run python iou computation 
        self._compute_iou()
        self.inputs = {'X': self.boxes1, 'Y': self.boxes2}
        self.attrs = {"box_normalized": self.box_normalized}
        self.outputs = {'Out': self.output}

    def _compute_iou(self, ):
B
baiyf 已提交
42 43 44 45
        for row in range(self.boxes1.shape[0]):
            for col in range(self.boxes2.shape[0]):
                xmin1, ymin1, xmax1, ymax1 = self.boxes1[row]
                xmin2, ymin2, xmax2, ymax2 = self.boxes2[col]
46 47 48 49 50 51 52
                if not self.box_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)

B
baiyf 已提交
53 54 55 56 57 58
                inter_xmax = min(xmax1, xmax2)
                inter_ymax = min(ymax1, ymax2)
                inter_xmin = max(xmin1, xmin2)
                inter_ymin = max(ymin1, ymin2)
                inter_height = inter_ymax - inter_ymin
                inter_width = inter_xmax - inter_xmin
59 60 61
                if not self.box_normalized:
                    inter_height += 1
                    inter_width += 1
B
baiyf 已提交
62 63 64 65 66 67
                inter_height = max(inter_height, 0)
                inter_width = max(inter_width, 0)
                inter_area = inter_width * inter_height
                union_area = area1 + area2 - inter_area
                sim_score = inter_area / union_area
                self.output[row, col] = sim_score
W
wanghaox 已提交
68 69


70 71
class TestIOUSimilarityOpWithLoD(TestIOUSimilarityOp):
    def test_check_output(self):
H
hong 已提交
72
        self.check_output(check_dygraph=False)
73 74 75

    def setUp(self):
        super(TestIOUSimilarityOpWithLoD, self).setUp()
76 77
        self.boxes1_lod = [[1, 1]]
        self.output_lod = [[1, 1]]
78 79 80 81 82 83 84 85 86 87 88
        self.box_normalized = False
        # run python iou computation 
        self._compute_iou()
        self.inputs = {'X': (self.boxes1, self.boxes1_lod), 'Y': self.boxes2}
        self.attrs = {"box_normalized": self.box_normalized}
        self.outputs = {'Out': (self.output, self.output_lod)}


class TestIOUSimilarityOpWithBoxNormalized(TestIOUSimilarityOp):
    def test_check_output(self):
        self.check_output(check_dygraph=False)
89

90 91 92 93 94 95 96
    def setUp(self):
        super(TestIOUSimilarityOpWithBoxNormalized, self).setUp()
        self.boxes1_lod = [[1, 1]]
        self.output_lod = [[1, 1]]
        self.box_normalized = True
        # run python iou computation 
        self._compute_iou()
97
        self.inputs = {'X': (self.boxes1, self.boxes1_lod), 'Y': self.boxes2}
98
        self.attrs = {"box_normalized": self.box_normalized}
99 100 101
        self.outputs = {'Out': (self.output, self.output_lod)}


W
wanghaox 已提交
102 103
if __name__ == '__main__':
    unittest.main()