-
Notifications
You must be signed in to change notification settings - Fork 0
/
modules.py
59 lines (54 loc) · 1.78 KB
/
modules.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
from torch import nn
class LeNet5(nn.Module):
def __init__(self, n_classes: int = 13):
super(LeNet5, self).__init__()
self.conv1 = nn.Conv2d(1, 6, 5)
self.relu1 = nn.ReLU()
self.pool1 = nn.MaxPool2d(2)
self.conv2 = nn.Conv2d(6, 16, 5)
self.relu2 = nn.ReLU()
self.pool2 = nn.MaxPool2d(2)
self.fc1 = nn.Linear(256, 120)
self.relu3 = nn.ReLU()
self.fc2 = nn.Linear(120, 84)
self.relu4 = nn.ReLU()
self.fc3 = nn.Linear(84, n_classes)
self.relu5 = nn.ReLU()
def forward(self, x):
y = self.conv1(x)
y = self.relu1(y)
y = self.pool1(y)
y = self.conv2(y)
y = self.relu2(y)
y = self.pool2(y)
y = y.view(y.shape[0], -1)
y = self.fc1(y)
y = self.relu3(y)
y = self.fc2(y)
y = self.relu4(y)
y = self.fc3(y)
y = self.relu5(y)
return y
class CNN(nn.Module):
def __init__(self, n_classes: int = 13):
super(CNN, self).__init__()
self.conv0 = nn.Sequential(
nn.Conv2d(1, 16, (5,5), (2,2), (0,0)),
nn.BatchNorm2d(16),
nn.ReLU(),)
self.conv1 = nn.Sequential(
nn.Conv2d(16, 32, (3,3), (2,2), (0,0)),
nn.BatchNorm2d(32),
nn.ReLU(),)
self.conv2 = nn.Sequential(
nn.Conv2d(32, 16, (3,3), (1,1), (0,0)),
nn.BatchNorm2d(16),
nn.ReLU(),)
self.out = nn.Linear(16 * 3 * 3, n_classes)
def forward(self, x):
x = self.conv0(x)
x = self.conv1(x)
x = self.conv2(x)
x = x.view(x.size(0), -1)
x = self.out(x)
return x