-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmetrics.py
More file actions
19 lines (16 loc) · 728 Bytes
/
Copy pathmetrics.py
File metadata and controls
19 lines (16 loc) · 728 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
def compute_metrics(pred, target, threshold=0.5):
# Binarize predictions and target
pred = (pred > threshold).float()
target = (target > threshold).float()
TP = (pred * target).sum().item()
TN = ((1 - pred) * (1 - target)).sum().item()
FP = (pred * (1 - target)).sum().item()
FN = ((1 - pred) * target).sum().item()
accuracy = (TP + TN) / (TP + TN + FP + FN)
precision = TP / (TP + FP + 1e-8)
recall = TP / (TP + FN + 1e-8)
specificity = TN / (TN + FP + 1e-8)
sensitivity = recall # Sensitivity is the same as recall
dice = 2 * TP / (2 * TP + FP + FN + 1e-8)
iou = TP / (TP + FP + FN + 1e-8)
return accuracy, precision, recall, specificity, sensitivity, dice, iou