Merge patch feature map. This layer groups feature map by kernel_size, and applies norm and linear layers to the grouped feature map. Our implementation uses `nn.Unfold` to merge patch, which is about 25% faster than original implementation. Instead, we need to modify pretrained mod
| 198 | |
| 199 | |
| 200 | class PatchMerging(BaseModule): |
| 201 | """Merge patch feature map. |
| 202 | |
| 203 | This layer groups feature map by kernel_size, and applies norm and linear |
| 204 | layers to the grouped feature map. Our implementation uses `nn.Unfold` to |
| 205 | merge patch, which is about 25% faster than original implementation. |
| 206 | Instead, we need to modify pretrained models for compatibility. |
| 207 | |
| 208 | Args: |
| 209 | in_channels (int): The num of input channels. |
| 210 | out_channels (int): The num of output channels. |
| 211 | kernel_size (int | tuple, optional): the kernel size in the unfold |
| 212 | layer. Defaults to 2. |
| 213 | stride (int | tuple, optional): the stride of the sliding blocks in the |
| 214 | unfold layer. Default: None. (Would be set as `kernel_size`) |
| 215 | padding (int | tuple | string ): The padding length of |
| 216 | embedding conv. When it is a string, it means the mode |
| 217 | of adaptive padding, support "same" and "corner" now. |
| 218 | Default: "corner". |
| 219 | dilation (int | tuple, optional): dilation parameter in the unfold |
| 220 | layer. Default: 1. |
| 221 | bias (bool, optional): Whether to add bias in linear layer or not. |
| 222 | Defaults: False. |
| 223 | norm_cfg (dict, optional): Config dict for normalization layer. |
| 224 | Default: dict(type='LN'). |
| 225 | init_cfg (dict, optional): The extra config for initialization. |
| 226 | Default: None. |
| 227 | """ |
| 228 | |
| 229 | def __init__( |
| 230 | self, |
| 231 | in_channels, |
| 232 | out_channels, |
| 233 | kernel_size=2, |
| 234 | stride=None, |
| 235 | padding="corner", |
| 236 | dilation=1, |
| 237 | bias=False, |
| 238 | norm_cfg=dict(type="LN"), |
| 239 | init_cfg=None, |
| 240 | ): |
| 241 | super().__init__(init_cfg=init_cfg) |
| 242 | self.in_channels = in_channels |
| 243 | self.out_channels = out_channels |
| 244 | if stride: |
| 245 | stride = stride |
| 246 | else: |
| 247 | stride = kernel_size |
| 248 | |
| 249 | kernel_size = to_2tuple(kernel_size) |
| 250 | stride = to_2tuple(stride) |
| 251 | dilation = to_2tuple(dilation) |
| 252 | |
| 253 | if isinstance(padding, str): |
| 254 | self.adap_padding = AdaptivePadding( |
| 255 | kernel_size=kernel_size, stride=stride, dilation=dilation, padding=padding |
| 256 | ) |
| 257 | # disable the padding of unfold |