Gated-DConv Feed-Forward Network (GDFN) that controls feature flow using gating mechanism. Uses depth-wise convolutions for local context mixing and GELU-activated gating for refined feature selection. Args: spatial_dims: Number of spatial dimensions (2D or 3D) dim: Number o
| 25 | |
| 26 | |
| 27 | class FeedForward(nn.Module): |
| 28 | """Gated-DConv Feed-Forward Network (GDFN) that controls feature flow using gating mechanism. |
| 29 | Uses depth-wise convolutions for local context mixing and GELU-activated gating for refined feature selection. |
| 30 | |
| 31 | Args: |
| 32 | spatial_dims: Number of spatial dimensions (2D or 3D) |
| 33 | dim: Number of input channels |
| 34 | ffn_expansion_factor: Factor to expand hidden features dimension |
| 35 | bias: Whether to use bias in convolution layers |
| 36 | """ |
| 37 | |
| 38 | def __init__(self, spatial_dims: int, dim: int, ffn_expansion_factor: float, bias: bool): |
| 39 | super().__init__() |
| 40 | hidden_features = int(dim * ffn_expansion_factor) |
| 41 | |
| 42 | self.project_in = Convolution( |
| 43 | spatial_dims=spatial_dims, |
| 44 | in_channels=dim, |
| 45 | out_channels=hidden_features * 2, |
| 46 | kernel_size=1, |
| 47 | bias=bias, |
| 48 | conv_only=True, |
| 49 | ) |
| 50 | |
| 51 | self.dwconv = Convolution( |
| 52 | spatial_dims=spatial_dims, |
| 53 | in_channels=hidden_features * 2, |
| 54 | out_channels=hidden_features * 2, |
| 55 | kernel_size=3, |
| 56 | strides=1, |
| 57 | padding=1, |
| 58 | groups=hidden_features * 2, |
| 59 | bias=bias, |
| 60 | conv_only=True, |
| 61 | ) |
| 62 | |
| 63 | self.project_out = Convolution( |
| 64 | spatial_dims=spatial_dims, |
| 65 | in_channels=hidden_features, |
| 66 | out_channels=dim, |
| 67 | kernel_size=1, |
| 68 | bias=bias, |
| 69 | conv_only=True, |
| 70 | ) |
| 71 | |
| 72 | def forward(self, x: torch.Tensor) -> torch.Tensor: |
| 73 | x = self.project_in(x) |
| 74 | x1, x2 = self.dwconv(x).chunk(2, dim=1) |
| 75 | return cast(torch.Tensor, self.project_out(F.gelu(x1) * x2)) |
| 76 | |
| 77 | |
| 78 | class CABlock(nn.Module): |
no outgoing calls
searching dependent graphs…