-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmodels.py
68 lines (62 loc) · 1.76 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
64
65
66
67
68
import torch
import torch.nn as nn
from .blocks import *
__all__ = ["AlexNet"]
class AlexNet(nn.Module):
def __init__(
self,
image_channels: int,
num_classes: int,
dropout_rate: float = 0.5,
) -> None:
super().__init__()
self.feature_extractor = nn.Sequential(
ConvBlock(
in_channels=image_channels,
out_channels=64,
kernel_size=11,
stride=4,
padding=2,
),
nn.MaxPool2d(kernel_size=3, stride=2),
ConvBlock(
in_channels=64,
out_channels=192,
kernel_size=5,
stride=1,
padding=2,
),
nn.MaxPool2d(kernel_size=3, stride=2),
ConvBlock(
in_channels=192,
out_channels=384,
kernel_size=3,
stride=1,
padding=1,
),
ConvBlock(
in_channels=384,
out_channels=256,
kernel_size=3,
stride=1,
padding=1,
),
ConvBlock(
in_channels=256,
out_channels=256,
kernel_size=3,
stride=1,
padding=1,
),
nn.MaxPool2d(kernel_size=3, stride=2),
nn.AdaptiveAvgPool2d(6),
)
self.classifier = Classifier(
dim=[256 * 6 * 6, 4096, num_classes],
dropout_rate=dropout_rate,
)
def forward(self, x: torch.Tensor) -> torch.Tensor:
x = self.feature_extractor(x)
x = torch.flatten(x, 1)
logits = self.classifier(x)
return logits