Repository navigation
fix(networks): honor explicit padding=0 in ResidualUnit #9118
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
base: dev
Are you sure you want to change the base?
Changes from all commits
35aace8
59568cf
fb4ef6e
f7a6d64
8ee2e8c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -269,7 +269,8 @@ def __init__( | |
| self.out_channels = out_channels | ||
| self.conv = nn.Sequential() | ||
| self.residual = nn.Identity() | ||
| if not padding: | ||
| same_pad = padding is None | ||
| if padding is None: | ||
| padding = same_padding(kernel_size, dilation) | ||
| schannels = in_channels | ||
| sstrides = strides | ||
|
|
@@ -301,16 +302,33 @@ def __init__( | |
| sstrides = 1 | ||
|
|
||
| # apply convolution to input to change number of output channels and size to match that coming from self.conv | ||
| if np.prod(strides) != 1 or in_channels != out_channels: | ||
| rkernel_size = kernel_size | ||
| rpadding = padding | ||
|
|
||
| if np.prod(strides) == 1: # if only adapting number of channels a 1x1 kernel is used with no padding | ||
| rkernel_size = 1 | ||
| rpadding = 0 | ||
|
|
||
| if np.prod(strides) != 1 or in_channels != out_channels or not same_pad: | ||
| conv_type = Conv[Conv.CONV, self.spatial_dims] | ||
| self.residual = conv_type(in_channels, out_channels, rkernel_size, strides, rpadding, bias=bias) | ||
| if not same_pad and subunits > 1: | ||
| # mirror the subunit chain so the residual shrinks exactly like the main path | ||
| residual = nn.Sequential() | ||
| schannels = in_channels | ||
| sstrides = strides | ||
| for su in range(subunits): | ||
| residual.add_module( | ||
| f"unit{su:d}", | ||
| conv_type( | ||
| schannels, out_channels, kernel_size, sstrides, padding, bias=bias, dilation=dilation | ||
| ), | ||
| ) | ||
| schannels = out_channels | ||
| sstrides = 1 | ||
| self.residual = residual | ||
|
Comment on lines
+307
to
+321
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This changes the behaviour and weights of this block significantly. The original idea was that the residual wouldn't be change at all, we added the convolution to adjust for channel dimensions only which should preserve spatial features and size. Doing this sequence of convolutions represents significantly more computation, and is breaking in terms of the saved weights for networks using this block. I don't think we can change this block in any way which changes the weights. Some configurations will not be valid in that case, it's better to detect these in the constructor and raise exceptions rather than try to adapt like this. |
||
| else: | ||
| rkernel_size = kernel_size | ||
| rpadding = padding | ||
|
|
||
| # if only adapting number of channels a 1x1 kernel is used with no padding | ||
| if np.prod(strides) == 1 and same_pad: | ||
| rkernel_size = 1 | ||
| rpadding = 0 | ||
|
|
||
| self.residual = conv_type(in_channels, out_channels, rkernel_size, strides, rpadding, bias=bias) | ||
|
|
||
| def forward(self, x: torch.Tensor) -> torch.Tensor: | ||
| res: torch.Tensor = self.residual(x) # create the additive residual from x | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.