nnunet_export.py 3.32 KB
Newer Older
mibaumgartner's avatar
mibaumgartner committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
"""
Copyright 2020 Division of Medical Image Computing, German Cancer Research Center (DKFZ), Heidelberg, Germany

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""

import copy
import os
import argparse
import sys

from pathlib import Path

from loguru import logger
from omegaconf import OmegaConf
26
from hydra import initialize_config_module
mibaumgartner's avatar
mibaumgartner committed
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94

from nnunet.paths import nnUNet_raw_data

from nndet.io import get_task
from nndet.utils.config import compose
from nndet.utils.nnunet import Exporter


def run(cfg, target_dir, stuff: bool):
    base_dir = Path(cfg.host.splitted_4d_output_dir)
    target_dir.mkdir(exist_ok=True, parents=True)

    if (base_dir / "imagesTs").is_dir():
        logger.info("Found test images and will export them too")
        ts_image_dir = base_dir / "imagesTs"
    else:
        ts_image_dir = None

    exporter = Exporter(data_info=OmegaConf.to_container(cfg.data),
                        tr_image_dir=base_dir / "imagesTr",
                        ts_image_dir=ts_image_dir,
                        label_dir=base_dir / "labelsTr",
                        target_dir=target_dir,
                        export_stuff=stuff,
                        ).export()


if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('tasks', type=str, nargs='+',
                        help="Single or multiple task identifiers to process consecutively",
                        )
    parser.add_argument('-nt', '--new_tasks', type=str, nargs='+',
                        help="Rename the tasks.",
                        required=False, default=None,
                        )
    parser.add_argument("--stuff", action='store_true',
                        help="Export stuff and things classes."
                             "The final detection evaluation will be performed on things classes only.")
    parser.add_argument('-o', '--overwrites', type=str, nargs='+',
                        help="overwrites for config file",
                        required=False,
                        )

    args = parser.parse_args()
    tasks = args.tasks
    new_tasks = args.new_tasks
    ov = args.overwrites
    stuff = args.stuff
    print(f"Overwrites: {ov}")
    initialize_config_module(config_module="nndet.conf")

    if new_tasks is None:
        new_tasks = tasks

    for task, new_task in zip(tasks, new_tasks):
        task = get_task(task, name=True)

        if nnUNet_raw_data is None:
            raise RuntimeError(f"Please set `nnUNet_raw_data` for nnUNet!")
        target_dir = Path(nnUNet_raw_data) / new_task

        logger.remove()
        logger.add(sys.stdout, level="INFO")
        logger.add(target_dir / "nnunet_export.log", level="DEBUG")

        _ov = copy.deepcopy(ov) if ov is not None else []
        cfg = compose(task, "config.yaml", overrides=ov if ov is not None else [])
mibaumgartner's avatar
mibaumgartner committed
95
        print(cfg)
mibaumgartner's avatar
mibaumgartner committed
96
        run(cfg, target_dir, stuff=stuff)