Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add AlexNet to paddle vision #36031

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions python/paddle/tests/test_vision_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ def test_mobilenetv2_pretrained(self):
def test_mobilenetv1(self):
self.models_infer('mobilenet_v1')

def test_alexnet(self):
self.models_infer('AlexNet')

def test_vgg11(self):
self.models_infer('vgg11')

Expand Down
4 changes: 3 additions & 1 deletion python/paddle/vision/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
from .vgg import vgg16 # noqa: F401
from .vgg import vgg19 # noqa: F401
from .lenet import LeNet # noqa: F401
from .alexnet import AlexNet # noqa: F401

__all__ = [ #noqa
'ResNet',
Expand All @@ -45,5 +46,6 @@
'mobilenet_v1',
'MobileNetV2',
'mobilenet_v2',
'LeNet'
'LeNet',
'AlexNet'
]
71 changes: 71 additions & 0 deletions python/paddle/vision/models/alexnet.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve.
#
#Licensed under the Apache License, Version 2.0 (the "License");
#you may not use this file except in compliance with the License.
#You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
#Unless required by applicable law or agreed to in writing, software
#distributed under the License is distributed on an "AS IS" BASIS,
#WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#See the License for the specific language governing permissions and
#limitations under the License.

import paddle
import paddle.nn as nn

__all__ = []


class AlexNet(nn.Layer):
def __init__(self, num_classes=10):
"""AlexNet model architecture from the
`"One weird trick..." <https://arxiv.org/abs/1404.5997>`_ paper.

Args:
num_classes (int): output dim of the classifier. If num_classes <=0, the classifier
will not be defined. Default: 10.

Examples:
.. code-block:: python

from paddle.vision.models import AlexNet

model = AlexNet()
"""
super(AlexNet, self).__init__()
self.num_classes = num_classes
self.features = nn.Sequential(
nn.Conv2D(3, 64, kernel_size=3, stride=2, padding=1),
nn.ReLU(),
nn.MaxPool2D(kernel_size=3, stride=2),
nn.Conv2D(64, 192, kernel_size=3, stride=1, padding=1),
nn.ReLU(),
nn.MaxPool2D(kernel_size=3, stride=2),
nn.Conv2D(192, 384, kernel_size=3, stride=1, padding=1),
nn.ReLU(),
nn.Conv2D(384, 256, kernel_size=3, stride=1, padding=1),
nn.ReLU(),
nn.Conv2D(256, 256, kernel_size=3, stride=1, padding=1),
nn.ReLU(),
nn.MaxPool2D(kernel_size=3, stride=2),
)

if num_classes > 0:
self.classifier = nn.Sequential(
nn.Dropout(),
nn.Linear(256 * 1 * 1, 4096),
nn.ReLU(),
nn.Dropout(),
nn.Linear(4096, 4096),
nn.ReLU(),
nn.Linear(4096, num_classes),
)

def forward(self, x):
x = self.features(x)
if self.num_classes > 0:
x = paddle.flatten(x, 1)
x = self.classifier(x)
return x