alexnet.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. from functools import partial
  2. from typing import Any, Optional
  3. import torch
  4. import torch.nn as nn
  5. from ..transforms._presets import ImageClassification
  6. from ..utils import _log_api_usage_once
  7. from ._api import register_model, Weights, WeightsEnum
  8. from ._meta import _IMAGENET_CATEGORIES
  9. from ._utils import _ovewrite_named_param, handle_legacy_interface
  10. __all__ = ["AlexNet", "AlexNet_Weights", "alexnet"]
  11. class AlexNet(nn.Module):
  12. def __init__(self, num_classes: int = 1000, dropout: float = 0.5) -> None:
  13. super().__init__()
  14. _log_api_usage_once(self)
  15. self.features = nn.Sequential(
  16. nn.Conv2d(3, 64, kernel_size=11, stride=4, padding=2),
  17. nn.ReLU(inplace=True),
  18. nn.MaxPool2d(kernel_size=3, stride=2),
  19. nn.Conv2d(64, 192, kernel_size=5, padding=2),
  20. nn.ReLU(inplace=True),
  21. nn.MaxPool2d(kernel_size=3, stride=2),
  22. nn.Conv2d(192, 384, kernel_size=3, padding=1),
  23. nn.ReLU(inplace=True),
  24. nn.Conv2d(384, 256, kernel_size=3, padding=1),
  25. nn.ReLU(inplace=True),
  26. nn.Conv2d(256, 256, kernel_size=3, padding=1),
  27. nn.ReLU(inplace=True),
  28. nn.MaxPool2d(kernel_size=3, stride=2),
  29. )
  30. self.avgpool = nn.AdaptiveAvgPool2d((6, 6))
  31. self.classifier = nn.Sequential(
  32. nn.Dropout(p=dropout),
  33. nn.Linear(256 * 6 * 6, 4096),
  34. nn.ReLU(inplace=True),
  35. nn.Dropout(p=dropout),
  36. nn.Linear(4096, 4096),
  37. nn.ReLU(inplace=True),
  38. nn.Linear(4096, num_classes),
  39. )
  40. def forward(self, x: torch.Tensor) -> torch.Tensor:
  41. x = self.features(x)
  42. x = self.avgpool(x)
  43. x = torch.flatten(x, 1)
  44. x = self.classifier(x)
  45. return x
  46. class AlexNet_Weights(WeightsEnum):
  47. IMAGENET1K_V1 = Weights(
  48. url="https://download.pytorch.org/models/alexnet-owt-7be5be79.pth",
  49. transforms=partial(ImageClassification, crop_size=224),
  50. meta={
  51. "num_params": 61100840,
  52. "min_size": (63, 63),
  53. "categories": _IMAGENET_CATEGORIES,
  54. "recipe": "https://github.com/pytorch/vision/tree/main/references/classification#alexnet-and-vgg",
  55. "_metrics": {
  56. "ImageNet-1K": {
  57. "acc@1": 56.522,
  58. "acc@5": 79.066,
  59. }
  60. },
  61. "_ops": 0.714,
  62. "_file_size": 233.087,
  63. "_docs": """
  64. These weights reproduce closely the results of the paper using a simplified training recipe.
  65. """,
  66. },
  67. )
  68. DEFAULT = IMAGENET1K_V1
  69. @register_model()
  70. @handle_legacy_interface(weights=("pretrained", AlexNet_Weights.IMAGENET1K_V1))
  71. def alexnet(*, weights: Optional[AlexNet_Weights] = None, progress: bool = True, **kwargs: Any) -> AlexNet:
  72. """AlexNet model architecture from `One weird trick for parallelizing convolutional neural networks <https://arxiv.org/abs/1404.5997>`__.
  73. .. note::
  74. AlexNet was originally introduced in the `ImageNet Classification with
  75. Deep Convolutional Neural Networks
  76. <https://papers.nips.cc/paper/2012/hash/c399862d3b9d6b76c8436e924a68c45b-Abstract.html>`__
  77. paper. Our implementation is based instead on the "One weird trick"
  78. paper above.
  79. Args:
  80. weights (:class:`~torchvision.models.AlexNet_Weights`, optional): The
  81. pretrained weights to use. See
  82. :class:`~torchvision.models.AlexNet_Weights` below for
  83. more details, and possible values. By default, no pre-trained
  84. weights are used.
  85. progress (bool, optional): If True, displays a progress bar of the
  86. download to stderr. Default is True.
  87. **kwargs: parameters passed to the ``torchvision.models.squeezenet.AlexNet``
  88. base class. Please refer to the `source code
  89. <https://github.com/pytorch/vision/blob/main/torchvision/models/alexnet.py>`_
  90. for more details about this class.
  91. .. autoclass:: torchvision.models.AlexNet_Weights
  92. :members:
  93. """
  94. weights = AlexNet_Weights.verify(weights)
  95. if weights is not None:
  96. _ovewrite_named_param(kwargs, "num_classes", len(weights.meta["categories"]))
  97. model = AlexNet(**kwargs)
  98. if weights is not None:
  99. model.load_state_dict(weights.get_state_dict(progress=progress, check_hash=True))
  100. return model