Skip to content

Commit

Permalink
Merge branch 'MIC-DKFZ:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
fitzjalen authored Jan 25, 2024
2 parents b9173c5 + 891483a commit 4026afe
Show file tree
Hide file tree
Showing 28 changed files with 625 additions and 497 deletions.
30 changes: 26 additions & 4 deletions documentation/dataset_format.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ T2 MRI, …) and FILE_ENDING is the file extension used by your image format (.p
The dataset.json file connects channel names with the channel identifiers in the 'channel_names' key (see below for details).

Side note: Typically, each channel/modality needs to be stored in a separate file and is accessed with the XXXX channel identifier.
Exception are natural images (RGB; .png) where the three color channels can all be stored in one file (see the [road segmentation](../nnunetv2/dataset_conversion/Dataset120_RoadSegmentation.py) dataset as an example).
Exception are natural images (RGB; .png) where the three color channels can all be stored in one file (see the
[road segmentation](../nnunetv2/dataset_conversion/Dataset120_RoadSegmentation.py) dataset as an example).

**Segmentations** must share the same geometry with their corresponding images (same shape etc.). Segmentations are
integer maps with each value representing a semantic class. The background must be 0. If there is no background, then
Expand Down Expand Up @@ -57,14 +58,14 @@ of what the raw data was provided in! This is for performance reasons.


By default, the following file formats are supported:

- NaturalImage2DIO: .png, .bmp, .tif
- NibabelIO: .nii.gz, .nrrd, .mha
- NibabelIOWithReorient: .nii.gz, .nrrd, .mha. This reader will reorient images to RAS!
- SimpleITKIO: .nii.gz, .nrrd, .mha
- Tiff3DIO: .tif, .tiff. 3D tif images! Since TIF does not have a standardized way of storing spacing information,
nnU-Net expects each TIF file to be accompanied by an identically named .json file that contains three numbers
(no units, no comma. Just separated by whitespace), one for each dimension.

nnU-Net expects each TIF file to be accompanied by an identically named .json file that contains this information (see
[here](#datasetjson)).

The file extension lists are not exhaustive and depend on what the backend supports. For example, nibabel and SimpleITK
support more than the three given here. The file endings given here are just the ones we tested!
Expand Down Expand Up @@ -200,6 +201,27 @@ There is a utility with which you can generate the dataset.json automatically. Y
[here](../nnunetv2/dataset_conversion/generate_dataset_json.py).
See our examples in [dataset_conversion](../nnunetv2/dataset_conversion) for how to use it. And read its documentation!

As described above, a json file that contains spacing information is required for TIFF files.
An example for a 3D TIFF stack with units corresponding to 7.6 in x and y, 80 in z is:

```
{
"spacing": [7.6, 7.6, 80.0]
}
```

Within the dataset folder, this file (named `cell6.json` in this example) would be placed in the following folders:

nnUNet_raw/Dataset123_Foo/
├── dataset.json
├── imagesTr
│   ├── cell6.json
│   └── cell6_0000.tif
└── labelsTr
├── cell6.json
└── cell6.tif


## How to use nnU-Net v1 Tasks
If you are migrating from the old nnU-Net, convert your existing datasets with `nnUNetv2_convert_old_nnUNet_dataset`!

Expand Down
2 changes: 1 addition & 1 deletion documentation/installation_instructions.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,6 @@ easy identification.

Note that these commands simply execute python scripts. If you installed nnU-Net in a virtual environment, this
environment must be activated when executing the commands. You can see what scripts/functions are executed by
checking the entry_points in the setup.py file.
checking the project.scripts in the [pyproject.toml](../pyproject.toml) file.

All nnU-Net commands have a `-h` option which gives information on how to use them.
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import os.path
import shutil
from copy import deepcopy
from functools import lru_cache
Expand Down Expand Up @@ -81,6 +80,10 @@ def __init__(self, dataset_name_or_id: Union[str, int],

self.plans = None

if isfile(join(self.raw_dataset_folder, 'splits_final.json')):
_maybe_copy_splits_file(join(self.raw_dataset_folder, 'splits_final.json'),
join(preprocessed_folder, 'splits_final.json'))

def determine_reader_writer(self):
example_image = self.dataset[self.dataset.keys().__iter__().__next__()]['images'][0]
return determine_reader_writer_from_dataset_json(self.dataset_json, example_image)
Expand Down Expand Up @@ -642,5 +645,23 @@ def load_plans(self, fname: str):
self.plans = load_json(fname)


def _maybe_copy_splits_file(splits_file: str, target_fname: str):
if not isfile(target_fname):
shutil.copy(splits_file, target_fname)
else:
# split already exists, do not copy, but check that the splits match.
# This code allows target_fname to contain more splits than splits_file. This is OK.
splits_source = load_json(splits_file)
splits_target = load_json(target_fname)
# all folds in the source file must match the target file
for i in range(len(splits_source)):
train_source = set(splits_source[i]['train'])
train_target = set(splits_target[i]['train'])
assert train_target == train_source
val_source = set(splits_source[i]['val'])
val_target = set(splits_target[i]['val'])
assert val_source == val_target


if __name__ == '__main__':
ExperimentPlanner(2, 8).plan_experiment()
9 changes: 4 additions & 5 deletions nnunetv2/experiment_planning/plan_and_preprocess_api.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
import shutil
from typing import List, Type, Optional, Tuple, Union

import nnunetv2
from batchgenerators.utilities.file_and_folder_operations import join, maybe_mkdir_p, subfiles, load_json
from batchgenerators.utilities.file_and_folder_operations import join, maybe_mkdir_p, load_json

import nnunetv2
from nnunetv2.configuration import default_num_processes
from nnunetv2.experiment_planning.dataset_fingerprint.fingerprint_extractor import DatasetFingerprintExtractor
from nnunetv2.experiment_planning.experiment_planners.default_experiment_planner import ExperimentPlanner
from nnunetv2.experiment_planning.verify_dataset_integrity import verify_dataset_integrity
from nnunetv2.paths import nnUNet_raw, nnUNet_preprocessed
from nnunetv2.utilities.dataset_name_id_conversion import convert_id_to_dataset_name, maybe_convert_to_dataset_name
from nnunetv2.utilities.dataset_name_id_conversion import convert_id_to_dataset_name
from nnunetv2.utilities.find_class_by_name import recursive_find_python_class
from nnunetv2.utilities.plans_handling.plans_handler import PlansManager
from nnunetv2.configuration import default_num_processes
from nnunetv2.utilities.utils import get_filenames_of_train_images_and_targets


Expand Down
2 changes: 1 addition & 1 deletion nnunetv2/experiment_planning/verify_dataset_integrity.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def check_cases(image_files: List[str], label_file: str, expected_num_channels:
if not np.allclose(spacing_seg, spacing_images):
print('Error: Spacing mismatch between segmentation and corresponding images. \nSpacing images: %s. '
'\nSpacing seg: %s. \nImage files: %s. \nSeg file: %s\n' %
(shape_image, shape_seg, image_files, label_file))
(spacing_images, spacing_seg, image_files, label_file))
ret = False

# check modalities
Expand Down
2 changes: 1 addition & 1 deletion nnunetv2/imageio/reader_writer_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from batchgenerators.utilities.file_and_folder_operations import join

import nnunetv2
from nnunetv2.imageio.natural_image_reager_writer import NaturalImage2DIO
from nnunetv2.imageio.natural_image_reader_writer import NaturalImage2DIO
from nnunetv2.imageio.nibabel_reader_writer import NibabelIO, NibabelIOWithReorient
from nnunetv2.imageio.simpleitk_reader_writer import SimpleITKIO
from nnunetv2.imageio.tif_reader_writer import Tiff3DIO
Expand Down
2 changes: 1 addition & 1 deletion nnunetv2/inference/examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
tile_step_size=0.5,
use_gaussian=True,
use_mirroring=True,
perform_everything_on_gpu=True,
perform_everything_on_device=True,
device=torch.device('cuda', 0),
verbose=False,
verbose_preprocessing=False,
Expand Down
Loading

0 comments on commit 4026afe

Please sign in to comment.