From 7551099c0d1e911d94d5b08b8db7db80df1042a2 Mon Sep 17 00:00:00 2001 From: "marcus.wirtz" Date: Fri, 19 Jul 2024 11:26:08 +0200 Subject: [PATCH 1/3] Fix randomness for threading Signed-off-by: marcus.wirtz --- monai/transforms/compose.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/monai/transforms/compose.py b/monai/transforms/compose.py index 236d3cc4c5..bf0ae46b80 100644 --- a/monai/transforms/compose.py +++ b/monai/transforms/compose.py @@ -107,7 +107,11 @@ def execute_compose( for _transform in transforms[start:end]: if threading: - _transform = deepcopy(_transform) if isinstance(_transform, ThreadUnsafe) else _transform + if isinstance(_transform, ThreadUnsafe): + if isinstance(_transform, Randomizable): + # update the random state before deepcopy, otherwise there is no randomness + _transform.set_random_state() + _transform = deepcopy(_transform) data = apply_transform( _transform, data, map_items, unpack_items, lazy=lazy, overrides=overrides, log_stats=log_stats ) From dd45f3b5178d723b0c92bf7b217095322aaa776e Mon Sep 17 00:00:00 2001 From: "marcus.wirtz" Date: Fri, 26 Jul 2024 18:11:50 +0200 Subject: [PATCH 2/3] Enable deterministic randomness --- monai/transforms/compose.py | 12 ++++++------ monai/transforms/transform.py | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/monai/transforms/compose.py b/monai/transforms/compose.py index bf0ae46b80..dc29908e94 100644 --- a/monai/transforms/compose.py +++ b/monai/transforms/compose.py @@ -106,12 +106,12 @@ def execute_compose( return data for _transform in transforms[start:end]: - if threading: - if isinstance(_transform, ThreadUnsafe): - if isinstance(_transform, Randomizable): - # update the random state before deepcopy, otherwise there is no randomness - _transform.set_random_state() - _transform = deepcopy(_transform) + if threading and isinstance(_transform, ThreadUnsafe): + if isinstance(_transform, Randomizable): + # iterate the random state before deepcopy, otherwise there is no randomness + d = dict(data) if isinstance(data, Mapping) else data + _transform.randomize(d) + _transform = deepcopy(_transform) data = apply_transform( _transform, data, map_items, unpack_items, lazy=lazy, overrides=overrides, log_stats=log_stats ) diff --git a/monai/transforms/transform.py b/monai/transforms/transform.py index 3d09cea545..88f17adb01 100644 --- a/monai/transforms/transform.py +++ b/monai/transforms/transform.py @@ -229,7 +229,7 @@ def randomize(self, data: Any) -> None: Raises: NotImplementedError: When the subclass does not override this method. """ - raise NotImplementedError(f"Subclass {self.__class__.__name__} must implement this method.") + raise NotImplementedError(f"Subclass {self.__class__.__name__} must implement the randomize() method.") class Transform(ABC): From 4d49755c7a9e683f176d72bbd366cf1f74306868 Mon Sep 17 00:00:00 2001 From: Marcus Wirtz <24655255+marcus-wirtz-snkeos@users.noreply.github.com> Date: Mon, 21 Oct 2024 12:37:32 +0200 Subject: [PATCH 3/3] Update monai/transforms/compose.py Co-authored-by: Ben Murray Signed-off-by: Marcus Wirtz <24655255+marcus-wirtz-snkeos@users.noreply.github.com> --- monai/transforms/compose.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/monai/transforms/compose.py b/monai/transforms/compose.py index dc29908e94..981f99fbce 100644 --- a/monai/transforms/compose.py +++ b/monai/transforms/compose.py @@ -106,12 +106,13 @@ def execute_compose( return data for _transform in transforms[start:end]: - if threading and isinstance(_transform, ThreadUnsafe): - if isinstance(_transform, Randomizable): - # iterate the random state before deepcopy, otherwise there is no randomness - d = dict(data) if isinstance(data, Mapping) else data - _transform.randomize(d) - _transform = deepcopy(_transform) + with lock: + if threading and isinstance(_transform, ThreadUnsafe): + if isinstance(_transform, Randomizable): + # iterate the random state before deepcopy, otherwise there is no randomness + d = dict(data) if isinstance(data, Mapping) else data + _transform.randomize(d) + _transform = deepcopy(_transform) data = apply_transform( _transform, data, map_items, unpack_items, lazy=lazy, overrides=overrides, log_stats=log_stats )