-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcombine.test.py
More file actions
216 lines (157 loc) · 6.69 KB
/
Copy pathcombine.test.py
File metadata and controls
216 lines (157 loc) · 6.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import unittest
from clustering import combine
import numpy as np
import cytools
def create_sample_bbox(bbox=[]):
# Generate score values
score = np.zeros(81) # Score + numebr of teachers inferences param
score[-1] = 1 # At least one teacher have made inference
category = 7 # Random hardcoded category
category_value = np.random.random_sample()
score[category] = category_value
# Generate bbox values
if len(bbox) == 0:
bbox = np.random.randint(1, 100, size=2)
bbox = np.concatenate((bbox, bbox*2), axis=None)
return np.concatenate((bbox, score), axis=None)
# Create n_of_bboxes with overlap smaller relative to bboxes than thr
def create_bbox_no_overlaps(bboxes, n_of_bboxes, thr=.5):
result_correct = []
while not len(result_correct):
bboxes_test = np.array([])
# Generate bbox values
for i in range(n_of_bboxes):
bbox = np.random.randint(1, 100, size=2)
bbox = np.concatenate((bbox, bbox*2), axis=None)
bbox = bbox.astype(float)
try:
bboxes_test = np.vstack((bboxes_test, bbox))
except:
bboxes_test = np.array(bbox)
ious = cytools.bbox_overlaps(bboxes_test[:, :4], bboxes[:, :4])
# [
# [0.4, 0.0, 0.05, 0.1, 0.9],
# [0.9, 0.23, 0.0, 0.1, 0.1],
# [0.6, 0.1, 0.2, 0.1, 0.1],
# ]
max_ious = ious.max(1) # [0.9, 0.9, 0.6]
oks = max_ious > thr
# [True, True, False]
max_iousB = ious.max(0) # [0.9, 0.23, 0.2, 0.1, 0.9]
oksB = max_iousB > thr
# [True, False, False, False, True]
if np.all(oks == False) and np.all(oksB == False):
result_correct = bboxes_test
return result_correct
class Test(unittest.TestCase):
'''
Combine methods tests
'''
def test_all_overlaps(self):
'''
Test with all bboxesA overlapping (with IoU greater than Threshold) bboxesB
'''
# We create two lists of random almost equal bboxes
bboxesA = np.array([
create_sample_bbox(),
create_sample_bbox()
])
bboxesB = bboxesA
def make_assert(bboxesA, bboxesB):
expected_result = (bboxesA + bboxesB)/2
expected_result[:, -1] = bboxesA[:, -1] + bboxesB[:, -1]
result = combine(bboxesA, bboxesB)
self.assertTrue(np.all(result == expected_result))
return result
result = make_assert(bboxesA, bboxesB)
# Also test with aditional teacher with same inferences
result2 = make_assert(result, result)
def test_all_a_overlaps_but_b(self):
'''
Test with all bboxesA overlapping (with IoU greater than Threshold) bboxesB, but with some
bboxes in B not present in A
'''
# We create two lists of random almost equal bboxes
bboxesA = np.array([
create_sample_bbox(),
create_sample_bbox()
])
bboxesB = np.array([
*bboxesA,
*[create_sample_bbox(x) for x in create_bbox_no_overlaps(bboxesA, 4)]
])
def make_assert(bboxesA, bboxesB):
# In this test we assume that all bboxes A are in bboxes B,
# and only B have different bboxes
n_equal_bboxes = len(bboxesA)
expected_result = (bboxesA + bboxesB[:n_equal_bboxes])/2
# Bbboxes with multiple teachers prediction
expected_result[:, -1] = (bboxesA[:, -1] +
bboxesB[:n_equal_bboxes, -1])
expected_result = np.vstack(
(expected_result, bboxesB[n_equal_bboxes:]))
# Bboxes only predicted by teacher B
expected_result[n_equal_bboxes:, -1] = 1
result = combine(bboxesA, bboxesB)
self.assertTrue(np.all(result == expected_result))
return result
result = make_assert(bboxesA, bboxesB)
bboxesC = np.array([*result, *[create_sample_bbox(x)
for x in create_bbox_no_overlaps(result, 2)]])
# Also test with aditional teacher with same inferences
result2 = make_assert(result, bboxesC)
def test_all_B_overlaps_but_a(self):
'''
Test with all bboxesB overlapping (with IoU greater than Threshold) bboxesA, but with some
bboxes in A not present in B
'''
# We create two lists of random almost equal bboxes
bboxesB = np.array([
create_sample_bbox(),
create_sample_bbox()
])
bboxesA = np.array([
*bboxesB,
*[create_sample_bbox(x) for x in create_bbox_no_overlaps(bboxesB, 2)]
])
def make_assert(bboxesA, bboxesB):
# In this test we assume that all bboxes A are in bboxes B,
# and only B have different bboxes
n_equal_bboxes = len(bboxesB)
expected_result = (bboxesB + bboxesA[:n_equal_bboxes])/2
# Bbboxes with multiple teachers prediction
expected_result[:, -1] = (bboxesB[:, -1] +
bboxesA[:n_equal_bboxes, -1])
expected_result = np.vstack(
(expected_result, bboxesA[n_equal_bboxes:]))
# Bboxes only predicted by teacher B
expected_result[n_equal_bboxes:, -1] = 1
result = combine(bboxesA, bboxesB)
self.assertTrue(np.all(result == expected_result))
return result
result = make_assert(bboxesA, bboxesB)
bboxesC = np.array([*result, *[create_sample_bbox(x)
for x in create_bbox_no_overlaps(result, 3)]])
# Also test with aditional teacher with same inferences
result2 = make_assert(bboxesC, result)
def test_no_overlaps(self):
'''
Test with no overlaps between A and B
'''
# We create two lists of random almost equal bboxes
bboxesA = np.array([
create_sample_bbox(),
create_sample_bbox()
])
bboxesB = np.array([create_sample_bbox(x) for x in create_bbox_no_overlaps(
bboxesA, 5)]) # 5 is a random number
expected_result = np.vstack((bboxesA, bboxesB))
result = combine(bboxesA, bboxesB)
self.assertTrue(np.all(result == expected_result))
bboxesC = np.array([create_sample_bbox(x)
for x in create_bbox_no_overlaps(result, 3)])
expected_result_add = np.vstack((result, bboxesC))
result_add = combine(result, bboxesC)
self.assertTrue(np.all(result_add == expected_result_add))
if __name__ == '__main__':
unittest.main()