monai-weekly 1.5.dev2511__py3-none-any.whl → 1.5.dev2513__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- monai/__init__.py +1 -1
- monai/_version.py +3 -3
- monai/data/utils.py +1 -1
- monai/metrics/meandice.py +132 -76
- monai/networks/blocks/__init__.py +2 -1
- monai/networks/blocks/cablock.py +182 -0
- monai/networks/blocks/downsample.py +241 -2
- monai/networks/nets/restormer.py +337 -0
- monai/networks/utils.py +44 -1
- monai/utils/__init__.py +1 -0
- monai/utils/enums.py +13 -0
- monai/utils/misc.py +1 -1
- {monai_weekly-1.5.dev2511.dist-info → monai_weekly-1.5.dev2513.dist-info}/METADATA +3 -2
- {monai_weekly-1.5.dev2511.dist-info → monai_weekly-1.5.dev2513.dist-info}/RECORD +22 -17
- {monai_weekly-1.5.dev2511.dist-info → monai_weekly-1.5.dev2513.dist-info}/WHEEL +1 -1
- tests/metrics/test_compute_meandice.py +3 -3
- tests/networks/blocks/test_CABlock.py +150 -0
- tests/networks/blocks/test_downsample_block.py +184 -0
- tests/networks/nets/test_restormer.py +147 -0
- tests/networks/utils/test_pixelunshuffle.py +51 -0
- tests/integration/test_downsample_block.py +0 -50
- {monai_weekly-1.5.dev2511.dist-info → monai_weekly-1.5.dev2513.dist-info/licenses}/LICENSE +0 -0
- {monai_weekly-1.5.dev2511.dist-info → monai_weekly-1.5.dev2513.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,147 @@
|
|
1
|
+
# Copyright (c) MONAI Consortium
|
2
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
3
|
+
# you may not use this file except in compliance with the License.
|
4
|
+
# You may obtain a copy of the License at
|
5
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
6
|
+
# Unless required by applicable law or agreed to in writing, software
|
7
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
8
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
9
|
+
# See the License for the specific language governing permissions and
|
10
|
+
# limitations under the License.
|
11
|
+
|
12
|
+
from __future__ import annotations
|
13
|
+
|
14
|
+
import unittest
|
15
|
+
from unittest import skipUnless
|
16
|
+
|
17
|
+
import torch
|
18
|
+
from parameterized import parameterized
|
19
|
+
|
20
|
+
from monai.networks import eval_mode
|
21
|
+
from monai.networks.nets.restormer import MDTATransformerBlock, OverlapPatchEmbed, Restormer
|
22
|
+
from monai.utils import optional_import
|
23
|
+
|
24
|
+
einops, has_einops = optional_import("einops")
|
25
|
+
|
26
|
+
TEST_CASES_TRANSFORMER = [
|
27
|
+
# [spatial_dims, dim, num_heads, ffn_factor, bias, layer_norm_use_bias, flash_attn, input_shape]
|
28
|
+
[2, 48, 8, 2.66, True, True, False, (2, 48, 64, 64)],
|
29
|
+
[2, 96, 8, 2.66, False, False, False, (2, 96, 32, 32)],
|
30
|
+
[3, 48, 4, 2.66, True, True, False, (2, 48, 32, 32, 32)],
|
31
|
+
[3, 96, 8, 2.66, False, False, True, (2, 96, 16, 16, 16)],
|
32
|
+
]
|
33
|
+
|
34
|
+
TEST_CASES_PATCHEMBED = [
|
35
|
+
# spatial_dims, in_channels, embed_dim, input_shape, expected_shape
|
36
|
+
[2, 1, 48, (2, 1, 64, 64), (2, 48, 64, 64)],
|
37
|
+
[2, 3, 96, (2, 3, 32, 32), (2, 96, 32, 32)],
|
38
|
+
[3, 1, 48, (2, 1, 32, 32, 32), (2, 48, 32, 32, 32)],
|
39
|
+
[3, 4, 64, (2, 4, 16, 16, 16), (2, 64, 16, 16, 16)],
|
40
|
+
]
|
41
|
+
|
42
|
+
RESTORMER_CONFIGS = [
|
43
|
+
# 2-level architecture
|
44
|
+
{"num_blocks": [1, 1], "heads": [1, 1]},
|
45
|
+
{"num_blocks": [2, 1], "heads": [2, 1]},
|
46
|
+
# 3-level architecture
|
47
|
+
{"num_blocks": [1, 1, 1], "heads": [1, 1, 1]},
|
48
|
+
{"num_blocks": [2, 1, 1], "heads": [2, 1, 1]},
|
49
|
+
]
|
50
|
+
|
51
|
+
TEST_CASES_RESTORMER = []
|
52
|
+
for config in RESTORMER_CONFIGS:
|
53
|
+
# 2D cases
|
54
|
+
TEST_CASES_RESTORMER.extend(
|
55
|
+
[
|
56
|
+
[
|
57
|
+
{
|
58
|
+
"spatial_dims": 2,
|
59
|
+
"in_channels": 1,
|
60
|
+
"out_channels": 1,
|
61
|
+
"dim": 48,
|
62
|
+
"num_blocks": config["num_blocks"],
|
63
|
+
"heads": config["heads"],
|
64
|
+
"num_refinement_blocks": 2,
|
65
|
+
"ffn_expansion_factor": 1.5,
|
66
|
+
},
|
67
|
+
(2, 1, 64, 64),
|
68
|
+
(2, 1, 64, 64),
|
69
|
+
],
|
70
|
+
# 3D cases
|
71
|
+
[
|
72
|
+
{
|
73
|
+
"spatial_dims": 3,
|
74
|
+
"in_channels": 1,
|
75
|
+
"out_channels": 1,
|
76
|
+
"dim": 16,
|
77
|
+
"num_blocks": config["num_blocks"],
|
78
|
+
"heads": config["heads"],
|
79
|
+
"num_refinement_blocks": 2,
|
80
|
+
"ffn_expansion_factor": 1.5,
|
81
|
+
},
|
82
|
+
(2, 1, 32, 32, 32),
|
83
|
+
(2, 1, 32, 32, 32),
|
84
|
+
],
|
85
|
+
]
|
86
|
+
)
|
87
|
+
|
88
|
+
|
89
|
+
class TestMDTATransformerBlock(unittest.TestCase):
|
90
|
+
|
91
|
+
@parameterized.expand(TEST_CASES_TRANSFORMER)
|
92
|
+
@skipUnless(has_einops, "Requires einops")
|
93
|
+
def test_shape(self, spatial_dims, dim, heads, ffn_factor, bias, layer_norm_use_bias, flash, shape):
|
94
|
+
if flash and not torch.cuda.is_available():
|
95
|
+
self.skipTest("Flash attention requires CUDA")
|
96
|
+
block = MDTATransformerBlock(
|
97
|
+
spatial_dims=spatial_dims,
|
98
|
+
dim=dim,
|
99
|
+
num_heads=heads,
|
100
|
+
ffn_expansion_factor=ffn_factor,
|
101
|
+
bias=bias,
|
102
|
+
layer_norm_use_bias=layer_norm_use_bias,
|
103
|
+
flash_attention=flash,
|
104
|
+
)
|
105
|
+
with eval_mode(block):
|
106
|
+
x = torch.randn(shape)
|
107
|
+
output = block(x)
|
108
|
+
self.assertEqual(output.shape, x.shape)
|
109
|
+
|
110
|
+
|
111
|
+
class TestOverlapPatchEmbed(unittest.TestCase):
|
112
|
+
|
113
|
+
@parameterized.expand(TEST_CASES_PATCHEMBED)
|
114
|
+
def test_shape(self, spatial_dims, in_channels, embed_dim, input_shape, expected_shape):
|
115
|
+
net = OverlapPatchEmbed(spatial_dims=spatial_dims, in_channels=in_channels, embed_dim=embed_dim)
|
116
|
+
with eval_mode(net):
|
117
|
+
result = net(torch.randn(input_shape))
|
118
|
+
self.assertEqual(result.shape, expected_shape)
|
119
|
+
|
120
|
+
|
121
|
+
class TestRestormer(unittest.TestCase):
|
122
|
+
|
123
|
+
@parameterized.expand(TEST_CASES_RESTORMER)
|
124
|
+
@skipUnless(has_einops, "Requires einops")
|
125
|
+
def test_shape(self, input_param, input_shape, expected_shape):
|
126
|
+
if input_param.get("flash_attention", False) and not torch.cuda.is_available():
|
127
|
+
self.skipTest("Flash attention requires CUDA")
|
128
|
+
net = Restormer(**input_param)
|
129
|
+
with eval_mode(net):
|
130
|
+
result = net(torch.randn(input_shape))
|
131
|
+
self.assertEqual(result.shape, expected_shape)
|
132
|
+
|
133
|
+
@skipUnless(has_einops, "Requires einops")
|
134
|
+
def test_small_input_error_2d(self):
|
135
|
+
net = Restormer(spatial_dims=2, in_channels=1, out_channels=1)
|
136
|
+
with self.assertRaises(AssertionError):
|
137
|
+
net(torch.randn(1, 1, 8, 8))
|
138
|
+
|
139
|
+
@skipUnless(has_einops, "Requires einops")
|
140
|
+
def test_small_input_error_3d(self):
|
141
|
+
net = Restormer(spatial_dims=3, in_channels=1, out_channels=1)
|
142
|
+
with self.assertRaises(AssertionError):
|
143
|
+
net(torch.randn(1, 1, 8, 8, 8))
|
144
|
+
|
145
|
+
|
146
|
+
if __name__ == "__main__":
|
147
|
+
unittest.main()
|
@@ -0,0 +1,51 @@
|
|
1
|
+
# Copyright (c) MONAI Consortium
|
2
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
3
|
+
# you may not use this file except in compliance with the License.
|
4
|
+
# You may obtain a copy of the License at
|
5
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
6
|
+
# Unless required by applicable law or agreed to in writing, software
|
7
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
8
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
9
|
+
# See the License for the specific language governing permissions and
|
10
|
+
# limitations under the License.
|
11
|
+
|
12
|
+
from __future__ import annotations
|
13
|
+
|
14
|
+
import unittest
|
15
|
+
|
16
|
+
import torch
|
17
|
+
|
18
|
+
from monai.networks.utils import pixelshuffle, pixelunshuffle
|
19
|
+
|
20
|
+
|
21
|
+
class TestPixelUnshuffle(unittest.TestCase):
|
22
|
+
|
23
|
+
def test_2d_basic(self):
|
24
|
+
x = torch.randn(2, 4, 16, 16)
|
25
|
+
out = pixelunshuffle(x, spatial_dims=2, scale_factor=2)
|
26
|
+
self.assertEqual(out.shape, (2, 16, 8, 8))
|
27
|
+
|
28
|
+
def test_3d_basic(self):
|
29
|
+
x = torch.randn(2, 4, 16, 16, 16)
|
30
|
+
out = pixelunshuffle(x, spatial_dims=3, scale_factor=2)
|
31
|
+
self.assertEqual(out.shape, (2, 32, 8, 8, 8))
|
32
|
+
|
33
|
+
def test_non_square_input(self):
|
34
|
+
x = torch.arange(192).reshape(1, 2, 12, 8)
|
35
|
+
out = pixelunshuffle(x, spatial_dims=2, scale_factor=2)
|
36
|
+
torch.testing.assert_close(out, torch.pixel_unshuffle(x, 2))
|
37
|
+
|
38
|
+
def test_different_scale_factor(self):
|
39
|
+
x = torch.arange(360).reshape(1, 2, 12, 15)
|
40
|
+
out = pixelunshuffle(x, spatial_dims=2, scale_factor=3)
|
41
|
+
torch.testing.assert_close(out, torch.pixel_unshuffle(x, 3))
|
42
|
+
|
43
|
+
def test_inverse_operation(self):
|
44
|
+
x = torch.arange(4096).reshape(1, 8, 8, 8, 8)
|
45
|
+
shuffled = pixelshuffle(x, spatial_dims=3, scale_factor=2)
|
46
|
+
unshuffled = pixelunshuffle(shuffled, spatial_dims=3, scale_factor=2)
|
47
|
+
torch.testing.assert_close(x, unshuffled)
|
48
|
+
|
49
|
+
|
50
|
+
if __name__ == "__main__":
|
51
|
+
unittest.main()
|
@@ -1,50 +0,0 @@
|
|
1
|
-
# Copyright (c) MONAI Consortium
|
2
|
-
# Licensed under the Apache License, Version 2.0 (the "License");
|
3
|
-
# you may not use this file except in compliance with the License.
|
4
|
-
# You may obtain a copy of the License at
|
5
|
-
# http://www.apache.org/licenses/LICENSE-2.0
|
6
|
-
# Unless required by applicable law or agreed to in writing, software
|
7
|
-
# distributed under the License is distributed on an "AS IS" BASIS,
|
8
|
-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
9
|
-
# See the License for the specific language governing permissions and
|
10
|
-
# limitations under the License.
|
11
|
-
|
12
|
-
from __future__ import annotations
|
13
|
-
|
14
|
-
import unittest
|
15
|
-
|
16
|
-
import torch
|
17
|
-
from parameterized import parameterized
|
18
|
-
|
19
|
-
from monai.networks import eval_mode
|
20
|
-
from monai.networks.blocks import MaxAvgPool
|
21
|
-
|
22
|
-
TEST_CASES = [
|
23
|
-
[{"spatial_dims": 2, "kernel_size": 2}, (7, 4, 64, 48), (7, 8, 32, 24)], # 4-channel 2D, batch 7
|
24
|
-
[{"spatial_dims": 1, "kernel_size": 4}, (16, 4, 63), (16, 8, 15)], # 4-channel 1D, batch 16
|
25
|
-
[{"spatial_dims": 1, "kernel_size": 4, "padding": 1}, (16, 4, 63), (16, 8, 16)], # 4-channel 1D, batch 16
|
26
|
-
[ # 4-channel 3D, batch 16
|
27
|
-
{"spatial_dims": 3, "kernel_size": 3, "ceil_mode": True},
|
28
|
-
(16, 4, 32, 24, 48),
|
29
|
-
(16, 8, 11, 8, 16),
|
30
|
-
],
|
31
|
-
[ # 1-channel 3D, batch 16
|
32
|
-
{"spatial_dims": 3, "kernel_size": 3, "ceil_mode": False},
|
33
|
-
(16, 1, 32, 24, 48),
|
34
|
-
(16, 2, 10, 8, 16),
|
35
|
-
],
|
36
|
-
]
|
37
|
-
|
38
|
-
|
39
|
-
class TestMaxAvgPool(unittest.TestCase):
|
40
|
-
|
41
|
-
@parameterized.expand(TEST_CASES)
|
42
|
-
def test_shape(self, input_param, input_shape, expected_shape):
|
43
|
-
net = MaxAvgPool(**input_param)
|
44
|
-
with eval_mode(net):
|
45
|
-
result = net(torch.randn(input_shape))
|
46
|
-
self.assertEqual(result.shape, expected_shape)
|
47
|
-
|
48
|
-
|
49
|
-
if __name__ == "__main__":
|
50
|
-
unittest.main()
|
File without changes
|
File without changes
|