A basic focal modulation layer for one stage. Args: dim (int): Number of feature channels depth (int): Depths of this stage. mlp_ratio (float): Ratio of mlp hidden dim to embedding dim. Default: 4. drop (float, optional): Dropout rate. Default: 0.0 drop_
| 204 | return x |
| 205 | |
| 206 | class BasicLayer(nn.Module): |
| 207 | """ A basic focal modulation layer for one stage. |
| 208 | |
| 209 | Args: |
| 210 | dim (int): Number of feature channels |
| 211 | depth (int): Depths of this stage. |
| 212 | mlp_ratio (float): Ratio of mlp hidden dim to embedding dim. Default: 4. |
| 213 | drop (float, optional): Dropout rate. Default: 0.0 |
| 214 | drop_path (float | tuple[float], optional): Stochastic depth rate. Default: 0.0 |
| 215 | norm_layer (nn.Module, optional): Normalization layer. Default: nn.LayerNorm |
| 216 | downsample (nn.Module | None, optional): Downsample layer at the end of the layer. Default: None |
| 217 | focal_level (int): Number of focal levels |
| 218 | focal_window (int): Focal window size at focal level 1 |
| 219 | use_conv_embed (bool): Use overlapped convolution for patch embedding or now. Default: False |
| 220 | use_checkpoint (bool): Whether to use checkpointing to save memory. Default: False |
| 221 | """ |
| 222 | |
| 223 | def __init__(self, |
| 224 | dim, |
| 225 | depth, |
| 226 | mlp_ratio=4., |
| 227 | drop=0., |
| 228 | drop_path=0., |
| 229 | norm_layer=nn.LayerNorm, |
| 230 | downsample=None, |
| 231 | focal_window=9, |
| 232 | focal_level=2, |
| 233 | use_conv_embed=False, |
| 234 | use_postln=False, |
| 235 | use_postln_in_modulation=False, |
| 236 | scaling_modulator=False, |
| 237 | use_layerscale=False, |
| 238 | use_checkpoint=False, |
| 239 | use_pre_norm=False, |
| 240 | ): |
| 241 | super().__init__() |
| 242 | self.depth = depth |
| 243 | self.use_checkpoint = use_checkpoint |
| 244 | |
| 245 | # build blocks |
| 246 | self.blocks = nn.ModuleList([ |
| 247 | FocalModulationBlock( |
| 248 | dim=dim, |
| 249 | mlp_ratio=mlp_ratio, |
| 250 | drop=drop, |
| 251 | drop_path=drop_path[i] if isinstance(drop_path, list) else drop_path, |
| 252 | focal_window=focal_window, |
| 253 | focal_level=focal_level, |
| 254 | use_postln=use_postln, |
| 255 | use_postln_in_modulation=use_postln_in_modulation, |
| 256 | scaling_modulator=scaling_modulator, |
| 257 | use_layerscale=use_layerscale, |
| 258 | norm_layer=norm_layer) |
| 259 | for i in range(depth)]) |
| 260 | |
| 261 | # patch merging layer |
| 262 | if downsample is not None: |
| 263 | self.downsample = downsample( |