| 23 | |
| 24 | |
| 25 | def jaccard_metric(y_pred, y_true, n_classes, one_hot=False): |
| 26 | |
| 27 | assert (y_pred.ndim == 2) or (y_pred.ndim == 1) |
| 28 | |
| 29 | # y_pred to indices |
| 30 | if y_pred.ndim == 2: |
| 31 | y_pred = T.argmax(y_pred, axis=1) |
| 32 | |
| 33 | if one_hot: |
| 34 | y_true = T.argmax(y_true, axis=1) |
| 35 | |
| 36 | # Compute confusion matrix |
| 37 | # cm = T.nnet.confusion_matrix(y_pred, y_true) |
| 38 | cm = T.zeros((n_classes, n_classes)) |
| 39 | for i in range(n_classes): |
| 40 | for j in range(n_classes): |
| 41 | cm = T.set_subtensor( |
| 42 | cm[i, j], T.sum(T.eq(y_pred, i) * T.eq(y_true, j))) |
| 43 | |
| 44 | # Compute Jaccard Index |
| 45 | TP_perclass = T.cast(cm.diagonal(), _FLOATX) |
| 46 | FP_perclass = cm.sum(1) - TP_perclass |
| 47 | FN_perclass = cm.sum(0) - TP_perclass |
| 48 | |
| 49 | num = TP_perclass |
| 50 | denom = TP_perclass + FP_perclass + FN_perclass |
| 51 | |
| 52 | return T.stack([num, denom], axis=0) |
| 53 | |
| 54 | |
| 55 | def accuracy_metric(y_pred, y_true, void_labels, one_hot=False): |