MCPcopy Create free account
hub / github.com/alinlab/SelfPatch / DeconvModule

Class DeconvModule

segmentation/backbones/unet.py:90–147  ·  view source on GitHub ↗

Deconvolution upsample module in decoder for UNet (2X upsample). This module uses deconvolution to upsample feature map in the decoder of UNet. Args: in_channels (int): Number of input channels. out_channels (int): Number of output channels. with_cp (bool): Use

Source from the content-addressed store, hash-verified

88
89@UPSAMPLE_LAYERS.register_module()
90class DeconvModule(nn.Module):
91 """Deconvolution upsample module in decoder for UNet (2X upsample).
92
93 This module uses deconvolution to upsample feature map in the decoder
94 of UNet.
95
96 Args:
97 in_channels (int): Number of input channels.
98 out_channels (int): Number of output channels.
99 with_cp (bool): Use checkpoint or not. Using checkpoint will save some
100 memory while slowing down the training speed. Default: False.
101 norm_cfg (dict | None): Config dict for normalization layer.
102 Default: dict(type='BN').
103 act_cfg (dict | None): Config dict for activation layer in ConvModule.
104 Default: dict(type='ReLU').
105 kernel_size (int): Kernel size of the convolutional layer. Default: 4.
106 """
107
108 def __init__(self,
109 in_channels,
110 out_channels,
111 with_cp=False,
112 norm_cfg=dict(type='BN'),
113 act_cfg=dict(type='ReLU'),
114 *,
115 kernel_size=4,
116 scale_factor=2):
117 super(DeconvModule, self).__init__()
118
119 assert (kernel_size - scale_factor >= 0) and\
120 (kernel_size - scale_factor) % 2 == 0,\
121 f'kernel_size should be greater than or equal to scale_factor '\
122 f'and (kernel_size - scale_factor) should be even numbers, '\
123 f'while the kernel size is {kernel_size} and scale_factor is '\
124 f'{scale_factor}.'
125
126 stride = scale_factor
127 padding = (kernel_size - scale_factor) // 2
128 self.with_cp = with_cp
129 deconv = nn.ConvTranspose2d(
130 in_channels,
131 out_channels,
132 kernel_size=kernel_size,
133 stride=stride,
134 padding=padding)
135
136 norm_name, norm = build_norm_layer(norm_cfg, out_channels)
137 activate = build_activation_layer(act_cfg)
138 self.deconv_upsamping = nn.Sequential(deconv, norm, activate)
139
140 def forward(self, x):
141 """Forward function."""
142
143 if self.with_cp and x.requires_grad:
144 out = cp.checkpoint(self.deconv_upsamping, x)
145 else:
146 out = self.deconv_upsamping(x)
147 return out

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected