Todayβs goal was to turn theory into code β I implemented the MobileNetV2 building block from scratch in PyTorch.
stride == 1
and in_channels == out_channels
class InvertedResidual(nn.Module):
def __init__(self, in_channels, out_channels, stride, expand_ratio):
super().__init__()
hidden_dim = in_channels * expand_ratio
self.use_res_connect = (stride == 1 and in_channels == out_channels)
layers = []
if expand_ratio != 1:
layers += [
nn.Conv2d(in_channels, hidden_dim, 1, bias=False),
nn.BatchNorm2d(hidden_dim),
nn.ReLU6(inplace=True)
]
layers += [
nn.Conv2d(hidden_dim, hidden_dim, 3, stride, 1, groups=hidden_dim, bias=False),
nn.BatchNorm2d(hidden_dim),
nn.ReLU6(inplace=True),
nn.Conv2d(hidden_dim, out_channels, 1, bias=False),
nn.BatchNorm2d(out_channels)
]
self.conv = nn.Sequential(*layers)
def forward(self, x):
if self.use_res_connect:
return x + self.conv(x)
else:
return self.conv(x)
Implementing the MobileNetV2 block deepened my understanding of how lightweight CNNs are constructed.
Each design choice β from activation placement to depthwise grouping β serves to optimize for speed, memory, and representational efficiency.
This block will serve as a reusable module for future experiments and real-time CV applications.
π Note: Full model accuracy (ACC) evaluation will be updated tomorrow after full inference and validation.