Skip to main content

Python Framework to calibrate confidence estimates of classifiers like Neural Networks

Project description

Calibration Framework

Calibration framework in Python 3 for Neural Networks.

Copyright (C) 2019 Ruhr West University of Applied Sciences, Bottrop, Germany AND Visteon Electronics Germany GmbH, Kerpen, Germany

This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.

Overview

This framework is designed to calibrate the confidence estimates of classifiers like Neural Networks. Modern Neural Networks are likely to be overconfident with their predictions. However, reliable confidence estimates of such classifiers are crucial especially in safety-critical applications.

For example: given 100 predictions with a confidence of 80% of each prediction, the observed accuracy should also match 80% (neither more nor less). This behaviour is achievable with several calibration methods.

The framework is structured as follows:

netcal
  .binning         # binning methods
  .scaling         # scaling methods
  .regularization  # regularization methods
  .presentation    # presentation methods
  .metrics         # metrics for measuring miscalibration

examples           # example code snippets

Installation

The installation of the calibration suite is quite easy with setuptools. You can either install this framework using PIP:

pip3 install netcal

Or simply invoke the following command to install the calibration suite:

python3 setup.py install

Calibration Metrics

The most common metric to determine miscalibration is the Expected Calibration Error (ECE) [1]. This metric divides the confidence space into several bins and measures the observed accuracy in each bin. The bin gaps between observed accuracy and bin confidence are summed up and weighted by the amount of samples in each bin. The Maximum Calibration Error (MCE) denotes the highest gap over all bins. The Average Calibration Error (ACE) [11] denotes the average miscalibration where each bin gets weighted equally.

Another group are the regularization tools which are added to the loss during the training of a Neural Network.

Methods

The calibration methods are separated into binning and scaling methods. The binning methods divide the confidence space into several bins (like ECE) and perform calibration on each bin. The scaling methods scale the confidence estimates or logits directly to calibrated confidence estimates.

Most of the calibration methods are designed for binary classification tasks. Multi-class calibration is performed in “one vs. all” by default.

Some methods like “Isotonic Regression” utilize methods from the scikit-learn API [9].

Binning

Implemented binning methods are:

  • Histogram Binning [3], [4]

  • Isotonic Regression [4], [5]

  • Bayesian Binning into Quantiles (BBQ) [1]

  • Ensemble of Near Isotonic Regression (ENIR) [6]

Scaling

Implemented scaling methods are:

  • Logistic Calibration/Platt Scaling [10]

  • Temperature Scaling [7]

  • Beta Calibration [2]

Regularization

Implemented regularization methods are:

  • Confidence Penalty [8]

Visualization

For visualization of miscalibration, one can use a Confidence Histograms & Reliability Diagrams. These diagrams are similar to ECE, the output space is divided into equally spaced bins. The calibration gap between bin accuracy and bin confidence is visualized as a histogram.

Examples

The calibration methods work with the predicted confidence estimates of a Neural Network. This is a basic example which uses softmax predictions of a classification task with 10 classes and the given NumPy arrays:

ground_truth  # this is a NumPy 1-D array with ground truth digits between 0-9 - shape: (n_samples,)
confidences   # this is a NumPy 2-D array with confidence estimates between 0-1 - shape: (n_samples, n_classes)

This is an example for Temperature Scaling but also works for every calibration method (remind different constructor parameters):

import numpy as np
from netcal.scaling import TemperatureScaling

temperature = TemperatureScaling()
temperature.fit(confidences, ground_truth)
calibrated = temperature.transform(confidences)

The miscalibration can be determined with the ECE:

from netcal.metrics import ECE

n_bins = 10

ece = ECE(n_bins)
uncalibrated_score = ece.measure(confidences)
calibrated_score = ece.measure(calibrated)

The miscalibration can be visualized with a Reliability Diagram:

from netcal.presentation import ReliabilityDiagram

n_bins = 10

diagram = ReliabilityDiagram(n_bins)
diagram.plot(confidences, ground_truth)  # visualize miscalibration of uncalibrated
diagram.plot(calibrated, ground_truth)   # visualize miscalibration of calibrated

References

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

netcal-1.0.tar.gz (29.2 kB view details)

Uploaded Source

Built Distribution

netcal-1.0-py3-none-any.whl (55.2 kB view details)

Uploaded Python 3

File details

Details for the file netcal-1.0.tar.gz.

File metadata

  • Download URL: netcal-1.0.tar.gz
  • Upload date:
  • Size: 29.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/3.6.8

File hashes

Hashes for netcal-1.0.tar.gz
Algorithm Hash digest
SHA256 fcdc9594d327c78b3cbe4666179aa009dab36e2649b4765582dc30cf2309e82b
MD5 fa9eb394b70949b66b4e25aca7393f66
BLAKE2b-256 12e33f6cc2af101ecc12c7f44116213a8763b31749fcc844089431eb5e9c1266

See more details on using hashes here.

File details

Details for the file netcal-1.0-py3-none-any.whl.

File metadata

  • Download URL: netcal-1.0-py3-none-any.whl
  • Upload date:
  • Size: 55.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/3.6.8

File hashes

Hashes for netcal-1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 50640a08ca48c9cbcf9163f709ea264caf1c3dfc6d1efb59c30a1b3d3bb7f56f
MD5 8fdd2d30d58a7e53b30e6eeb2b9196dc
BLAKE2b-256 d73ce05c8dd950d856a81b48e88ceac1aef6dd5693f06fe52f6c9ceec2e6bf08

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page