-
Notifications
You must be signed in to change notification settings - Fork 1
/
temperature_scaling.py
173 lines (152 loc) · 7.42 KB
/
temperature_scaling.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
"""
Temperature scaling method, forked from github
"""
import torch
from torch import nn, optim
from torch.nn import functional as F
import math
class ModelWithTemperature(nn.Module):
"""
A thin decorator, which wraps a model with temperature scaling
model (nn.Module):
A classification neural network
NB: Output of the neural network should be the classification logits,
NOT the softmax (or log softmax)!
"""
def __init__(self, model):
super(ModelWithTemperature, self).__init__()
self.model = model
self.temperature = nn.Parameter(torch.ones(1) * 1.5)
def forward(self, input):
logits = self.model(input)
return self.temperature_scale(logits)
def temperature_scale(self, logits):
"""
Perform temperature scaling on logits
"""
# Expand temperature to match the size of logits
temperature = self.temperature.unsqueeze(1).expand(logits.size(0), logits.size(1))
return logits / temperature
# This function probably should live outside of this class, but whatever
def set_temperature(self, valid_loader):
"""
Tune the tempearature of the model (using the validation set).
We're going to set it to optimize NLL.
valid_loader (DataLoader): validation set loader
"""
self.cuda()
nll_criterion = nn.CrossEntropyLoss().cuda()
ece_criterion = _ECELoss().cuda()
# First: collect all the logits and labels for the validation set
logits_list = []
labels_list = []
with torch.no_grad():
for input, label in valid_loader:
input = input.cuda()
logits = self.model(input)
logits_list.append(logits)
labels_list.append(label)
logits = torch.cat(logits_list).cuda()
labels = torch.cat(labels_list).cuda()
# Calculate NLL and ECE before temperature scaling
before_temperature_nll = nll_criterion(logits, labels).item()
before_temperature_ece = ece_criterion(logits, labels).item()
print('Before temperature - NLL: %.3f, ECE: %.3f' % (before_temperature_nll, before_temperature_ece))
# Next: optimize the temperature w.r.t. NLL
optimizer = optim.LBFGS([self.temperature], lr=0.01, max_iter=50)
def eval():
loss = nll_criterion(self.temperature_scale(logits), labels)
loss.backward()
return loss
optimizer.step(eval)
# Calculate NLL and ECE after temperature scaling
after_temperature_nll = nll_criterion(self.temperature_scale(logits), labels).item()
after_temperature_ece = ece_criterion(self.temperature_scale(logits), labels).item()
print('Optimal temperature: %.3f' % self.temperature.item())
print('After temperature - NLL: %.3f, ECE: %.3f' % (after_temperature_nll, after_temperature_ece))
return self
class _ECELoss(nn.Module):
"""
Calculates the Expected Calibration Error of a model.
(This isn't necessary for temperature scaling, just a cool metric).
The input to this loss is the logits of a model, NOT the softmax scores.
This divides the confidence outputs into equally-sized interval bins.
In each bin, we compute the confidence gap:
bin_gap = | avg_confidence_in_bin - accuracy_in_bin |
We then return a weighted average of the gaps, based on the number
of samples in each bin
See: Naeini, Mahdi Pakdaman, Gregory F. Cooper, and Milos Hauskrecht.
"Obtaining Well Calibrated Probabilities Using Bayesian Binning." AAAI.
2015.
"""
def __init__(self, n_bins=15):
"""
n_bins (int): number of confidence interval bins
"""
super(_ECELoss, self).__init__()
bin_boundaries = torch.linspace(0, 1, n_bins + 1)
self.bin_lowers = bin_boundaries[:-1]
self.bin_uppers = bin_boundaries[1:]
def forward(self, logits, labels):
softmaxes = F.softmax(logits, dim=1)
confidences, predictions = torch.max(softmaxes, 1)
accuracies = predictions.eq(labels)
ece = torch.zeros(1, device=logits.device)
for bin_lower, bin_upper in zip(self.bin_lowers, self.bin_uppers):
# Calculated |confidence - accuracy| in each bin
in_bin = confidences.gt(bin_lower.item()) * confidences.le(bin_upper.item())
prop_in_bin = in_bin.float().mean()
if prop_in_bin.item() > 0:
accuracy_in_bin = accuracies[in_bin].float().mean()
avg_confidence_in_bin = confidences[in_bin].mean()
ece += torch.abs(avg_confidence_in_bin - accuracy_in_bin) * prop_in_bin
return ece
from torchvision import transforms, datasets
from deep_coral import source_net
def dataloader(root_path, dir, batch_size, train, kwargs):
transform = {
'train': transforms.Compose(
[transforms.Resize([256, 256]),
transforms.RandomCrop(224),
transforms.RandomHorizontalFlip(),
transforms.ToTensor()]),
'test': transforms.Compose(
[transforms.Resize([224, 224]),
transforms.ToTensor()])
}
data = datasets.ImageFolder(root=root_path + dir, transform=transform['train' if train else 'test'])
data_loader = torch.utils.data.DataLoader(data, batch_size=batch_size, shuffle=True, drop_last=True)
return data_loader
def entropy(p):
p[p<1e-20] = 1e-20
return -torch.sum(p.mul(torch.log2(p)))
if __name__=="__main__":
source = "amazon"
target = "webcam"
N_CLASSES = 31
orig_model = torch.load("models/sourceOnly_" + source + "_" + target + ".pkl", encoding="iso-8859-1")
valid_loader = dataloader("office/", source, 64, True, None)
test_loader = dataloader("office/", target, 64, True, None)
scaled_model = ModelWithTemperature(orig_model)
scaled_model.set_temperature(valid_loader)
ce_func = nn.CrossEntropyLoss()
entropy_clas, test_loss, test_acc, mis_entropy_clas, mis_num, cor_entropy_clas, cor_num, num_test = 0, 0, 0, 0, 0, 0, 0, 0
with torch.no_grad():
for data, label in test_loader:
num_test += data.shape[0]
data = data.cuda()
label = label.cuda()
target_out = scaled_model(data)
prediction_t = F.softmax(target_out, dim=1)
entropy_clas += entropy(prediction_t) / math.log(N_CLASSES, 2)
test_loss += float(ce_func(target_out, label))
test_acc += torch.sum(torch.argmax(prediction_t, dim=1) == label).float()
mis_idx = (torch.argmax(prediction_t, dim=1) != label).nonzero().reshape(-1, )
mis_pred = prediction_t[mis_idx]
cor_idx = (torch.argmax(prediction_t, dim=1) == label).nonzero().reshape(-1, )
cor_pred = prediction_t[cor_idx]
mis_entropy_clas += entropy(mis_pred) / math.log(N_CLASSES, 2)
mis_num += mis_idx.shape[0]
cor_entropy_clas += entropy(cor_pred) / math.log(N_CLASSES, 2)
cor_num += cor_idx.shape[0]
print("test_loss: %.3f, test_acc: %.4f, ent_clas: %.3f, mis_ent_clas: %.3f, cor_ent: %.3f" % (test_loss * 1e3 / num_test, test_acc / num_test, entropy_clas / num_test, mis_entropy_clas / mis_num, cor_entropy_clas / cor_num))