| 4 | from typing import Optional |
| 5 | |
| 6 | class ConvBlock(nn.Module): |
| 7 | def __init__(self, in_channels, out_channels, kernel_size=3, stride=1, padding=1, |
| 8 | use_bn=True, activation='relu', drop_rate=0.0): |
| 9 | super().__init__() |
| 10 | self.conv = nn.Conv2d(in_channels, out_channels, kernel_size, stride, padding, bias=not use_bn) |
| 11 | self.bn = nn.BatchNorm2d(out_channels) if use_bn else nn.Identity() |
| 12 | |
| 13 | if activation == 'relu': |
| 14 | self.activation = nn.ReLU(inplace=True) |
| 15 | elif activation == 'gelu': |
| 16 | self.activation = nn.GELU() |
| 17 | elif activation == 'silu': |
| 18 | self.activation = nn.SiLU(inplace=True) |
| 19 | elif activation == 'mish': |
| 20 | self.activation = nn.Mish(inplace=True) |
| 21 | else: |
| 22 | self.activation = nn.Identity() |
| 23 | |
| 24 | self.dropout = nn.Dropout2d(drop_rate) if drop_rate > 0 else nn.Identity() |
| 25 | |
| 26 | def forward(self, x): |
| 27 | x = self.conv(x) |
| 28 | x = self.bn(x) |
| 29 | x = self.activation(x) |
| 30 | x = self.dropout(x) |
| 31 | return x |
| 32 | |
| 33 | class ResidualBlock(nn.Module): |
| 34 | def __init__(self, channels, kernel_size=3, drop_rate=0.0): |