MCPcopy Create free account
hub / github.com/UX-Decoder/Semantic-SAM / BasicLayer

Class BasicLayer

semantic_sam/backbone/swin_new.py:340–453  ·  view source on GitHub ↗

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

Source from the content-addressed store, hash-verified

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

Callers 1

__init__Method · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected