From d2aa54fce9826d65e46dc1fb7431e634a2ce0cc0 Mon Sep 17 00:00:00 2001 From: "veera.puthiran" Date: Tue, 17 Sep 2024 16:55:10 +0530 Subject: [PATCH 1/7] filter nan --- src/torchmetrics/functional/audio/pesq.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/torchmetrics/functional/audio/pesq.py b/src/torchmetrics/functional/audio/pesq.py index ad1d9506f08..d8d17dd0cc2 100644 --- a/src/torchmetrics/functional/audio/pesq.py +++ b/src/torchmetrics/functional/audio/pesq.py @@ -103,7 +103,7 @@ def perceptual_evaluation_speech_quality( pesq_val_np = np.empty(shape=(preds_np.shape[0])) for b in range(preds_np.shape[0]): pesq_val_np[b] = pesq_backend.pesq(fs, target_np[b, :], preds_np[b, :], mode) - pesq_val = torch.from_numpy(pesq_val_np) + pesq_val = torch.from_numpy(list(filter(lambda x: isinstance(x, (int, float)), pesq_val_np))) pesq_val = pesq_val.reshape(preds.shape[:-1]) if keep_same_device: From f08cca8853d4075791746af7d0ef463c52b026ae Mon Sep 17 00:00:00 2001 From: "veera.puthiran" Date: Tue, 17 Sep 2024 18:02:56 +0530 Subject: [PATCH 2/7] use np --- src/torchmetrics/functional/audio/pesq.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/torchmetrics/functional/audio/pesq.py b/src/torchmetrics/functional/audio/pesq.py index d8d17dd0cc2..10242f796bf 100644 --- a/src/torchmetrics/functional/audio/pesq.py +++ b/src/torchmetrics/functional/audio/pesq.py @@ -83,6 +83,8 @@ def perceptual_evaluation_speech_quality( ) import pesq as pesq_backend + _filter_error_msg = np.vectorize(lambda x: np.issubdtype(type(x), np.number)) + if fs not in (8000, 16000): raise ValueError(f"Expected argument `fs` to either be 8000 or 16000 but got {fs}") if mode not in ("wb", "nb"): @@ -103,7 +105,7 @@ def perceptual_evaluation_speech_quality( pesq_val_np = np.empty(shape=(preds_np.shape[0])) for b in range(preds_np.shape[0]): pesq_val_np[b] = pesq_backend.pesq(fs, target_np[b, :], preds_np[b, :], mode) - pesq_val = torch.from_numpy(list(filter(lambda x: isinstance(x, (int, float)), pesq_val_np))) + pesq_val = torch.from_numpy(_filter_error_msg(pesq_val_np)) pesq_val = pesq_val.reshape(preds.shape[:-1]) if keep_same_device: From 10f1e06720c5b3644f0fd210c5fec3ed859618e1 Mon Sep 17 00:00:00 2001 From: "veera.puthiran" Date: Tue, 17 Sep 2024 18:53:18 +0530 Subject: [PATCH 3/7] fix tests --- src/torchmetrics/functional/audio/pesq.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/torchmetrics/functional/audio/pesq.py b/src/torchmetrics/functional/audio/pesq.py index 10242f796bf..e7464a22efc 100644 --- a/src/torchmetrics/functional/audio/pesq.py +++ b/src/torchmetrics/functional/audio/pesq.py @@ -105,7 +105,7 @@ def perceptual_evaluation_speech_quality( pesq_val_np = np.empty(shape=(preds_np.shape[0])) for b in range(preds_np.shape[0]): pesq_val_np[b] = pesq_backend.pesq(fs, target_np[b, :], preds_np[b, :], mode) - pesq_val = torch.from_numpy(_filter_error_msg(pesq_val_np)) + pesq_val = torch.from_numpy(pesq_val_np[_filter_error_msg(pesq_val_np)]) pesq_val = pesq_val.reshape(preds.shape[:-1]) if keep_same_device: From 434c1059774f4c5355a6ce78e6c018888d8f2bff Mon Sep 17 00:00:00 2001 From: "veera.puthiran" Date: Tue, 17 Sep 2024 23:12:19 +0530 Subject: [PATCH 4/7] fix dtype from object --- src/torchmetrics/functional/audio/pesq.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/torchmetrics/functional/audio/pesq.py b/src/torchmetrics/functional/audio/pesq.py index e7464a22efc..530b0c14c79 100644 --- a/src/torchmetrics/functional/audio/pesq.py +++ b/src/torchmetrics/functional/audio/pesq.py @@ -105,8 +105,8 @@ def perceptual_evaluation_speech_quality( pesq_val_np = np.empty(shape=(preds_np.shape[0])) for b in range(preds_np.shape[0]): pesq_val_np[b] = pesq_backend.pesq(fs, target_np[b, :], preds_np[b, :], mode) - pesq_val = torch.from_numpy(pesq_val_np[_filter_error_msg(pesq_val_np)]) - pesq_val = pesq_val.reshape(preds.shape[:-1]) + pesq_val = torch.from_numpy(pesq_val_np[_filter_error_msg(pesq_val_np)].astype(np.float32)) + pesq_val = pesq_val.reshape(len(pesq_val)) if keep_same_device: return pesq_val.to(preds.device) From 7a67f8f89701daeee3dd4b444bc44337f75fcca6 Mon Sep 17 00:00:00 2001 From: Jirka Borovec <6035284+Borda@users.noreply.github.com> Date: Tue, 8 Oct 2024 19:28:34 +0200 Subject: [PATCH 5/7] as func --- src/torchmetrics/functional/audio/pesq.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/torchmetrics/functional/audio/pesq.py b/src/torchmetrics/functional/audio/pesq.py index 530b0c14c79..ef0e4e81701 100644 --- a/src/torchmetrics/functional/audio/pesq.py +++ b/src/torchmetrics/functional/audio/pesq.py @@ -83,7 +83,10 @@ def perceptual_evaluation_speech_quality( ) import pesq as pesq_backend - _filter_error_msg = np.vectorize(lambda x: np.issubdtype(type(x), np.number)) + def _issubtype_number(x) -> bool: + return np.issubdtype(type(x), np.number) + + _filter_error_msg = np.vectorize(_issubtype_number) if fs not in (8000, 16000): raise ValueError(f"Expected argument `fs` to either be 8000 or 16000 but got {fs}") From f83debeea028aefb22acadbc27ee64243094c106 Mon Sep 17 00:00:00 2001 From: Jirka B Date: Tue, 8 Oct 2024 19:32:17 +0200 Subject: [PATCH 6/7] typing --- src/torchmetrics/functional/audio/pesq.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/torchmetrics/functional/audio/pesq.py b/src/torchmetrics/functional/audio/pesq.py index ef0e4e81701..516014434bf 100644 --- a/src/torchmetrics/functional/audio/pesq.py +++ b/src/torchmetrics/functional/audio/pesq.py @@ -11,6 +11,8 @@ # 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 Any + import numpy as np import torch from torch import Tensor @@ -83,7 +85,7 @@ def perceptual_evaluation_speech_quality( ) import pesq as pesq_backend - def _issubtype_number(x) -> bool: + def _issubtype_number(x: Any) -> bool: return np.issubdtype(type(x), np.number) _filter_error_msg = np.vectorize(_issubtype_number) From 0831873585a9d52e40eea3e8760c31b56a9ed00b Mon Sep 17 00:00:00 2001 From: Nicki Skafte Detlefsen Date: Wed, 9 Oct 2024 10:44:06 +0200 Subject: [PATCH 7/7] changelog --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5700a43a98b..40c5082370a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -47,6 +47,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Fixed for Pearson changes inputs ([#2765](https://github.com/Lightning-AI/torchmetrics/pull/2765)) +- Fixed bug in `PESQ` metric where `NoUtterancesError` prevented calculating on a batch of data ([#2753](https://github.com/Lightning-AI/torchmetrics/pull/2753)) + + ## [1.4.2] - 2022-09-12 ### Added