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