Replies: 1 comment 2 replies
-
I'm converting this to a discussion as it is mainly a usage issue:
put them together, here is a working example: import numpy as np
import monai
def create_random_img():
Random = np.random.default_rng()
image = Random.standard_normal((1, 256, 240, 35))
original_affine = np.asarray(
[
[1.1e-2, -4e-3, 4.3, 3.3e1],
[-7e-1, -1e-2, 6.5e-2, 2.1e2],
[-1e-2, 7e1, 2.9e-2, -1.9e2],
[0, 0, 0, 1],
]
) # Copied from a dataset (So a valid sample!)
original_file_name = "Test.nii.gz"
meta_data = {
"affine": original_affine,
"original_affine": original_affine,
"filename_or_obj": original_file_name,
"spatial_shape": image.shape[1:],
}
return {"img": image, "img_meta_dict": meta_data}
def resample_image(image):
RESAMPLERD = monai.transforms.SpacingD(
keys=["img"], pixdim=[1, 1, 12], diagonal=True
)
return RESAMPLERD(image)
def save_image(image):
SAVER = monai.data.NiftiSaver(resample=True)
image['img_meta_dict']['original_affine'] = image['img_meta_dict']['affine']
SAVER.save(image["img"], meta_data=image["img_meta_dict"])
new_file_name = "./Test/Test_seg.nii.gz" # Trust me on this
return image, new_file_name
def load_image(file_name):
LOADER = monai.transforms.LoadImage()
return LOADER(file_name)
def main():
image = create_random_img()
resampled_img = resample_image(image)
X = resampled_img
_, file_name = save_image(X)
X_hat = load_image(file_name)
image_hat = X_hat[0]
print(np.allclose(X["img"], image_hat)) # Expecting True
if __name__ == "__main__":
main() |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Intro
The other day, I tried to speed up the training process of our project by saving training images after the deterministic transformations had been executed. (Resampling in particular). I encountered the following problem:
Expectation
Saved images are equal or almost equal (
np.allclose(X, X_hat) == True
)Procedure
Concluding thoughts
It seems that the orientation changes during saving, even if resampling is off. Is this by design, or is this a bug? I have tried all kinds of saving images, i.e.
NiftiSaver
, the transformation, etcetera. idem for the loader, i.e.LoadImage
,ImageDataset
, etcetera. Probably, they all share the same implementation, so that does not really help.kind regards,
Code
Beta Was this translation helpful? Give feedback.
All reactions