-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetrics.py
175 lines (128 loc) · 6.5 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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import numpy as np
import logging
import torch
from utils import compute_binary_accuracy, compute_accuracy
CLUSTER_VAR_TAG = "variances"
CLUSTER_STDDEV_TAG = "stddevs"
# Actual cluster means (including corrupted instances)
CLUSTER_MEAN_TAG = "means"
CLUSTER_UNCORRUPTED_STDDEV_TAG = "uncorrupted_stddevs"
CLUSTER_UNCORRUPTED_MEAN_TAG = "uncorrupted_means"
CLUSTER_CORRUPTED_MEAN_TAG = "corrupted_means"
CORRUPTION_BIASES_TAG = "corruption_biases"
def prepare_features_and_labels(model, trainloader, args, fc_features):
instances = []
labels = []
for batch_idx, (inputs, targets) in enumerate(trainloader):
# Targets store potentially corrupted and original label
inputs, targets = inputs.to(args.device), targets.to(args.device)
model.eval()
with torch.no_grad():
model_outputs = model(inputs)
features = fc_features.outputs[0][0]
fc_features.clear()
instances.append(features)
labels.append(targets)
return instances, labels
def get_class_means(instances, labels, classes, num_features=2):
class_means = np.zeros([len(classes), num_features])
for class_idx in classes:
selected_instances = instances[labels[:, 0] == class_idx]
class_means[class_idx] = np.mean(selected_instances, axis=0)
return class_means
def get_class_variances(instances, labels, classes, class_means):
# Statistics based on Euclidean distances (reduction applied)
variances = np.zeros(len(classes))
std_devs = np.zeros(len(classes))
for class_idx in classes:
selected_instances = instances[labels[:, 0] == class_idx]
class_mean = class_means[class_idx]
variances[class_idx] = np.var(np.linalg.norm(selected_instances - class_mean, axis=-1))
std_devs[class_idx] = np.std(np.linalg.norm(selected_instances - class_mean, axis=-1))
return variances, std_devs
def get_sigma_W(instances, labels, classes, class_means):
Sigma_W = np.zeros(2)
K = len(classes)
for i in range(Sigma_W.shape[0]):
n = 1
for class_idx in classes:
selected_instances = instances[labels[:, 0] == class_idx][:, i]
n = selected_instances.shape[0]
Sigma_W[i] += (selected_instances - class_means[class_idx, i]) @ np.transpose(
selected_instances - class_means[class_idx, i])
Sigma_W[i] /= K * n
return Sigma_W
def get_centroids_by_corruption(args, instances, labels, classes, num_features=2):
uncorrupted_centroids = np.zeros([len(classes), num_features])
corrupted_mask = get_corrupted_instance_mask(args, labels)
uncorrupted_mask = np.logical_not(corrupted_mask)
for class_idx in classes:
uncorrupted_instances = instances[uncorrupted_mask]
uncorrupted_centroids[class_idx] = np.mean(uncorrupted_instances[labels[uncorrupted_mask][:, 0] == class_idx],
axis=0)
corrupted_centroids = np.zeros([len(classes), len(classes), num_features])
if args.fourclass_problem and args.fourclass_twofeatures:
for class_idx in classes:
for inner_class_idx in classes:
# The second label dimension indicates the original class id
corrupted_class_instances = instances[corrupted_mask][labels[corrupted_mask][:, 1] == inner_class_idx]
corrupted_centroids[class_idx, inner_class_idx] = np.mean(corrupted_class_instances, axis=0)
else:
for class_idx in classes:
for inner_class_idx in classes:
# The second label dimension indicates the original class id
corrupted_class_instances = instances[corrupted_mask][
np.logical_and(labels[corrupted_mask][:, 0] == class_idx,
labels[corrupted_mask][:, 1] == inner_class_idx)]
corrupted_centroids[class_idx, inner_class_idx] = np.mean(corrupted_class_instances, axis=0)
return uncorrupted_centroids, corrupted_centroids
def get_uncorrupted_stddevs(args, instances, labels, uncorrupted_centroids, classes):
"""
Calculates the standard deviation of the uncorrupted instances.
:param instances:
:param labels:
:param uncorrupted_centroids:
:param classes:
:return:
"""
uncorrupted_stddevs = np.zeros(len(classes))
for class_idx in classes:
uncorrupted_mask = np.logical_not(get_corrupted_instance_mask(args, labels))
uncorrupted_instances = instances[uncorrupted_mask][labels[uncorrupted_mask, 0] == class_idx]
uncorrupted_stddevs[class_idx] = np.std(
np.linalg.norm(uncorrupted_instances - uncorrupted_centroids[class_idx], axis=-1))
return uncorrupted_stddevs
def get_corrupted_instance_mask(args, labels):
if "fourclass_problem" in args.__dict__ and args.fourclass_problem:
return labels[:, 0] > 1
else:
return labels[:, 0] != labels[:, 1]
def get_dot_product_bias(instances, labels, classes, class_means):
corruption_biases = np.zeros([len(classes), len(classes) - 1])
for class_idx in classes:
class_mask = labels[:, 0] == class_idx
corrupted_instances = instances[class_mask][labels[class_mask][:, 0] != labels[class_mask][:, 1]]
corrupted_mean = np.mean(corrupted_instances, axis=0)
loc_idx = 0
for other_class_idx in classes:
if other_class_idx == class_idx:
continue
corrupted_vector = corrupted_mean - class_means[class_idx]
cls_to_cls_centroid = class_means[other_class_idx] - class_means[class_idx]
tmp_bias = np.dot(corrupted_vector, cls_to_cls_centroid) / np.linalg.norm(cls_to_cls_centroid)
corruption_biases[
class_idx, loc_idx] = tmp_bias # max(tmp_bias, np.dot(cls_to_cls_centroid, corrupted_vector))
loc_idx += 1
logging.info("Corruption biases: {}".format(corruption_biases))
return corruption_biases
def update_accuracy(top1, inputs, outputs, targets, is_binary):
if is_binary:
prec1 = compute_binary_accuracy(outputs[0].detach().data, targets.detach().data)
top1.update(prec1, inputs.size(0))
else:
prec1, _ = compute_accuracy(outputs[0].detach().data, targets.detach().data, topk=(1, 2))
top1.update(prec1.item(), inputs.size(0))
def relative_mse(predictions, targets):
return torch.mean(torch.square(predictions - targets) / (targets + 1e-7))
def relative_mae(predictions, targets):
return torch.mean(torch.abs(predictions - targets) / (targets + 1e-7))