Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions monai/networks/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,14 +281,16 @@ def pixelshuffle(
f"divisible by scale_factor ** dimensions ({factor}**{dim}={scale_divisor})."
)

org_channels = channels // scale_divisor
org_channels = int(channels // scale_divisor)
output_size = [batch_size, org_channels] + [d * factor for d in input_size[2:]]

indices = tuple(range(2, 2 + 2 * dim))
indices_factor, indices_dim = indices[:dim], indices[dim:]
permute_indices = (0, 1) + sum(zip(indices_dim, indices_factor), ())
indices = list(range(2, 2 + 2 * dim))
indices = indices[dim:] + indices[:dim]
permute_indices = [0, 1]
for idx in range(dim):
permute_indices.extend(indices[idx::dim])

x = x.reshape(batch_size, org_channels, *([factor] * dim + input_size[2:]))
x = x.reshape([batch_size, org_channels] + [factor] * dim + input_size[2:])
x = x.permute(permute_indices).reshape(output_size)
return x

Expand Down
8 changes: 8 additions & 0 deletions tests/test_subpixel_upsample.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from monai.networks import eval_mode
from monai.networks.blocks import SubpixelUpsample
from monai.networks.layers.factories import Conv
from tests.utils import SkipIfBeforePyTorchVersion, test_script_save

TEST_CASE_SUBPIXEL = []
for inch in range(1, 5):
Expand Down Expand Up @@ -73,6 +74,13 @@ def test_subpixel_shape(self, input_param, input_shape, expected_shape):
result = net.forward(torch.randn(input_shape))
self.assertEqual(result.shape, expected_shape)

@SkipIfBeforePyTorchVersion((1, 8, 1))
def test_script(self):
input_param, input_shape, _ = TEST_CASE_SUBPIXEL[0]
net = SubpixelUpsample(**input_param)
test_data = torch.randn(input_shape)
test_script_save(net, test_data)


if __name__ == "__main__":
unittest.main()