Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve calibration error speed by replacing for loop #769

Merged
merged 26 commits into from
Jan 20, 2022
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
5523655
Improve speed by removing for loop and using bucketize + scatter_add.
Jan 17, 2022
b3a9362
device
Borda Jan 18, 2022
7fe4dcf
Merge branch 'master' into ce-speed
Borda Jan 18, 2022
a8cfeb6
Merge branch 'master' into ce-speed
mergify[bot] Jan 18, 2022
b866c96
Merge branch 'master' into ce-speed
mergify[bot] Jan 18, 2022
2b37933
Merge branch 'master' into ce-speed
Borda Jan 18, 2022
cad1cb9
Merge branch 'master' into ce-speed
Borda Jan 18, 2022
0848523
Remove deprecated functions, and warnings - Text (#773)
ashutoshml Jan 18, 2022
dd40465
Merge branch 'master' into ce-speed
mergify[bot] Jan 18, 2022
43a2261
Remove deprecated functions, and warnings - Text (#773)
ashutoshml Jan 18, 2022
eb9ed50
Merge branch 'master' into ce-speed
mergify[bot] Jan 18, 2022
cfe5e87
Fix Matthews correlation coefficient when the denominator is 0 (#781)
SkafteNicki Jan 19, 2022
a3b4606
Merge branch 'master' into ce-speed
mergify[bot] Jan 19, 2022
727c1fb
Merge branch 'master' into ce-speed
mergify[bot] Jan 19, 2022
ec75b14
fast and slow binning
SkafteNicki Jan 20, 2022
102d772
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jan 20, 2022
0163b3a
changelog
SkafteNicki Jan 20, 2022
b62ecd9
Merge branch 'ce-speed' of https://github.com/ramonemiliani93/metrics…
SkafteNicki Jan 20, 2022
0d16d5f
Merge branch 'master' into ce-speed
mergify[bot] Jan 20, 2022
b77b751
Apply suggestions from code review
Borda Jan 20, 2022
f6741ae
cleaning
Borda Jan 20, 2022
7ff78c4
flake8
Borda Jan 20, 2022
8f4ec57
increase to 1.8
SkafteNicki Jan 20, 2022
e5efbb6
Merge branch 'master' into ce-speed
Borda Jan 20, 2022
344e52a
Merge branch 'master' into ce-speed
mergify[bot] Jan 20, 2022
97be5af
chlog
Borda Jan 20, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed

- Use torch.bucketize in calibration error when torch>1.6 for faster computations ([#769](https://github.com/PyTorchLightning/metrics/pull/769))

### Deprecated

Expand Down
76 changes: 64 additions & 12 deletions torchmetrics/functional/classification/calibration_error.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,65 @@

from torchmetrics.utilities.checks import _input_format_classification
from torchmetrics.utilities.enums import DataType
from torchmetrics.utilities.imports import _TORCH_GREATER_EQUAL_1_6


def _slow_binning(
confidences: FloatTensor, accuracies: FloatTensor, bin_boundaries: FloatTensor
) -> Tuple[FloatTensor, FloatTensor, FloatTensor]:
"""
Compute calibration bins using for loops. Use for pytorch < 1.6
Args:
confidences (FloatTensor): The confidence (i.e. predicted prob) of the top1 prediction.
accuracies (FloatTensor): 1.0 if the top-1 prediction was correct, 0.0 otherwise.
bin_boundaries (FloatTensor): Bin boundaries separating the linspace from 0 to 1.

Returns:
tuple with binned accuracy, binned confidence and binned probabilities
"""
conf_bin = torch.zeros_like(bin_boundaries)
acc_bin = torch.zeros_like(bin_boundaries)
prop_bin = torch.zeros_like(bin_boundaries)
for i, (bin_lower, bin_upper) in enumerate(zip(bin_boundaries[:-1], bin_boundaries[1:])):
# Calculated confidence and 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:
acc_bin[i] = accuracies[in_bin].float().mean()
conf_bin[i] = confidences[in_bin].mean()
prop_bin[i] = prop_in_bin
return acc_bin, conf_bin, prop_bin


def _fast_binning(
confidences: FloatTensor, accuracies: FloatTensor, bin_boundaries: FloatTensor
) -> Tuple[FloatTensor, FloatTensor, FloatTensor]:
"""Compute calibration bins using torch.bucketize. Use for pytorch >= 1.6.

Args:
confidences (FloatTensor): The confidence (i.e. predicted prob) of the top1 prediction.
accuracies (FloatTensor): 1.0 if the top-1 prediction was correct, 0.0 otherwise.
bin_boundaries (FloatTensor): Bin boundaries separating the linspace from 0 to 1.

Returns:
tuple with binned accuracy, binned confidence and binned probabilities
"""
acc_bin = torch.zeros(len(bin_boundaries) - 1, device=confidences.device)
conf_bin = torch.zeros(len(bin_boundaries) - 1, device=confidences.device)
count_bin = torch.zeros(len(bin_boundaries) - 1, device=confidences.device)
Borda marked this conversation as resolved.
Show resolved Hide resolved

indices = torch.bucketize(confidences, bin_boundaries) - 1

count_bin.scatter_add_(dim=0, index=indices, src=torch.ones_like(confidences))

conf_bin.scatter_add_(dim=0, index=indices, src=confidences)
conf_bin = torch.nan_to_num(conf_bin / count_bin)

acc_bin.scatter_add_(dim=0, index=indices, src=accuracies)
acc_bin = torch.nan_to_num(acc_bin / count_bin)

prop_bin = count_bin / count_bin.sum()
return acc_bin, conf_bin, prop_bin


def _ce_compute(
Expand Down Expand Up @@ -46,17 +105,10 @@ def _ce_compute(
if norm not in {"l1", "l2", "max"}:
raise ValueError(f"Norm {norm} is not supported. Please select from l1, l2, or max. ")

conf_bin = torch.zeros_like(bin_boundaries)
acc_bin = torch.zeros_like(bin_boundaries)
prop_bin = torch.zeros_like(bin_boundaries)
for i, (bin_lower, bin_upper) in enumerate(zip(bin_boundaries[:-1], bin_boundaries[1:])):
# Calculated confidence and 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:
acc_bin[i] = accuracies[in_bin].float().mean()
conf_bin[i] = confidences[in_bin].mean()
prop_bin[i] = prop_in_bin
if _TORCH_GREATER_EQUAL_1_6:
acc_bin, conf_bin, prop_bin = _fast_binning(confidences, accuracies, bin_boundaries)
else:
acc_bin, conf_bin, prop_bin = _slow_binning(confidences, accuracies, bin_boundaries)

if norm == "l1":
ce = torch.sum(torch.abs(acc_bin - conf_bin) * prop_bin)
Expand Down Expand Up @@ -86,7 +138,7 @@ def _ce_update(preds: Tensor, target: Tensor) -> Tuple[FloatTensor, FloatTensor]
ValueError: If the dataset shape is not binary, multiclass, or multidimensional-multiclass.

Returns:
Tuple[FloatTensor, FloatTensor]: [description]
tuple with confidences and accuracies
"""
_, _, mode = _input_format_classification(preds, target)

Expand Down