-
Notifications
You must be signed in to change notification settings - Fork 8
/
all_iou_mask.py
336 lines (270 loc) · 11.3 KB
/
all_iou_mask.py
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
import numpy as np
def calculate_mask_iou(mask1, mask2):
intersection = np.logical_and(mask1, mask2).sum()
union = np.logical_or(mask1, mask2).sum()
if union == 0:
return 0.0
iou = intersection / union
return iou
def calculate_mask_giou(mask1, mask2):
intersection = np.logical_and(mask1, mask2).sum()
union = np.logical_or(mask1, mask2).sum()
if union == 0:
return 0.0
# Check if either mask has no non-zero elements
mask1_nonzero = mask1.nonzero()
mask2_nonzero = mask2.nonzero()
if mask1_nonzero[0].size == 0 or mask2_nonzero[0].size == 0:
return 0.0
enclose_x_min = min(mask1_nonzero[0].min(), mask2_nonzero[0].min())
enclose_x_max = max(mask1_nonzero[0].max(), mask2_nonzero[0].max())
enclose_y_min = min(mask1_nonzero[1].min(), mask2_nonzero[1].min())
enclose_y_max = max(mask1_nonzero[1].max(), mask2_nonzero[1].max())
enclose_area = (enclose_x_max - enclose_x_min + 1) * (
enclose_y_max - enclose_y_min + 1
)
giou = (intersection / union) - ((enclose_area - union) / enclose_area)
return giou
def calculate_mask_diou(mask1, mask2):
intersection = np.logical_and(mask1, mask2).sum()
union = np.logical_or(mask1, mask2).sum()
if union == 0:
return 0.0
# Check if either mask has no non-zero elements
mask1_nonzero = mask1.nonzero()
mask2_nonzero = mask2.nonzero()
if mask1_nonzero[0].size == 0 or mask2_nonzero[0].size == 0:
return 0.0
mask1_center = np.mean(np.argwhere(mask1 == 1), axis=0)
mask2_center = np.mean(np.argwhere(mask2 == 1), axis=0)
distance = np.linalg.norm(mask1_center - mask2_center)
enclose_x_min = min(mask1_nonzero[0].min(), mask2_nonzero[0].min())
enclose_x_max = max(mask1_nonzero[0].max(), mask2_nonzero[0].max())
enclose_y_min = min(mask1_nonzero[1].min(), mask2_nonzero[1].min())
enclose_y_max = max(mask1_nonzero[1].max(), mask2_nonzero[1].max())
c_diag = np.linalg.norm(
np.array([enclose_x_max, enclose_y_max])
- np.array([enclose_x_min, enclose_y_min])
)
diou = (intersection / union) - (distance**2 / c_diag**2)
return diou
def calculate_mask_ciou(mask1, mask2):
intersection = np.logical_and(mask1, mask2).sum()
union = np.logical_or(mask1, mask2).sum()
if union == 0:
return 0.0
# Check if either mask has no non-zero elements
mask1_nonzero = mask1.nonzero()
mask2_nonzero = mask2.nonzero()
if mask1_nonzero[0].size == 0 or mask2_nonzero[0].size == 0:
return 0.0
mask1_center = np.mean(np.argwhere(mask1 == 1), axis=0)
mask2_center = np.mean(np.argwhere(mask2 == 1), axis=0)
distance = np.linalg.norm(mask1_center - mask2_center)
enclose_x_min = min(mask1.nonzero()[0].min(), mask2.nonzero()[0].min())
enclose_x_max = max(mask1.nonzero()[0].max(), mask2.nonzero()[0].max())
enclose_y_min = min(mask1.nonzero()[1].min(), mask2.nonzero()[1].min())
enclose_y_max = max(mask1.nonzero()[1].max(), mask2.nonzero()[1].max())
c_diag = np.linalg.norm(
np.array([enclose_x_max, enclose_y_max])
- np.array([enclose_x_min, enclose_y_min])
)
mask1_shape = mask1.shape[0] / mask1.shape[1]
mask2_shape = mask2.shape[0] / mask2.shape[1]
v = (4 / np.pi**2) * (np.arctan(mask1_shape) - np.arctan(mask2_shape)) ** 2
denominator = 1 - intersection / union + v
if denominator != 0:
alpha = v / denominator
else:
alpha = 0
ciou = (intersection / union) - (distance**2 / c_diag**2 + alpha * v)
return ciou
def calculate_mask_eiou(mask1, mask2):
intersection = np.logical_and(mask1, mask2).sum()
union = np.logical_or(mask1, mask2).sum()
if union == 0:
return 0.0
# Check if either mask has no non-zero elements
mask1_nonzero = mask1.nonzero()
mask2_nonzero = mask2.nonzero()
if mask1_nonzero[0].size == 0 or mask2_nonzero[0].size == 0:
return 0.0
mask1_center = np.mean(np.argwhere(mask1 == 1), axis=0)
mask2_center = np.mean(np.argwhere(mask2 == 1), axis=0)
distance = np.linalg.norm(mask1_center - mask2_center)
enclose_x_min = min(mask1.nonzero()[0].min(), mask2.nonzero()[0].min())
enclose_x_max = max(mask1.nonzero()[0].max(), mask2.nonzero()[0].max())
enclose_y_min = min(mask1.nonzero()[1].min(), mask2.nonzero()[1].min())
enclose_y_max = max(mask1.nonzero()[1].max(), mask2.nonzero()[1].max())
c_diag = np.linalg.norm(
np.array([enclose_x_max, enclose_y_max])
- np.array([enclose_x_min, enclose_y_min])
)
eiou = (intersection / union) - (distance**2 / c_diag**2)
return eiou
def calculate_focal_mask_eiou(mask1, mask2, gamma=2.0):
# Calculate intersection and union
intersection = np.logical_and(mask1, mask2).sum()
union = np.logical_or(mask1, mask2).sum()
if union == 0:
return 0.0
# Determine the non-zero elements
mask1_nonzero = np.argwhere(mask1 == 1)
mask2_nonzero = np.argwhere(mask2 == 1)
# Handle cases where the mask is entirely zeros
if mask1_nonzero.size == 0 or mask2_nonzero.size == 0:
return 0.0
# Calculate the center only if there are non-zero elements
mask1_center = np.mean(mask1_nonzero, axis=0)
mask2_center = np.mean(mask2_nonzero, axis=0)
distance = np.linalg.norm(mask1_center - mask2_center)
# Determine the enclosing box coordinates
enclose_x_min = min(mask1_nonzero[:, 0].min(), mask2_nonzero[:, 0].min())
enclose_x_max = max(mask1_nonzero[:, 0].max(), mask2_nonzero[:, 0].max())
enclose_y_min = min(mask1_nonzero[:, 1].min(), mask2_nonzero[:, 1].min())
enclose_y_max = max(mask1_nonzero[:, 1].max(), mask2_nonzero[:, 1].max())
c_diag = np.linalg.norm(
np.array([enclose_x_max, enclose_y_max])
- np.array([enclose_x_min, enclose_y_min])
)
# Ensure c_diag is non-zero to avoid division errors
if c_diag == 0:
eiou = 0.0 # Change to 0.0 for consistency
else:
eiou = (intersection / union) - (distance**2 / c_diag**2)
# Calculate Focal EIoU
# Modify Focal EIoU formula to handle negative values
if eiou < 0:
focal_eiou = eiou * (1 + gamma)
else:
focal_eiou = eiou * (1 - (1 - eiou) ** gamma)
return focal_eiou
def calculate_mask_siou(mask1, mask2):
intersection = np.logical_and(mask1, mask2).sum()
union = np.logical_or(mask1, mask2).sum()
if union == 0:
return 0.0
# Check if either mask has no non-zero elements
mask1_nonzero = mask1.nonzero()
mask2_nonzero = mask2.nonzero()
if mask1_nonzero[0].size == 0 or mask2_nonzero[0].size == 0:
return 0.0
mask1_shape = mask1.shape[0] / mask1.shape[1]
mask2_shape = mask2.shape[0] / mask2.shape[1]
v = (4 / np.pi**2) * (np.arctan(mask1_shape) - np.arctan(mask2_shape)) ** 2
siou = (intersection / union) - v
return siou
def calculate_mask_alpha_iou(mask1, mask2, alpha=0.5):
intersection = np.logical_and(mask1, mask2).sum()
union = np.logical_or(mask1, mask2).sum()
if union == 0:
return 0.0
# Check if either mask has no non-zero elements
mask1_nonzero = mask1.nonzero()
mask2_nonzero = mask2.nonzero()
if mask1_nonzero[0].size == 0 or mask2_nonzero[0].size == 0:
return 0.0
iou = intersection / union
alpha_iou = iou**alpha
return alpha_iou
def calculate_mask_wiou(mask1, mask2, weight=1):
intersection = np.logical_and(mask1, mask2).sum()
union = np.logical_or(mask1, mask2).sum()
if union == 0:
return 0.0
# Check if either mask has no non-zero elements
mask1_nonzero = mask1.nonzero()
mask2_nonzero = mask2.nonzero()
if mask1_nonzero[0].size == 0 or mask2_nonzero[0].size == 0:
return 0.0
iou = intersection / union
wiou = iou * weight
return wiou
def calculate_mask_mpdiou(mask1, mask2):
intersection = np.logical_and(mask1, mask2).sum()
union = np.logical_or(mask1, mask2).sum()
if union == 0:
return 0.0
# Check if either mask has no non-zero elements
mask1_nonzero = mask1.nonzero()
mask2_nonzero = mask2.nonzero()
if mask1_nonzero[0].size == 0 or mask2_nonzero[0].size == 0:
return 0.0
mask1_center = np.mean(np.argwhere(mask1 == 1), axis=0)
mask2_center = np.mean(np.argwhere(mask2 == 1), axis=0)
distance = np.linalg.norm(mask1_center - mask2_center)
enclose_x_min = min(mask1.nonzero()[0].min(), mask2.nonzero()[0].min())
enclose_x_max = max(mask1.nonzero()[0].max(), mask2.nonzero()[0].max())
enclose_y_min = min(mask1.nonzero()[1].min(), mask2.nonzero()[1].min())
enclose_y_max = max(mask1.nonzero()[1].max(), mask2.nonzero()[1].max())
c_diag = np.linalg.norm(
np.array([enclose_x_max, enclose_y_max])
- np.array([enclose_x_min, enclose_y_min])
)
mpdiou = (intersection / union) - (distance**2 / c_diag**2) - min(distance, c_diag)
return mpdiou
# Test cases
test_cases = [
(
"完全重疊 (Complete Overlap)",
np.array([[1, 1, 0], [1, 1, 0], [0, 0, 0]]),
np.array([[1, 1, 0], [1, 1, 0], [0, 0, 0]]),
),
(
"部分重疊 (Partial Overlap)",
np.array([[1, 1, 0], [1, 1, 0], [0, 0, 0]]),
np.array([[0, 1, 1], [1, 0, 0], [0, 0, 1]]),
),
(
"不重疊 (No Overlap)",
np.array([[1, 1, 0], [1, 1, 0], [0, 0, 0]]),
np.array([[0, 0, 1], [0, 0, 1], [1, 1, 0]]),
),
(
"邊界接觸 (Touching at Edges)",
np.array([[1, 1, 0], [1, 1, 0], [0, 0, 0]]),
np.array([[0, 0, 0], [0, 1, 1], [0, 1, 1]]),
),
(
"小遮罩在大遮罩內 (Small Mask Inside Large Mask)",
np.array([[0, 0, 0], [0, 1, 0], [0, 0, 0]]),
np.array([[1, 1, 1], [1, 1, 1], [1, 1, 1]]),
),
(
"交錯重疊 (Interleaved Overlap)",
np.array([[1, 0, 1], [0, 1, 0], [1, 0, 1]]),
np.array([[0, 1, 0], [1, 0, 1], [0, 1, 0]]),
),
(
"不同形狀 (Different Shapes)",
np.array([[1, 1, 0], [1, 1, 0], [0, 0, 0]]),
np.array([[1, 0, 0], [1, 0, 0], [1, 1, 1]]),
),
(
"相似形狀但位置偏移 (Similar Shapes but Offset)",
np.array([[0, 1, 1], [0, 1, 1], [0, 0, 0]]),
np.array([[1, 1, 0], [1, 1, 0], [0, 0, 0]]),
),
(
"大面積交疊 (Large Area Overlap)",
np.array([[1, 1, 1], [1, 1, 1], [0, 0, 0]]),
np.array([[1, 1, 0], [1, 1, 1], [1, 0, 0]]),
),
(
"一個遮罩全為零 (One Mask All Zero)",
np.array([[0, 0, 0], [0, 0, 0], [0, 0, 0]]),
np.array([[1, 1, 1], [1, 0, 0], [0, 0, 1]]),
),
]
for description, m1, m2 in test_cases:
print(f"{description} - Mask IoU:", calculate_mask_iou(m1, m2))
print(f"{description} - Mask GIoU:", calculate_mask_giou(m1, m2))
print(f"{description} - Mask DIoU:", calculate_mask_diou(m1, m2))
print(f"{description} - Mask CIoU:", calculate_mask_ciou(m1, m2))
print(f"{description} - Mask EIoU:", calculate_mask_eiou(m1, m2))
print(f"{description} - Mask Focal EIoU:", calculate_focal_mask_eiou(m1, m2))
print(f"{description} - Mask SIoU:", calculate_mask_siou(m1, m2))
print(f"{description} - Mask Alpha-IoU:", calculate_mask_alpha_iou(m1, m2))
print(f"{description} - Mask WIoU:", calculate_mask_wiou(m1, m2))
print(f"{description} - Mask MPDIoU:", calculate_mask_mpdiou(m1, m2))
print()