-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
[PaddlePaddle Hackathon] add Squeezenet #36066
Merged
Merged
Changes from all commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
9736436
add squeezenet
fuqianya b9a26d9
py2 support
fuqianya db5d41f
add unit test
fuqianya d753f10
Update squeezenet.py
fuqianya d54e23e
fix code style
fuqianya 14500e8
fix code style
fuqianya 7b10fb7
deleted unreferenced package
fuqianya b486ba4
add en doc
fuqianya 92e461f
update pretrained model
fuqianya 7ee491b
add pretrained test
fuqianya 2de7106
remove name of weights
fuqianya e99ae39
update SqueezeNet API
fuqianya 67dfda8
fix CI
fuqianya 460178f
Fix merge conflicts
fuqianya 9c29d42
fix merge conflicts
fuqianya 5fd51ab
fix merge conflicts
fuqianya 5084c36
add squeezenet version checking
fuqianya fbd459f
fix CI
fuqianya 8c9f23b
fix merge conflicts & add code block
fuqianya ba4f10b
fix merge conflicts & add code block
fuqianya 1ba6a6e
fix CI
fuqianya e1c0cfe
fix CI
fuqianya fe9421a
fix CI
fuqianya 1057929
fix CI
fuqianya e3d621b
fix merge conflicts
fuqianya 6ba9791
fix merge conflicts & fix CI
fuqianya File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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 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 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 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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,240 @@ | ||
# copyright (c) 2021 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. | ||
|
||
from __future__ import absolute_import | ||
from __future__ import division | ||
from __future__ import print_function | ||
|
||
import paddle | ||
import paddle.nn as nn | ||
import paddle.nn.functional as F | ||
|
||
from paddle.nn import Conv2D, Dropout | ||
from paddle.nn import AdaptiveAvgPool2D, MaxPool2D | ||
from paddle.fluid.param_attr import ParamAttr | ||
from paddle.utils.download import get_weights_path_from_url | ||
|
||
__all__ = [] | ||
|
||
model_urls = { | ||
'squeezenet1_0': | ||
('https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/SqueezeNet1_0_pretrained.pdparams', | ||
'30b95af60a2178f03cf9b66cd77e1db1'), | ||
'squeezenet1_1': | ||
('https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/SqueezeNet1_1_pretrained.pdparams', | ||
'a11250d3a1f91d7131fd095ebbf09eee'), | ||
} | ||
|
||
|
||
class MakeFireConv(nn.Layer): | ||
def __init__(self, input_channels, output_channels, filter_size, padding=0): | ||
super(MakeFireConv, self).__init__() | ||
self._conv = Conv2D( | ||
input_channels, | ||
output_channels, | ||
filter_size, | ||
padding=padding, | ||
weight_attr=ParamAttr(), | ||
bias_attr=ParamAttr()) | ||
|
||
def forward(self, x): | ||
x = self._conv(x) | ||
x = F.relu(x) | ||
return x | ||
|
||
|
||
class MakeFire(nn.Layer): | ||
def __init__(self, input_channels, squeeze_channels, expand1x1_channels, | ||
expand3x3_channels): | ||
super(MakeFire, self).__init__() | ||
self._conv = MakeFireConv(input_channels, squeeze_channels, 1) | ||
self._conv_path1 = MakeFireConv(squeeze_channels, expand1x1_channels, 1) | ||
self._conv_path2 = MakeFireConv( | ||
squeeze_channels, expand3x3_channels, 3, padding=1) | ||
|
||
def forward(self, inputs): | ||
x = self._conv(inputs) | ||
x1 = self._conv_path1(x) | ||
x2 = self._conv_path2(x) | ||
return paddle.concat([x1, x2], axis=1) | ||
|
||
|
||
class SqueezeNet(nn.Layer): | ||
"""SqueezeNet model from | ||
`"SqueezeNet: AlexNet-level accuracy with 50x fewer parameters and <0.5MB model size" | ||
<https://arxiv.org/pdf/1602.07360.pdf>`_ | ||
|
||
Args: | ||
version (str): version of squeezenet, which can be "1.0" or "1.1". | ||
num_classes (int): output dim of last fc layer. Default: 1000. | ||
with_pool (bool): use pool before the last fc layer or not. Default: True. | ||
|
||
Examples: | ||
.. code-block:: python | ||
import paddle | ||
from paddle.vision.models import SqueezeNet | ||
|
||
# build v1.0 model | ||
model = SqueezeNet(version='1.0') | ||
|
||
# build v1.1 model | ||
# model = SqueezeNet(version='1.1') | ||
|
||
x = paddle.rand([1, 3, 224, 224]) | ||
out = model(x) | ||
|
||
print(out.shape) | ||
|
||
""" | ||
|
||
def __init__(self, version, num_classes=1000, with_pool=True): | ||
super(SqueezeNet, self).__init__() | ||
self.version = version | ||
self.num_classes = num_classes | ||
self.with_pool = with_pool | ||
|
||
supported_versions = ['1.0', '1.1'] | ||
assert version in supported_versions, \ | ||
"supported versions are {} but input version is {}".format( | ||
supported_versions, version) | ||
|
||
if self.version == "1.0": | ||
self._conv = Conv2D( | ||
3, | ||
96, | ||
7, | ||
stride=2, | ||
weight_attr=ParamAttr(), | ||
bias_attr=ParamAttr()) | ||
self._pool = MaxPool2D(kernel_size=3, stride=2, padding=0) | ||
self._conv1 = MakeFire(96, 16, 64, 64) | ||
self._conv2 = MakeFire(128, 16, 64, 64) | ||
self._conv3 = MakeFire(128, 32, 128, 128) | ||
self._conv4 = MakeFire(256, 32, 128, 128) | ||
self._conv5 = MakeFire(256, 48, 192, 192) | ||
self._conv6 = MakeFire(384, 48, 192, 192) | ||
self._conv7 = MakeFire(384, 64, 256, 256) | ||
self._conv8 = MakeFire(512, 64, 256, 256) | ||
else: | ||
self._conv = Conv2D( | ||
3, | ||
64, | ||
3, | ||
stride=2, | ||
padding=1, | ||
weight_attr=ParamAttr(), | ||
bias_attr=ParamAttr()) | ||
self._pool = MaxPool2D(kernel_size=3, stride=2, padding=0) | ||
self._conv1 = MakeFire(64, 16, 64, 64) | ||
self._conv2 = MakeFire(128, 16, 64, 64) | ||
self._conv3 = MakeFire(128, 32, 128, 128) | ||
self._conv4 = MakeFire(256, 32, 128, 128) | ||
self._conv5 = MakeFire(256, 48, 192, 192) | ||
self._conv6 = MakeFire(384, 48, 192, 192) | ||
self._conv7 = MakeFire(384, 64, 256, 256) | ||
self._conv8 = MakeFire(512, 64, 256, 256) | ||
|
||
self._drop = Dropout(p=0.5, mode="downscale_in_infer") | ||
self._conv9 = Conv2D( | ||
512, num_classes, 1, weight_attr=ParamAttr(), bias_attr=ParamAttr()) | ||
self._avg_pool = AdaptiveAvgPool2D(1) | ||
|
||
def forward(self, inputs): | ||
x = self._conv(inputs) | ||
x = F.relu(x) | ||
x = self._pool(x) | ||
if self.version == "1.0": | ||
x = self._conv1(x) | ||
x = self._conv2(x) | ||
x = self._conv3(x) | ||
x = self._pool(x) | ||
x = self._conv4(x) | ||
x = self._conv5(x) | ||
x = self._conv6(x) | ||
x = self._conv7(x) | ||
x = self._pool(x) | ||
x = self._conv8(x) | ||
else: | ||
x = self._conv1(x) | ||
x = self._conv2(x) | ||
x = self._pool(x) | ||
x = self._conv3(x) | ||
x = self._conv4(x) | ||
x = self._pool(x) | ||
x = self._conv5(x) | ||
x = self._conv6(x) | ||
x = self._conv7(x) | ||
x = self._conv8(x) | ||
if self.num_classes > 0: | ||
x = self._drop(x) | ||
x = self._conv9(x) | ||
if self.with_pool: | ||
x = F.relu(x) | ||
x = self._avg_pool(x) | ||
x = paddle.squeeze(x, axis=[2, 3]) | ||
|
||
return x | ||
|
||
|
||
def _squeezenet(arch, version, pretrained, **kwargs): | ||
model = SqueezeNet(version, **kwargs) | ||
if pretrained: | ||
assert arch in model_urls, "{} model do not have a pretrained model now, you should set pretrained=False".format( | ||
arch) | ||
weight_path = get_weights_path_from_url(model_urls[arch][0], | ||
model_urls[arch][1]) | ||
param = paddle.load(weight_path) | ||
model.set_dict(param) | ||
|
||
return model | ||
|
||
|
||
def squeezenet1_0(pretrained=False, **kwargs): | ||
"""SqueezeNet v1.0 model | ||
|
||
Args: | ||
pretrained (bool): If True, returns a model pre-trained on ImageNet. Default: False. | ||
|
||
Examples: | ||
.. code-block:: python | ||
|
||
from paddle.vision.models import squeezenet1_0 | ||
|
||
# build model | ||
model = squeezenet1_0() | ||
|
||
# build model and load imagenet pretrained weight | ||
# model = squeezenet1_0(pretrained=True) | ||
""" | ||
return _squeezenet('squeezenet1_0', '1.0', pretrained, **kwargs) | ||
|
||
|
||
def squeezenet1_1(pretrained=False, **kwargs): | ||
"""SqueezeNet v1.1 model | ||
|
||
Args: | ||
pretrained (bool): If True, returns a model pre-trained on ImageNet. Default: False. | ||
|
||
Examples: | ||
.. code-block:: python | ||
|
||
from paddle.vision.models import squeezenet1_1 | ||
|
||
# build model | ||
model = squeezenet1_1() | ||
|
||
# build model and load imagenet pretrained weight | ||
# model = squeezenet1_1(pretrained=True) | ||
""" | ||
return _squeezenet('squeezenet1_1', '1.1', pretrained, **kwargs) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
示例代码的部分,可以参考 这个PR,给一个输入和输出;#36064
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
好的