-
Notifications
You must be signed in to change notification settings - Fork 92
Expand file tree
/
Copy pathtest_brats_mri_axial_slices_generative_diffusion.py
More file actions
167 lines (147 loc) · 6.3 KB
/
test_brats_mri_axial_slices_generative_diffusion.py
File metadata and controls
167 lines (147 loc) · 6.3 KB
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
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# Copyright (c) MONAI Consortium
# 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 json
import os
import shutil
import sys
import tempfile
import unittest
import nibabel as nib
import numpy as np
from monai.bundle import ConfigWorkflow
from parameterized import parameterized
TEST_CASE_1 = [
{
"bundle_root": "models/brats_mri_axial_slices_generative_diffusion",
"images": "$list(sorted(glob.glob(@dataset_dir + '/image_*.nii.gz')))",
"labels": "$list(sorted(glob.glob(@dataset_dir + '/label_*.nii.gz')))",
"train#trainer#max_epochs": 1,
"train#dataset#cache_rate": 0.0,
"train_batch_size_slice": 4,
}
]
TEST_CASE_2 = [{"bundle_root": "models/brats_mri_axial_slices_generative_diffusion"}]
def test_order(test_name1, test_name2):
# specify test order.
# The "train_autoencoder.json" config should be tested first in order to
# produce model weights for inference, and train diffusion.
def get_order(name):
if "autoencoder" in name:
return 1 if "train" in name else 2
if "diffusion" in name:
return 3 if "train" in name else 4
return 5
return get_order(test_name1) - get_order(test_name2)
class TestLdm2d(unittest.TestCase):
def setUp(self):
self.dataset_dir = tempfile.mkdtemp()
dataset_size = 10
input_shape = (256, 256, 112)
sub_dir = os.path.join(self.dataset_dir, "Task01_BrainTumour")
os.makedirs(sub_dir)
data_list = []
for s in range(dataset_size):
test_image = np.random.randint(low=0, high=2, size=input_shape).astype(np.int8)
test_label = np.random.randint(low=0, high=2, size=input_shape).astype(np.int8)
image_filename = os.path.join(self.dataset_dir, f"image_{s}.nii.gz")
label_filename = os.path.join(self.dataset_dir, f"label_{s}.nii.gz")
nib.save(nib.Nifti1Image(test_image, np.eye(4)), image_filename)
nib.save(nib.Nifti1Image(test_label, np.eye(4)), label_filename)
sample_dict = {"image": image_filename, "label": label_filename}
data_list.append(sample_dict)
# prepare a datalist file that "monai.apps.DecathlonDataset" requires
full_dict = {
"name": "",
"description": "",
"reference": "",
"licence": "",
"tensorImageSize": "",
"modality": "",
"labels": "",
"numTraining": 10,
"numTest": 0,
"training": data_list,
}
with open(os.path.join(sub_dir, "dataset.json"), "w") as f:
json.dump(full_dict, f)
def tearDown(self):
shutil.rmtree(self.dataset_dir)
@parameterized.expand([TEST_CASE_1])
def test_autoencoder_train(self, override):
override["dataset_dir"] = self.dataset_dir
bundle_root = override["bundle_root"]
sys.path = [bundle_root] + sys.path
trainer = ConfigWorkflow(
workflow="train",
config_file=os.path.join(bundle_root, "configs/train_autoencoder.json"),
logging_file=os.path.join(bundle_root, "configs/logging.conf"),
meta_file=os.path.join(bundle_root, "configs/metadata.json"),
**override,
)
trainer.initialize()
trainer.run()
trainer.finalize()
@parameterized.expand([TEST_CASE_2])
def test_autoencoder_infer(self, override):
override["dataset_dir"] = self.dataset_dir
bundle_root = override["bundle_root"]
sys.path = [bundle_root] + sys.path
inferrer = ConfigWorkflow(
workflow="infer",
config_file=os.path.join(bundle_root, "configs/inference_autoencoder.json"),
logging_file=os.path.join(bundle_root, "configs/logging.conf"),
meta_file=os.path.join(bundle_root, "configs/metadata.json"),
**override,
)
inferrer.initialize()
inferrer.run()
inferrer.finalize()
@parameterized.expand([TEST_CASE_1])
def test_diffusion_train(self, override):
override["dataset_dir"] = self.dataset_dir
bundle_root = override["bundle_root"]
sys.path = [bundle_root] + sys.path
autoencoder_file = os.path.join(bundle_root, "configs/train_autoencoder.json")
diffusion_file = os.path.join(bundle_root, "configs/train_diffusion.json")
trainer = ConfigWorkflow(
workflow="train",
config_file=[autoencoder_file, diffusion_file],
logging_file=os.path.join(bundle_root, "configs/logging.conf"),
meta_file=os.path.join(bundle_root, "configs/metadata.json"),
**override,
)
trainer.initialize()
# TODO: uncomment the following check after we have monai > 1.2.0
# https://github.com/Project-MONAI/MONAI/issues/6602
# check_result = trainer.check_properties()
# if check_result is not None and len(check_result) > 0:
# raise ValueError(f"check properties for overrided train config failed: {check_result}")
trainer.run()
trainer.finalize()
@parameterized.expand([TEST_CASE_2])
def test_diffusion_infer(self, override):
override["dataset_dir"] = self.dataset_dir
bundle_root = override["bundle_root"]
sys.path = [bundle_root] + sys.path
inferrer = ConfigWorkflow(
workflow="infer",
config_file=os.path.join(bundle_root, "configs/inference.json"),
logging_file=os.path.join(bundle_root, "configs/logging.conf"),
meta_file=os.path.join(bundle_root, "configs/metadata.json"),
**override,
)
inferrer.initialize()
inferrer.run()
inferrer.finalize()
if __name__ == "__main__":
loader = unittest.TestLoader()
loader.sortTestMethodsUsing = test_order
unittest.main(testLoader=loader)