| 21 | |
| 22 | |
| 23 | def accuracy_metric(y_pred, y_true, void_labels, one_hot=False): |
| 24 | assert (y_pred.ndim == 2) or (y_pred.ndim == 1) |
| 25 | |
| 26 | # y_pred to indices |
| 27 | if y_pred.ndim == 2: |
| 28 | y_pred = T.argmax(y_pred, axis=1) |
| 29 | |
| 30 | if one_hot: |
| 31 | y_true = T.argmax(y_true, axis=1) |
| 32 | |
| 33 | # Compute accuracy |
| 34 | acc = T.eq(y_pred, y_true).astype(_FLOATX) |
| 35 | |
| 36 | # Create mask |
| 37 | mask = T.ones_like(y_true, dtype=_FLOATX) |
| 38 | for el in void_labels: |
| 39 | indices = T.eq(y_true, el).nonzero() |
| 40 | if any(indices): |
| 41 | mask = T.set_subtensor(mask[indices], 0.) |
| 42 | |
| 43 | # Apply mask |
| 44 | acc *= mask |
| 45 | acc = T.sum(acc) / T.sum(mask) |
| 46 | |
| 47 | return acc |
| 48 | |
| 49 | |
| 50 | def jaccard(y_pred, y_true, n_classes, one_hot=False): |