Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change num_res_blocks to Sequence[int] | int #238

Merged
merged 6 commits into from
Feb 10, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
17 changes: 12 additions & 5 deletions generative/networks/nets/autoencoderkl.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ def __init__(
in_channels: int,
num_channels: Sequence[int],
out_channels: int,
num_res_blocks: int,
num_res_blocks: Sequence[int],
norm_num_groups: int,
norm_eps: float,
attention_levels: Sequence[bool],
Expand Down Expand Up @@ -350,7 +350,7 @@ def __init__(
output_channel = num_channels[i]
is_final_block = i == len(num_channels) - 1

for _ in range(self.num_res_blocks):
for _ in range(self.num_res_blocks[i]):
blocks.append(
ResBlock(
spatial_dims=spatial_dims,
Expand Down Expand Up @@ -449,7 +449,7 @@ def __init__(
num_channels: Sequence[int],
in_channels: int,
out_channels: int,
num_res_blocks: int,
num_res_blocks: Sequence[int],
norm_num_groups: int,
norm_eps: float,
attention_levels: Sequence[bool],
Expand Down Expand Up @@ -511,13 +511,14 @@ def __init__(
)

reversed_attention_levels = list(reversed(attention_levels))
reversed_num_res_blocks = list(reversed(num_res_blocks))
block_out_ch = reversed_block_out_channels[0]
for i in range(len(reversed_block_out_channels)):
block_in_ch = block_out_ch
block_out_ch = reversed_block_out_channels[i]
is_final_block = i == len(num_channels) - 1

for _ in range(self.num_res_blocks):
for _ in range(reversed_num_res_blocks[i]):
blocks.append(
ResBlock(
spatial_dims=spatial_dims,
Expand Down Expand Up @@ -588,7 +589,7 @@ def __init__(
spatial_dims: int,
in_channels: int = 1,
out_channels: int = 1,
num_res_blocks: int = 2,
num_res_blocks: Sequence[int] | int = (2, 2, 2, 2),
num_channels: Sequence[int] = (32, 64, 64, 64),
attention_levels: Sequence[bool] = (False, False, True, True),
latent_channels: int = 3,
Expand All @@ -606,6 +607,12 @@ def __init__(
if len(num_channels) != len(attention_levels):
raise ValueError("AutoencoderKL expects num_channels being same size of attention_levels")

if isinstance(num_res_blocks, int):
Warvito marked this conversation as resolved.
Show resolved Hide resolved
num_res_blocks = (num_res_blocks,) * len(num_channels)

if len(num_res_blocks) != len(num_channels):
raise ValueError("num_res_blocks should have the same length as num_channels.")
Warvito marked this conversation as resolved.
Show resolved Hide resolved

self.encoder = Encoder(
spatial_dims=spatial_dims,
in_channels=in_channels,
Expand Down
13 changes: 10 additions & 3 deletions generative/networks/nets/diffusion_model_unet.py
Original file line number Diff line number Diff line change
Expand Up @@ -1610,7 +1610,7 @@ def __init__(
spatial_dims: int,
in_channels: int,
out_channels: int,
num_res_blocks: int,
num_res_blocks: Sequence[int] | int = (2, 2, 2, 2),
num_channels: Sequence[int] = (32, 64, 64, 64),
attention_levels: Sequence[bool] = (False, False, True, True),
norm_num_groups: int = 32,
Expand Down Expand Up @@ -1650,6 +1650,12 @@ def __init__(
" i.e. `attention_level[i]=False`, the num_head_channels[i] will be ignored."
)

if isinstance(num_res_blocks, int):
Warvito marked this conversation as resolved.
Show resolved Hide resolved
num_res_blocks = (num_res_blocks,) * len(num_channels)

if len(num_res_blocks) != len(num_channels):
raise ValueError("num_res_blocks should have the same length as num_channels.")
Warvito marked this conversation as resolved.
Show resolved Hide resolved

self.in_channels = in_channels
self.block_out_channels = num_channels
self.out_channels = out_channels
Expand Down Expand Up @@ -1693,7 +1699,7 @@ def __init__(
in_channels=input_channel,
out_channels=output_channel,
temb_channels=time_embed_dim,
num_res_blocks=num_res_blocks,
num_res_blocks=num_res_blocks[i],
norm_num_groups=norm_num_groups,
norm_eps=norm_eps,
add_downsample=not is_final_block,
Expand Down Expand Up @@ -1725,6 +1731,7 @@ def __init__(
# up
self.up_blocks = nn.ModuleList([])
reversed_block_out_channels = list(reversed(num_channels))
reversed_num_res_blocks = list(reversed(num_res_blocks))
reversed_attention_levels = list(reversed(attention_levels))
reversed_num_head_channels = list(reversed(num_head_channels))
output_channel = reversed_block_out_channels[0]
Expand All @@ -1741,7 +1748,7 @@ def __init__(
prev_output_channel=prev_output_channel,
out_channels=output_channel,
temb_channels=time_embed_dim,
num_res_blocks=num_res_blocks + 1,
num_res_blocks=reversed_num_res_blocks[i] + 1,
norm_num_groups=norm_num_groups,
norm_eps=norm_eps,
add_upsample=not is_final_block,
Expand Down
28 changes: 28 additions & 0 deletions tests/test_autoencoderkl.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,21 @@
(1, 1, 16, 16),
(1, 4, 4, 4),
],
[
{
"spatial_dims": 2,
"in_channels": 1,
"out_channels": 1,
"num_channels": (4, 4, 4),
"latent_channels": 4,
"attention_levels": (False, False, False),
"num_res_blocks": (1, 1, 2),
"norm_num_groups": 4,
},
(1, 1, 16, 16),
(1, 1, 16, 16),
(1, 4, 4, 4),
],
[
{
"spatial_dims": 2,
Expand Down Expand Up @@ -161,6 +176,19 @@ def test_model_num_channels_not_same_size_of_attention_levels(self):
norm_num_groups=16,
)

def test_model_num_channels_not_same_size_of_num_res_blocks(self):
with self.assertRaises(ValueError):
AutoencoderKL(
spatial_dims=2,
in_channels=1,
out_channels=1,
num_channels=(24, 24, 24),
attention_levels=(False, False, False),
latent_channels=8,
num_res_blocks=(8, 8),
norm_num_groups=16,
)

def test_shape_reconstruction(self):
input_param, input_shape, expected_shape, _ = CASES[0]
net = AutoencoderKL(**input_param).to(device)
Expand Down
23 changes: 23 additions & 0 deletions tests/test_diffusion_model_unet.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,17 @@
"norm_num_groups": 8,
}
],
[
{
"spatial_dims": 2,
"in_channels": 1,
"out_channels": 1,
"num_res_blocks": (1, 1, 2),
"num_channels": (8, 8, 8),
"attention_levels": (False, False, False),
"norm_num_groups": 8,
}
],
[
{
"spatial_dims": 2,
Expand Down Expand Up @@ -270,6 +281,18 @@ def test_attention_levels_with_different_length_num_head_channels(self):
norm_num_groups=8,
)

def test_num_res_blocks_with_different_length_num_channels(self):
with self.assertRaises(ValueError):
DiffusionModelUNet(
spatial_dims=2,
in_channels=1,
out_channels=1,
num_res_blocks=(1, 1),
num_channels=(8, 8, 8),
attention_levels=(False, False, False),
norm_num_groups=8,
)

def test_shape_conditioned_models(self):
net = DiffusionModelUNet(
spatial_dims=2,
Expand Down