Created
June 18, 2019 10:22
-
-
Save quanhua92/c415ee43db7cfce306bb550be8cb9667 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
This is AlexNet implementation from pytorch/torchvision. | |
Note: | |
- The number of nn.Conv2d doesn't match with the original paper. | |
- This model uses `nn.AdaptiveAvgPool2d` to allow the model to process images with arbitrary image size. [PR #746] | |
- This model doesn't use Local Response Normalization as described in the original paper. | |
- This model is implemented in Jan 2017 with pretrained model. | |
- PyTorch's Local Response Normalization layer is implemented in Jan 2018. [PR #4667] | |
References: | |
- Model: https://github.com/pytorch/vision/blob/ac2e995a4352267f65e7cc6d354bde683a4fb402/torchvision/models/alexnet.py | |
- PR #746: https://github.com/pytorch/vision/pull/746 | |
- PR #4667: https://github.com/pytorch/pytorch/pull/4667 | |
""" | |
import torch.nn as nn | |
from models.utils import load_state_dict_from_url | |
__all__ = ['AlexNet', 'alexnet'] | |
model_urls = { | |
'alexnet': 'https://download.pytorch.org/models/alexnet-owt-4df8aa71.pth', | |
} | |
class AlexNet(nn.Module): | |
def __init__(self, num_classes=1000): | |
super(AlexNet, self).__init__() | |
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), | |
) | |
self.avgpool = nn.AdaptiveAvgPool2d((6, 6)) | |
self.classifier = nn.Sequential( | |
nn.Dropout(), | |
nn.Linear(256 * 6 * 6, 4096), | |
nn.ReLU(inplace=True), | |
nn.Dropout(), | |
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) | |
return x | |
def alexnet(pretrained=False, progress=True, **kwargs): | |
r"""AlexNet model architecture from the | |
`"One weird trick..." <https://arxiv.org/abs/1404.5997>`_ paper. | |
Args: | |
pretrained (bool): If True, returns a model pre-trained on ImageNet | |
progress (bool): If True, displays a progress bar of the download to stderr | |
""" | |
model = AlexNet(**kwargs) | |
if pretrained: | |
state_dict = load_state_dict_from_url(model_urls['alexnet'], | |
progress=progress) | |
model.load_state_dict(state_dict) | |
return model |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment