-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetrics.py
68 lines (52 loc) · 2.21 KB
/
metrics.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import numpy as np
from sklearn.metrics import confusion_matrix as sklearn_confusion_matrix
class Metrics:
"""
A class to compute and manage evaluation metrics for the material segmentation task.
"""
def __init__(self, num_classes=44, ignore_index=255):
"""
Initialize the Metrics class.
Args:
num_classes (int): Number of classes in the dataset.
ignore_index (int): Label index to ignore when updating metrics.
"""
self.ignore_index = ignore_index
self.num_classes = num_classes
self.confusion_matrix = np.zeros((num_classes, num_classes))
def compute(self):
"""
Compute pixel accuracy, mean accuracy, and mean Intersection over Union (IoU).
Returns:
tuple: pixel accuracy, mean accuracy, mean IoU
"""
pixel_acc = np.diag(self.confusion_matrix).sum() / (self.confusion_matrix.sum() + 1e-9)
per_class_acc = np.diag(self.confusion_matrix) / (self.confusion_matrix.sum(axis=1) + 1e-9)
macc = per_class_acc.sum() / ((np.sum(self.confusion_matrix, axis=1) > 0).sum() + 1e-9)
per_class_iou = np.diag(self.confusion_matrix) / (
np.sum(self.confusion_matrix, axis=1)
+ np.sum(self.confusion_matrix, axis=0)
- np.diag(self.confusion_matrix)
+ 1e-9
)
miou = per_class_iou.sum() / ((np.sum(self.confusion_matrix, axis=1) > 0).sum() + 1e-9)
return pixel_acc, macc, miou
def update(self, pred, label):
"""
Update the confusion matrix based on predictions and labels.
Args:
pred (torch.Tensor): Predictions from the model.
label (torch.Tensor): Ground truth labels.
"""
gt = label.cpu().numpy()
mask = gt != self.ignore_index
pred = pred.numpy()
# Update confusion matrix with valid entries
self.confusion_matrix += sklearn_confusion_matrix(
gt[mask], pred[mask], labels=[_ for _ in range(self.num_classes)]
)
def reset(self):
"""
Reset the confusion matrix to zero.
"""
self.confusion_matrix = np.zeros((self.num_classes, self.num_classes))