-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdensity_ratio_calibration.py
459 lines (348 loc) · 18.9 KB
/
density_ratio_calibration.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
'''
This file contains the code of the density estimation calibration method.
'''
import sys, math
import numpy as np
import pandas as pd
import time, pdb
from sklearn.metrics import log_loss, brier_score_loss
from sklearn.preprocessing import normalize
import torch
import torch.nn as nn
import torch.nn.functional as F
# Imports to get "utility" package
# sys.path.append( path.dirname( path.dirname( path.abspath("utility") ) ) )
# from utility.unpickle_probs import unpickle_probs
# from utility.evaluation import ECE, MCE
import statsmodels.api as sm
from scipy.special import softmax
# import KDEpy as kde
from scipy.stats import gaussian_kde
from sklearn.neighbors import KernelDensity
class DensityRatioCalibration():
def __init__(self):
pass
# Find the temperature
def fit(self, probs, preds, true, proximity, bandwidth='normal_reference'):
"""
Train the density estimator for correctly classified samples and misclassified samples
1. split samples into correctly classified and misclassified
3. count their numbers and show the ratio
4. learn the conditional distribution of the confidence given the proximity -> the distributions are represented as <dens_true, dens_false>
5. Compute acc_ratio = p(correct=false, d) / p(correct=true, d) for every bins -> samples in the bins are assigned the corresponding acc_ratio
6. Compute the calibration score for each sample
Params:
probs: the confidenc vector of every classes (shape [samples, classes])
preds: the predicted class for each sample (shape [samples, ])
true: true labels (shape [samples,])
proximity: the exponential function of the negative average distance to K nearest neighbors (shape [samples,])
Returns:
None
"""
assert np.all(probs >= 0) and np.all(probs <= 1), "All elements in 'probs' should be in the range [0, 1]."
confs = np.max(probs, axis=-1)
val_df = pd.DataFrame({'ys':true, 'proximity':proximity, 'conf':confs, 'pred':preds})
val_df['correct'] = (val_df.pred == val_df.ys).astype('int')
val_df_true = val_df[val_df['correct'] == 1]
val_df_false = val_df[val_df['correct'] == 0]
indep = val_df_true['proximity']
dep = val_df_true['conf']
self.dens_true = sm.nonparametric.KDEMultivariate(data=[dep, indep], var_type='cc', bw=bandwidth)
indep = val_df_false['proximity']
dep = val_df_false['conf']
self.dens_false = sm.nonparametric.KDEMultivariate(data=[dep, indep], var_type='cc', bw=bandwidth)
self.false_true_ratio = (val_df.pred != val_df.ys).sum() / (val_df.pred == val_df.ys).sum()
print('Density Estimation Done.')
def predict(self, probs, proximities):
"""
use Bayes' rule to compute the posterior probability
p(\hat{y}=y | h(x), d)=\frac{p(h(x), d|\hat{y}=y)} {p(h(x), d|\hat{y}=y) + p(h(x), d|\hat{y} \neq y) \cdot \frac{p(\hat{y} \neq y)}{p(\hat{y}=y)}}
Params:
probs: the confidenc vector of every classes (shape [samples, classes])
preds: the predicted class for each sample (shape [samples, ])
true: true labels (shape [samples,])
proximity: the exponential function of the negative average distance to K nearest neighbors (shape [samples,])
Returns:
calibrated probabilities (nd.array with shape [samples,])
"""
assert np.all(probs >= 0) and np.all(probs <= 1), "All elements in 'probs' should be in the range [0, 1]."
preds = np.argmax(probs, axis=-1)
confs = np.max(probs, axis=-1)
data = np.array([confs, proximities]).T # shape [samples, 2]
conf_reg_true = self.dens_true.pdf(data) # shape [samples,]
conf_reg_false = self.dens_false.pdf(data)
# eps is to avoid division by 0
eps = 1e-10
conf_calibrated = conf_reg_true / np.maximum(conf_reg_true + conf_reg_false * self.false_true_ratio, eps)
# Normalize the rest of the values in each row to sum to 1-conf_max
mask = np.ones(probs.shape, dtype=bool)
mask[range(probs.shape[0]), preds] = False
probs = probs * mask
probs = probs * ((1 - conf_calibrated) / probs.sum(axis=-1))[:, np.newaxis]
# Add the calibrated confidence to the predicted class
probs[range(probs.shape[0]), preds] = conf_calibrated # dtype64 -> dtype32
return probs
def mirror_1d(d, xmin=None, xmax=None):
"""
If necessary apply reflecting boundary conditions.
input data d: [samples,]
"""
if xmin is not None and xmax is not None:
xmed = (xmin+xmax)/2
return np.concatenate(((2*xmin-d[d < xmed]), d, (2*xmax-d[d >= xmed])))
elif xmin is not None:
return np.concatenate((2*xmin-d, d))
elif xmax is not None:
return np.concatenate((d, 2*xmax-d))
else:
return d
def mirror_1d_along_axis(data, axis=0, xmin=0, xmax=1):
"""
data: [samples, 2] (in our case dim_features=2)
data1 is the confidence; [samples,]
data2 is the proximity; [samples,]
"""
# 1. mirror data1
if xmin is not None and xmax is not None:
xmed = (xmin + xmax)/2
d_left = np.copy(data[data[:, axis] < xmed])
d_right = np.copy(data[data[:, axis] >= xmed])
d_left[:, axis] = 2*xmin - d_left[:, axis]
d_right[:, axis] = 2*xmax - d_right[:, axis]
data_mirror = np.concatenate((d_left, data, d_right), axis=0)
return data_mirror
elif xmin is not None:
d_left = np.copy(data)
d_left[:, axis] = 2*xmin - d_left[:, axis]
return np.concatenate((d_left, data), axis=0)
elif xmax is not None:
d_right = np.copy(data)
d_right[:, axis] = 2*xmax - d_right[:, axis]
return np.concatenate((data, d_right), axis=0)
else:
return data
def mirror_2d(data, xmin=0, xmax=1, ymin=0, ymax=None):
"""
first axis: conf; second axis: proximity
data: [samples, dim_features] (in our case dim_features=2)
"""
data_mirror1 = mirror_1d_along_axis(data, axis=0, xmin=xmin, xmax=xmax)
data_mirror2 = mirror_1d_along_axis(data_mirror1, axis=1, xmin=ymin, xmax=ymax)
return data_mirror2
class CustomizedDensityRatioCalibration():
"""customized version """
def __init__(self, kernel, kernel_func, mirror=False, bandwidth=0.1, norm=2):
self.kernel = kernel
self.kernel_func = kernel_func
self.bandwidth = bandwidth
self.norm = norm
self.mirror = mirror
# Find the temperature
def fit(self, logits, preds, true, proximity, is_conf=True):
"""
Train the density estimator for correctly classified samples and misclassified samples
1. split samples into correctly classified and misclassified
3. count their numbers and show the ratio
4. learn the conditional distribution of the confidence given the proximity -> the distributions are represented as <dens_true, dens_false>
5. Compute acc_ratio = p(correct=false, d) / p(correct=true, d) for every bins -> samples in the bins are assigned the corresponding acc_ratio
6. Compute the calibration score for each sample
Params:
if is_conf == true:
logits: the output from neural network for each class (shape [samples, classes])
else:
logits: confidence scores (shape [samples,])
preds: the predicted class for each sample (shape [samples, ])
true: true labels (shape [samples,])
proximity: the exponential function of the negative average distance to K nearest neighbors (shape [samples,])
Returns:
None
"""
# if is_conf == true, think the logits are actually confidences; otherwise compute confidence scores
if is_conf:
confs = logits
else:
confs = np.max(softmax(logits, axis=-1), axis=-1) # TODO
val_df = pd.DataFrame({'ys':true, 'proximity':proximity, 'conf':confs, 'pred':preds})
val_df['correct'] = (val_df.pred == val_df.ys).astype('int')
val_df_true = val_df[val_df['correct'] == 1]
val_df_false = val_df[val_df['correct'] == 0]
# mirror the data to avoid boundary effects
low_bound = 0.0
up_bound = 1.0
true_proximity = val_df_true['proximity'].to_numpy()
true_conf = val_df_true['conf'].to_numpy()
true_data = np.array([true_conf, true_proximity]).T
false_proximity = val_df_false['proximity'].to_numpy()
false_conf = val_df_false['conf'].to_numpy()
false_data = np.array([false_conf, false_proximity]).T # (n_samples, n_features)
if self.mirror:
true_data = mirror_2d(true_data, xmin=low_bound, xmax=up_bound, ymin=0.0, ymax=None)
false_data = mirror_2d(false_data, xmin=low_bound, xmax=up_bound, ymin=0.0, ymax=None)
if self.kernel == 'scipy_gaussian_kde':
# data shape: shape (# of dims, # of data).
self.dens_true = gaussian_kde(true_data.T, bw_method=self.bandwidth)
self.dens_false = gaussian_kde(false_data.T, bw_method=self.bandwidth)
elif self.kernel == 'sklearn_kde':
# (n_samples, n_features)
self.dens_true = KernelDensity(kernel=self.kernel_func, bandwidth=self.bandwidth).fit(true_data)
self.dens_false = KernelDensity(kernel=self.kernel_func, bandwidth=self.bandwidth).fit(false_data)
# elif self.kernel == 'NaiveKDE':
# # true_data shape: (num_samples, 2)
# self.dens_true = kde.NaiveKDE(kernel=self.kernel_func, bw=self.bandwidth, norm=self.norm).fit(true_data)
# self.dens_false = kde.NaiveKDE(kernel=self.kernel_func, bw=self.bandwidth, norm=self.norm).fit(false_data)
# elif self.kernel == 'FFTKDE':
# # true_data shape: (num_samples, 2)
# self.dens_true = kde.FFTKDE(kernel=self.kernel_func, bw=self.bandwidth, norm=self.norm).fit(true_data)
# self.dens_false = kde.FFTKDE(kernel=self.kernel_func, bw=self.bandwidth, norm=self.norm).fit(false_data)
elif self.kernel == 'KDEMultivariate':
self.dens_true = sm.nonparametric.KDEMultivariate(data=true_data, var_type='cc', bw=self.bandwidth)
self.dens_false = sm.nonparametric.KDEMultivariate(data=false_data, var_type='cc', bw=self.bandwidth)
else:
raise NotImplementedError
self.false_true_ratio = (val_df.pred != val_df.ys).sum() / (val_df.pred == val_df.ys).sum()
self.get_bw()
print('Density Estimation Done.')
def dens_true_pdf(self, logits, proximities, is_conf=True):
"""get the pdf for correctly classified samples """
if is_conf:
confs = logits
else:
confs = np.max(softmax(logits, axis=-1), axis=-1)
data = np.array([confs, proximities]).T
if self.kernel in ['NaiveKDE', 'TreeKDE', 'FFTKDE']:
# Sort the data in one of the dimensions
sorted_indices = np.argsort(data[:, 0])
sorted_data = data[sorted_indices, :]
conf_reg_true = self.dens_true.evaluate(sorted_data)
conf_reg_false = self.dens_false.evaluate(sorted_data)
# Use the original indices to sort the density values back into the original order
conf_reg_true = conf_reg_true[np.argsort(sorted_indices), :]
conf_reg_false = conf_reg_false[np.argsort(sorted_indices), :]
elif self.kernel == 'KDEMultivariate':
conf_reg_true = self.dens_true.pdf(data)
# conf_reg_false = self.dens_false.pdf(data)
elif self.kernel == 'scipy_gaussian_kde':
conf_reg_true = self.dens_true.pdf(data.T)
# conf_reg_false = self.dens_false.pdf(data.T)
elif self.kernel == 'sklearn_kde':
conf_reg_true = np.exp(self.dens_true.score_samples(data))
# conf_reg_false = self.dens_false.score_samples(data)
else:
raise NotImplementedError
if self.mirror:
conf_reg_true[confs<0.0] = 0 # Set the KDE to zero outside of the domain
conf_reg_true[confs>1.0] = 0
# conf_reg_false[confs<0.0] = 0
# conf_reg_false[confs>1.0] = 0
# # Double the density to get integral of ~1 -> two dimension 4
conf_reg_true = conf_reg_true * 4
# conf_reg_false = conf_reg_false * 4
return conf_reg_true
def dens_false_pdf(self, logits, proximities, is_conf=True):
"""get the pdf for correctly classified samples """
if is_conf:
confs = logits
else:
confs = np.max(softmax(logits, axis=-1), axis=-1)
data = np.array([confs, proximities]).T
if self.kernel in ['NaiveKDE', 'TreeKDE', 'FFTKDE']:
# Sort the data in one of the dimensions
sorted_indices = np.argsort(data[:, 0])
sorted_data = data[sorted_indices, :]
conf_reg_true = self.dens_true.evaluate(sorted_data)
conf_reg_false = self.dens_false.evaluate(sorted_data)
# Use the original indices to sort the density values back into the original order
conf_reg_true = conf_reg_true[np.argsort(sorted_indices), :]
conf_reg_false = conf_reg_false[np.argsort(sorted_indices), :]
elif self.kernel == 'KDEMultivariate':
# conf_reg_true = self.dens_true.pdf(data)
conf_reg_false = self.dens_false.pdf(data)
elif self.kernel == 'scipy_gaussian_kde':
# conf_reg_true = self.dens_true.pdf(data.T)
conf_reg_false = self.dens_false.pdf(data.T)
elif self.kernel == 'sklearn_kde':
# conf_reg_true = self.dens_true.score_samples(data)
conf_reg_false = np.exp(self.dens_false.score_samples(data))
else:
raise NotImplementedError
if self.mirror:
# conf_reg_true[confs<0.0] = 0 # Set the KDE to zero outside of the domain
# conf_reg_true[confs>1.0] = 0
conf_reg_false[confs<0.0] = 0
conf_reg_false[confs>1.0] = 0
# # Double the density to get integral of ~1 -> two dimension 4
# conf_reg_true = conf_reg_true * 4
conf_reg_false = conf_reg_false * 4
return conf_reg_false
def get_bw(self):
if self.kernel in ['KDEMultivariate', 'FFTKDE', 'NaiveKDE']:
self.dens_true_bw = self.dens_true.bw
self.dens_false_bw = self.dens_false.bw
elif self.kernel == 'scipy_gaussian_kde':
self.dens_true_bw = self.dens_true.factor
self.dens_false_bw = self.dens_false.factor
elif self.kernel == 'sklearn_kde':
self.dens_true_bw = self.dens_true.bandwidth
self.dens_false_bw = self.dens_false.bandwidth
def predict(self, logits, proximities, is_conf=True):
"""
use Bayes' rule to compute the posterior probability
p(\hat{y}=y | h(x), d)=\frac{p(h(x), d|\hat{y}=y)} {p(h(x), d|\hat{y}=y) + p(h(x), d|\hat{y} \neq y) \cdot \frac{p(\hat{y} \neq y)}{p(\hat{y}=y)}}
Params:
if is_conf == true:
logits: the output from neural network for each class (shape [samples, classes])
else:
logits: confidence scores (shape [samples,])
preds: the predicted class for each sample (shape [samples, ])
true: true labels (shape [samples,])
proximity: the exponential function of the negative average distance to K nearest neighbors (shape [samples,])
Returns:
calibrated probabilities (nd.array with shape [samples,])
"""
# if is_conf == true, think the logits are actually confidences; otherwise compute confidence scores
if is_conf:
confs = logits
else:
confs = np.max(softmax(logits, axis=-1), axis=-1)
data = np.array([confs, proximities]).T
if self.kernel in ['NaiveKDE', 'TreeKDE', 'FFTKDE']:
# Sort the data in one of the dimensions
sorted_indices = np.argsort(data[:, 0])
sorted_data = data[sorted_indices, :]
conf_reg_true = self.dens_true.evaluate(sorted_data)
conf_reg_false = self.dens_false.evaluate(sorted_data)
# Use the original indices to sort the density values back into the original order
conf_reg_true = conf_reg_true[np.argsort(sorted_indices), :]
conf_reg_false = conf_reg_false[np.argsort(sorted_indices), :]
elif self.kernel == 'KDEMultivariate':
conf_reg_true = self.dens_true.pdf(data)
conf_reg_false = self.dens_false.pdf(data)
elif self.kernel == 'scipy_gaussian_kde':
conf_reg_true = self.dens_true.pdf(data.T)
conf_reg_false = self.dens_false.pdf(data.T)
elif self.kernel == 'sklearn_kde':
conf_reg_true = np.exp(self.dens_true.score_samples(data))
conf_reg_false = np.exp(self.dens_false.score_samples(data))
else:
raise NotImplementedError
if self.mirror:
conf_reg_true[confs<0.0] = 0 # Set the KDE to zero outside of the domain
conf_reg_true[confs>1.0] = 0
conf_reg_false[confs<0.0] = 0
conf_reg_false[confs>1.0] = 0
# # Double the density to get integral of ~1 -> two dimension 4
conf_reg_true = conf_reg_true * 4
conf_reg_false = conf_reg_false * 4
# eps is to avoid division by 0
eps = 1e-10
conf_calibrated = conf_reg_true / np.maximum(conf_reg_true + conf_reg_false * self.false_true_ratio, eps)
probs = softmax(logits, axis=-1)
preds = np.argmax(probs, axis=-1)
# Normalize the rest of the values in each row to sum to 1-conf_max
mask = np.ones(probs.shape, dtype=bool)
mask[range(probs.shape[0]), preds] = False
probs = probs * mask
probs = probs * ((1 - conf_calibrated) / probs.sum(axis=-1))[:, np.newaxis]
# Add the calibrated confidence to the predicted class
probs[range(probs.shape[0]), preds] = conf_calibrated # dtype64 -> dtype32
return probs