| 74 | |
| 75 | class InvertedResidualBlock(nn.Module): |
| 76 | def __init__(self, in_channels, out_channels, stride=1, expand_ratio=6): |
| 77 | super().__init__() |
| 78 | hidden_dim = in_channels * expand_ratio |
| 79 | self.use_residual = stride == 1 and in_channels == out_channels |
| 80 | |
| 81 | layers = [] |
| 82 | if expand_ratio != 1: |
| 83 | layers.append(ConvBlock(in_channels, hidden_dim, kernel_size=1, padding=0)) |
| 84 | |
| 85 | layers.extend([ |
| 86 | ConvBlock(hidden_dim, hidden_dim, kernel_size=3, stride=stride, padding=1, activation='relu'), |
| 87 | nn.Conv2d(hidden_dim, out_channels, kernel_size=1, bias=False), |
| 88 | nn.BatchNorm2d(out_channels) |
| 89 | ]) |
| 90 | |
| 91 | self.conv = nn.Sequential(*layers) |
| 92 | |
| 93 | def forward(self, x): |
| 94 | if self.use_residual: |