From f2a01d468d1a3e96236442790601829165a0834b Mon Sep 17 00:00:00 2001 From: Nic Ma Date: Wed, 20 Jul 2022 20:33:49 +0800 Subject: [PATCH 1/4] [DLMED] add track_meta option Signed-off-by: Nic Ma --- monai/transforms/utility/array.py | 13 +++++++++++-- monai/transforms/utility/dictionary.py | 5 ++++- tests/test_to_tensor.py | 2 +- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/monai/transforms/utility/array.py b/monai/transforms/utility/array.py index a28f468cf4..48c44d82fc 100644 --- a/monai/transforms/utility/array.py +++ b/monai/transforms/utility/array.py @@ -400,18 +400,25 @@ class ToTensor(Transform): device: target device to put the converted Tensor data. wrap_sequence: if `False`, then lists will recursively call this function, default to `True`. E.g., if `False`, `[1, 2]` -> `[tensor(1), tensor(2)]`, if `True`, then `[1, 2]` -> `tensor([1, 2])`. + track_meta: whether to convert to `MetaTensor` when `data_type` is "tensor". + If False, the output data type will be `torch.Tensor`. Default to the return value of ``get_track_meta``. """ backend = [TransformBackends.TORCH, TransformBackends.NUMPY] def __init__( - self, dtype: Optional[torch.dtype] = None, device: Optional[torch.device] = None, wrap_sequence: bool = True + self, + dtype: Optional[torch.dtype] = None, + device: Optional[torch.device] = None, + wrap_sequence: bool = True, + track_meta: Optional[bool] = None, ) -> None: super().__init__() self.dtype = dtype self.device = device self.wrap_sequence = wrap_sequence + self.track_meta = get_track_meta() if track_meta is None else bool(track_meta) def __call__(self, img: NdarrayOrTensor): """ @@ -419,7 +426,9 @@ def __call__(self, img: NdarrayOrTensor): """ if isinstance(img, MetaTensor): img.applied_operations = [] # drops tracking info - return convert_to_tensor(img, dtype=self.dtype, device=self.device, wrap_sequence=self.wrap_sequence) + return convert_to_tensor( + img, dtype=self.dtype, device=self.device, wrap_sequence=self.wrap_sequence, track_meta=self.track_meta, + ) class EnsureType(Transform): diff --git a/monai/transforms/utility/dictionary.py b/monai/transforms/utility/dictionary.py index b65439f067..b9439a6e32 100644 --- a/monai/transforms/utility/dictionary.py +++ b/monai/transforms/utility/dictionary.py @@ -485,6 +485,7 @@ def __init__( dtype: Optional[torch.dtype] = None, device: Optional[torch.device] = None, wrap_sequence: bool = True, + track_meta: Optional[bool] = None, allow_missing_keys: bool = False, ) -> None: """ @@ -495,10 +496,12 @@ def __init__( device: specify the target device to put the Tensor data. wrap_sequence: if `False`, then lists will recursively call this function, default to `True`. E.g., if `False`, `[1, 2]` -> `[tensor(1), tensor(2)]`, if `True`, then `[1, 2]` -> `tensor([1, 2])`. + track_meta: whether to convert to `MetaTensor` when `data_type` is "tensor". + If False, the output data type will be `torch.Tensor`. Default to the return value of `get_track_meta`. allow_missing_keys: don't raise exception if key is missing. """ super().__init__(keys, allow_missing_keys) - self.converter = ToTensor(dtype=dtype, device=device, wrap_sequence=wrap_sequence) + self.converter = ToTensor(dtype=dtype, device=device, wrap_sequence=wrap_sequence, track_meta=track_meta) def __call__(self, data: Mapping[Hashable, NdarrayOrTensor]) -> Dict[Hashable, NdarrayOrTensor]: d = dict(data) diff --git a/tests/test_to_tensor.py b/tests/test_to_tensor.py index fe6c4fb0d4..058c0e14ef 100644 --- a/tests/test_to_tensor.py +++ b/tests/test_to_tensor.py @@ -40,7 +40,7 @@ def test_array_input(self, test_data, expected_shape): @parameterized.expand(TESTS_SINGLE) def test_single_input(self, test_data): - result = ToTensor()(test_data) + result = ToTensor(track_meta=False)(test_data) self.assertTrue(isinstance(result, torch.Tensor)) assert_allclose(result, test_data, type_test=False) self.assertEqual(result.ndim, 0) From bc3f14ae27ffac5897273cb0a27ee029d4004377 Mon Sep 17 00:00:00 2001 From: monai-bot Date: Wed, 20 Jul 2022 12:40:15 +0000 Subject: [PATCH 2/4] [MONAI] code formatting Signed-off-by: monai-bot --- monai/transforms/utility/array.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/monai/transforms/utility/array.py b/monai/transforms/utility/array.py index 48c44d82fc..4b2d3051d4 100644 --- a/monai/transforms/utility/array.py +++ b/monai/transforms/utility/array.py @@ -427,7 +427,7 @@ def __call__(self, img: NdarrayOrTensor): if isinstance(img, MetaTensor): img.applied_operations = [] # drops tracking info return convert_to_tensor( - img, dtype=self.dtype, device=self.device, wrap_sequence=self.wrap_sequence, track_meta=self.track_meta, + img, dtype=self.dtype, device=self.device, wrap_sequence=self.wrap_sequence, track_meta=self.track_meta ) From 3c540ab4484c220cb06348ee1822c4ad13ec1180 Mon Sep 17 00:00:00 2001 From: Nic Ma Date: Thu, 21 Jul 2022 17:02:28 +0800 Subject: [PATCH 3/4] [DLMED] fix CI errors Signed-off-by: Nic Ma --- monai/transforms/utility/array.py | 6 +++--- monai/transforms/utility/dictionary.py | 7 ++++--- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/monai/transforms/utility/array.py b/monai/transforms/utility/array.py index 4b2d3051d4..c88d5c5835 100644 --- a/monai/transforms/utility/array.py +++ b/monai/transforms/utility/array.py @@ -400,8 +400,8 @@ class ToTensor(Transform): device: target device to put the converted Tensor data. wrap_sequence: if `False`, then lists will recursively call this function, default to `True`. E.g., if `False`, `[1, 2]` -> `[tensor(1), tensor(2)]`, if `True`, then `[1, 2]` -> `tensor([1, 2])`. - track_meta: whether to convert to `MetaTensor` when `data_type` is "tensor". - If False, the output data type will be `torch.Tensor`. Default to the return value of ``get_track_meta``. + track_meta: whether to convert to `MetaTensor`, default to `False`, output type will be `torch.Tensor`. + if `None`, use the return value of ``get_track_meta``. """ @@ -412,7 +412,7 @@ def __init__( dtype: Optional[torch.dtype] = None, device: Optional[torch.device] = None, wrap_sequence: bool = True, - track_meta: Optional[bool] = None, + track_meta: Optional[bool] = False, ) -> None: super().__init__() self.dtype = dtype diff --git a/monai/transforms/utility/dictionary.py b/monai/transforms/utility/dictionary.py index b9439a6e32..68f556cb2b 100644 --- a/monai/transforms/utility/dictionary.py +++ b/monai/transforms/utility/dictionary.py @@ -485,7 +485,7 @@ def __init__( dtype: Optional[torch.dtype] = None, device: Optional[torch.device] = None, wrap_sequence: bool = True, - track_meta: Optional[bool] = None, + track_meta: Optional[bool] = False, allow_missing_keys: bool = False, ) -> None: """ @@ -496,9 +496,10 @@ def __init__( device: specify the target device to put the Tensor data. wrap_sequence: if `False`, then lists will recursively call this function, default to `True`. E.g., if `False`, `[1, 2]` -> `[tensor(1), tensor(2)]`, if `True`, then `[1, 2]` -> `tensor([1, 2])`. - track_meta: whether to convert to `MetaTensor` when `data_type` is "tensor". - If False, the output data type will be `torch.Tensor`. Default to the return value of `get_track_meta`. + track_meta: whether to convert to `MetaTensor`, default to `False`, output type will be `torch.Tensor`. + if `None`, use the return value of ``get_track_meta``. allow_missing_keys: don't raise exception if key is missing. + """ super().__init__(keys, allow_missing_keys) self.converter = ToTensor(dtype=dtype, device=device, wrap_sequence=wrap_sequence, track_meta=track_meta) From a7c65b828cd6213637176e6edf0eeab4e42d039c Mon Sep 17 00:00:00 2001 From: Nic Ma Date: Thu, 21 Jul 2022 17:05:47 +0800 Subject: [PATCH 4/4] [DLMED] update test Signed-off-by: Nic Ma --- tests/test_to_tensor.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_to_tensor.py b/tests/test_to_tensor.py index 058c0e14ef..cd1a814f21 100644 --- a/tests/test_to_tensor.py +++ b/tests/test_to_tensor.py @@ -40,7 +40,7 @@ def test_array_input(self, test_data, expected_shape): @parameterized.expand(TESTS_SINGLE) def test_single_input(self, test_data): - result = ToTensor(track_meta=False)(test_data) + result = ToTensor(track_meta=True)(test_data) self.assertTrue(isinstance(result, torch.Tensor)) assert_allclose(result, test_data, type_test=False) self.assertEqual(result.ndim, 0)