A basic Swin Transformer layer for one stage. Args: dim (int): Number of feature channels depth (int): Depths of this stage. num_heads (int): Number of attention head. window_size (int): Local window size. Default: 7. mlp_ratio (float): Ratio of mlp hidden
| 349 | |
| 350 | |
| 351 | class BasicLayer(nn.Module): |
| 352 | """A basic Swin Transformer layer for one stage. |
| 353 | Args: |
| 354 | dim (int): Number of feature channels |
| 355 | depth (int): Depths of this stage. |
| 356 | num_heads (int): Number of attention head. |
| 357 | window_size (int): Local window size. Default: 7. |
| 358 | mlp_ratio (float): Ratio of mlp hidden dim to embedding dim. Default: 4. |
| 359 | qkv_bias (bool, optional): If True, add a learnable bias to query, key, value. Default: True |
| 360 | qk_scale (float | None, optional): Override default qk scale of head_dim ** -0.5 if set. |
| 361 | drop (float, optional): Dropout rate. Default: 0.0 |
| 362 | attn_drop (float, optional): Attention dropout rate. Default: 0.0 |
| 363 | drop_path (float | tuple[float], optional): Stochastic depth rate. Default: 0.0 |
| 364 | norm_layer (nn.Module, optional): Normalization layer. Default: nn.LayerNorm |
| 365 | downsample (nn.Module | None, optional): Downsample layer at the end of the layer. Default: None |
| 366 | use_checkpoint (bool): Whether to use checkpointing to save memory. Default: False. |
| 367 | """ |
| 368 | |
| 369 | def __init__( |
| 370 | self, |
| 371 | dim, |
| 372 | depth, |
| 373 | num_heads, |
| 374 | window_size=7, |
| 375 | mlp_ratio=4.0, |
| 376 | qkv_bias=True, |
| 377 | qk_scale=None, |
| 378 | drop=0.0, |
| 379 | attn_drop=0.0, |
| 380 | drop_path=0.0, |
| 381 | norm_layer=nn.LayerNorm, |
| 382 | downsample=None, |
| 383 | use_checkpoint=False, |
| 384 | ): |
| 385 | super().__init__() |
| 386 | self.window_size = window_size |
| 387 | self.shift_size = window_size // 2 |
| 388 | self.depth = depth |
| 389 | self.use_checkpoint = use_checkpoint |
| 390 | |
| 391 | # build blocks |
| 392 | self.blocks = nn.ModuleList( |
| 393 | [ |
| 394 | SwinTransformerBlock( |
| 395 | dim=dim, |
| 396 | num_heads=num_heads, |
| 397 | window_size=window_size, |
| 398 | shift_size=0 if (i % 2 == 0) else window_size // 2, |
| 399 | mlp_ratio=mlp_ratio, |
| 400 | qkv_bias=qkv_bias, |
| 401 | qk_scale=qk_scale, |
| 402 | drop=drop, |
| 403 | attn_drop=attn_drop, |
| 404 | drop_path=drop_path[i] if isinstance(drop_path, list) else drop_path, |
| 405 | norm_layer=norm_layer, |
| 406 | ) |
| 407 | for i in range(depth) |
| 408 | ] |