Skip to content
This repository has been archived by the owner on Oct 9, 2023. It is now read-only.

Deprecate text metrics #648

Merged
merged 17 commits into from
Nov 9, 2021
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
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
4 changes: 2 additions & 2 deletions flash/core/utilities/imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@ def _compare_version(package: str, op, version) -> bool:
_TORCH_SPARSE_AVAILABLE = _module_available("torch_sparse")
_TORCH_GEOMETRIC_AVAILABLE = _module_available("torch_geometric")
_TORCHAUDIO_AVAILABLE = _module_available("torchaudio")
_ROUGE_SCORE_AVAILABLE = _module_available("rouge_score")
_SENTENCEPIECE_AVAILABLE = _module_available("sentencepiece")
_DATASETS_AVAILABLE = _module_available("datasets")
_TM_TEXT_AVAILABLE: bool = _module_available("torchmetrics.text")
_ICEVISION_AVAILABLE = _module_available("icevision")

if Version:
Expand All @@ -103,9 +103,9 @@ def _compare_version(package: str, op, version) -> bool:
_TEXT_AVAILABLE = all(
[
_TRANSFORMERS_AVAILABLE,
_ROUGE_SCORE_AVAILABLE,
_SENTENCEPIECE_AVAILABLE,
_DATASETS_AVAILABLE,
_TM_TEXT_AVAILABLE,
]
)
_TABULAR_AVAILABLE = _TABNET_AVAILABLE and _PANDAS_AVAILABLE
Expand Down
18 changes: 18 additions & 0 deletions flash/text/seq2seq/core/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
# Link: https://pytorch.org/text/_modules/torchtext/data/metrics.html#bleu_score
from collections import Counter
from typing import Dict, List, Tuple
from warnings import warn

import numpy as np
import torch
Expand Down Expand Up @@ -64,6 +65,9 @@ class BLEUScore(Metric):
>>> metric = BLEUScore()
>>> metric(translate_corpus, reference_corpus)
tensor(0.7598)

.. deprecated:: v0.5
Use :func:`torchmetrics.text.BLEUScore` instead. Will be removed in v0.6.
"""

def __init__(self, n_gram: int = 4, smooth: bool = False):
Expand All @@ -72,6 +76,12 @@ def __init__(self, n_gram: int = 4, smooth: bool = False):
n_gram: Gram value ranged from 1 to 4 (Default 4)
smooth: Whether or not to apply smoothing – Lin et al. 2004
"""
warn(
"Metric `text.seq2seq.core.metrics.BLEUScore` is deprecated in v0.5 and will be removed in v0.6."
" Use `torchmetrics.text.BLEUScore` instead.",
DeprecationWarning,
)

super().__init__()
self.n_gram = n_gram
self.smooth = smooth
Expand Down Expand Up @@ -151,6 +161,9 @@ class RougeMetric(Metric):
'rougeLsum_fmeasure': 0.25,
'rougeLsum_precision': 0.25,
'rougeLsum_recall': 0.25}

.. deprecated:: v0.5
Use :func:`torchmetrics.text.ROUGEScore` instead. Will be removed in v0.6.
"""

@requires_extras("text")
Expand All @@ -160,6 +173,11 @@ def __init__(
use_stemmer: bool = False,
rouge_keys: Tuple[str] = ("rouge1", "rouge2", "rougeL", "rougeLsum"),
):
warn(
"Metric `text.seq2seq.core.metrics.RougeScore` is deprecated in v0.5 and will be removed in v0.6."
" Use `torchmetrics.text.ROUGEScore` instead.",
DeprecationWarning,
)
super().__init__()

self.rouge_newline_sep = rouge_newline_sep
Expand Down
7 changes: 3 additions & 4 deletions flash/text/seq2seq/summarization/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@
from typing import Any, Callable, Dict, List, Mapping, Optional, Sequence, Type, Union

import torch
from torchmetrics import Metric
from torchmetrics import Metric, ROUGEScore

from flash.text.seq2seq.core.metrics import RougeMetric
from flash.text.seq2seq.core.model import Seq2SeqTask


Expand Down Expand Up @@ -66,8 +65,8 @@ def __init__(
val_target_max_length=val_target_max_length,
num_beams=num_beams,
)
self.rouge = RougeMetric(
rouge_newline_sep=rouge_newline_sep,
self.rouge = ROUGEScore(
newline_sep=rouge_newline_sep,
use_stemmer=use_stemmer,
)

Expand Down
3 changes: 1 addition & 2 deletions flash/text/seq2seq/translation/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@
from typing import Any, Callable, Dict, List, Mapping, Optional, Sequence, Type, Union

import torch
from torchmetrics import Metric
from torchmetrics import BLEUScore, Metric

from flash.text.seq2seq.core.metrics import BLEUScore
from flash.text.seq2seq.core.model import Seq2SeqTask


Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
packaging
torch
torchmetrics
torchmetrics>=0.5.0
pytorch-lightning>=1.4.0
pyDeprecate
pandas<1.3.0
Expand Down
2 changes: 1 addition & 1 deletion requirements/datatype_text.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
rouge-score>=0.0.4
sentencepiece>=0.1.95
filelock
transformers>=4.5
datasets>=1.2, <1.3
torchmetrics[text]
34 changes: 0 additions & 34 deletions tests/text/seq2seq/core/test_metrics.py

This file was deleted.