MCPcopy Create free account
hub / github.com/alinlab/SelfPatch / TransformerEncoderLayer

Class TransformerEncoderLayer

segmentation/backbones/mit.py:174–242  ·  view source on GitHub ↗

Implements one encoder layer in Segformer. 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.

Source from the content-addressed store, hash-verified

172
173
174class TransformerEncoderLayer(BaseModule):
175 """Implements one encoder layer in Segformer.
176
177 Args:
178 embed_dims (int): The feature dimension.
179 num_heads (int): Parallel attention heads.
180 feedforward_channels (int): The hidden dimension for FFNs.
181 drop_rate (float): Probability of an element to be zeroed.
182 after the feed forward layer. Default 0.0.
183 attn_drop_rate (float): The drop out rate for attention layer.
184 Default 0.0.
185 drop_path_rate (float): stochastic depth rate. Default 0.0.
186 qkv_bias (bool): enable bias for qkv if True.
187 Default: True.
188 act_cfg (dict): The activation config for FFNs.
189 Defalut: dict(type='GELU').
190 norm_cfg (dict): Config dict for normalization layer.
191 Default: dict(type='LN').
192 batch_first (bool): Key, Query and Value are shape of
193 (batch, n, embed_dim)
194 or (n, batch, embed_dim). Default: False.
195 init_cfg (dict, optional): Initialization config dict.
196 Default:None.
197 sr_ratio (int): The ratio of spatial reduction of Efficient Multi-head
198 Attention of Segformer. Default: 1.
199 """
200
201 def __init__(self,
202 embed_dims,
203 num_heads,
204 feedforward_channels,
205 drop_rate=0.,
206 attn_drop_rate=0.,
207 drop_path_rate=0.,
208 qkv_bias=True,
209 act_cfg=dict(type='GELU'),
210 norm_cfg=dict(type='LN'),
211 batch_first=True,
212 sr_ratio=1):
213 super(TransformerEncoderLayer, self).__init__()
214
215 # The ret[0] of build_norm_layer is norm name.
216 self.norm1 = build_norm_layer(norm_cfg, embed_dims)[1]
217
218 self.attn = EfficientMultiheadAttention(
219 embed_dims=embed_dims,
220 num_heads=num_heads,
221 attn_drop=attn_drop_rate,
222 proj_drop=drop_rate,
223 dropout_layer=dict(type='DropPath', drop_prob=drop_path_rate),
224 batch_first=batch_first,
225 qkv_bias=qkv_bias,
226 norm_cfg=norm_cfg,
227 sr_ratio=sr_ratio)
228
229 # The ret[0] of build_norm_layer is norm name.
230 self.norm2 = build_norm_layer(norm_cfg, embed_dims)[1]
231

Callers 1

__init__Method · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected