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
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,16 @@ uv run python scripts/preprocess_data.py data_dir=Data_dir # split=[0.7
uv run python scripts/train.py data_dir=Data_dir name=my_training
```

**4. Testing:** predictions are written to `Data_dir/0Work/my_training/prediction/test`.
**4. Testing:** predictions of the test patients are written to `Data_dir/0Work/my_training/prediction/test`: a
probability map per patient, and its detections (center and radius in mm, size, probability) as CSV and as `.fcsv`
markups for 3D Slicer.
```
uv run python scripts/predict.py train_dir=Data_dir/0Work/my_training
```
Any other scan, or folder of scans, can be predicted the same way (skull-stripped first, like the training data):
```
uv run python scripts/predict.py train_dir=Data_dir/0Work/my_training input=scan.nii.gz output_dir=results
```

# Differences from the published code
The Kappa metric/loss follows Cohen's definition and the focal loss is computed per voxel (Lin et al.), so
Expand Down
7 changes: 6 additions & 1 deletion configs/predict.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
# Predicts the test patients with a trained model; data and model settings are read from the training's config
# Predicts with a trained model; data and model settings are read from the training's config
train_dir: ??? # training directory written by train.py
input: null # NIfTI scan or folder of scans; null: the test patients of the training's split
output_dir: null # null: <train_dir>/prediction/test, or <train_dir>/prediction/scans for input
checkpoint: last_checkpoint.pytorch
volume: noskull volume # key, in each patient's config.json, of the volume to predict on
skull_strip: true # skull-strip input scans as remove_skull.py does, to match volume above
threshold: 0.5 # detections: connected components above this probability
min_size: null # detections smaller than this, in voxels, are ignored; null keeps all
margin: 8 # voxels discarded at each patch border when stitching predictions
batch_size: null # null: the training's validation_batch_size

Expand Down
25 changes: 14 additions & 11 deletions deepaneseg/data/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,19 @@ def save_nii_to_file(fname, vol, vox2met):
ni.save(ni.Nifti1Image(vol, vox2met), fname)


def normalize_volume(vol, normalize):
"""
normalize: None or 'None' (unchanged), 'Linear' (rescaled to [0, 1]) or 'Normal' (zero mean, unit variance)
"""
if normalize == "Linear":
return (vol - np.min(vol)) / (np.max(vol) - np.min(vol))
if normalize == "Normal":
return (vol - np.mean(vol)) / np.std(vol)
if normalize in (None, "None"):
return vol
raise ValueError(f"Unsupported normalization '{normalize}', expected Linear, Normal or None")


def read_points_from_csv(fname):
"""
returns a list of points, read from a csv file. Coordinates are stored in columns named 'x', 'y' and 'z'
Expand Down Expand Up @@ -88,17 +101,7 @@ def read_patient_data(vol_file, ane_file, normalize=None, vessel_file=None):
else:
s = None

# should we use scikit.learn.normalize (or transform)?
if normalize == "Linear":
m = np.min(d.ravel())
M = np.max(d.ravel())
d = (d - m) / (M - m)
elif normalize == "Normal":
m = np.mean(d.ravel())
std = np.std(d.ravel())
d = (d - m) / std

return {"data": d, "affine": a, "aneurysms": s, "vessel": seg}
return {"data": normalize_volume(d, normalize), "affine": a, "aneurysms": s, "vessel": seg}


def read_patient_data_from_config(cfg_name, volume="init volume", normalize=None, vessel=None):
Expand Down
77 changes: 47 additions & 30 deletions deepaneseg/inference/prediction.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import os
import math

import nibabel as nib
import numpy as np
import pandas as pd
import skimage.measure as skme
import skimage.morphology as skmo
import SimpleITK as sitk
from tqdm import tqdm

Expand Down Expand Up @@ -88,57 +90,72 @@ def patch_wise_prediction(device, model, data, patch_shape, margin=0, batch_size
return output[tuple(slice(0, n) for n in image_shape)]


def predict_patient(pat_dict, device, model, patch_size, patch_shape, output_dir, margin=0, batch_size=1):
@torch.no_grad()
def predict_volume(data, affine, device, model, patch_size, patch_shape, margin=0, batch_size=1, name=""):
"""
Predicts one patient and writes the (sigmoid) prediction, on the patient's voxel grid, to
<output_dir>/<patient>.nii.gz.
:param pat_dict: patient data, as returned by data.io.read_patient_data_base (including the 'dir' key)
Predicts a (normalized) volume and returns the (sigmoid) prediction on the volume's own voxel grid.
:param patch_size: size in mm of a patch, and patch_shape its size in voxels (as used for training)
"""
os.makedirs(output_dir, exist_ok=True)
name = os.path.basename(pat_dict["dir"])
affine, data = pat_dict["affine"], pat_dict["data"]
print(f"Processing {name}")

spacing = np.linalg.norm(affine[:3, :3], axis=0)
work_spacing = np.asarray(patch_size, float) / np.asarray(patch_shape)
work = resample_to_spacing(data, spacing, work_spacing)
prediction = patch_wise_prediction(
device, model, work, patch_shape, margin=margin, batch_size=batch_size, patient_name=name
)
prediction = resample_to_grid(prediction, work_spacing, data.shape, spacing)

nib.Nifti1Image(prediction, affine).to_filename(os.path.join(output_dir, name + ".nii.gz"))
return resample_to_grid(prediction, work_spacing, data.shape, spacing)


@torch.no_grad()
def predict_patients(pat_db, device, model, patch_size, patch_shape, output_dir=".", margin=0, batch_size=1):
for p in pat_db:
predict_patient(p, device, model, patch_size, patch_shape, output_dir, margin=margin, batch_size=batch_size)
def find_detections(prediction, affine, threshold=0.5, min_size=None):
"""
Connected components of the prediction above threshold, as a DataFrame with one row per detection:
center (x, y, z) and radius (half the largest extent) in mm, size in voxels and maximum probability.
min_size: components smaller than this, in voxels, are ignored
"""
labels = skme.label(prediction >= threshold)
if min_size is not None:
labels = skme.label(skmo.remove_small_objects(labels, min_size=min_size))
rows = []
for region in skme.regionprops(labels, intensity_image=prediction):
points = region.coords @ affine[:3, :3].T + affine[:3, 3]
x, y, z = points.mean(axis=0)
radius = np.max(points.max(axis=0) - points.min(axis=0)) / 2
rows.append(
{"x": x, "y": y, "z": z, "radius": radius, "voxels": int(region.area), "probability": region.intensity_max}
)
return pd.DataFrame(rows, columns=["x", "y", "z", "radius", "voxels", "probability"])


def save_detections(detections, csv_path):
"""Writes the detections to csv_path and, for 3D Slicer, as markups to the matching .fcsv file."""
detections.assign(type="Detection").to_csv(csv_path)
dio.csv2fcsv(csv_path)


@torch.no_grad()
def get_patient_prediction(
pat_path, device, model, patch_size, patch_shape=(48, 48, 48), margin=8, batch_size=10, normalization="Normal"
pat_path,
device,
model,
patch_size,
patch_shape=(48, 48, 48),
margin=8,
batch_size=10,
normalization="Normal",
):
"""Predicts one patient directory; returns the prediction (on the patient's grid), its affine and the
ground-truth aneurysm spheres."""
pat_dict = dio.read_patient_data_base([pat_path], volume="init volume", normalize=normalization)[0]
affine, data = pat_dict["affine"], pat_dict["data"]

spacing = np.linalg.norm(affine[:3, :3], axis=0)
work_spacing = np.asarray(patch_size, float) / np.asarray(patch_shape)
work = resample_to_spacing(data, spacing, work_spacing)
prediction = patch_wise_prediction(
affine = pat_dict["affine"]
prediction = predict_volume(
pat_dict["data"],
affine,
device,
model,
work,
patch_size,
patch_shape,
margin=margin,
batch_size=batch_size,
patient_name=os.path.basename(pat_dict["dir"]),
margin,
batch_size,
name=os.path.basename(pat_dict["dir"]),
)
prediction = resample_to_grid(prediction, work_spacing, data.shape, spacing)

spheres = dio.points_to_spheres(pat_dict["aneurysms"])
return prediction, affine, spheres
9 changes: 9 additions & 0 deletions deepaneseg/volume/selection.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,15 @@ def remove_skull_mask(vol, percent=60):
return mask


def skull_strip(vol):
"""Skull-stripped volume, rescaled by its maximum (as written to 'noskull volume' by remove_skull.py)."""
vol = np.asarray(vol, dtype=np.float32)
vol = remove_skull_mask(vol) * vol
if np.max(vol) <= 0:
raise ValueError(f"skull stripping left no brain voxel in a volume of shape {vol.shape}")
return vol / np.max(vol)


def connected_components_to_spheres(vol, vox2met):
"""
Takes a binary volume and returns a sphere for each connected components
Expand Down
81 changes: 61 additions & 20 deletions scripts/predict.py
Original file line number Diff line number Diff line change
@@ -1,47 +1,88 @@
#!/usr/bin/env python3
"""Predict the test patients with a trained model; configuration in configs/predict.yaml.
"""Predict with a trained model; configuration in configs/predict.yaml.

Example: python scripts/predict.py train_dir=/data/0Work/exp1
Examples:
python scripts/predict.py train_dir=/data/0Work/exp1 # test patients of the split
python scripts/predict.py train_dir=/data/0Work/exp1 input=scan.nii.gz # any scan, or a folder of scans
Data and model settings come from the training's resolved configuration (<train_dir>/.hydra/config.yaml).
Predicted volumes are written to <train_dir>/prediction/test.
For each case, writes the probability map <name>.nii.gz and the detections <name>_detections.csv (and .fcsv,
for 3D Slicer).
"""

import glob
import os

import hydra
import torch
from omegaconf import OmegaConf

import deepaneseg.data.io as dio
from deepaneseg.inference.prediction import predict_patients
from deepaneseg.inference.prediction import find_detections, predict_volume, save_detections
from deepaneseg.utils import get_logger, load_model
from deepaneseg.volume.selection import skull_strip

logger = get_logger("Prediction")


def scan_name(path):
return os.path.basename(path).removesuffix(".gz").removesuffix(".nii")


def input_scans(path):
if os.path.isdir(path):
files = sorted(glob.glob(os.path.join(path, "*.nii")) + glob.glob(os.path.join(path, "*.nii.gz")))
if not files:
raise FileNotFoundError(f"No .nii or .nii.gz file in {path}")
return files
if not os.path.isfile(path):
raise FileNotFoundError(f"No such scan or folder: {path}")
return [path]


def cases(cfg, normalize):
"""Yields (name, normalized volume, affine) for the input scans or the test patients."""
if cfg.input is not None:
for path in input_scans(cfg.input):
data, affine = dio.read_nii_from_file(path)
if cfg.skull_strip:
data = skull_strip(data)
yield scan_name(path), dio.normalize_volume(data, normalize), affine
return
train_cfg = OmegaConf.load(os.path.join(cfg.train_dir, ".hydra", "config.yaml"))
_, _, test_list = dio.read_split(train_cfg.data.split_file)
logger.info(f"{len(test_list)} patients for testing, '{cfg.volume}' with '{normalize}' normalization")
for patient in dio.read_patient_data_base(test_list, volume=cfg.volume, normalize=normalize):
yield os.path.basename(patient["dir"]), patient["data"], patient["affine"]


@hydra.main(version_base="1.3", config_path="../configs", config_name="predict")
def main(cfg):
logger = get_logger("Prediction")
train_cfg = OmegaConf.load(os.path.join(cfg.train_dir, ".hydra", "config.yaml"))

_, _, test_list = dio.read_split(train_cfg.data.split_file)
logger.info(f"{len(test_list)} Patients for testing")
test_db = dio.read_patient_data_base(test_list, volume=cfg.volume, normalize=train_cfg.data.normalize)
logger.info(f"'{cfg.volume}' successfully loaded with '{train_cfg.data.normalize}' normalization")
default_output = os.path.join(cfg.train_dir, "prediction", "test" if cfg.input is None else "scans")
output_dir = cfg.output_dir or default_output
os.makedirs(output_dir, exist_ok=True)

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = load_model(train_cfg.model, os.path.join(cfg.train_dir, cfg.checkpoint)).to(device)
model.eval()
logger.info(f"Model '{cfg.checkpoint}' loaded on '{device}' in evaluation mode")

predict_patients(
test_db,
device=device,
model=model,
patch_size=list(train_cfg.data.patch_size),
patch_shape=list(train_cfg.data.patch_shape),
output_dir=os.path.join(cfg.train_dir, "prediction", "test"),
margin=cfg.margin,
batch_size=cfg.batch_size or train_cfg.validation_batch_size,
)
for name, data, affine in cases(cfg, train_cfg.data.normalize):
prediction = predict_volume(
data,
affine,
device,
model,
patch_size=list(train_cfg.data.patch_size),
patch_shape=list(train_cfg.data.patch_shape),
margin=cfg.margin,
batch_size=cfg.batch_size or train_cfg.validation_batch_size,
name=name,
)
dio.save_nii_to_file(os.path.join(output_dir, name + ".nii.gz"), prediction, affine)
detections = find_detections(prediction, affine, threshold=cfg.threshold, min_size=cfg.min_size)
save_detections(detections, os.path.join(output_dir, name + "_detections.csv"))
logger.info(f"{name}: {len(detections)} detections written to {output_dir}")


if __name__ == "__main__":
Expand Down
5 changes: 1 addition & 4 deletions scripts/remove_skull.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@ def remove_skull(patient_dir, output_name):
with open(config_file) as f:
config = json.load(f)
ni_vol = ni.load(os.path.join(patient_dir, config["init volume"]))
vol = np.asarray(ni_vol.dataobj).astype(np.float32)

vol = vs.remove_skull_mask(vol) * vol
vol /= np.max(vol)
vol = vs.skull_strip(np.asarray(ni_vol.dataobj))

ni.Nifti1Image(vol, ni_vol.affine).to_filename(os.path.join(patient_dir, output_name))
config["noskull volume"] = output_name
Expand Down
Loading
Loading