Skip to content

Commit

Permalink
Extract lambda to named function in monai transforms for dataloader
Browse files Browse the repository at this point in the history
Pickling of lambda functions is not supported in Windows, causing issue with get_features when iterating over dataset. Extraction of lambda x: x["image_path"].as_tensor() into named function called image_as_tensor on get_transforms sidesteps the issue.
  • Loading branch information
iamrjgs authored Oct 7, 2024
1 parent b45a498 commit b03c1b7
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions fmcib/preprocessing/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ def preprocess(image, spatial_size=(50, 50, 50)):
T = get_transforms(spatial_size=spatial_size)
return T(image)

def image_as_tensor(x):
return x["image_path"].as_tensor()

def get_transforms(spatial_size=(50, 50, 50), precropped=False):
if precropped:
Expand All @@ -20,7 +22,7 @@ def get_transforms(spatial_size=(50, 50, 50), precropped=False):
monai_transforms.NormalizeIntensityd(keys=["image_path"], subtrahend=-1024, divisor=3072),
monai_transforms.SelectItemsd(keys=["image_path"]),
monai_transforms.SpatialPadd(keys=["image_path"], spatial_size=spatial_size),
torchvision.transforms.Lambda(lambda x: x["image_path"].as_tensor()),
torchvision.transforms.Lambda(image_as_tensor),
]
)
else:
Expand All @@ -39,7 +41,7 @@ def get_transforms(spatial_size=(50, 50, 50), precropped=False):
monai_transforms.SelectItemsd(keys=["image_path"]),
monai_transforms.Transposed(keys=["image_path"], indices=(0, 3, 2, 1)),
monai_transforms.SpatialPadd(keys=["image_path"], spatial_size=spatial_size),
torchvision.transforms.Lambda(lambda x: x["image_path"].as_tensor()),
torchvision.transforms.Lambda(image_as_tensor),
]
)

Expand Down

0 comments on commit b03c1b7

Please sign in to comment.