def imagenormalize(data, divisor=None): """kspace generated by normalizing image space""" #getting image from masked data image = transforms.ifft2(data) #normalizing the image nimage, divisor = normalize(image, divisor) #getting kspace data from normalized image data = transforms.ifftshift(image, dim=(-3, -2)) data = torch.fft(data, 2) data = transforms.fftshift(data, dim=(-3, -2)) return data, divisor
def mnormalize(masked_kspace): #getting image from masked data image = transforms.ifft2(masked_kspace) #normalizing the image nimage, mean, std = transforms.normalize_instance(image, eps=1e-11) #getting kspace data from normalized image maksed_kspace_fni = transforms.ifftshift(nimage, dim=(-3, -2)) maksed_kspace_fni = torch.fft(maksed_kspace_fni, 2) maksed_kspace_fni = transforms.fftshift(maksed_kspace_fni, dim=(-3, -2)) maksed_kspace_fni, mean, std = transforms.normalize_instance(masked_kspace, eps=1e-11) return maksed_kspace_fni, mean, std
def onormalize(original_kspace, mean, std, eps=1e-11): #getting image from masked data image = transforms.ifft2(original_kspace) #normalizing the image nimage = transforms.normalize(image, mean, std, eps=1e-11) #getting kspace data from normalized image original_kspace_fni = transforms.ifftshift(nimage, dim=(-3, -2)) original_kspace_fni = torch.fft(original_kspace_fni, 2) original_kspace_fni = transforms.fftshift(original_kspace_fni, dim=(-3, -2)) original_kspace_fni = transforms.normalize(original_kspace, mean, std, eps=1e-11) return original_kspace_fni
def nkspacetoimage(args, kspace_fni, mean, std, eps=1e-11): #nkspace to image assert kspace_fni.size(-1) == 2 image = transforms.ifftshift(kspace_fni, dim=(-3, -2)) image = torch.ifft(image, 2) image = transforms.fftshift(image, dim=(-3, -2)) #denormalizing the nimage image = (image * std) + mean image = image[0] image = transforms.complex_center_crop(image, (args.resolution, args.resolution)) # Absolute value image = transforms.complex_abs(image) # Normalize input image, mean, std = transforms.normalize_instance(image, eps=1e-11) image = image.clamp(-6, 6) return image
class C3Convert: def __init__(self, shp=(320, 320)): self.c3m = transforms.c3_torch(shp) # ksp.shape[-2:] def apply(self, ksp): # expect (bat, w, h, 2) return ksp * self.c3m # torch fft requires (bat, w,h, 2) ifft_c3 = lambda kspc3: torch.ifft( transforms.ifftshift(kspc3, dim=(-3, -2)), 2, normalized=True) fft_c3 = lambda im: transforms.fftshift(torch.fft(im, 2, normalized=True), dim=(-3, -2)) class NoTransform: def __init__(self): pass def __call__(self, kspace, target, attrs, fname, slice): return kspace class BasicMaskingTransform: def __init__(self, mask_func, resolution, which_challenge,
def test_fftshift(shape): input = np.arange(np.product(shape)).reshape(shape) out_torch = transforms.fftshift(torch.from_numpy(input)).numpy() out_numpy = np.fft.fftshift(input) assert np.allclose(out_torch, out_numpy)