From 45400be921e950f2027bd0ae6bd1bb567a7bdc0b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mochol=C3=AD?= Date: Thu, 24 Mar 2022 10:56:35 +0100 Subject: [PATCH] Do not print empty evaluation result tables (#12427) --- CHANGELOG.md | 3 +++ pytorch_lightning/loops/dataloader/evaluation_loop.py | 2 ++ tests/trainer/logging_/test_eval_loop_logging.py | 5 +++++ 3 files changed, 10 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9c242d435c..b90cf41b07 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -175,6 +175,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/). - Implemented a new native and rich format in `_print_results` method of the `EvaluationLoop` ([#11332](https://github.com/PyTorchLightning/pytorch-lightning/pull/11332)) +- Do not print an empty table at the end of the `EvaluationLoop` ([#12427](https://github.com/PyTorchLightning/pytorch-lightning/pull/12427)) + + - Set the `prog_bar` flag to False in `LightningModule.log_grad_norm` ([#11472](https://github.com/PyTorchLightning/pytorch-lightning/pull/11472)) diff --git a/pytorch_lightning/loops/dataloader/evaluation_loop.py b/pytorch_lightning/loops/dataloader/evaluation_loop.py index 8b5611e214..c88395ca71 100644 --- a/pytorch_lightning/loops/dataloader/evaluation_loop.py +++ b/pytorch_lightning/loops/dataloader/evaluation_loop.py @@ -325,6 +325,8 @@ class EvaluationLoop(DataLoaderLoop): # remove the dl idx suffix results = [{k.split("/dataloader_idx_")[0]: v for k, v in result.items()} for result in results] metrics = sorted({k for keys in apply_to_collection(results, dict, EvaluationLoop._get_keys) for k in keys}) + if not metrics: + return headers = [f"DataLoader {i}" for i in range(len(results))] # fallback is useful for testing of printed output diff --git a/tests/trainer/logging_/test_eval_loop_logging.py b/tests/trainer/logging_/test_eval_loop_logging.py index 2235e35a59..0d8804dae1 100644 --- a/tests/trainer/logging_/test_eval_loop_logging.py +++ b/tests/trainer/logging_/test_eval_loop_logging.py @@ -838,6 +838,9 @@ expected3 = """ ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── """ +inputs4 = ([{}], "foo") +expected4 = "" + @pytest.mark.parametrize( ["inputs", "expected"], @@ -846,6 +849,7 @@ expected3 = """ pytest.param(inputs1, expected1, id="case1"), pytest.param(inputs2, expected2, id="case2"), pytest.param(inputs3, expected3, id="case3"), + pytest.param(inputs4, expected4, id="empty case"), ], ) def test_native_print_results(monkeypatch, inputs, expected): @@ -916,6 +920,7 @@ expected3 = """ pytest.param(inputs1, expected1, id="case1"), pytest.param(inputs2, expected2, id="case2"), pytest.param(inputs3, expected3, id="case3"), + pytest.param(inputs4, expected4, id="empty case"), ], ) @RunIf(rich=True, skip_windows=True)