Skip to content

Commit c356f0f

Browse files
refactoring
1 parent 79fe2b9 commit c356f0f

10 files changed

Lines changed: 66 additions & 35 deletions

File tree

pyproject.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,6 @@ dependencies = [
88
"nibabel>=5.3.3",
99
"tqdm>=4.67.3",
1010
]
11+
12+
[tool.setuptools.packages.find]
13+
where = ["src"]

src/baseline/README.md

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,19 @@ Despite MRI and topogram being available, this baseline uses only NAC-PET to kee
1414

1515
## Inference
1616

17-
Build and run with Docker:
17+
The baseline model has already been pretrained
18+
19+
Run with Docker:
1820

1921
```bash
20-
docker build -t bic-mac-baseline .
21-
timeout 5m docker run --rm --gpus 1 \
22-
--memory 120g \
22+
docker run ghcr.io/bic-mac-challenge/baseline \
23+
--memory 120g \
2324
-v /path/to/sub-XXX/features:/data/features:ro \
2425
-v /path/to/output:/data/output \
2526
bic-mac-baseline
2627
```
2728

28-
The predicted CT is written to `/data/output/ct.nii.gz`.
29-
30-
Inference uses sliding window with 192³ patches, 50% overlap, and Gaussian blending.
31-
32-
Without Docker:
29+
Or without Docker:
3330

3431
```bash
3532
pip install -r requirements.txt
@@ -46,3 +43,8 @@ python train.py
4643

4744
Weights are saved to `outputs/checkpoints/best_model.pth` (lowest validation loss).
4845

46+
Move new weights to `weights/best_model.pth` and build the image:
47+
48+
```bash
49+
docker build -t my-baseline .
50+
```

src/baseline/config.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
data_dir: /home/hinge_local/bic-mac-data/train
2-
1+
data_dir: /data/bic-mac-data/train
2+
cache_dir: /data/bic-mac-cache
33
batch_size: 1 #batch_size is actually 2, since monai samples two patches per volume
4-
num_workers: 8
4+
num_workers: 2
55

66
epochs: 250
77
learning_rate: 0.0003

src/baseline/requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ monai
22
nibabel
33
numpy
44
tqdm
5-
pyyaml
5+
pyyaml
6+
matplotlib

src/baseline/train.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import matplotlib.pyplot as plt
55
import torch.nn.functional as F
66

7-
from monai.data import Dataset, DataLoader
7+
from monai.data import Dataset, DataLoader, CacheDataset, PersistentDataset
88
from tqdm import tqdm
99

1010
from dataset import get_dataset
@@ -41,11 +41,18 @@ def main():
4141
cfg["patch_size"],
4242
)
4343

44-
print("Preparing dataset cache...")
45-
46-
dataset = Dataset(
44+
print("Preparing dataset ...")
45+
dataset = PersistentDataset(
4746
data=data,
4847
transform=transforms,
48+
cache_dir=cfg["cache_dir"]
49+
)
50+
51+
print("Caching dataset...")
52+
dataset = CacheDataset(
53+
data=dataset,
54+
cache_rate=1.0,
55+
num_workers=8,
4956
)
5057

5158
loader = DataLoader(
@@ -82,6 +89,8 @@ def main():
8289

8390
loss_history = []
8491

92+
print("Starting training...")
93+
8594
for epoch in range(cfg["epochs"]):
8695

8796
model.train()
File renamed without changes.

src/evaluation/__init__.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,14 @@
99
- Brain outlier robustness score
1010
- Organ bias
1111
- TAC bias
12-
"""
12+
"""
13+
14+
from .eval_dataset import eval_dataset
15+
from .eval_case import evaluate_case
16+
from .metrics import (
17+
compute_whole_body_suv_mae,
18+
compute_brain_outlier_score,
19+
compute_organ_bias_from_totalseg,
20+
compute_tac_bias,
21+
compute_whole_body_mu_mae,
22+
)

src/evaluation/eval_case.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import os
1616
import numpy as np
1717

18-
from metrics import (
18+
from .metrics import (
1919
compute_whole_body_suv_mae,
2020
compute_organ_bias_from_totalseg,
2121
compute_whole_body_mu_mae,

src/evaluation/eval_dataset.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626

2727
import numpy as np
2828

29-
from eval_case import evaluate_case
30-
from metrics import compute_brain_outlier_score
29+
from .eval_case import evaluate_case
30+
from .metrics import compute_brain_outlier_score
3131

3232

3333
def eval_dataset(dataset_path, pred_dir, subjects=None):

src/recon/main.py

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -203,17 +203,23 @@ def reconstruction_pipeline(
203203
recon_template = os.path.join(_recon_dir, "recon_OSEM_template.par")
204204
acf_forwardprojector = os.path.join(_recon_dir, "acf_forwardprojector.par")
205205

206-
reconstruction_pipeline(
207-
output_dir=output_dir,
208-
ct_path=args.ct,
209-
ct_face_and_bed_path=ct_face_and_bed_path,
210-
face_and_bed_mask_path=face_and_bed_mask_path,
211-
add_sino_path=add_sino_path,
212-
mult_sino_path=mult_sino_path,
213-
prompts_sino_path=prompts_sino_path,
214-
offset_json_path=offset_json_path,
215-
recon_template=recon_template,
216-
acf_forwardprojector=acf_forwardprojector,
217-
overwrite=args.overwrite,
218-
verbose=args.verbose,
219-
)
206+
log_path = os.path.join(output_dir, "intermediates", "recon.log")
207+
try:
208+
reconstruction_pipeline(
209+
output_dir=output_dir,
210+
ct_path=args.ct,
211+
ct_face_and_bed_path=ct_face_and_bed_path,
212+
face_and_bed_mask_path=face_and_bed_mask_path,
213+
add_sino_path=add_sino_path,
214+
mult_sino_path=mult_sino_path,
215+
prompts_sino_path=prompts_sino_path,
216+
offset_json_path=offset_json_path,
217+
recon_template=recon_template,
218+
acf_forwardprojector=acf_forwardprojector,
219+
overwrite=args.overwrite,
220+
verbose=args.verbose,
221+
)
222+
except Exception:
223+
if not args.verbose:
224+
print(f"\nReconstruction failed. Check {log_path} for details, or rerun with -v/--verbose for more output.", flush=True)
225+
raise

0 commit comments

Comments
 (0)