-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathtest_train.py
214 lines (193 loc) · 6.45 KB
/
test_train.py
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import pathlib
import tempfile
from dataclasses import field, make_dataclass
import pytest
from omegaconf import DictConfig, OmegaConf
from direct.config.defaults import (
CheckpointerConfig,
DefaultConfig,
FunctionConfig,
InferenceConfig,
LossConfig,
ModelConfig,
TrainingConfig,
ValidationConfig,
)
from direct.data.datasets_config import (
CropTransformConfig,
DatasetConfig,
MaskingConfig,
NormalizationTransformConfig,
SensitivityMapEstimationTransformConfig,
TransformsConfig,
)
from direct.launch import launch
from direct.train import setup_train
from direct.types import MaskFuncMode
def create_test_transform_cfg(transforms_type):
transforms_config = TransformsConfig(
normalization=NormalizationTransformConfig(scaling_key="masked_kspace"),
masking=MaskingConfig(name="FastMRIRandom", mode=MaskFuncMode.STATIC),
cropping=CropTransformConfig(crop="(32, 32)"),
sensitivity_map_estimation=SensitivityMapEstimationTransformConfig(estimate_sensitivity_maps=True),
transforms_type=transforms_type,
)
return transforms_config
def create_test_cfg(
train_dataset_shape,
val_dataset_shape,
train_batch_size,
val_batch_size,
loss_fns,
train_iters,
val_iters,
checkpointer_iters,
inference_batch_size,
transforms_type,
):
# Configs
train_transforms_config = create_test_transform_cfg(transforms_type)
new_class = make_dataclass(
"",
fields=[
("sample_size", int, field(init=False)),
("spatial_shape", list, field(init=False)),
("num_coils", int, field(init=False)),
],
bases=(DatasetConfig,),
)
train_dataset_config = DatasetConfig(
name="FakeMRIBlobs", transforms=train_transforms_config, text_description="training"
)
train_dataset_config.__class__ = new_class
train_dataset_config.sample_size = train_dataset_shape[0]
train_dataset_config.num_coils = train_dataset_shape[2]
train_dataset_config.spatial_shape = (train_dataset_shape[1],) + train_dataset_shape[3:]
val_transforms_config = create_test_transform_cfg("SUPERVISED")
val_dataset_config = DatasetConfig(
name="FakeMRIBlobs", transforms=val_transforms_config, text_description="validation"
)
val_dataset_config.__class__ = new_class
val_dataset_config.sample_size = val_dataset_shape[0]
val_dataset_config.num_coils = val_dataset_shape[2]
val_dataset_config.spatial_shape = (val_dataset_shape[1],) + val_dataset_shape[3:]
checkpointer_config = CheckpointerConfig(checkpoint_steps=checkpointer_iters)
loss_config = LossConfig(losses=[FunctionConfig(loss) for loss in loss_fns])
training_config = TrainingConfig(
loss=loss_config,
checkpointer=checkpointer_config,
num_iterations=train_iters,
validation_steps=val_iters,
datasets=[train_dataset_config],
batch_size=train_batch_size,
)
validation_config = ValidationConfig(crop=None, datasets=[val_dataset_config], batch_size=val_batch_size)
inference_config = InferenceConfig(dataset=DatasetConfig(name="FakeMRIBlobs"), batch_size=inference_batch_size)
model = ModelConfig(
model_name="unet.unet_2d.Unet2d", engine_name=None if transforms_type == "SUPERVISED" else "Unet2dSSLEngine"
)
config = DefaultConfig(
training=training_config, validation=validation_config, inference=inference_config, model=model
)
config.__class__ = make_dataclass(
"",
fields=[("additional_models", DictConfig, field(init=False))],
bases=(DefaultConfig,),
)
config.additional_models = DictConfig({"senistivity_model": ModelConfig(model_name="unet.unet_2d.UnetModel2d")})
return OmegaConf.create(config)
@pytest.mark.parametrize(
"train_dataset_shape, val_dataset_shape,",
[[(6, 5, 3, 120, 120), (6, 5, 3, 120, 120)]],
)
@pytest.mark.parametrize(
"train_batch_size, val_batch_size, inference_batch_size",
[[3, 3, 5]],
)
@pytest.mark.parametrize(
"loss_fns",
[["l1_loss", "ssim_loss", "l2_loss"]],
)
@pytest.mark.parametrize(
"train_iters, val_iters, checkpointer_iters",
[[41, 20, 20]],
)
@pytest.mark.parametrize(
"is_ssl",
[False, True],
)
def test_setup_train(
train_dataset_shape,
val_dataset_shape,
train_batch_size,
val_batch_size,
loss_fns,
train_iters,
val_iters,
checkpointer_iters,
inference_batch_size,
is_ssl,
):
cfg = create_test_cfg(
train_dataset_shape,
val_dataset_shape,
train_batch_size,
val_batch_size,
loss_fns,
train_iters,
val_iters,
checkpointer_iters,
inference_batch_size,
transforms_type="SSL_SSDU" if is_ssl else "SUPERVISED",
)
with tempfile.TemporaryDirectory() as tempdir:
cfg_filename = pathlib.Path(tempdir) / "cfg_test.yaml"
with open(cfg_filename, "w", encoding="utf-8") as f:
f.write(OmegaConf.to_yaml(cfg))
run_name = "test"
training_root = None
validation_root = None
base_directory = pathlib.Path(tempdir) / "base_dir"
num_machines = 1
num_gpus = 0
dist_url = "tcp://127.0.0.1:1234"
force_validation = False
initialization_checkpoint = None
initialization_images = None
initialization_kspace = None
noise = None
device = "cpu"
num_workers = 1
resume = False
machine_rank = 0
mixed_precision = False
debug = False
launch(
setup_train,
num_machines,
num_gpus,
machine_rank,
dist_url,
run_name,
training_root,
validation_root,
base_directory,
cfg_filename,
force_validation,
initialization_checkpoint,
initialization_images,
initialization_kspace,
noise,
device,
num_workers,
resume,
machine_rank,
mixed_precision,
debug,
)
save_directory = base_directory / run_name
assert cfg_filename.is_file()
for idx in range(checkpointer_iters, train_iters + 1, checkpointer_iters):
assert (save_directory / f"model_{idx}.pt").is_file()
for idx in range(val_iters, train_iters + 1, val_iters):
assert (save_directory / f"metrics_val_validation_{idx}.json").is_file()