-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathtest_metrics.py
220 lines (169 loc) · 4.38 KB
/
test_metrics.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
import pytest
import numpy as np
import segmentation_models as sm
from segmentation_models.metrics import IOUScore, FScore
from segmentation_models.losses import JaccardLoss, DiceLoss
if sm.framework() == sm._TF_KERAS_FRAMEWORK_NAME:
from tensorflow import keras
elif sm.framework() == sm._KERAS_FRAMEWORK_NAME:
import keras
else:
raise ValueError('Incorrect framework {}'.format(sm.framework()))
METRICS = [
IOUScore,
FScore,
]
LOSSES = [
JaccardLoss,
DiceLoss,
]
GT0 = np.array(
[
[0, 0, 0],
[0, 0, 0],
[0, 0, 0],
],
dtype='float32',
)
GT1 = np.array(
[
[1, 1, 0],
[1, 1, 0],
[0, 0, 0],
],
dtype='float32',
)
PR1 = np.array(
[
[0, 0, 0],
[1, 1, 0],
[0, 0, 0],
],
dtype='float32',
)
PR2 = np.array(
[
[0, 0, 0],
[1, 1, 0],
[1, 1, 0],
],
dtype='float32',
)
PR3 = np.array(
[
[0, 0, 0],
[0, 0, 0],
[1, 0, 0],
],
dtype='float32',
)
IOU_CASES = (
(GT0, GT0, 1.00),
(GT1, GT1, 1.00),
(GT0, PR1, 0.00),
(GT0, PR2, 0.00),
(GT0, PR3, 0.00),
(GT1, PR1, 0.50),
(GT1, PR2, 1. / 3.),
(GT1, PR3, 0.00),
)
F1_CASES = (
(GT0, GT0, 1.00),
(GT1, GT1, 1.00),
(GT0, PR1, 0.00),
(GT0, PR2, 0.00),
(GT0, PR3, 0.00),
(GT1, PR1, 2. / 3.),
(GT1, PR2, 0.50),
(GT1, PR3, 0.00),
)
F2_CASES = (
(GT0, GT0, 1.00),
(GT1, GT1, 1.00),
(GT0, PR1, 0.00),
(GT0, PR2, 0.00),
(GT0, PR3, 0.00),
(GT1, PR1, 5. / 9.),
(GT1, PR2, 0.50),
(GT1, PR3, 0.00),
)
def _to_4d(x):
if x.ndim == 2:
return x[None, :, :, None]
elif x.ndim == 3:
return x[None, :, :]
def _add_4d(x):
if x.ndim == 3:
return x[..., None]
@pytest.mark.parametrize('case', IOU_CASES)
def test_iou_metric(case):
gt, pr, res = case
gt = _to_4d(gt)
pr = _to_4d(pr)
iou_score = IOUScore(smooth=10e-12)
score = keras.backend.eval(iou_score(gt, pr))
assert np.allclose(score, res)
@pytest.mark.parametrize('case', IOU_CASES)
def test_jaccard_loss(case):
gt, pr, res = case
gt = _to_4d(gt)
pr = _to_4d(pr)
jaccard_loss = JaccardLoss(smooth=10e-12)
score = keras.backend.eval(jaccard_loss(gt, pr))
assert np.allclose(score, 1 - res)
def _test_f_metric(case, beta=1):
gt, pr, res = case
gt = _to_4d(gt)
pr = _to_4d(pr)
f_score = FScore(beta=beta, smooth=10e-12)
score = keras.backend.eval(f_score(gt, pr))
assert np.allclose(score, res)
@pytest.mark.parametrize('case', F1_CASES)
def test_f1_metric(case):
_test_f_metric(case, beta=1)
@pytest.mark.parametrize('case', F2_CASES)
def test_f2_metric(case):
_test_f_metric(case, beta=2)
@pytest.mark.parametrize('case', F1_CASES)
def test_dice_loss(case):
gt, pr, res = case
gt = _to_4d(gt)
pr = _to_4d(pr)
dice_loss = DiceLoss(smooth=10e-12)
score = keras.backend.eval(dice_loss(gt, pr))
assert np.allclose(score, 1 - res)
@pytest.mark.parametrize('func', METRICS + LOSSES)
def test_per_image(func):
gt = np.stack([GT0, GT1], axis=0)
pr = np.stack([PR1, PR2], axis=0)
gt = _add_4d(gt)
pr = _add_4d(pr)
# calculate score per image
score_1 = keras.backend.eval(func(per_image=True, smooth=10e-12)(gt, pr))
score_2 = np.mean([
keras.backend.eval(func(smooth=10e-12)(_to_4d(GT0), _to_4d(PR1))),
keras.backend.eval(func(smooth=10e-12)(_to_4d(GT1), _to_4d(PR2))),
])
assert np.allclose(score_1, score_2)
@pytest.mark.parametrize('func', METRICS + LOSSES)
def test_per_batch(func):
gt = np.stack([GT0, GT1], axis=0)
pr = np.stack([PR1, PR2], axis=0)
gt = _add_4d(gt)
pr = _add_4d(pr)
# calculate score per batch
score_1 = keras.backend.eval(func(per_image=False, smooth=10e-12)(gt, pr))
gt1 = np.concatenate([GT0, GT1], axis=0)
pr1 = np.concatenate([PR1, PR2], axis=0)
score_2 = keras.backend.eval(func(per_image=True, smooth=10e-12)(_to_4d(gt1), _to_4d(pr1)))
assert np.allclose(score_1, score_2)
@pytest.mark.parametrize('case', IOU_CASES)
def test_threshold_iou(case):
gt, pr, res = case
gt = _to_4d(gt)
pr = _to_4d(pr) * 0.51
iou_score = IOUScore(smooth=10e-12, threshold=0.5)
score = keras.backend.eval(iou_score(gt, pr))
assert np.allclose(score, res)
if __name__ == '__main__':
pytest.main([__file__])