From 46fc7c016388062fc77a23e08fab3f75e245ac53 Mon Sep 17 00:00:00 2001 From: Wenqi Li Date: Fri, 29 Jan 2021 19:32:56 +0000 Subject: [PATCH 1/3] fixes #1526 Signed-off-by: Wenqi Li --- monai/losses/dice.py | 2 +- tests/test_dice_ce_loss.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/monai/losses/dice.py b/monai/losses/dice.py index 9bc5ad28ea..f14aa6955f 100644 --- a/monai/losses/dice.py +++ b/monai/losses/dice.py @@ -593,7 +593,7 @@ def _compute_alpha_generalized_true_positives(self, flat_target: torch.Tensor) - return alpha -class DiceCELoss: +class DiceCELoss(_Loss): """ Compute both Dice loss and Cross Entropy Loss, and return the sum of these two losses. Input logits `input` (BNHW[D] where N is number of classes) is compared with ground truth `target` (BNHW[D]). diff --git a/tests/test_dice_ce_loss.py b/tests/test_dice_ce_loss.py index 7e9a0a0153..443d9a9baf 100644 --- a/tests/test_dice_ce_loss.py +++ b/tests/test_dice_ce_loss.py @@ -56,13 +56,13 @@ class TestDiceCELoss(unittest.TestCase): @parameterized.expand(TEST_CASES) def test_result(self, input_param, input_data, expected_val): - result = DiceCELoss(**input_param).forward(**input_data) + result = DiceCELoss(**input_param)(**input_data) np.testing.assert_allclose(result.detach().cpu().numpy(), expected_val, atol=1e-4, rtol=1e-4) def test_ill_shape(self): loss = DiceCELoss() with self.assertRaisesRegex(ValueError, ""): - loss.forward(torch.ones((1, 2, 3)), torch.ones((1, 1, 2, 3))) + loss(torch.ones((1, 2, 3)), torch.ones((1, 1, 2, 3))) if __name__ == "__main__": From b8246f87aa3c284514242f5a71a474c670ea6584 Mon Sep 17 00:00:00 2001 From: Wenqi Li Date: Fri, 29 Jan 2021 19:41:53 +0000 Subject: [PATCH 2/3] fixes Useless inheritance from object https://deepsource.io/gh/Project-MONAI/MONAI/issue/PYL-R0205 Signed-off-by: Wenqi Li --- monai/handlers/tensorboard_handlers.py | 4 ++-- monai/utils/decorators.py | 2 +- tests/utils.py | 8 ++++---- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/monai/handlers/tensorboard_handlers.py b/monai/handlers/tensorboard_handlers.py index 56d8f50678..15fa6a5eed 100644 --- a/monai/handlers/tensorboard_handlers.py +++ b/monai/handlers/tensorboard_handlers.py @@ -29,7 +29,7 @@ DEFAULT_TAG = "Loss" -class TensorBoardStatsHandler(object): +class TensorBoardStatsHandler: """ TensorBoardStatsHandler defines a set of Ignite Event-handlers for all the TensorBoard logics. It's can be used for any Ignite Engine(trainer, validator and evaluator). @@ -172,7 +172,7 @@ def _default_iteration_writer(self, engine: Engine, writer: SummaryWriter) -> No writer.flush() -class TensorBoardImageHandler(object): +class TensorBoardImageHandler: """ TensorBoardImageHandler is an Ignite Event handler that can visualize images, labels and outputs as 2D/3D images. 2D output (shape in Batch, channel, H, W) will be shown as simple image using the first element in the batch, diff --git a/monai/utils/decorators.py b/monai/utils/decorators.py index a3e6e3f980..1931d703c9 100644 --- a/monai/utils/decorators.py +++ b/monai/utils/decorators.py @@ -27,7 +27,7 @@ def __iter__(self): return self.create_gen() -class MethodReplacer(object): +class MethodReplacer: """ Base class for method decorators which can be used to replace methods pass to replace_method() with wrapped versions. """ diff --git a/tests/utils.py b/tests/utils.py index d73cb5fdc7..ebc9bff99f 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -57,7 +57,7 @@ def skip_if_quick(obj): return unittest.skipIf(is_quick, "Skipping slow tests")(obj) -class SkipIfNoModule(object): +class SkipIfNoModule: """Decorator to be used if test should be skipped when optional module is not present.""" @@ -69,7 +69,7 @@ def __call__(self, obj): return unittest.skipIf(self.module_missing, f"optional module not present: {self.module_name}")(obj) -class SkipIfModule(object): +class SkipIfModule: """Decorator to be used if test should be skipped when optional module is present.""" @@ -102,7 +102,7 @@ def skip_if_windows(obj): return unittest.skipIf(sys.platform == "win32", "Skipping tests on Windows")(obj) -class SkipIfBeforePyTorchVersion(object): +class SkipIfBeforePyTorchVersion: """Decorator to be used if test should be skipped with PyTorch versions older than that given.""" @@ -119,7 +119,7 @@ def __call__(self, obj): )(obj) -class SkipIfAtLeastPyTorchVersion(object): +class SkipIfAtLeastPyTorchVersion: """Decorator to be used if test should be skipped with PyTorch versions newer than that given.""" From 82f1839bf187117715085dec06d8a413ba5f8589 Mon Sep 17 00:00:00 2001 From: Wenqi Li Date: Fri, 29 Jan 2021 20:12:40 +0000 Subject: [PATCH 3/3] fixes https://github.com/Project-MONAI/MONAI/pull/1527/checks?check_run_id=1794062607#step:10:7763 Signed-off-by: Wenqi Li --- tests/test_normalize_intensity.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_normalize_intensity.py b/tests/test_normalize_intensity.py index 156725873a..ecf162e12f 100644 --- a/tests/test_normalize_intensity.py +++ b/tests/test_normalize_intensity.py @@ -61,7 +61,7 @@ def test_default(self): normalized = normalizer(self.imt) self.assertTrue(normalized.dtype == np.float32) expected = (self.imt - np.mean(self.imt)) / np.std(self.imt) - np.testing.assert_allclose(normalized, expected, rtol=1e-6) + np.testing.assert_allclose(normalized, expected, rtol=1e-5) @parameterized.expand(TEST_CASES) def test_nonzero(self, input_param, input_data, expected_data):