MCPcopy Create free account
hub / github.com/lisa-lab/DeepLearningTutorials / jaccard

Function jaccard

code/cnn_1D_segm/train_fcn1D.py:50–75  ·  view source on GitHub ↗
(y_pred, y_true, n_classes, one_hot=False)

Source from the content-addressed store, hash-verified

48
49
50def jaccard(y_pred, y_true, n_classes, one_hot=False):
51 assert (y_pred.ndim == 2) or (y_pred.ndim == 1)
52
53 # y_pred to indices
54 if y_pred.ndim == 2:
55 y_pred = T.argmax(y_pred, axis=1)
56
57 if one_hot:
58 y_true = T.argmax(y_true, axis=1)
59
60 # Compute confusion matrix
61 cm = T.zeros((n_classes, n_classes))
62 for i in range(n_classes):
63 for j in range(n_classes):
64 cm = T.set_subtensor(
65 cm[i, j], T.sum(T.eq(y_pred, i) * T.eq(y_true, j)))
66
67 # Compute Jaccard Index
68 TP_perclass = T.cast(cm.diagonal(), _FLOATX)
69 FP_perclass = cm.sum(1) - TP_perclass
70 FN_perclass = cm.sum(0) - TP_perclass
71
72 num = TP_perclass
73 denom = TP_perclass + FP_perclass + FN_perclass
74
75 return T.stack([num, denom], axis=0)
76
77
78SAVEPATH = 'save_models/'

Callers 1

trainFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected