-
Notifications
You must be signed in to change notification settings - Fork 2
/
alexnet.py
57 lines (47 loc) · 1.86 KB
/
alexnet.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
import torch
import torch.nn as nn
from torchsummary import summary
# Design choice: Simple architecture, just use nn.Sequential to group operations (easy to understand)
class AlexNet(nn.Module):
def __init__(self, num_classes):
super().__init__()
self.feature_extractor = nn.Sequential(
nn.Conv2d(3, 64, kernel_size=11, stride=4, padding=2),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=3, stride=2),
nn.Conv2d(64, 192, kernel_size=5, padding=2),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=3, stride=2),
nn.Conv2d(192, 384, kernel_size=3, padding=1),
nn.ReLU(inplace=True),
nn.Conv2d(384, 256, kernel_size=3, padding=1),
nn.ReLU(inplace=True),
nn.Conv2d(256, 256, kernel_size=3, padding=1),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=3, stride=2),
nn.AdaptiveAvgPool2d(6),
nn.Flatten()
)
self.classifier = nn.Sequential(
nn.Dropout(p=0.5),
nn.Linear(256 * 6 * 6, 4096),
nn.ReLU(inplace=True),
nn.Dropout(p=0.5),
nn.Linear(4096, 4096),
nn.ReLU(inplace=True),
nn.Linear(4096, num_classes),
)
def forward(self, x):
x = self.feature_extractor(x)
x = self.classifier(x)
return x
if __name__ == "__main__":
batch_size = 10
num_classes = 1000
x = torch.randn(batch_size, 3, 256, 256)
model = AlexNet(num_classes)
output = model(x)
# Good sanity check to have for your output, expected output is [batch, class] size.
assert output.shape[0] == batch_size and output.shape[1] == num_classes
print(f"Output shape: {output.shape}, batch size: {batch_size}, number of classes: {num_classes}")
summary(model, (3, 256, 256))