Skip to content

Commit a2a60f1

Browse files
committed
evaluate cases
Signed-off-by: Wenqi Li <wenqil@nvidia.com>
1 parent 0fbbd78 commit a2a60f1

File tree

5 files changed

+18
-13
lines changed

5 files changed

+18
-13
lines changed

monai/transforms/compose.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def eval_lazy_stack(
4949
if not lazy_evaluation:
5050
return data # eager evaluation
5151
if isinstance(data, monai.data.MetaTensor):
52-
if not isinstance(upcoming, LazyTransform):
52+
if not (isinstance(upcoming, LazyTransform) and upcoming.lazy_evaluation):
5353
data, _ = mt.apply_transforms(data, mode=mode, padding_mode=padding_mode)
5454
return data
5555
if isinstance(data, Mapping):

monai/transforms/croppad/array.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -800,9 +800,9 @@ def __init__(
800800
self.padder = Pad(mode=mode, **pad_kwargs)
801801

802802
@Crop.lazy_evaluation.setter # type: ignore
803-
def lazy_evaluation(self, val: bool):
804-
self.lazy_evaluation = val
805-
self.padder.lazy_evaluation = val
803+
def lazy_evaluation(self, _val: bool):
804+
self._lazy_evaluation = False # foreground can't be computed lazily
805+
self.padder.lazy_evaluation = False
806806

807807
def compute_bounding_box(self, img: torch.Tensor):
808808
"""
@@ -839,10 +839,8 @@ def crop_pad(
839839
ret = self.padder.__call__(img=cropped, to_pad=pad_width, mode=mode, **pad_kwargs)
840840
# combine the traced cropping and padding into one transformation
841841
# by taking the padded info and placing it in a key inside the crop info.
842-
if get_track_meta():
843-
ret_: MetaTensor = ret # type: ignore
844-
app_op = ret_.applied_operations.pop(-1)
845-
ret_.applied_operations[-1][TraceKeys.EXTRA_INFO]["pad_info"] = app_op
842+
if get_track_meta() and isinstance(ret, MetaTensor):
843+
ret.applied_operations[-1][TraceKeys.EXTRA_INFO]["pad_info"] = ret.applied_operations.pop()
846844
return ret
847845

848846
def __call__(self, img: torch.Tensor, mode: str | None = None, **pad_kwargs): # type: ignore

monai/transforms/croppad/dictionary.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -684,6 +684,11 @@ def __init__(
684684
super().__init__(keys, cropper=cropper, allow_missing_keys=allow_missing_keys)
685685
self.mode = ensure_tuple_rep(mode, len(self.keys))
686686

687+
@LazyTransform.lazy_evaluation.setter # type: ignore
688+
def lazy_evaluation(self, val: bool):
689+
self._lazy_evaluation = False # foreground can't be computed lazily
690+
self.cropper.lazy_evaluation = False
691+
687692
def __call__(self, data: Mapping[Hashable, torch.Tensor]) -> dict[Hashable, torch.Tensor]:
688693
d = dict(data)
689694
self.cropper: CropForeground

monai/transforms/inverse.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -163,11 +163,11 @@ def track_transform_tensor(
163163
orig_affine = convert_to_dst_type(orig_affine, affine)[0]
164164
affine = orig_affine @ to_affine_nd(len(orig_affine) - 1, affine, dtype=affine.dtype)
165165
out_obj.meta[MetaKeys.AFFINE] = convert_to_tensor(affine, device=torch.device("cpu"))
166-
if (
167-
not isinstance(data_t, MetaTensor)
168-
or not get_track_meta()
169-
or not transform_info
170-
or not transform_info.get(TraceKeys.TRACING)
166+
if not (
167+
isinstance(data_t, MetaTensor)
168+
and get_track_meta()
169+
and transform_info
170+
and transform_info.get(TraceKeys.TRACING)
171171
):
172172
if key is not None:
173173
data[key] = data_t.copy_meta_from(out_obj) if isinstance(data_t, MetaTensor) else data_t

monai/transforms/lazy/functional.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,5 +70,7 @@ def apply_transforms(
7070
data = resample(data, cumulative_xform, sp_size, cur_kwargs)
7171
if isinstance(data, MetaTensor):
7272
for p in pending:
73+
for attr in LazyAttr:
74+
p.pop(attr, None)
7375
data.push_applied_operation(p)
7476
return data, pending

0 commit comments

Comments
 (0)