-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
86 lines (72 loc) · 2.4 KB
/
model.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import torch
import torch.nn as nn
""" AlexNet model architecture using PyTorch
Paper: ImageNet Classification with Deep Convolutional Neural Networks
Reference: https://papers.nips.cc/paper/2012/file/c399862d3b9d6b76c8436e924a68c45b-Paper.pdf
"""
class AlexNet(nn.Module):
def __init__(self, num_classes=1000):
super().__init__()
self.features = nn.Sequential(
# Layer 1
nn.Conv2d(
in_channels=3, out_channels=96, kernel_size=11, stride=4, padding=2
),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=3, stride=2),
# Layer 2
nn.Conv2d(
in_channels=96, out_channels=256, kernel_size=5, stride=1, padding=2
),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=3, stride=2),
# Layer 3
nn.Conv2d(
in_channels=256,
out_channels=384,
kernel_size=3,
stride=1,
padding=1,
),
nn.ReLU(inplace=True),
# Layer 4
nn.Conv2d(
in_channels=384,
out_channels=384,
kernel_size=3,
stride=1,
padding=1,
),
nn.ReLU(inplace=True),
# Layer 5
nn.Conv2d(
in_channels=384,
out_channels=256,
kernel_size=3,
stride=1,
padding=1,
),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=3, stride=2),
)
# Adding an adaptive pooling layer to ensure the input size to the classifier is fixed as 6x6
self.avgpool = nn.AdaptiveAvgPool2d((6, 6))
self.classifier = nn.Sequential(
# Fully connected layer 1
nn.Dropout(p=0.5),
nn.Linear(256 * 6 * 6, 4096),
nn.ReLU(inplace=True),
# Fully connected layer 2
nn.Dropout(p=0.5),
nn.Linear(4096, 4096),
nn.ReLU(inplace=True),
# Output layer
nn.Linear(4096, num_classes),
# Softmax is ommited, because it's included in the loss function
)
def forward(self, x):
x = self.features(x)
x = self.avgpool(x)
x = torch.flatten(x, 1)
x = self.classifier(x)
return x