-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathreliability_diagrams.py
280 lines (220 loc) · 11 KB
/
reliability_diagrams.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
import os
import numpy as np
import matplotlib.pyplot as plt
def compute_calibration(true_labels, pred_labels, confidences, num_bins=10):
"""Collects predictions into bins used to draw a reliability diagram.
Arguments:
true_labels: the true labels for the test examples
pred_labels: the predicted labels for the test examples
confidences: the predicted confidences for the test examples
num_bins: number of bins
The true_labels, pred_labels, confidences arguments must be NumPy arrays;
pred_labels and true_labels may contain numeric or string labels.
For a multi-class model, the predicted label and confidence should be those
of the highest scoring class.
Returns a dictionary containing the following NumPy arrays:
accuracies: the average accuracy for each bin
confidences: the average confidence for each bin
counts: the number of examples in each bin
bins: the confidence thresholds for each bin
avg_accuracy: the accuracy over the entire test set
avg_confidence: the average confidence over the entire test set
expected_calibration_error: a weighted average of all calibration gaps
max_calibration_error: the largest calibration gap across all bins
"""
assert(len(confidences) == len(pred_labels))
assert(len(confidences) == len(true_labels))
assert(num_bins > 0)
bin_size = 1.0 / num_bins
bins = np.linspace(0.0, 1.0, num_bins + 1)
indices = np.digitize(confidences, bins, right=True)
bin_accuracies = np.zeros(num_bins, dtype=np.float)
bin_confidences = np.zeros(num_bins, dtype=np.float)
bin_counts = np.zeros(num_bins, dtype=np.int)
for b in range(num_bins):
selected = np.where(indices == b + 1)[0]
if len(selected) > 0:
bin_accuracies[b] = np.mean(true_labels[selected] == pred_labels[selected])
bin_confidences[b] = np.mean(confidences[selected])
bin_counts[b] = len(selected)
avg_acc = np.sum(bin_accuracies * bin_counts) / np.sum(bin_counts)
avg_conf = np.sum(bin_confidences * bin_counts) / np.sum(bin_counts)
gaps = np.abs(bin_accuracies - bin_confidences)
ece = np.sum(gaps * bin_counts) / np.sum(bin_counts)
mce = np.max(gaps)
return { "accuracies": bin_accuracies,
"confidences": bin_confidences,
"counts": bin_counts,
"bins": bins,
"avg_accuracy": avg_acc,
"avg_confidence": avg_conf,
"expected_calibration_error": ece,
"max_calibration_error": mce }
def _reliability_diagram_subplot(ax, bin_data,
draw_ece=True,
draw_bin_importance=False,
title="Reliability Diagram",
xlabel="Confidence",
ylabel="Expected Accuracy"):
"""Draws a reliability diagram into a subplot."""
accuracies = bin_data["accuracies"]
confidences = bin_data["confidences"]
counts = bin_data["counts"]
bins = bin_data["bins"]
bin_size = 1.0 / len(counts)
positions = bins[:-1] + bin_size/2.0
widths = bin_size
alphas = 0.3
min_count = np.min(counts)
max_count = np.max(counts)
normalized_counts = (counts - min_count) / (max_count - min_count)
if draw_bin_importance == "alpha":
alphas = 0.2 + 0.8*normalized_counts
elif draw_bin_importance == "width":
widths = 0.1*bin_size + 0.9*bin_size*normalized_counts
colors = np.zeros((len(counts), 4))
colors[:, 0] = 240 / 255.
colors[:, 1] = 60 / 255.
colors[:, 2] = 60 / 255.
colors[:, 3] = alphas
gap_plt = ax.bar(positions, np.abs(accuracies - confidences),
bottom=np.minimum(accuracies, confidences), width=widths,
edgecolor=colors, color=colors, linewidth=1, label="Gap")
acc_plt = ax.bar(positions, 0, bottom=accuracies, width=widths,
edgecolor="black", color="black", alpha=1.0, linewidth=3,
label="Accuracy")
ax.set_aspect("equal")
ax.plot([0,1], [0,1], linestyle = "--", color="gray")
if draw_ece:
ece = (bin_data["expected_calibration_error"] * 100)
ax.text(0.98, 0.02, "ECE=%.2f" % ece, color="black",
ha="right", va="bottom", transform=ax.transAxes)
ax.set_xlim(0, 1)
ax.set_ylim(0, 1)
#ax.set_xticks(bins)
ax.set_title(title)
ax.set_xlabel(xlabel)
ax.set_ylabel(ylabel)
ax.legend(handles=[gap_plt, acc_plt])
def _confidence_histogram_subplot(ax, bin_data,
draw_averages=True,
title="Examples per bin",
xlabel="Confidence",
ylabel="Count"):
"""Draws a confidence histogram into a subplot."""
counts = bin_data["counts"]
bins = bin_data["bins"]
bin_size = 1.0 / len(counts)
positions = bins[:-1] + bin_size/2.0
ax.bar(positions, counts, width=bin_size * 0.9)
ax.set_xlim(0, 1)
ax.set_title(title)
ax.set_xlabel(xlabel)
ax.set_ylabel(ylabel)
if draw_averages:
acc_plt = ax.axvline(x=bin_data["avg_accuracy"], ls="solid", lw=3,
c="black", label="Accuracy")
conf_plt = ax.axvline(x=bin_data["avg_confidence"], ls="dotted", lw=3,
c="#444", label="Avg. confidence")
ax.legend(handles=[acc_plt, conf_plt])
def _reliability_diagram_combined(bin_data,
draw_ece, draw_bin_importance, draw_averages,
title, figsize, dpi, return_fig):
"""Draws a reliability diagram and confidence histogram using the output
from compute_calibration()."""
figsize = (figsize[0], figsize[0] * 1.4)
fig, ax = plt.subplots(nrows=2, ncols=1, sharex=True, figsize=figsize, dpi=dpi,
gridspec_kw={"height_ratios": [4, 1]})
plt.tight_layout()
plt.subplots_adjust(hspace=-0.1)
_reliability_diagram_subplot(ax[0], bin_data, draw_ece, draw_bin_importance,
title=title, xlabel="")
# Draw the confidence histogram upside down.
orig_counts = bin_data["counts"]
bin_data["counts"] = -bin_data["counts"]
_confidence_histogram_subplot(ax[1], bin_data, draw_averages, title="")
bin_data["counts"] = orig_counts
# Also negate the ticks for the upside-down histogram.
new_ticks = np.abs(ax[1].get_yticks()).astype(np.int)
ax[1].set_yticklabels(new_ticks)
plt.show()
if return_fig: return fig
def reliability_diagram(true_labels, pred_labels, confidences, num_bins=10,
draw_ece=True, draw_bin_importance=False,
draw_averages=True, title="Reliability Diagram",
figsize=(6, 6), dpi=72, return_fig=False):
"""Draws a reliability diagram and confidence histogram in a single plot.
First, the model's predictions are divided up into bins based on their
confidence scores.
The reliability diagram shows the gap between average accuracy and average
confidence in each bin. These are the red bars.
The black line is the accuracy, the other end of the bar is the confidence.
Ideally, there is no gap and the black line is on the dotted diagonal.
In that case, the model is properly calibrated and we can interpret the
confidence scores as probabilities.
The confidence histogram visualizes how many examples are in each bin.
This is useful for judging how much each bin contributes to the calibration
error.
The confidence histogram also shows the overall accuracy and confidence.
The closer these two lines are together, the better the calibration.
The ECE or Expected Calibration Error is a summary statistic that gives the
difference in expectation between confidence and accuracy. In other words,
it's a weighted average of the gaps across all bins. A lower ECE is better.
Arguments:
true_labels: the true labels for the test examples
pred_labels: the predicted labels for the test examples
confidences: the predicted confidences for the test examples
num_bins: number of bins
draw_ece: whether to include the Expected Calibration Error
draw_bin_importance: whether to represent how much each bin contributes
to the total accuracy: False, "alpha", "widths"
draw_averages: whether to draw the overall accuracy and confidence in
the confidence histogram
title: optional title for the plot
figsize: setting for matplotlib; height is ignored
dpi: setting for matplotlib
return_fig: if True, returns the matplotlib Figure object
"""
bin_data = compute_calibration(true_labels, pred_labels, confidences, num_bins)
return _reliability_diagram_combined(bin_data, draw_ece, draw_bin_importance,
draw_averages, title, figsize=figsize,
dpi=dpi, return_fig=return_fig)
def reliability_diagrams(results, num_bins=10,
draw_ece=True, draw_bin_importance=False,
num_cols=4, dpi=72, return_fig=False):
"""Draws reliability diagrams for one or more models.
Arguments:
results: dictionary where the key is the model name and the value is
a dictionary containing the true labels, predicated labels, and
confidences for this model
num_bins: number of bins
draw_ece: whether to include the Expected Calibration Error
draw_bin_importance: whether to represent how much each bin contributes
to the total accuracy: False, "alpha", "widths"
num_cols: how wide to make the plot
dpi: setting for matplotlib
return_fig: if True, returns the matplotlib Figure object
"""
ncols = num_cols
nrows = (len(results) + ncols - 1) // ncols
figsize = (ncols * 4, nrows * 4)
fig, ax = plt.subplots(nrows=nrows, ncols=ncols, sharex=True, sharey=True,
figsize=figsize, dpi=dpi, constrained_layout=True)
for i, (plot_name, data) in enumerate(results.items()):
y_true = data["true_labels"]
y_pred = data["pred_labels"]
y_conf = data["confidences"]
bin_data = compute_calibration(y_true, y_pred, y_conf, num_bins)
row = i // ncols
col = i % ncols
_reliability_diagram_subplot(ax[row, col], bin_data, draw_ece,
draw_bin_importance,
title="\n".join(plot_name.split()),
xlabel="Confidence" if row == nrows - 1 else "",
ylabel="Expected Accuracy" if col == 0 else "")
for i in range(i + 1, nrows * ncols):
row = i // ncols
col = i % ncols
ax[row, col].axis("off")
plt.show()
if return_fig: return fig