convert_cls2fg.py 3.68 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
26
27
"""
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 argparse
import shutil
import sys
from pathlib import Path
from tokenize import PseudoExtras

from hydra.experimental import initialize_config_module
from loguru import logger

from nndet.io import get_task, load_json, save_json
from nndet.utils.config import compose, load_dataset_info
28
from nndet.utils.check import env_guard
mibaumgartner's avatar
mibaumgartner committed
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
95
96
97
98
99
100
101
102


def convert_raw(task, overwrite, ov):
    task_name_full = get_task(task, name=True)
    task_num, task_name = task_name_full[4:].split('_', 1)
    new_task_name_full = f"Task{task_num}FG_{task_name}"

    cfg = compose(task, "config.yaml", overrides=ov if ov is not None else [])
    print(cfg.pretty())

    source_splitted_dir = Path(cfg["host"]["splitted_4d_output_dir"])
    target_splitted_dir = Path(str(source_splitted_dir).replace(task_name_full, new_task_name_full))
    if target_splitted_dir.is_dir() and overwrite:
        shutil.rmtree(target_splitted_dir)
    target_splitted_dir.mkdir(parents=True)

    logger.remove()
    logger.add(sys.stdout, level="INFO")
    logger.add(target_splitted_dir.parent / "convert_cls2fg.log", level="DEBUG")

    # update dataset_info
    source_data_info = Path(cfg["host"]["data_dir"])
    data_info = load_dataset_info(source_data_info)
    data_info.pop("labels")
    data_info["labels"] = {"0": "fg"}
    data_info["task"] = new_task_name_full
    save_json(data_info, target_splitted_dir.parent / "dataset.json", indent=4)

    for postfix in ["Tr", "Ts"]:
        source_image_dir = source_splitted_dir / f"images{postfix}"
        source_label_dir = source_splitted_dir / f"labels{postfix}"

        if not source_image_dir.is_dir():
            logger.info(f"{source_image_dir} is not a dir. Skipping it.")
            continue

        # copy images and labels
        shutil.copytree(source_image_dir, target_splitted_dir / f"images{postfix}")
        shutil.copytree(source_label_dir, target_splitted_dir / f"labels{postfix}")

        # remap properties file to foreground class
        target_label_dir = target_splitted_dir / f"labels{postfix}"
        for f in [l for l in target_label_dir.glob("*.json")]:
            props = load_json(f)
            props["instances"] = {key: 0 for key in props["instances"].keys()}
            save_json(props, f)


@env_guard
def main():
    """
    Convert raw splitted data with class sensitive annotations into
    a new dataset which only distinguishes fg and bg
    """
    parser = argparse.ArgumentParser()
    parser.add_argument('tasks', type=str, nargs='+',
                        help="Single or multiple task identifiers to process consecutively",
                        )
    parser.add_argument('--overwrite', action='store_true')
    parser.add_argument('-o', '--overwrites', type=str, nargs='+',
                        help="overwrites for config file",
                        required=False)
    args = parser.parse_args()
    tasks = args.tasks
    ov = args.overwrites
    overwrite = args.overwrite
    initialize_config_module(config_module="nndet.conf")

    for task in tasks:
        convert_raw(task, overwrite, ov)


if __name__ == '__main__':
    main()