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
4 changes: 4 additions & 0 deletions monai/networks/nets/autoencoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ def __init__(
self.inter_channels = inter_channels if inter_channels is not None else list()
self.inter_dilations = list(inter_dilations or [1] * len(self.inter_channels))

# The number of channels and strides should match
if len(channels) != len(strides):
raise ValueError("Autoencoder expects matching number of channels and strides")

self.encoded_channels = in_channels
decode_channel_list = list(channels[-2::-1]) + [out_channels]

Expand Down
13 changes: 13 additions & 0 deletions tests/test_autoencoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,15 @@
CASES = [TEST_CASE_0, TEST_CASE_1, TEST_CASE_2, TEST_CASE_3]


TEST_CASE_FAIL = { # 2-channel 2D, should fail because of stride/channel mismatch.
"dimensions": 2,
"in_channels": 2,
"out_channels": 2,
"channels": (4, 8, 16),
"strides": (2, 2),
}


class TestAutoEncoder(unittest.TestCase):
@parameterized.expand(CASES)
def test_shape(self, input_param, input_shape, expected_shape):
Expand All @@ -76,6 +85,10 @@ def test_script(self):
test_data = torch.randn(2, 1, 32, 32)
test_script_save(net, test_data)

def test_channel_stride_difference(self):
with self.assertRaises(ValueError):
net = AutoEncoder(**TEST_CASE_FAIL)


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