1
|
|
|
# coding=utf-8 |
2
|
|
|
|
3
|
|
|
""" |
4
|
|
|
Tests for deepreg/_model/network/ddf_dvf.py |
5
|
|
|
""" |
6
|
|
|
import itertools |
7
|
|
|
from copy import deepcopy |
8
|
|
|
from unittest.mock import MagicMock, patch |
9
|
|
|
|
10
|
|
|
import pytest |
11
|
|
|
|
12
|
|
|
from deepreg.model.network import RegistrationModel |
13
|
|
|
from deepreg.registry import REGISTRY |
14
|
|
|
|
15
|
|
|
moving_image_size = (1, 3, 5) |
16
|
|
|
fixed_image_size = (2, 4, 6) |
17
|
|
|
index_size = 2 |
18
|
|
|
batch_size = 3 |
19
|
|
|
backbone_args = { |
20
|
|
|
"local": {"extract_levels": [1, 2]}, |
21
|
|
|
"global": {"extract_levels": [1, 2]}, |
22
|
|
|
"unet": {"depth": 2}, |
23
|
|
|
} |
24
|
|
|
config = { |
25
|
|
|
"backbone": { |
26
|
|
|
"num_channel_initial": 4, |
27
|
|
|
}, |
28
|
|
|
"loss": { |
29
|
|
|
"image": {"name": "lncc", "weight": 0.1}, |
30
|
|
|
"label": { |
31
|
|
|
"name": "dice", |
32
|
|
|
"weight": 1, |
33
|
|
|
"scales": [0, 1], |
34
|
|
|
}, |
35
|
|
|
"regularization": {"weight": 0.1, "name": "bending"}, |
36
|
|
|
}, |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
config_multiple_losses = deepcopy(config) |
40
|
|
|
config_multiple_losses["loss"]["image"] = [ |
41
|
|
|
{"name": "lncc", "weight": 0.1}, |
42
|
|
|
{"name": "ssd", "weight": 0.1}, |
43
|
|
|
{"name": "gmi", "weight": 0.1}, |
44
|
|
|
] |
45
|
|
|
|
46
|
|
|
|
47
|
|
|
@pytest.fixture |
48
|
|
|
def model(method: str, labeled: bool, backbone: str) -> RegistrationModel: |
49
|
|
|
""" |
50
|
|
|
A specific registration model object. |
51
|
|
|
|
52
|
|
|
:param method: name of method |
53
|
|
|
:param labeled: whether the data is labeled |
54
|
|
|
:param backbone: name of backbone |
55
|
|
|
:return: the built object |
56
|
|
|
""" |
57
|
|
|
copied = deepcopy(config) |
58
|
|
|
copied["method"] = method |
59
|
|
|
copied["backbone"]["name"] = backbone |
60
|
|
|
copied["backbone"] = {**backbone_args[backbone], **copied["backbone"]} |
61
|
|
|
return REGISTRY.build_model( |
62
|
|
|
config=dict( |
63
|
|
|
name=method, # TODO we store method twice |
64
|
|
|
moving_image_size=moving_image_size, |
65
|
|
|
fixed_image_size=fixed_image_size, |
66
|
|
|
index_size=index_size, |
67
|
|
|
labeled=labeled, |
68
|
|
|
batch_size=batch_size, |
69
|
|
|
config=copied, |
70
|
|
|
) |
71
|
|
|
) |
72
|
|
|
|
73
|
|
|
|
74
|
|
|
def pytest_generate_tests(metafunc): |
|
|
|
|
75
|
|
|
""" |
76
|
|
|
Test parameter generator. |
77
|
|
|
|
78
|
|
|
This function is called once per each test function. |
79
|
|
|
It takes the attribute `params` from the test class, |
80
|
|
|
and then use the same `params` for all tests inside the class. |
81
|
|
|
This is specific for test of registration models only. |
82
|
|
|
|
83
|
|
|
This is modified from the pytest documentation, |
84
|
|
|
where their version defined the params for each test function separately. |
85
|
|
|
|
86
|
|
|
https://docs.pytest.org/en/stable/example/parametrize.html#parametrizing-test-methods-through-per-class-configuration |
87
|
|
|
|
88
|
|
|
:param metafunc: |
89
|
|
|
:return: |
90
|
|
|
""" |
91
|
|
|
# |
92
|
|
|
funcarglist = metafunc.cls.params |
93
|
|
|
argnames = sorted(funcarglist[0]) |
94
|
|
|
metafunc.parametrize( |
95
|
|
|
argnames, [[funcargs[name] for name in argnames] for funcargs in funcarglist] |
96
|
|
|
) |
97
|
|
|
|
98
|
|
|
|
99
|
|
|
class TestRegistrationModel: |
|
|
|
|
100
|
|
|
params = [dict(labeled=True), dict(labeled=False)] |
101
|
|
|
|
102
|
|
|
@pytest.fixture |
103
|
|
|
def empty_model(self, labeled: bool) -> RegistrationModel: |
104
|
|
|
""" |
105
|
|
|
A RegistrationModel with build_model and build_loss mocked/overwritten. |
106
|
|
|
|
107
|
|
|
:param labeled: whether the data is labeled |
108
|
|
|
:return: the mocked object |
109
|
|
|
""" |
110
|
|
|
with patch.multiple( |
111
|
|
|
RegistrationModel, |
112
|
|
|
build_model=MagicMock(return_value=None), |
113
|
|
|
build_loss=MagicMock(return_value=None), |
114
|
|
|
): |
115
|
|
|
return RegistrationModel( |
116
|
|
|
moving_image_size=moving_image_size, |
117
|
|
|
fixed_image_size=fixed_image_size, |
118
|
|
|
index_size=index_size, |
119
|
|
|
labeled=labeled, |
120
|
|
|
batch_size=batch_size, |
121
|
|
|
config=dict(), |
122
|
|
|
) |
123
|
|
|
|
124
|
|
|
def test_get_config(self, empty_model, labeled): |
|
|
|
|
125
|
|
|
got = empty_model.get_config() |
126
|
|
|
assert got == dict( |
127
|
|
|
moving_image_size=moving_image_size, |
128
|
|
|
fixed_image_size=fixed_image_size, |
129
|
|
|
index_size=index_size, |
130
|
|
|
labeled=labeled, |
131
|
|
|
batch_size=batch_size, |
132
|
|
|
config=dict(), |
133
|
|
|
num_devices=1, |
134
|
|
|
name="RegistrationModel", |
135
|
|
|
) |
136
|
|
|
|
137
|
|
|
def test_build_inputs(self, empty_model, labeled): |
|
|
|
|
138
|
|
|
inputs = empty_model.build_inputs() |
139
|
|
|
expected_inputs_len = 5 if labeled else 3 |
140
|
|
|
assert len(inputs) == expected_inputs_len |
141
|
|
|
|
142
|
|
|
moving_image = inputs["moving_image"] |
143
|
|
|
fixed_image = inputs["fixed_image"] |
144
|
|
|
indices = inputs["indices"] |
145
|
|
|
assert moving_image.shape == (batch_size, *moving_image_size) |
146
|
|
|
assert fixed_image.shape == (batch_size, *fixed_image_size) |
147
|
|
|
assert indices.shape == (batch_size, index_size) |
148
|
|
|
|
149
|
|
|
if labeled: |
150
|
|
|
moving_label = inputs["moving_label"] |
151
|
|
|
fixed_label = inputs["fixed_label"] |
152
|
|
|
assert moving_label.shape == (batch_size, *moving_image_size) |
153
|
|
|
assert fixed_label.shape == (batch_size, *fixed_image_size) |
154
|
|
|
|
155
|
|
|
def test_concat_images(self, empty_model, labeled): |
|
|
|
|
156
|
|
|
inputs = empty_model.build_inputs() |
157
|
|
|
moving_image = inputs["moving_image"] |
158
|
|
|
fixed_image = inputs["fixed_image"] |
159
|
|
|
if labeled: |
160
|
|
|
moving_label = inputs["moving_label"] |
161
|
|
|
images = empty_model.concat_images(moving_image, fixed_image, moving_label) |
162
|
|
|
assert images.shape == (batch_size, *fixed_image_size, 3) |
163
|
|
|
else: |
164
|
|
|
images = empty_model.concat_images(moving_image, fixed_image) |
165
|
|
|
assert images.shape == (batch_size, *fixed_image_size, 2) |
166
|
|
|
|
167
|
|
|
|
168
|
|
|
class TestBuildLoss: |
|
|
|
|
169
|
|
|
params = [ |
170
|
|
|
dict(config=config, option=0, expected=2), |
171
|
|
|
dict(config=config, option=1, expected=2), |
172
|
|
|
dict(config=config, option=2, expected=3), |
173
|
|
|
dict(config=config_multiple_losses, option=3, expected=5), |
174
|
|
|
] |
175
|
|
|
|
176
|
|
|
def test_image_loss(self, config: dict, option: int, expected: int): |
|
|
|
|
177
|
|
|
method = "ddf" |
178
|
|
|
backbone = "local" |
179
|
|
|
labeled = True |
180
|
|
|
copied = deepcopy(config) |
181
|
|
|
copied["method"] = method |
182
|
|
|
copied["backbone"]["name"] = backbone |
183
|
|
|
copied["backbone"] = {**backbone_args[backbone], **copied["backbone"]} |
184
|
|
|
|
185
|
|
|
if option == 0: |
186
|
|
|
# remove image loss config, so loss is not used |
187
|
|
|
copied["loss"].pop("image") |
188
|
|
|
elif option == 1: |
189
|
|
|
# set image loss weight to zero, so loss is not used |
190
|
|
|
copied["loss"]["image"]["weight"] = 0.0 |
191
|
|
|
elif option == 2: |
192
|
|
|
# remove image loss weight, so loss is used with default weight 1 |
193
|
|
|
copied["loss"]["image"].pop("weight") |
194
|
|
|
|
195
|
|
|
ddf_model = REGISTRY.build_model( |
196
|
|
|
config=dict( |
197
|
|
|
name=method, # TODO we store method twice |
198
|
|
|
moving_image_size=moving_image_size, |
199
|
|
|
fixed_image_size=fixed_image_size, |
200
|
|
|
index_size=index_size, |
201
|
|
|
labeled=labeled, |
202
|
|
|
batch_size=batch_size, |
203
|
|
|
config=copied, |
204
|
|
|
) |
205
|
|
|
) |
206
|
|
|
|
207
|
|
|
assert len(ddf_model._model.losses) == expected |
|
|
|
|
208
|
|
|
|
209
|
|
|
|
210
|
|
|
class TestDDFModel: |
|
|
|
|
211
|
|
|
params = [ |
212
|
|
|
dict(method=method, labeled=labeled, backbone=backbone) |
213
|
|
|
for method, labeled, backbone in itertools.product( |
214
|
|
|
["ddf"], [True, False], ["local", "global", "unet"] |
215
|
|
|
) |
216
|
|
|
] |
217
|
|
|
|
218
|
|
View Code Duplication |
def test_build_model(self, model, labeled, backbone): |
|
|
|
|
219
|
|
|
expected_outputs_len = 3 if labeled else 2 |
220
|
|
|
if backbone == "global": |
221
|
|
|
expected_outputs_len += 1 |
222
|
|
|
theta = model._outputs["theta"] |
|
|
|
|
223
|
|
|
assert theta.shape == (batch_size, 4, 3) |
224
|
|
|
assert len(model._outputs) == expected_outputs_len |
|
|
|
|
225
|
|
|
|
226
|
|
|
ddf = model._outputs["ddf"] |
|
|
|
|
227
|
|
|
pred_fixed_image = model._outputs["pred_fixed_image"] |
|
|
|
|
228
|
|
|
assert ddf.shape == (batch_size, *fixed_image_size, 3) |
229
|
|
|
assert pred_fixed_image.shape == (batch_size, *fixed_image_size) |
230
|
|
|
|
231
|
|
|
if labeled: |
232
|
|
|
pred_fixed_label = model._outputs["pred_fixed_label"] |
|
|
|
|
233
|
|
|
assert pred_fixed_label.shape == (batch_size, *fixed_image_size) |
234
|
|
|
|
235
|
|
|
def test_build_loss(self, model, labeled, backbone): |
|
|
|
|
236
|
|
|
expected = 3 if labeled else 2 |
237
|
|
|
assert len(model._model.losses) == expected |
|
|
|
|
238
|
|
|
|
239
|
|
|
def test_postprocess(self, model, labeled, backbone): |
|
|
|
|
240
|
|
|
indices, processed = model.postprocess( |
241
|
|
|
inputs=model._inputs, outputs=model._outputs |
|
|
|
|
242
|
|
|
) |
243
|
|
|
assert indices.shape == (batch_size, index_size) |
244
|
|
|
expected = 7 if labeled else 4 |
245
|
|
|
if backbone == "global": |
246
|
|
|
expected += 1 |
247
|
|
|
assert len(processed) == expected |
248
|
|
|
|
249
|
|
|
|
250
|
|
|
class TestDVFModel: |
|
|
|
|
251
|
|
|
params = [ |
252
|
|
|
dict(method=method, labeled=labeled, backbone=backbone) |
253
|
|
|
for method, labeled, backbone in itertools.product( |
254
|
|
|
["dvf"], [True, False], ["local", "unet"] |
255
|
|
|
) |
256
|
|
|
] |
257
|
|
|
|
258
|
|
View Code Duplication |
def test_build_model(self, model, labeled, backbone): |
|
|
|
|
259
|
|
|
expected_outputs_len = 4 if labeled else 3 |
260
|
|
|
assert len(model._outputs) == expected_outputs_len |
|
|
|
|
261
|
|
|
|
262
|
|
|
dvf = model._outputs["dvf"] |
|
|
|
|
263
|
|
|
ddf = model._outputs["ddf"] |
|
|
|
|
264
|
|
|
pred_fixed_image = model._outputs["pred_fixed_image"] |
|
|
|
|
265
|
|
|
assert dvf.shape == (batch_size, *fixed_image_size, 3) |
266
|
|
|
assert ddf.shape == (batch_size, *fixed_image_size, 3) |
267
|
|
|
assert pred_fixed_image.shape == (batch_size, *fixed_image_size) |
268
|
|
|
|
269
|
|
|
if labeled: |
270
|
|
|
pred_fixed_label = model._outputs["pred_fixed_label"] |
|
|
|
|
271
|
|
|
assert pred_fixed_label.shape == (batch_size, *fixed_image_size) |
272
|
|
|
|
273
|
|
|
def test_build_loss(self, model, labeled, backbone): |
|
|
|
|
274
|
|
|
expected = 3 if labeled else 2 |
275
|
|
|
assert len(model._model.losses) == expected |
|
|
|
|
276
|
|
|
|
277
|
|
|
def test_postprocess(self, model, labeled, backbone): |
|
|
|
|
278
|
|
|
indices, processed = model.postprocess( |
279
|
|
|
inputs=model._inputs, outputs=model._outputs |
|
|
|
|
280
|
|
|
) |
281
|
|
|
assert indices.shape == (batch_size, index_size) |
282
|
|
|
expected = 8 if labeled else 5 |
283
|
|
|
assert len(processed) == expected |
284
|
|
|
|
285
|
|
|
|
286
|
|
|
class TestConditionalModel: |
|
|
|
|
287
|
|
|
params = [ |
288
|
|
|
dict(method=method, labeled=labeled, backbone=backbone) |
289
|
|
|
for method, labeled, backbone in itertools.product( |
290
|
|
|
["conditional"], [True], ["local", "unet"] |
291
|
|
|
) |
292
|
|
|
] |
293
|
|
|
|
294
|
|
|
def test_build_model(self, model, labeled, backbone): |
|
|
|
|
295
|
|
|
assert len(model._outputs) == 1 |
|
|
|
|
296
|
|
|
pred_fixed_label = model._outputs["pred_fixed_label"] |
|
|
|
|
297
|
|
|
assert pred_fixed_label.shape == (batch_size, *fixed_image_size) |
298
|
|
|
|
299
|
|
|
def test_build_loss(self, model, labeled, backbone): |
|
|
|
|
300
|
|
|
assert len(model._model.losses) == 1 |
|
|
|
|
301
|
|
|
|
302
|
|
|
def test_postprocess(self, model, labeled, backbone): |
|
|
|
|
303
|
|
|
indices, processed = model.postprocess( |
304
|
|
|
inputs=model._inputs, outputs=model._outputs |
|
|
|
|
305
|
|
|
) |
306
|
|
|
assert indices.shape == (batch_size, index_size) |
307
|
|
|
assert len(processed) == 5 |
308
|
|
|
|