From 58cfc8b536a461ce7272b5df4ec8987b79ce8ad6 Mon Sep 17 00:00:00 2001 From: Youssef Assis Date: Tue, 29 Sep 2026 20:28:23 +0200 Subject: [PATCH 1/3] [PERF] Keep volumes in memory as float32 Volumes were loaded as float64 (nibabel's default) and all training and validation patients stay in memory, e.g. 300 MiB per 512x512x150 scan and 32.5 GiB for 111 patients. Reading them as float32 halves that, and the loading peak, without changing what the model receives (patches were already converted to float32). Predictions are float32 too. --- deepaneseg/data/io.py | 7 ++++--- deepaneseg/inference/prediction.py | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/deepaneseg/data/io.py b/deepaneseg/data/io.py index 75bd7a0..1c6b52e 100644 --- a/deepaneseg/data/io.py +++ b/deepaneseg/data/io.py @@ -28,12 +28,13 @@ def fetch_patient_dirs(base_dir): return sorted(os.path.dirname(conf) for conf in glob.glob(os.path.join(base_dir, "P????", "config.json"))) -def read_nii_from_file(fname): +def read_nii_from_file(fname, dtype=np.float32): """ - Read a nii volume and return a 2-tuple with the voxel data (np array) and the voxel2metric tranform + Read a nii volume and return a 2-tuple with the voxel data (np array, float32 by default: half the memory of + nibabel's float64) and the voxel2metric tranform """ nv = ni.load(fname) - return nv.get_fdata(), nv.affine + return nv.get_fdata(dtype=dtype), nv.affine def save_nii_to_file(fname, vol, vox2met): diff --git a/deepaneseg/inference/prediction.py b/deepaneseg/inference/prediction.py index e92a2c0..01454e1 100644 --- a/deepaneseg/inference/prediction.py +++ b/deepaneseg/inference/prediction.py @@ -73,7 +73,7 @@ def patch_wise_prediction(device, model, data, patch_shape, margin=0, batch_size n_tiles = np.ceil(image_shape / core).astype(int) pad_after = n_tiles * core - image_shape + margin padded = np.pad(data, list(zip(margin, pad_after)), mode="constant", constant_values=data.min()) - output = np.zeros(n_tiles * core) + output = np.zeros(n_tiles * core, dtype=np.float32) starts = np.mgrid[: n_tiles[0], : n_tiles[1], : n_tiles[2]].reshape((3, -1)).T * core loop = tqdm(range(0, len(starts), batch_size), leave=True, unit="batch", total=math.ceil(len(starts) / batch_size)) From 0ea4db12a876857f293d179773c445258c2dbc3c Mon Sep 17 00:00:00 2001 From: Youssef Assis Date: Tue, 29 Sep 2026 20:28:23 +0200 Subject: [PATCH 2/3] [TEST] Check that volumes stay float32 from loading to prediction --- tests/test_memory.py | 49 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 tests/test_memory.py diff --git a/tests/test_memory.py b/tests/test_memory.py new file mode 100644 index 0000000..cd31512 --- /dev/null +++ b/tests/test_memory.py @@ -0,0 +1,49 @@ +"""Volumes are kept in memory as float32 from loading to prediction.""" + +import json + +import nibabel as ni +import numpy as np +import pytest +import torch + +from deepaneseg.data import io as dio +from deepaneseg.inference.prediction import predict_volume +from deepaneseg.volume.patch import get_patch + +AFFINE = np.diag([0.5, 0.5, 0.5, 1.0]) + + +@pytest.fixture +def patient(tmp_path): + vol = np.random.default_rng(0).integers(0, 2000, (30, 30, 30)).astype(np.int16) + ni.save(ni.Nifti1Image(vol, AFFINE), tmp_path / "volume.nii.gz") + (tmp_path / "config.json").write_text(json.dumps({"init volume": "volume.nii.gz"})) + return tmp_path + + +def test_volumes_are_read_as_float32(patient): + vol, _ = dio.read_nii_from_file(str(patient / "volume.nii.gz")) + + assert vol.dtype == np.float32 + + +@pytest.mark.parametrize("normalize", [None, "Linear", "Normal"]) +def test_patient_data_stays_float32_after_normalization(patient, normalize): + data = dio.read_patient_data_base([str(patient)], normalize=normalize)[0]["data"] + + assert data.dtype == np.float32 + if normalize == "Linear": + assert (data.min(), data.max()) == (0.0, 1.0) + if normalize == "Normal": + assert abs(data.mean()) < 1e-5 and data.std() == pytest.approx(1.0, abs=1e-5) + + +def test_patches_and_predictions_are_float32(patient): + data = dio.read_patient_data_base([str(patient)], normalize="Linear")[0]["data"] + + patch, _ = get_patch(data, AFFINE, center=[7.5, 7.5, 7.5], size=8, dim=16) + prediction = predict_volume(data, AFFINE, "cpu", torch.nn.Identity(), [8, 8, 8], [16, 16, 16], margin=2) + + assert patch.dtype == np.float32 and prediction.dtype == np.float32 + np.testing.assert_allclose(prediction, data, atol=1e-5) From 98025af60782131b63e56e479906a209987713b9 Mon Sep 17 00:00:00 2001 From: Youssef Assis Date: Tue, 29 Sep 2026 20:28:23 +0200 Subject: [PATCH 3/3] [DOCS] Note the memory used by the training volumes --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index bae9842..5fd5f5c 100644 --- a/README.md +++ b/README.md @@ -77,6 +77,8 @@ uv run python scripts/preprocess_data.py data_dir=Data_dir # split=[0.7 ``` **3. Training:** outputs go to `Data_dir/0Work/my_training`; running the same command again resumes training. +The training and validation volumes are kept in memory as float32, about 4 bytes per voxel (150 MiB for a +512×512×150 scan). ``` uv run python scripts/train.py data_dir=Data_dir name=my_training ```