Add student_mobilenet.py
Browse files- student_mobilenet.py +78 -0
student_mobilenet.py
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
student_mobilenet.py
|
| 3 |
+
MobileNetV2-based lightweight student model for DLoc knowledge distillation.
|
| 4 |
+
|
| 5 |
+
Architecture (from professor's Stage 1 spec):
|
| 6 |
+
Encoder: MobileNetV2 backbone (first conv modified for 4-channel input)
|
| 7 |
+
Decoder: Upsample + Conv (320->128->64->32->1) + Sigmoid
|
| 8 |
+
Output: [B, 1, 161, 361] location heatmap
|
| 9 |
+
|
| 10 |
+
~2.3M params vs DLoc's ~15M params.
|
| 11 |
+
"""
|
| 12 |
+
import torch
|
| 13 |
+
import torch.nn as nn
|
| 14 |
+
from torchvision.models import mobilenet_v2, MobileNet_V2_Weights
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
class MobileNetStudent(nn.Module):
|
| 18 |
+
# Target output spatial size
|
| 19 |
+
OUTPUT_SIZE = (161, 361)
|
| 20 |
+
|
| 21 |
+
def __init__(self, input_nc=4):
|
| 22 |
+
super().__init__()
|
| 23 |
+
|
| 24 |
+
# Load MobileNetV2 backbone (features[0:18] = 320-channel output)
|
| 25 |
+
mobilenet = mobilenet_v2(weights=MobileNet_V2_Weights.DEFAULT)
|
| 26 |
+
self.encoder = mobilenet.features[:-1] # drop final 320->1280 conv
|
| 27 |
+
|
| 28 |
+
# Replace first conv: 3-channel RGB -> 4-channel AoA
|
| 29 |
+
old_conv = self.encoder[0][0]
|
| 30 |
+
self.encoder[0][0] = nn.Conv2d(
|
| 31 |
+
input_nc, old_conv.out_channels,
|
| 32 |
+
kernel_size=old_conv.kernel_size,
|
| 33 |
+
stride=old_conv.stride,
|
| 34 |
+
padding=old_conv.padding,
|
| 35 |
+
bias=False,
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
# Decoder: 320 -> 128 -> 64 -> 32 -> 1
|
| 39 |
+
self.decoder = nn.Sequential(
|
| 40 |
+
nn.Upsample(scale_factor=2, mode='bilinear', align_corners=False),
|
| 41 |
+
nn.Conv2d(320, 128, kernel_size=3, padding=1, bias=False),
|
| 42 |
+
nn.BatchNorm2d(128),
|
| 43 |
+
nn.ReLU(inplace=True),
|
| 44 |
+
|
| 45 |
+
nn.Upsample(scale_factor=2, mode='bilinear', align_corners=False),
|
| 46 |
+
nn.Conv2d(128, 64, kernel_size=3, padding=1, bias=False),
|
| 47 |
+
nn.BatchNorm2d(64),
|
| 48 |
+
nn.ReLU(inplace=True),
|
| 49 |
+
|
| 50 |
+
nn.Upsample(scale_factor=2, mode='bilinear', align_corners=False),
|
| 51 |
+
nn.Conv2d(64, 32, kernel_size=3, padding=1, bias=False),
|
| 52 |
+
nn.BatchNorm2d(32),
|
| 53 |
+
nn.ReLU(inplace=True),
|
| 54 |
+
|
| 55 |
+
nn.Conv2d(32, 1, kernel_size=1),
|
| 56 |
+
nn.Sigmoid(),
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
+
def forward(self, x):
|
| 60 |
+
feat = self.encoder(x)
|
| 61 |
+
out = self.decoder(feat)
|
| 62 |
+
# Ensure exact output size (MobileNetV2 downsamples aggressively)
|
| 63 |
+
if out.shape[2:] != self.OUTPUT_SIZE:
|
| 64 |
+
out = nn.functional.interpolate(out, size=self.OUTPUT_SIZE, mode='bilinear', align_corners=False)
|
| 65 |
+
return out
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
if __name__ == "__main__":
|
| 69 |
+
model = MobileNetStudent(input_nc=4)
|
| 70 |
+
n = sum(p.numel() for p in model.parameters())
|
| 71 |
+
print(f"Parameters: {n:,} ({n/1e6:.2f}M)")
|
| 72 |
+
x = torch.randn(2, 4, 161, 361)
|
| 73 |
+
with torch.no_grad():
|
| 74 |
+
y = model(x)
|
| 75 |
+
print(f"Input: {x.shape}")
|
| 76 |
+
print(f"Output: {y.shape}")
|
| 77 |
+
assert y.shape == (2, 1, 161, 361), f"Wrong shape: {y.shape}"
|
| 78 |
+
print("OK")
|