Implements one encoder layer in Vision Transformer. Args: embed_dims (int): The feature dimension. num_heads (int): Parallel attention heads. feedforward_channels (int): The hidden dimension for FFNs. drop_rate (float): Probability of an element to be zeroed
| 19 | |
| 20 | |
| 21 | class TransformerEncoderLayer(BaseModule): |
| 22 | """Implements one encoder layer in Vision Transformer. |
| 23 | |
| 24 | Args: |
| 25 | embed_dims (int): The feature dimension. |
| 26 | num_heads (int): Parallel attention heads. |
| 27 | feedforward_channels (int): The hidden dimension for FFNs. |
| 28 | drop_rate (float): Probability of an element to be zeroed |
| 29 | after the feed forward layer. Default: 0.0. |
| 30 | attn_drop_rate (float): The drop out rate for attention layer. |
| 31 | Default: 0.0. |
| 32 | drop_path_rate (float): stochastic depth rate. Default 0.0. |
| 33 | num_fcs (int): The number of fully-connected layers for FFNs. |
| 34 | Default: 2. |
| 35 | qkv_bias (bool): enable bias for qkv if True. Default: True |
| 36 | act_cfg (dict): The activation config for FFNs. |
| 37 | Default: dict(type='GELU'). |
| 38 | norm_cfg (dict): Config dict for normalization layer. |
| 39 | Default: dict(type='LN'). |
| 40 | batch_first (bool): Key, Query and Value are shape of |
| 41 | (batch, n, embed_dim) |
| 42 | or (n, batch, embed_dim). Default: True. |
| 43 | with_cp (bool): Use checkpoint or not. Using checkpoint will save |
| 44 | some memory while slowing down the training speed. Default: False. |
| 45 | """ |
| 46 | |
| 47 | def __init__( |
| 48 | self, |
| 49 | embed_dims, |
| 50 | num_heads, |
| 51 | feedforward_channels, |
| 52 | drop_rate=0.0, |
| 53 | attn_drop_rate=0.0, |
| 54 | drop_path_rate=0.0, |
| 55 | num_fcs=2, |
| 56 | qkv_bias=True, |
| 57 | act_cfg=dict(type="GELU"), |
| 58 | norm_cfg=dict(type="LN"), |
| 59 | batch_first=True, |
| 60 | attn_cfg=dict(), |
| 61 | ffn_cfg=dict(), |
| 62 | with_cp=False, |
| 63 | ): |
| 64 | super(TransformerEncoderLayer, self).__init__() |
| 65 | |
| 66 | self.norm1_name, norm1 = build_norm_layer(norm_cfg, embed_dims, postfix=1) |
| 67 | self.add_module(self.norm1_name, norm1) |
| 68 | |
| 69 | attn_cfg.update( |
| 70 | dict( |
| 71 | embed_dims=embed_dims, |
| 72 | num_heads=num_heads, |
| 73 | attn_drop=attn_drop_rate, |
| 74 | proj_drop=drop_rate, |
| 75 | batch_first=batch_first, |
| 76 | bias=qkv_bias, |
| 77 | ) |
| 78 | ) |