Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
Expand Down
7 changes: 4 additions & 3 deletions deepaneseg/data/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion deepaneseg/inference/prediction.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
49 changes: 49 additions & 0 deletions tests/test_memory.py
Original file line number Diff line number Diff line change
@@ -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)
Loading