Skip to content

Commit 9dc3690

Browse files
Update Fastmri reader for inference and add support of Tensor for switch_endianness (#5548)
Fixes #5552 . ### Description A few sentences describing the changes proposed in this pull request. Update Fastmri reader for inference ### Types of changes <!--- Put an `x` in all the boxes that apply, and remove the not applicable items --> - [x] Non-breaking change (fix or new feature that would not break existing functionality). - [ ] Breaking change (fix or new feature that would cause existing functionality to change). - [ ] New tests added to cover the changes. - [ ] Integration tests passed locally by running `./runtests.sh -f -u --net --coverage`. - [ ] Quick tests passed locally by running `./runtests.sh --quick --unittests --disttests`. - [ ] In-line docstrings updated. - [ ] Documentation updated, tested `make html` command in the `docs/` folder. Signed-off-by: Can Zhao <canz@nvidia.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent d5344a5 commit 9dc3690

2 files changed

Lines changed: 10 additions & 3 deletions

File tree

monai/apps/reconstruction/fastmri_reader.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
from monai.data.image_reader import ImageReader
2020
from monai.data.utils import is_supported_format
2121
from monai.utils import FastMRIKeys, optional_import, require_pkg
22-
from monai.utils.type_conversion import convert_to_tensor
2322

2423
h5py, has_h5py = optional_import("h5py")
2524

@@ -85,7 +84,7 @@ def get_data(self, dat: Dict) -> Tuple[ndarray, dict]:
8584
header = self._get_meta_dict(dat)
8685
data: ndarray = np.array(dat[FastMRIKeys.KSPACE])
8786
header[FastMRIKeys.MASK] = (
88-
convert_to_tensor(np.array(dat[FastMRIKeys.MASK])).unsqueeze(0)[None, ..., None]
87+
np.expand_dims(np.array(dat[FastMRIKeys.MASK]), 0)[None, ..., None]
8988
if FastMRIKeys.MASK in dat.keys()
9089
else np.zeros(data.shape)
9190
)

monai/transforms/io/array.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,15 @@ def switch_endianness(data, new="<"):
6868
data: input to be converted.
6969
new: the target endianness, currently support "<" or ">".
7070
"""
71-
if isinstance(data, np.ndarray):
71+
if isinstance(data, torch.Tensor):
72+
device = data.device
73+
requires_grad: bool = data.requires_grad
74+
data = (
75+
torch.from_numpy(switch_endianness(data.cpu().detach().numpy(), new))
76+
.to(device)
77+
.requires_grad_(requires_grad=requires_grad)
78+
)
79+
elif isinstance(data, np.ndarray):
7280
# default to system endian
7381
sys_native = "<" if (sys.byteorder == "little") else ">"
7482
current_ = sys_native if data.dtype.byteorder not in ("<", ">") else data.dtype.byteorder

0 commit comments

Comments
 (0)