Args: embed_dims (int): The feature dimension. num_heads (int): Parallel attention heads. feedforward_channels (int): The hidden dimension for FFNs. window_size (int, optional): The local window scale. Default: 7. shift (bool, optional): whether to shift
| 283 | |
| 284 | |
| 285 | class SwinBlock(BaseModule): |
| 286 | """" |
| 287 | Args: |
| 288 | embed_dims (int): The feature dimension. |
| 289 | num_heads (int): Parallel attention heads. |
| 290 | feedforward_channels (int): The hidden dimension for FFNs. |
| 291 | window_size (int, optional): The local window scale. Default: 7. |
| 292 | shift (bool, optional): whether to shift window or not. Default False. |
| 293 | qkv_bias (bool, optional): enable bias for qkv if True. Default: True. |
| 294 | qk_scale (float | None, optional): Override default qk scale of |
| 295 | head_dim ** -0.5 if set. Default: None. |
| 296 | drop_rate (float, optional): Dropout rate. Default: 0. |
| 297 | attn_drop_rate (float, optional): Attention dropout rate. Default: 0. |
| 298 | drop_path_rate (float, optional): Stochastic depth rate. Default: 0. |
| 299 | act_cfg (dict, optional): The config dict of activation function. |
| 300 | Default: dict(type='GELU'). |
| 301 | norm_cfg (dict, optional): The config dict of normalization. |
| 302 | Default: dict(type='LN'). |
| 303 | with_cp (bool, optional): Use checkpoint or not. Using checkpoint |
| 304 | will save some memory while slowing down the training speed. |
| 305 | Default: False. |
| 306 | init_cfg (dict | list | None, optional): The init config. |
| 307 | Default: None. |
| 308 | """ |
| 309 | |
| 310 | def __init__(self, |
| 311 | embed_dims, |
| 312 | num_heads, |
| 313 | feedforward_channels, |
| 314 | window_size=7, |
| 315 | shift=False, |
| 316 | qkv_bias=True, |
| 317 | qk_scale=None, |
| 318 | drop_rate=0., |
| 319 | attn_drop_rate=0., |
| 320 | drop_path_rate=0., |
| 321 | act_cfg=dict(type='GELU'), |
| 322 | norm_cfg=dict(type='LN'), |
| 323 | with_cp=False, |
| 324 | init_cfg=None): |
| 325 | |
| 326 | super(SwinBlock, self).__init__(init_cfg=init_cfg) |
| 327 | |
| 328 | self.with_cp = with_cp |
| 329 | |
| 330 | self.norm1 = build_norm_layer(norm_cfg, embed_dims)[1] |
| 331 | self.attn = ShiftWindowMSA( |
| 332 | embed_dims=embed_dims, |
| 333 | num_heads=num_heads, |
| 334 | window_size=window_size, |
| 335 | shift_size=window_size // 2 if shift else 0, |
| 336 | qkv_bias=qkv_bias, |
| 337 | qk_scale=qk_scale, |
| 338 | attn_drop_rate=attn_drop_rate, |
| 339 | proj_drop_rate=drop_rate, |
| 340 | dropout_layer=dict(type='DropPath', drop_prob=drop_path_rate), |
| 341 | init_cfg=None) |
| 342 |