Skip to content

Commit 663de49

Browse files
Carole SudreCarole Sudre
authored andcommitted
Updating NLL and reference to cheat sheet
1 parent aa56a00 commit 663de49

3 files changed

Lines changed: 126 additions & 16 deletions

File tree

MetricsReloaded/metrics/calibration_measures.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -460,18 +460,22 @@ def negative_log_likelihood(self):
460460
461461
George Cybenko, Dianne P O’Leary, and Jorma Rissanen. 1998. The Mathematics of Information Coding, Extraction
462462
and Distribution. Vol. 107. Springer Science & Business Media.
463+
Cheat Sheet p 116 - Figure SN 3.71
463464
464465
.. math::
465466
466-
NLL = -\sum_{i=1}{N} log(p_{i,k} | y_i=k)
467+
NLL = -\dfrac{1}{N}\sum_{i=1}^{N}\sum_{k=1}^{C} y_{ik} \dot log(p_{i,k})
468+
469+
where :math: `y_{ik}` the outcome is 1 if the class of :math: `y_{i}` is k and :math: `p_{ik}` is the predicted
470+
probability for sample :math: `x_i` and class k
467471
468472
:return: NLL
469473
470474
"""
471475
log_pred = np.log(self.pred)
472476
numb_samples = self.pred.shape[0]
473477
ll = np.sum(log_pred[range(numb_samples), self.ref])
474-
nll = -1 * ll
478+
nll = -1/numb_samples * ll
475479
return nll
476480

477481
def to_dict_meas(self, fmt="{:.4f}"):

test/test_metrics/test_calibration_metrics.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ def test_expected_calibration_error():
3535
assert_allclose(value_test3, expected_ece3, atol=0.01)
3636

3737
def test_maximum_calibration_error():
38+
"""
39+
Using figure 2.24 p67 of pitfalls as reference
40+
"""
3841
ppm1 = CalibrationMeasures(pred_224, ref_224, dict_args={"bins_mce": 2})
3942
ppm2 = CalibrationMeasures(pred_224, ref_224, dict_args={'bins_mce':5})
4043
ppm3 = CalibrationMeasures(pred_224, ref_224)
@@ -88,7 +91,7 @@ def test_negative_log_likelihood():
8891
pred_nll = [[0.1, 0.8, 0.05, 0.1], [0.6, 0.1, 0, 0.7], [0.3, 0.1, 0.95, 0.2]]
8992
ref_nll = np.asarray(ref_nll)
9093
pred_nll = np.asarray(pred_nll).T
91-
expected_nll = -1 * (np.log(0.8) + np.log(0.6) + np.log(0.7) + np.log(0.95))
94+
expected_nll = -1/4 * (np.log(0.8) + np.log(0.6) + np.log(0.7) + np.log(0.95))
9295
cm = CalibrationMeasures(pred_nll, ref_nll)
9396
value_test = cm.negative_log_likelihood()
9497
assert_allclose(value_test, expected_nll)

test/test_utility/test_assignment_localization.py

Lines changed: 116 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,46 @@
1111
from sklearn.metrics import matthews_corrcoef as mcc
1212

1313
#Data for figure 6a testing of assignment and average precision
14-
ref6c1 = np.asarray([3,2,7,5])
15-
ref6c2 = np.asarray([7,9,8,11])
16-
ref6c3 = np.asarray([1,16,3,18])
17-
ref6c4 = np.asarray([14,14,16,18])
14+
ref6a1 = np.asarray([3,2,7,5])
15+
ref6a2 = np.asarray([7,9,8,11])
16+
ref6a3 = np.asarray([1,16,3,18])
17+
ref6a4 = np.asarray([14,14,16,18])
1818

19-
pred6c1 = np.asarray([2,3,6,6])
20-
pred6c2 = np.asarray([2,15,4,17])
21-
pred6c3 = np.asarray([13,13,15,17])
22-
pred6c4 = np.asarray([16,7,19,10])
23-
pred6c5 = np.asarray([12,2,15,4])
19+
pred6a1 = np.asarray([2,3,6,6])
20+
pred6a2 = np.asarray([2,15,4,17])
21+
pred6a3 = np.asarray([13,13,15,17])
22+
pred6a4 = np.asarray([16,7,19,10])
23+
pred6a5 = np.asarray([12,2,15,4])
2424

25-
pred_proba_6c = [[0.05, 0.95],[0.30,0.70],[0.20,0.80],[0.20,0.80],[0.10,0.90]]
25+
pred_proba_6a = [[0.05, 0.95],[0.30,0.70],[0.20,0.80],[0.20,0.80],[0.10,0.90]]
2626

27-
pred_boxes_6c = [pred6c1, pred6c2, pred6c3, pred6c4, pred6c5]
28-
ref_boxes_6c = [ref6c1, ref6c2, ref6c3, ref6c4]
27+
pred_boxes_6a = [pred6a1, pred6a2, pred6a3, pred6a4, pred6a5]
28+
ref_boxes_6a = [ref6a1, ref6a2, ref6a3, ref6a4]
2929

30+
#Data from Panoptic Quality - 3.51 p96
31+
#Figure 3.51 p96
32+
pq_pred1 = np.zeros([18, 18])
33+
pq_pred1[ 3:7,1:3] = 1
34+
pq_pred1[3:6,3:7]=1
35+
pq_pred2 = np.zeros([18, 18])
36+
pq_pred2[13:16,4:6] = 1
37+
pq_pred3 = np.zeros([18, 18])
38+
pq_pred3[7:12,13:17] = 1
39+
pq_pred4 = np.zeros([18, 18])
40+
pq_pred4[13:15,13:17] = 1
41+
pq_pred4[15,15] = 1
42+
43+
pq_ref1 = np.zeros([18, 18])
44+
pq_ref1[2:7, 1:3] = 1
45+
pq_ref1[2:5,3:6] = 1
46+
pq_ref2 = np.zeros([18, 18])
47+
pq_ref2[6:12,12:17] = 1
48+
pq_ref3 = np.zeros([18, 18])
49+
pq_ref3[14:15:,7:10] = 1
50+
pq_ref3[13:16,8:9] = 1
51+
52+
ref_351 = [pq_ref1, pq_ref2, pq_ref3]
53+
pred_351 = [pq_pred1, pq_pred2, pq_pred3,pq_pred4]
3054

3155

3256
## Data for figure 59 and testing of localisation
@@ -41,7 +65,7 @@
4165
f59_pred2[4:8, 5:9] = 1
4266

4367
def test_assignment_6c():
44-
asm1 = AssignmentMapping(pred_loc=pred_boxes_6c, ref_loc=ref_boxes_6c, pred_prob=pred_proba_6c, thresh=0.1,localization='box_iou')
68+
asm1 = AssignmentMapping(pred_loc=pred_boxes_6a, ref_loc=ref_boxes_6a, pred_prob=pred_proba_6a, thresh=0.1,localization='box_iou')
4569
df_matching, df_fn, df_fp, list_valid = asm1.initial_mapping()
4670
print(asm1.matrix, df_matching, df_fp, df_fn, list_valid)
4771
numb_fn = df_fn.shape[0]
@@ -52,6 +76,7 @@ def test_assignment_6c():
5276
assert expected_fp == numb_fp
5377
assert_array_almost_equal(np.asarray(list_valid),np.asarray([0,1,2]))
5478

79+
5580
def test_check_localization():
5681
ref_box = [[2,2,4,4]]
5782
ref_com = [[3,3]]
@@ -209,6 +234,84 @@ def test_pairwise_pointcomdist():
209234
expected_matrix = np.asarray([[0, 9.22],[8.49, 1]])
210235
assert_allclose(am.matrix, expected_matrix,atol=0.01)
211236

237+
def test_pairwise_boxiou_6a():
238+
"""
239+
Using figure 6a p14 pitfalls as illustration for boxiou pairwise example
240+
"""
241+
asm1 = AssignmentMapping(pred_loc=pred_boxes_6a, ref_loc=ref_boxes_6a, pred_prob=pred_proba_6a, thresh=0.1,localization='box_iou')
242+
df_matching, df_fn, df_fp, list_valid = asm1.initial_mapping()
243+
expected_matrix = np.asarray([[0.42857143, 0. , 0. , 0. ],
244+
[0. , 0. , 0.28571429, 0. ],
245+
[0. , 0. , 0. , 0.36363636],
246+
[0. , 0. , 0. , 0. ],
247+
[0. , 0. , 0. , 0. ]])
248+
assert_array_almost_equal(expected_matrix, asm1.matrix)
249+
250+
def test_com_from_refbox_6a():
251+
"""
252+
Using figure 6a as illustration
253+
"""
254+
255+
asm1 = AssignmentMapping(pred_loc=pred_boxes_6a, ref_loc=ref_boxes_6a, pred_prob=pred_proba_6a, thresh=0.1,localization='box_iou')
256+
asm1.com_fromrefbox()
257+
test_com = asm1.ref_loc_mod
258+
expected_ref_com = [[5,3.5],[7.5,10],[2,17],[15,16]]
259+
assert_array_almost_equal(np.asarray(expected_ref_com), test_com)
260+
261+
def test_com_from_predbox_6a():
262+
"""
263+
Using figure 6a as illustration
264+
"""
265+
asm1 = AssignmentMapping(pred_loc=pred_boxes_6a, ref_loc=ref_boxes_6a, pred_prob=pred_proba_6a, thresh=0.1,localization='box_iou')
266+
asm1.com_frompredbox()
267+
test_com = asm1.pred_loc_mod
268+
print(test_com)
269+
expected_pred_com = [[4,4.5],[3,16],[14,15],[17.5,8.5],[13.5,3]]
270+
assert_array_almost_equal(np.asarray(expected_pred_com), test_com,decimal=2)
271+
272+
def test_comfrompredmask_351():
273+
"""
274+
Using figure 3.51 (p96 Panoptic quality) as illustration
275+
"""
276+
asm = AssignmentMapping(pred_loc=pred_351, ref_loc=ref_351,pred_prob=np.asarray([1,1,1,1]), thresh=0.1,localization='mask_iou')
277+
expected_predcom = [[4.2,3.3],[14,4.5],[9,14.5],[13.66,14.55]]
278+
asm.com_frompredmask()
279+
test_com = asm.pred_loc_mod
280+
assert_array_almost_equal(np.asarray(expected_predcom),test_com,decimal=2)
281+
282+
def test_comfromrefmask_351():
283+
"""
284+
Using figure 3.51 (p96 Panoptic quality) as illustrative example
285+
"""
286+
asm = AssignmentMapping(pred_loc=pred_351, ref_loc=ref_351,pred_prob=np.asarray([1,1,1,1]), thresh=0.1,localization='mask_iou')
287+
expected_refcom = [[3.53,2.68],[8.5,14],[14,8]]
288+
asm.com_fromrefmask()
289+
test_com = asm.ref_loc_mod
290+
assert_array_almost_equal(np.asarray(expected_refcom),test_com,decimal=2)
291+
292+
def test_box_fromrefmask():
293+
"""
294+
Using fig 3.51 p96 as illustrative example
295+
"""
296+
asm = AssignmentMapping(pred_loc=pred_351, ref_loc=ref_351,pred_prob=np.asarray([1,1,1,1]), thresh=0.1,localization='mask_iou')
297+
asm.box_fromrefmask()
298+
test_box = asm.ref_loc_mod
299+
expected_box = [[2,1,6,5],[6,12,11,16],[13,7,15,9]]
300+
assert_array_almost_equal(np.asarray(expected_box),test_box)
301+
302+
def test_box_frompredmask():
303+
"""
304+
Using fig 3.51 p96 as illustrative example
305+
"""
306+
asm = AssignmentMapping(pred_loc=pred_351, ref_loc=ref_351,pred_prob=np.asarray([1,1,1,1]), thresh=0.1,localization='mask_iou')
307+
asm.box_frompredmask()
308+
test_box = asm.pred_loc_mod
309+
expected_box = [[3,1,6,6],[13,4,15,5],[7,13,11,16],[13,13,15,16]]
310+
assert_array_almost_equal(np.asarray(expected_box),test_box)
311+
312+
313+
314+
212315

213316
def test_localization():
214317
ref = [f59_ref1, f59_ref2]

0 commit comments

Comments
 (0)