Skip to content

LoadImageD and MetaTensor #5509

Closed
Closed
@myron

Description

@myron

It's more of question to clarify, not a bug. can you help clarify plz

  • LoadImage() and LoadImaged() always return MetaTensor. Is it by design? I'm okay either way, just want to clarify. So we can't return the original data format ?

  • line 281 return img, img.meta if isinstance(img, MetaTensor) else meta_data. The img seems to be always a MetaTensor, is that check necessary?

meta_data[Key.FILENAME_OR_OBJ] = f"{ensure_tuple(filename)[0]}" # Path obj should be strings for data loader
img = MetaTensor.ensure_torch_and_prune_meta(
img_array, meta_data, self.simple_keys, pattern=self.pattern, sep=self.sep
)
if self.ensure_channel_first:
img = EnsureChannelFirst()(img)
if self.image_only:
return img
return img, img.meta if isinstance(img, MetaTensor) else meta_data

  • inside of MetaTensor.ensure_torch_and_prune_meta(), we seems to do conversion to Tensor twice on lines 487 and 504. on 487 we img = convert_to_tensor(im) and return MetaTensor(img, meta=meta) will internally call torch.as_tensor on img.

img = convert_to_tensor(im) # potentially ascontiguousarray
# if not tracking metadata, return `torch.Tensor`
if not get_track_meta() or meta is None:
return img
# remove any superfluous metadata.
if simple_keys:
# ensure affine is of type `torch.Tensor`
if MetaKeys.AFFINE in meta:
meta[MetaKeys.AFFINE] = convert_to_tensor(meta[MetaKeys.AFFINE]) # bc-breaking
remove_extra_metadata(meta) # bc-breaking
if pattern is not None:
meta = monai.transforms.DeleteItemsd(keys=pattern, sep=sep, use_re=True)(meta)
# return the `MetaTensor`
return MetaTensor(img, meta=meta)

Thank you

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions