From 1b701671cbf71b0f30e50589aeef56fafb93b23f Mon Sep 17 00:00:00 2001 From: Joshua Newton Date: Sat, 23 Mar 2024 11:16:42 -0400 Subject: [PATCH 1/2] `predict_single_npy_array`: Add doc notes re: SITK axis order This will hopefully help any users who try to call `predict_single_npy_array` on a npy array derived from a `nibabel`-loaded image. --- nnunetv2/inference/predict_from_raw_data.py | 3 +++ nnunetv2/inference/readme.md | 9 ++++++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/nnunetv2/inference/predict_from_raw_data.py b/nnunetv2/inference/predict_from_raw_data.py index 8015ed77d..e9d6f3966 100644 --- a/nnunetv2/inference/predict_from_raw_data.py +++ b/nnunetv2/inference/predict_from_raw_data.py @@ -422,6 +422,9 @@ def predict_single_npy_array(self, input_image: np.ndarray, image_properties: di output_file_truncated: str = None, save_or_return_probabilities: bool = False): """ + input_image must use SimpleITK axis ordering! + (NB: if array comes from a nibabel-loaded image, you must swap the + axes of both the array *and* the spacing from [x,y,z] to [z,y,x]!) image_properties must only have a 'spacing' key! """ ppa = PreprocessAdapterFromNpy([input_image], [segmentation_previous_stage], [image_properties], diff --git a/nnunetv2/inference/readme.md b/nnunetv2/inference/readme.md index 4f832a158..a9d6d65d8 100644 --- a/nnunetv2/inference/readme.md +++ b/nnunetv2/inference/readme.md @@ -147,6 +147,7 @@ cons: tldr: - you give one image as npy array +- array must use [SimpleITK axis order](http://insightsoftwareconsortium.github.io/SimpleITK-Notebooks/Python_html/03_Image_Details.html#Conversion-between-numpy-and-SimpleITK) (see examples below) - everything is done in the main process: preprocessing, prediction, resampling, (export) - no interlacing, slowest variant! - ONLY USE THIS IF YOU CANNOT GIVE NNUNET MULTIPLE IMAGES AT ONCE FOR SOME REASON @@ -160,9 +161,15 @@ cons: - never the right choice unless you can only give a single image at a time to nnU-Net ```python - # predict a single numpy array + # predict a single numpy array (SimpleITK) img, props = SimpleITKIO().read_images([join(nnUNet_raw, 'Dataset003_Liver/imagesTr/liver_63_0000.nii.gz')]) ret = predictor.predict_single_npy_array(img, props, None, None, False) + + # predict a single numpy array (Nibabel with axes swapped) + img_nii = nib.load('Dataset003_Liver/imagesTr/liver_63_0000.nii.gz') + img = np.asanyarray(img_nii.dataobj).transpose([2, 1, 0]) # reverse axis order to match SITK + props = {'spacing': img_nii.header.get_zooms()[::-1]} # reverse axis order to match SITK + ret = predictor.predict_single_npy_array(img, props, None, None, False) ``` ## Predicting with a custom data iterator From e9ea09f351ad56b8fadf687eb2f6f558cd102c5f Mon Sep 17 00:00:00 2001 From: Joshua Newton Date: Sat, 23 Mar 2024 11:19:35 -0400 Subject: [PATCH 2/2] `predict_single_npy_array`: Copy warning from inference/readme.md --- nnunetv2/inference/predict_from_raw_data.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/nnunetv2/inference/predict_from_raw_data.py b/nnunetv2/inference/predict_from_raw_data.py index e9d6f3966..daeead75e 100644 --- a/nnunetv2/inference/predict_from_raw_data.py +++ b/nnunetv2/inference/predict_from_raw_data.py @@ -422,6 +422,8 @@ def predict_single_npy_array(self, input_image: np.ndarray, image_properties: di output_file_truncated: str = None, save_or_return_probabilities: bool = False): """ + WARNING: SLOW. ONLY USE THIS IF YOU CANNOT GIVE NNUNET MULTIPLE IMAGES AT ONCE FOR SOME REASON. + input_image must use SimpleITK axis ordering! (NB: if array comes from a nibabel-loaded image, you must swap the axes of both the array *and* the spacing from [x,y,z] to [z,y,x]!)