Created
June 17, 2019 06:39
-
-
Save quanhua92/e3bb96a86f102b53129ac0db796b5b4c to your computer and use it in GitHub Desktop.
AlexNet: Review and Implementation - deeplearning.vn
This file contains hidden or 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 the implementation of AlexNet which is modified from [Jeicaoyu's AlexNet]. | |
Note: | |
- The number of Conv2d filters now matches with the original paper. | |
- Use PyTorch's Local Response Normalization layer which is implemented in Jan 2018. [PR #4667] | |
- This is for educational purpose only. We don't have pretrained weights for this model. | |
References: | |
- Jeicaoyu's AlexNet Model: [jiecaoyu](https://github.com/jiecaoyu/pytorch_imagenet/blob/984a2a988ba17b37e1173dd2518fa0f4dc4a1879/networks/model_list/alexnet.py) | |
- PR #4667: https://github.com/pytorch/pytorch/pull/4667 | |
""" | |
import torch.nn as nn | |
class AlexNet(nn.Module): | |
def __init__(self, num_classes=1000): | |
super(AlexNet, self).__init__() | |
self.features = nn.Sequential( | |
nn.Conv2d(3, 96, kernel_size=11, stride=4, padding=0), | |
nn.ReLU(inplace=True), | |
nn.LocalResponseNorm(size=5, alpha=0.0001, beta=0.75), | |
nn.MaxPool2d(kernel_size=3, stride=2), | |
nn.Conv2d(96, 256, kernel_size=5, padding=2, groups=2), | |
nn.ReLU(inplace=True), | |
nn.LocalResponseNorm(size=5, alpha=0.0001, beta=0.75), | |
nn.MaxPool2d(kernel_size=3, stride=2), | |
nn.Conv2d(256, 384, kernel_size=3, padding=1), | |
nn.ReLU(inplace=True), | |
nn.Conv2d(384, 384, kernel_size=3, padding=1, groups=2), | |
nn.ReLU(inplace=True), | |
nn.Conv2d(384, 256, kernel_size=3, padding=1, groups=2), | |
nn.ReLU(inplace=True), | |
nn.MaxPool2d(kernel_size=3, stride=2), | |
) | |
self.classifier = nn.Sequential( | |
nn.Linear(256 * 6 * 6, 4096), | |
nn.ReLU(inplace=True), | |
nn.Dropout(), | |
nn.Linear(4096, 4096), | |
nn.ReLU(inplace=True), | |
nn.Dropout(), | |
nn.Linear(4096, num_classes), | |
) | |
def forward(self, x): | |
x = self.features(x) | |
x = x.view(x.size(0), 256 * 6 * 6) | |
x = self.classifier(x) | |
return x |
This file contains hidden or 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