JustAGeek commited on
Commit
ecc201f
·
verified ·
1 Parent(s): e0ca160

Upload student_unet.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. student_unet.py +114 -0
student_unet.py ADDED
@@ -0,0 +1,114 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ student_unet.py
3
+ MobileNetV2 + U-Net skip connections student model for DLoc.
4
+
5
+ Architecture (Professor Phase 2, Step 5 Option 1):
6
+ Encoder: MobileNetV2 backbone split into 4 stages for skip connections
7
+ - inc: ConvBNReLU(4->32, stride=2)
8
+ - enc1: features[1:4] -> 24 channels (Skip 1)
9
+ - enc2: features[4:7] -> 32 channels (Skip 2)
10
+ - enc3: features[7:14] -> 96 channels (Skip 3)
11
+ - enc4: features[14:18] -> 320 channels (Bottleneck)
12
+ Decoder: Upsample + Concat skip + Conv
13
+ - dec1: 320+96=416 -> 128
14
+ - dec2: 128+32=160 -> 64
15
+ - dec3: 64+24=88 -> 32
16
+ - final: 32 -> 1 + Sigmoid
17
+ Output: [B, 1, 161, 361]
18
+ """
19
+ import torch
20
+ import torch.nn as nn
21
+ import torch.nn.functional as F
22
+ from torchvision.models import mobilenet_v2, MobileNet_V2_Weights
23
+
24
+
25
+ class MobileNetUNetStudent(nn.Module):
26
+ OUTPUT_SIZE = (161, 361)
27
+
28
+ def __init__(self, input_nc=4):
29
+ super().__init__()
30
+
31
+ mobilenet = mobilenet_v2(weights=MobileNet_V2_Weights.DEFAULT)
32
+
33
+ # Modify first conv for input_nc channels
34
+ orig_conv = mobilenet.features[0][0]
35
+ self.inc = nn.Sequential(
36
+ nn.Conv2d(input_nc, orig_conv.out_channels,
37
+ kernel_size=orig_conv.kernel_size,
38
+ stride=orig_conv.stride,
39
+ padding=orig_conv.padding, bias=False),
40
+ mobilenet.features[0][1], # BatchNorm
41
+ mobilenet.features[0][2], # ReLU6
42
+ )
43
+
44
+ # Encoder stages (split to capture skip connections)
45
+ self.enc1 = mobilenet.features[1:4] # -> 24 ch
46
+ self.enc2 = mobilenet.features[4:7] # -> 32 ch
47
+ self.enc3 = mobilenet.features[7:14] # -> 96 ch
48
+ self.enc4 = mobilenet.features[14:-1] # -> 320 ch (drop final 1x1 conv)
49
+
50
+ # Decoder with skip connections
51
+ self.up1 = nn.Upsample(scale_factor=2, mode='bilinear', align_corners=False)
52
+ self.dec1 = nn.Sequential(
53
+ nn.Conv2d(320 + 96, 128, 3, padding=1, bias=False),
54
+ nn.BatchNorm2d(128),
55
+ nn.ReLU(inplace=True),
56
+ )
57
+
58
+ self.up2 = nn.Upsample(scale_factor=2, mode='bilinear', align_corners=False)
59
+ self.dec2 = nn.Sequential(
60
+ nn.Conv2d(128 + 32, 64, 3, padding=1, bias=False),
61
+ nn.BatchNorm2d(64),
62
+ nn.ReLU(inplace=True),
63
+ )
64
+
65
+ self.up3 = nn.Upsample(scale_factor=2, mode='bilinear', align_corners=False)
66
+ self.dec3 = nn.Sequential(
67
+ nn.Conv2d(64 + 24, 32, 3, padding=1, bias=False),
68
+ nn.BatchNorm2d(32),
69
+ nn.ReLU(inplace=True),
70
+ )
71
+
72
+ self.final_conv = nn.Sequential(
73
+ nn.Conv2d(32, 1, kernel_size=1),
74
+ nn.Sigmoid(),
75
+ )
76
+
77
+ def forward(self, x):
78
+ # Encoder
79
+ x0 = self.inc(x)
80
+ s1 = self.enc1(x0) # skip 1: 24 ch
81
+ s2 = self.enc2(s1) # skip 2: 32 ch
82
+ s3 = self.enc3(s2) # skip 3: 96 ch
83
+ bottle = self.enc4(s3) # bottleneck: 320 ch
84
+
85
+ # Decoder with skip concatenation
86
+ d1 = self.up1(bottle)
87
+ s3r = F.interpolate(s3, size=d1.shape[2:], mode='bilinear', align_corners=False)
88
+ d1 = self.dec1(torch.cat([d1, s3r], dim=1))
89
+
90
+ d2 = self.up2(d1)
91
+ s2r = F.interpolate(s2, size=d2.shape[2:], mode='bilinear', align_corners=False)
92
+ d2 = self.dec2(torch.cat([d2, s2r], dim=1))
93
+
94
+ d3 = self.up3(d2)
95
+ s1r = F.interpolate(s1, size=d3.shape[2:], mode='bilinear', align_corners=False)
96
+ d3 = self.dec3(torch.cat([d3, s1r], dim=1))
97
+
98
+ out = self.final_conv(d3)
99
+ if out.shape[2:] != self.OUTPUT_SIZE:
100
+ out = F.interpolate(out, size=self.OUTPUT_SIZE, mode='bilinear', align_corners=False)
101
+ return out
102
+
103
+
104
+ if __name__ == "__main__":
105
+ model = MobileNetUNetStudent(input_nc=4)
106
+ n = sum(p.numel() for p in model.parameters())
107
+ print(f"Parameters: {n:,} ({n/1e6:.2f}M)")
108
+ x = torch.randn(2, 4, 161, 361)
109
+ with torch.no_grad():
110
+ y = model(x)
111
+ print(f"Input: {x.shape}")
112
+ print(f"Output: {y.shape}")
113
+ assert y.shape == (2, 1, 161, 361), f"Wrong shape: {y.shape}"
114
+ print("OK")