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