Open
Conversation
Ensure consistent spatial_shape metadata in LoadTiffd Signed-off-by: fanweiya <fanweiya@users.noreply.github.com>
for more information, see https://pre-commit.ci
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description:
This PR addresses a bug in the
vista2d/components.py:LoadTiffdtransform that causesRuntimeError: each element in list of batch should be of equal sizeduring data collation when usingpad_list_data_collateor similar functions.Problem:
The current implementation calculates and stores the
ImageMetaKey.SPATIAL_SHAPEmetadata before normalizing the image array's dimensions and channel count. Input image files can have different original dimensions (e.g., 2D grayscale(H, W), 3D channels-last(H, W, C), or 3D channels-first(C, H, W)). This leads to the storedspatial_shapemetadata being a tuple of inconsistent lengths (e.g., length 2 or length 3) across different samples in the dataset.When the
DataLoaderattempts to collate a batch containing samples with differentspatial_shapelengths, the default collation logic for metadata (monai.data.utils.collate_meta_tensor_fn->torch.utils.data._utils.collate.default_collate) fails because it cannot stack tuples/lists of varying sizes.Solution:
The fix implemented in this PR modifies
LoadTiffdas follows:ImageMetaKey.SPATIAL_SHAPEis moved to after all image array manipulations (loading, transposing, adding/repeating/selecting channels).(C, H, W)format internally before metadata creation.HandW) from the final processed 3D array.(H, W)tuple, which always has a length of 2, is stored as the value forImageMetaKey.SPATIAL_SHAPEin theMetaTensor.Outcome:
This change guarantees that the
spatial_shapemetadata associated with loaded images has a consistent length (always 2) across all samples, regardless of the original file's dimensions. This resolves the downstream collation error. Robustness for handling different input dimensions (2D, 3D channels-last/first, >3D) has also been slightly improved with added logging.