Views
No views yet
1class Net(nn.Module):
2 def __init__(self):
3 super().__init__()
4
5 # Feature extractor
6 self.conv1 = nn.Conv2d(4, 16, kernel_size=3, padding=1) # (RGB + EDGE: 3 + 1)
7 self.bn1 = nn.BatchNorm2d(16)
8
9 self.conv2 = nn.Conv2d(16, 32, kernel_size=3, padding=1)
10 self.bn2 = nn.BatchNorm2d(32)
11
12 self.conv3 = nn.Conv2d(32, 64, kernel_size=3, padding=1)
13 self.bn3 = nn.BatchNorm2d(64)
14
15 # After 3x maxpool (stride=2), 256 -> 128 -> 64 -> 32
16 self.fc1 = nn.Linear(64 * 32 * 32, 256)
17 self.fc2 = nn.Linear(256, 64)
18 self.fc3 = nn.Linear(64, 4) # 4 classes
19
20 self.dropout = nn.Dropout(0.5)
21
22 def forward(self, x):
23 # Conv layers
24 out = F.relu(self.bn1(self.conv1(x)))
25 out = F.max_pool2d(out, 2) # 256 -> 128
26
27 out = F.relu(self.bn2(self.conv2(out)))
28 out = F.max_pool2d(out, 2) # 128 -> 64
29
30 out = F.relu(self.bn3(self.conv3(out)))
31 out = F.max_pool2d(out, 2) # 64 -> 32
32
33 # Flatten
34 out = out.view(out.size(0), -1)
35
36 # Fully connected layers
37 out = F.relu(self.fc1(out))
38 out = self.dropout(out)
39
40 out = F.relu(self.fc2(out))
41 out = self.fc3(out) # logits, apply CrossEntropyLoss
42 return out
43
44

