Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Forced Fourier class to output contiguous tensors. #7969

Merged
merged 5 commits into from
Sep 3, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions monai/transforms/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1649,12 +1649,12 @@ def shift_fourier(x: NdarrayOrTensor, spatial_dims: int) -> NdarrayOrTensor:
k: NdarrayOrTensor
if isinstance(x, torch.Tensor):
if hasattr(torch.fft, "fftshift"): # `fftshift` is new in torch 1.8.0
k = torch.fft.fftshift(torch.fft.fftn(x, dim=dims), dim=dims)
k = torch.fft.fftshift(torch.fft.fftn(x, dim=dims), dim=dims).contiguous()
bwittmann marked this conversation as resolved.
Show resolved Hide resolved
else:
# if using old PyTorch, will convert to numpy array and return
k = np.fft.fftshift(np.fft.fftn(x.cpu().numpy(), axes=dims), axes=dims)
k = np.ascontiguousarray(np.fft.fftshift(np.fft.fftn(x.cpu().numpy(), axes=dims), axes=dims))
else:
k = np.fft.fftshift(np.fft.fftn(x, axes=dims), axes=dims)
k = np.ascontiguousarray(np.fft.fftshift(np.fft.fftn(x, axes=dims), axes=dims))
return k

@staticmethod
Expand All @@ -1674,12 +1674,12 @@ def inv_shift_fourier(k: NdarrayOrTensor, spatial_dims: int, n_dims: int | None
out: NdarrayOrTensor
if isinstance(k, torch.Tensor):
if hasattr(torch.fft, "ifftshift"): # `ifftshift` is new in torch 1.8.0
out = torch.fft.ifftn(torch.fft.ifftshift(k, dim=dims), dim=dims, norm="backward").real
out = torch.fft.ifftn(torch.fft.ifftshift(k, dim=dims), dim=dims, norm="backward").real.contiguous()
else:
# if using old PyTorch, will convert to numpy array and return
out = np.fft.ifftn(np.fft.ifftshift(k.cpu().numpy(), axes=dims), axes=dims).real
out = np.ascontiguousarray(np.fft.ifftn(np.fft.ifftshift(k.cpu().numpy(), axes=dims), axes=dims).real)
else:
out = np.fft.ifftn(np.fft.ifftshift(k, axes=dims), axes=dims).real
out = np.ascontiguousarray(np.fft.ifftn(np.fft.ifftshift(k, axes=dims), axes=dims).real)
return out


Expand Down
Loading