-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
63 lines (55 loc) · 2.12 KB
/
models.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
60
61
62
63
import torch
import torch.nn as nn
import torch.nn.functional as F
class Lenet5(nn.Module):
def __init__(self):
super(Lenet5, self).__init__()
self.conv1 = nn.Conv2d(1, 32, kernel_size=3, padding=1, stride=1)
self.relu1 = nn.ReLU(inplace=True)
self.maxpool1 = nn.MaxPool2d(2)
self.conv2 = nn.Conv2d(32, 64, kernel_size=3, padding=1, stride=1)
self.relu2 = nn.ReLU(inplace=True)
self.maxpool2 = nn.MaxPool2d(2)
self.linear1 = nn.Linear(7 * 7 * 64, 200)
self.relu3 = nn.ReLU(inplace=True)
self.linear2 = nn.Linear(200, 10)
def forward(self, x):
x = self.maxpool1(self.relu1(self.conv1(x)))
x = self.maxpool2(self.relu2(self.conv2(x)))
x = x.view(x.size(0), -1)
x = self.relu3(self.linear1(x))
x = self.linear2(x)
return x
class Alexnet(nn.Module):
def __init__(self, num_classes):
super(Alexnet, self).__init__()
self.features = nn.Sequential(
nn.Conv2d(3, 64, kernel_size=11, stride=1, padding=0),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=3, stride=1),
nn.Conv2d(64, 192, kernel_size=5, padding=0),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=3, stride=1),
nn.Conv2d(192, 384, kernel_size=3, padding=0),
nn.ReLU(inplace=True),
nn.Conv2d(384, 256, kernel_size=3, padding=0),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=3, stride=1),
)
self.avgpool = nn.AdaptiveAvgPool2d((6, 6))
self.classifier = nn.Sequential(
nn.Dropout(0.5),
nn.Linear(256 * 6 * 6, 4096),
nn.ReLU(inplace=True),
nn.Dropout(0.5),
nn.Linear(4096, 4096),
nn.ReLU(inplace=True),
nn.Linear(4096, num_classes)
)
def forward(self, x):
x = self.features(x)
x = self.avgpool(x)
x = x.view(x.size(0), 256 * 6 * 6)
x = self.classifier(x)
x = F.softmax(x, dim=1)
return x