-
Notifications
You must be signed in to change notification settings - Fork 194
/
mean_iou.py
137 lines (109 loc) · 4.61 KB
/
mean_iou.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
from tkinter import Y
import torch
import torch.nn.functional as F
import numpy as np
#######################################################
# First Alternative Mean IoU Calculations with PyTorch
########################################################
def mean_iou(predicted_label, label, eps=1e-10, num_classes=10):
"""
Calculates the mean Intersection over Union (IoU) for multiple classes between predicted labels and true labels.
The mean IoU is a common evaluation metric for semantic segmentation tasks.
It measures the average overlap between the predicted and true labels for each class.
Args:
predicted_label (Tensor): Predicted labels from the model.
label (Tensor): True labels or ground truth values.
eps (float, optional): Smoothing parameter to avoid division by zero. Default is 1e-10.
num_classes (int, optional): Number of classes. Default is 10.
Returns:
float: Mean IoU value.
"""
with torch.no_grad():
predicted_label = F.softmax(predicted_label, dim=1)
predicted_label = torch.argmax(predicted_label, dim=1)
predicted_label = predicted_label.contiguous().view(-1)
label = label.contiguous().view(-1)
iou_single_class = []
for class_number in range(0, num_classes):
true_predicted_class = predicted_label == class_number
true_label = label == class_number
if true_label.long().sum().item() == 0:
iou_single_class.append(np.nan)
else:
intersection = (
torch.logical_and(true_predicted_class, true_label)
.sum()
.float()
.item()
)
union = (
torch.logical_or(true_predicted_class, true_label)
.sum()
.float()
.item()
)
iou = (intersection + eps) / (union + eps)
iou_single_class.append(iou)
return np.nanmean(iou_single_class)
############################################
# Second Alternative Mean IoU Calculations
############################################
def meanIoU(target, predicted):
"""
Calculates the mean Intersection over Union (IoU) between target and predicted tensors.
The mean IoU is a commonly used evaluation metric for semantic segmentation tasks.
It measures the average overlap between the predicted and target tensors.
Args:
target (Tensor): Target tensor containing true labels or ground truth values.
predicted (Tensor): Predicted tensor containing predicted labels.
Returns:
float: Mean IoU value.
"""
if target.shape != predicted.shape:
print(
"target has dimension",
target.shape,
", predicted values have shape ",
predicted.shape,
)
return
if target.dim() != 4:
print("target has dim ", target.dim(), ", it must be 4")
return
iousum = 0
for i in range(target.shape[0]):
target_arr = target[i, :, :, :].clone().detach().cpu().numpy().argmax(0)
predicted_arr = predicted[i, :, :, :].clone().detach().cpu().numpy().argmax(0)
intersection = np.logical_and(target_arr, predicted_arr).sum()
union = np.logical_or(target_arr, predicted_arr).sum()
if union == 0:
iou_score = 0
else:
iou_score = intersection / union
iousum += iou_score
miou = iousum / target.shape[0]
return miou
#######################################################
# Third Alternative Mean IoU Calculations with sklearn
#######################################################
from sklearn.metrics import confusion_matrix
def mean_iou(y_pred, y_true):
"""
Calculates the mean Intersection over Union (IoU) between predicted labels and true labels.
The mean IoU is a common evaluation metric for semantic segmentation tasks.
It measures the average overlap between the predicted and true labels.
Args:
y_pred (array-like): Predicted labels.
y_true (array-like): True labels or ground truth values.
Returns:
float: Mean IoU value.
"""
y_pred = y_pred.flatten()
y_true = y_true.flatten()
confusion_tensor = confusion_matrix(y_true, y_pred, labels=[0, 1])
intersection = np.diag(confusion_tensor)
ground_truth_set = confusion_tensor.sum(axis=1)
predicted_set = confusion_tensor.sum(axis=0)
union = ground_truth_set + predicted_set - intersection
IoU = intersection / union.astype(np.float32)
return np.mean(IoU)