alexnet.py 2.28 KB
Newer Older
1
2
from typing import Any

3
import torch
4
import torch.nn as nn
5

6
from .._internally_replaced_utils import load_state_dict_from_url
7
from ..utils import _log_api_usage_once
8
9


10
__all__ = ["AlexNet", "alexnet"]
11
12
13


model_urls = {
14
    "alexnet": "https://download.pytorch.org/models/alexnet-owt-7be5be79.pth",
15
16
17
}


Soumith Chintala's avatar
Soumith Chintala committed
18
class AlexNet(nn.Module):
19
    def __init__(self, num_classes: int = 1000, dropout: float = 0.5) -> None:
20
        super().__init__()
21
        _log_api_usage_once(self)
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
        self.features = 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),
        )
37
        self.avgpool = nn.AdaptiveAvgPool2d((6, 6))
38
        self.classifier = nn.Sequential(
39
            nn.Dropout(p=dropout),
40
41
            nn.Linear(256 * 6 * 6, 4096),
            nn.ReLU(inplace=True),
42
            nn.Dropout(p=dropout),
43
44
45
46
47
            nn.Linear(4096, 4096),
            nn.ReLU(inplace=True),
            nn.Linear(4096, num_classes),
        )

48
    def forward(self, x: torch.Tensor) -> torch.Tensor:
49
        x = self.features(x)
50
        x = self.avgpool(x)
51
        x = torch.flatten(x, 1)
52
53
54
55
        x = self.classifier(x)
        return x


56
def alexnet(pretrained: bool = False, progress: bool = True, **kwargs: Any) -> AlexNet:
57
58
    r"""AlexNet model architecture from the
    `"One weird trick..." <https://arxiv.org/abs/1404.5997>`_ paper.
59
    The required minimum input size of the model is 63x63.
60
61
62

    Args:
        pretrained (bool): If True, returns a model pre-trained on ImageNet
63
        progress (bool): If True, displays a progress bar of the download to stderr
64
    """
65
    model = AlexNet(**kwargs)
66
    if pretrained:
67
        state_dict = load_state_dict_from_url(model_urls["alexnet"], progress=progress)
68
        model.load_state_dict(state_dict)
69
    return model