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

Word Information Lost and Preserved - ASR metrics #630

Merged
merged 29 commits into from
Nov 24, 2021
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
fbb6648
Word Information Lost and Preserved
mahinlma Nov 20, 2021
fdde1e2
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 20, 2021
78e3df3
Update CHANGELOG.md
mahinlma Nov 20, 2021
a0bb9a3
Merge branch 'master' of https://github.com/mahinlma/metrics
mahinlma Nov 20, 2021
9758719
issue fix
mahinlma Nov 20, 2021
b05ab0e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 20, 2021
020e2f0
Update test_wip.py
mahinlma Nov 20, 2021
b40da0a
Merge branch 'master' of https://github.com/mahinlma/metrics
mahinlma Nov 20, 2021
abcdecc
Update torchmetrics/text/wil.py
mahinlma Nov 23, 2021
b02e826
Update torchmetrics/text/wip.py
mahinlma Nov 23, 2021
e5d7a1c
edit distance updated
mahinlma Nov 23, 2021
d52068f
Merge branch 'master' of https://github.com/mahinlma/metrics
mahinlma Nov 23, 2021
419738a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 23, 2021
9b0cdfa
issue fix
mahinlma Nov 23, 2021
0b06b3f
Merge branch 'master' of https://github.com/mahinlma/metrics
mahinlma Nov 23, 2021
e968bb7
Merge branch 'master' into master
Borda Nov 23, 2021
d89b2be
update
mahinlma Nov 24, 2021
a912eb1
test update
mahinlma Nov 24, 2021
542aa65
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 24, 2021
d4e2c37
removed unused imports
mahinlma Nov 24, 2021
8564d6f
Merge branch 'master' of https://github.com/mahinlma/metrics
mahinlma Nov 24, 2021
f228ad0
Apply suggestions from code review
SkafteNicki Nov 24, 2021
121effd
Merge branch 'master' into master
Borda Nov 24, 2021
a274e64
Apply suggestions from code review
Borda Nov 24, 2021
0bab613
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 24, 2021
255ff7f
Merge branch 'master' into master
Borda Nov 24, 2021
0137345
Merge branch 'master' into master
mergify[bot] Nov 24, 2021
e6d5470
Update torchmetrics/functional/text/wip.py
SkafteNicki Nov 24, 2021
ebd2f8a
Update wip.py
mahinlma Nov 24, 2021
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 @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Added NLP metrics:
- `MatchErrorRate` ([#619](https://github.com/PyTorchLightning/metrics/pull/619))
- `WordInfoLost` and `WordInfoPreserved` ([#630](https://github.com/PyTorchLightning/metrics/pull/630))


- Added `MinMaxMetric` to wrappers ([#556](https://github.com/PyTorchLightning/metrics/pull/556))
Expand Down
12 changes: 12 additions & 0 deletions docs/source/references/functional.rst
Original file line number Diff line number Diff line change
Expand Up @@ -463,3 +463,15 @@ wer [func]

.. autofunction:: torchmetrics.functional.wer
:noindex:

word_information_lost [func]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~

.. autofunction:: torchmetrics.functional.word_information_lost
:noindex:

word_information_preserved [func]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Borda marked this conversation as resolved.
Show resolved Hide resolved

.. autofunction:: torchmetrics.functional.word_information_preserved
:noindex:
12 changes: 12 additions & 0 deletions docs/source/references/modules.rst
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,18 @@ WER
.. autoclass:: torchmetrics.WER
:noindex:

WordInfoLost
~~~~~~~~~~~~

.. autoclass:: torchmetrics.WordInfoLost
:noindex:

WordInfoPreserved
~~~~~~~~~~~~~~~~~

.. autoclass:: torchmetrics.WordInfoPreserved
:noindex:


********
Wrappers
Expand Down
70 changes: 70 additions & 0 deletions tests/text/test_wil.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
from typing import List, Union

import pytest
from jiwer import wil

from tests.text.helpers import INPUT_ORDER, TextTester
from torchmetrics.functional.text.wil import word_information_lost
from torchmetrics.text.wil import WordInfoLost
from torchmetrics.utilities.imports import _JIWER_AVAILABLE

BATCHES_1 = {"preds": [["hello world"], ["what a day"]], "targets": [["hello world"], ["what a wonderful day"]]}

BATCHES_2 = {
"preds": [
["i like python", "what you mean or swallow"],
["hello duck", "i like python"],
],
"targets": [
["i like monthy python", "what do you mean, african or european swallow"],
["hello world", "i like monthy python"],
],
}


def _compute_wil_metric_jiwer(prediction: Union[str, List[str]], reference: Union[str, List[str]]):
return wil(reference, prediction)


@pytest.mark.skipif(not _JIWER_AVAILABLE, reason="test requires jiwer")
@pytest.mark.parametrize(
["preds", "targets"],
[
pytest.param(BATCHES_1["preds"], BATCHES_1["targets"]),
pytest.param(BATCHES_2["preds"], BATCHES_2["targets"]),
Borda marked this conversation as resolved.
Show resolved Hide resolved
],
)
class TestWordInfoLost(TextTester):
@pytest.mark.parametrize("ddp", [False, True])
@pytest.mark.parametrize("dist_sync_on_step", [False, True])
def test_wil_class(self, ddp, dist_sync_on_step, preds, targets):

self.run_class_metric_test(
ddp=ddp,
preds=preds,
targets=targets,
metric_class=WordInfoLost,
sk_metric=_compute_wil_metric_jiwer,
dist_sync_on_step=dist_sync_on_step,
input_order=INPUT_ORDER.PREDS_FIRST,
)

def test_wil_functional(self, preds, targets):

self.run_functional_metric_test(
preds,
targets,
metric_functional=word_information_lost,
sk_metric=_compute_wil_metric_jiwer,
input_order=INPUT_ORDER.PREDS_FIRST,
)

def test_wil_differentiability(self, preds, targets):

self.run_differentiability_test(
preds=preds,
targets=targets,
metric_module=WordInfoLost,
metric_functional=word_information_lost,
input_order=INPUT_ORDER.PREDS_FIRST,
)
70 changes: 70 additions & 0 deletions tests/text/test_wip.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
from typing import List, Union

import pytest
from jiwer import wip

from tests.text.helpers import INPUT_ORDER, TextTester
from torchmetrics.functional.text.wip import word_information_preserved
from torchmetrics.text.wip import WordInfoPreserved
from torchmetrics.utilities.imports import _JIWER_AVAILABLE

BATCHES_1 = {"preds": [["hello world"], ["what a day"]], "targets": [["hello world"], ["what a wonderful day"]]}

BATCHES_2 = {
"preds": [
["i like python", "what you mean or swallow"],
["hello duck", "i like python"],
],
"targets": [
["i like monthy python", "what do you mean, african or european swallow"],
["hello world", "i like monthy python"],
],
}


def _compute_wip_metric_jiwer(prediction: Union[str, List[str]], reference: Union[str, List[str]]):
return wip(reference, prediction)


@pytest.mark.skipif(not _JIWER_AVAILABLE, reason="test requires jiwer")
@pytest.mark.parametrize(
["preds", "targets"],
[
pytest.param(BATCHES_1["preds"], BATCHES_1["targets"]),
pytest.param(BATCHES_2["preds"], BATCHES_2["targets"]),
Borda marked this conversation as resolved.
Show resolved Hide resolved
],
)
class TestWordInfoPreserved(TextTester):
@pytest.mark.parametrize("ddp", [False, True])
@pytest.mark.parametrize("dist_sync_on_step", [False, True])
def test_wip_class(self, ddp, dist_sync_on_step, preds, targets):

self.run_class_metric_test(
ddp=ddp,
preds=preds,
targets=targets,
metric_class=WordInfoPreserved,
sk_metric=_compute_wip_metric_jiwer,
dist_sync_on_step=dist_sync_on_step,
input_order=INPUT_ORDER.PREDS_FIRST,
)

def test_wip_functional(self, preds, targets):

self.run_functional_metric_test(
preds,
targets,
metric_functional=word_information_preserved,
sk_metric=_compute_wip_metric_jiwer,
input_order=INPUT_ORDER.PREDS_FIRST,
)

def test_wip_differentiability(self, preds, targets):

self.run_differentiability_test(
preds=preds,
targets=targets,
metric_module=WordInfoPreserved,
metric_functional=word_information_preserved,
input_order=INPUT_ORDER.PREDS_FIRST,
)
4 changes: 4 additions & 0 deletions torchmetrics/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@
MatchErrorRate,
ROUGEScore,
SacreBLEUScore,
WordInfoLost,
WordInfoPreserved,
)
from torchmetrics.wrappers import BootStrapper, MetricTracker, MinMaxMetric, MultioutputWrapper # noqa: E402

Expand Down Expand Up @@ -152,4 +154,6 @@
"WER",
"CharErrorRate",
"MatchErrorRate",
"WordInfoLost",
"WordInfoPreserved",
]
4 changes: 4 additions & 0 deletions torchmetrics/functional/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@
from torchmetrics.functional.text.rouge import rouge_score
from torchmetrics.functional.text.sacre_bleu import sacre_bleu_score
from torchmetrics.functional.text.wer import wer
from torchmetrics.functional.text.wil import word_information_lost
from torchmetrics.functional.text.wip import word_information_preserved

__all__ = [
"accuracy",
Expand Down Expand Up @@ -137,4 +139,6 @@
"wer",
"char_error_rate",
"match_error_rate",
"word_information_lost",
"word_information_preserved",
]
2 changes: 2 additions & 0 deletions torchmetrics/functional/text/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@
from torchmetrics.functional.text.mer import match_error_rate # noqa: F401
from torchmetrics.functional.text.sacre_bleu import sacre_bleu_score # noqa: F401
from torchmetrics.functional.text.wer import wer # noqa: F401
from torchmetrics.functional.text.wil import word_information_lost # noqa: F401
from torchmetrics.functional.text.wip import word_information_preserved # noqa: F401
31 changes: 5 additions & 26 deletions torchmetrics/functional/text/cer.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,7 @@
import torch
from torch import Tensor, tensor


def _edit_distance(prediction_tokens: List[str], reference_tokens: List[str]) -> int:
"""Standard dynamic programming algorithm to compute the edit distance.

Args:
prediction_tokens: A tokenized predicted sentence
reference_tokens: A tokenized reference sentence
Returns:
(int) Edit distance between the predicted sentence and the reference sentence
"""
dp = [[0] * (len(reference_tokens) + 1) for _ in range(len(prediction_tokens) + 1)]
for i in range(len(prediction_tokens) + 1):
dp[i][0] = i
for j in range(len(reference_tokens) + 1):
dp[0][j] = j
for i in range(1, len(prediction_tokens) + 1):
for j in range(1, len(reference_tokens) + 1):
if prediction_tokens[i - 1] == reference_tokens[j - 1]:
dp[i][j] = dp[i - 1][j - 1]
else:
dp[i][j] = min(dp[i - 1][j], dp[i][j - 1], dp[i - 1][j - 1]) + 1
return dp[-1][-1]
from torchmetrics.functional.text.helper import _edit_distance


def _cer_update(
Expand All @@ -51,8 +30,8 @@ def _cer_update(
predictions: Transcription(s) to score as a string or list of strings
references: Reference(s) for each speech input as a string or list of strings
Returns:
(Tensor) Number of edit operations to get from the reference to the prediction, summed over all samples
(Tensor) Number of character over all references
Number of edit operations to get from the reference to the prediction, summed over all samples
Number of character overall references
"""
if isinstance(predictions, str):
predictions = [predictions]
Expand All @@ -75,7 +54,7 @@ def _cer_compute(errors: Tensor, total: Tensor) -> Tensor:
errors: Number of edit operations to get from the reference to the prediction, summed over all samples
total: Number of characters over all references
Returns:
(Tensor) Character error rate
Character error rate score
"""
return errors / total

Expand All @@ -91,7 +70,7 @@ def char_error_rate(
predictions: Transcription(s) to score as a string or list of strings
references: Reference(s) for each speech input as a string or list of strings
Returns:
(Tensor) Character error rate
Character error rate score
Examples:
>>> predictions = ["this is the prediction", "there is an other sample"]
>>> references = ["this is the reference", "there is another one"]
Expand Down
38 changes: 38 additions & 0 deletions torchmetrics/functional/text/helper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Copyright The PyTorch Lightning team.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from typing import List


def _edit_distance(prediction_tokens: List[str], reference_tokens: List[str]) -> int:
"""Standard dynamic programming algorithm to compute the edit distance.

Args:
prediction_tokens: A tokenized predicted sentence
reference_tokens: A tokenized reference sentence
Returns:
(int) Edit distance between the predicted sentence and the reference sentence
Borda marked this conversation as resolved.
Show resolved Hide resolved
"""
dp = [[0] * (len(reference_tokens) + 1) for _ in range(len(prediction_tokens) + 1)]
for i in range(len(prediction_tokens) + 1):
dp[i][0] = i
for j in range(len(reference_tokens) + 1):
dp[0][j] = j
for i in range(1, len(prediction_tokens) + 1):
for j in range(1, len(reference_tokens) + 1):
if prediction_tokens[i - 1] == reference_tokens[j - 1]:
dp[i][j] = dp[i - 1][j - 1]
else:
dp[i][j] = min(dp[i - 1][j], dp[i][j - 1], dp[i - 1][j - 1]) + 1
return dp[-1][-1]
29 changes: 4 additions & 25 deletions torchmetrics/functional/text/mer.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,7 @@
import torch
from torch import Tensor, tensor


def _edit_distance(prediction_tokens: List[str], reference_tokens: List[str]) -> int:
"""Standard dynamic programming algorithm to compute the edit distance.

Args:
prediction_tokens: A tokenized predicted sentence
reference_tokens: A tokenized reference sentence

Returns:
Editing distance between the predicted sentence and the reference sentence
"""
dp = [[0] * (len(reference_tokens) + 1) for _ in range(len(prediction_tokens) + 1)]
dp[:][0] = list(range(len(prediction_tokens) + 1))
dp[0][:] = list(range(len(reference_tokens) + 1))
for i in range(1, len(prediction_tokens) + 1):
for j in range(1, len(reference_tokens) + 1):
if prediction_tokens[i - 1] == reference_tokens[j - 1]:
dp[i][j] = dp[i - 1][j - 1]
else:
dp[i][j] = min(dp[i - 1][j], dp[i][j - 1], dp[i - 1][j - 1]) + 1
return dp[-1][-1]
from torchmetrics.functional.text.helper import _edit_distance


def _mer_update(
Expand All @@ -52,7 +32,7 @@ def _mer_update(

Returns:
Number of edit operations to get from the reference to the prediction, summed over all samples
Number of words over all references
Number of words overall references
"""
if isinstance(predictions, str):
predictions = [predictions]
Expand All @@ -74,10 +54,10 @@ def _mer_compute(errors: Tensor, total: Tensor) -> Tensor:

Args:
errors: Number of edit operations to get from the reference to the prediction, summed over all samples
total: Number of words over all references
total: Number of words overall references

Returns:
(Tensor) Match error rate
Match error rate score
"""
return errors / total

Expand All @@ -94,7 +74,6 @@ def match_error_rate(
predictions: Transcription(s) to score as a string or list of strings
references: Reference(s) for each speech input as a string or list of strings


Returns:
Match error rate score

Expand Down
Loading