From a35a214e1f7b7ceb8dec8c7c3200c15eeb1e52c5 Mon Sep 17 00:00:00 2001 From: Yusuke Fujinami <223211143+yskfuji61@users.noreply.github.com> Date: Sat, 28 Nov 2026 11:16:03 +0900 Subject: [PATCH] Fix SegResNetDS modifying the caller's norm/act dicts in place split_args() returns the caller's kwargs dict, so the setdefault() calls for "affine" and "inplace" in SegResEncoder and SegResNetDS mutated the arguments passed by the user. Copy the dict before adding the defaults and add a regression test. Assisted-by: Claude Code Signed-off-by: Yusuke Fujinami <223211143+yskfuji61@users.noreply.github.com> --- monai/networks/nets/segresnet_ds.py | 26 +++++++++++++++--------- tests/networks/nets/test_segresnet_ds.py | 8 ++++++++ 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/monai/networks/nets/segresnet_ds.py b/monai/networks/nets/segresnet_ds.py index 8d466ab0c66..da92f8dfd38 100644 --- a/monai/networks/nets/segresnet_ds.py +++ b/monai/networks/nets/segresnet_ds.py @@ -157,15 +157,18 @@ def __init__( if spatial_dims not in (1, 2, 3): raise ValueError("`spatial_dims` can only be 1, 2 or 3.") - # ensure normalization has affine trainable parameters (if not specified) - norm = split_args(norm) + # ensure normalization has affine trainable parameters (if not specified); + # copy the keyword arguments so that the caller's dict is not modified in place + norm_name, norm_args = split_args(norm) + norm = (norm_name, dict(norm_args)) if has_option(Norm[norm[0], spatial_dims], "affine"): - norm[1].setdefault("affine", True) # type: ignore + norm[1].setdefault("affine", True) # ensure activation is inplace (if not specified) - act = split_args(act) + act_name, act_args = split_args(act) + act = (act_name, dict(act_args)) if has_option(Act[act[0]], "inplace"): - act[1].setdefault("inplace", True) # type: ignore + act[1].setdefault("inplace", True) filters = init_filters # base number of features @@ -312,15 +315,18 @@ def __init__( elif not all(r > 0 for r in resolution): raise ValueError("resolution must be positive") - # ensure normalization had affine trainable parameters (if not specified) - norm = split_args(norm) + # ensure normalization had affine trainable parameters (if not specified); + # copy the keyword arguments so that the caller's dict is not modified in place + norm_name, norm_args = split_args(norm) + norm = (norm_name, dict(norm_args)) if has_option(Norm[norm[0], spatial_dims], "affine"): - norm[1].setdefault("affine", True) # type: ignore + norm[1].setdefault("affine", True) # ensure activation is inplace (if not specified) - act = split_args(act) + act_name, act_args = split_args(act) + act = (act_name, dict(act_args)) if has_option(Act[act[0]], "inplace"): - act[1].setdefault("inplace", True) # type: ignore + act[1].setdefault("inplace", True) anisotropic_scales = None if resolution: diff --git a/tests/networks/nets/test_segresnet_ds.py b/tests/networks/nets/test_segresnet_ds.py index 4a2cf40e6f3..fd833c39cdb 100644 --- a/tests/networks/nets/test_segresnet_ds.py +++ b/tests/networks/nets/test_segresnet_ds.py @@ -128,6 +128,14 @@ def test_shape3(self, input_param, input_shape, expected_shapes): for i in range(dsdepth): self.assertEqual(result[i].shape, expected_shapes[i], msg=str(input_param)) + def test_args_not_modified_in_place(self): + norm = ("group", {"num_groups": 4}) + act = ("relu", {}) + SegResNetDS(spatial_dims=3, init_filters=8, norm=norm, act=act) + SegResNetDS2(spatial_dims=3, init_filters=8, norm=norm, act=act) + self.assertEqual(norm, ("group", {"num_groups": 4})) + self.assertEqual(act, ("relu", {})) + def test_ill_arg(self): with self.assertRaises(ValueError): SegResNetDS(spatial_dims=4)