Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
BaiXuePrincess
Paddle
提交
e8794250
P
Paddle
项目概览
BaiXuePrincess
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
e8794250
编写于
11月 03, 2021
作者:
F
fuqianya
提交者:
GitHub
11月 03, 2021
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[PaddlePaddle Hackathon] add Squeezenet (#36066)
* add squeezenet
上级
db8425ec
变更
5
显示空白变更内容
内联
并排
Showing
5 changed file
with
258 addition
and
2 deletion
+258
-2
python/paddle/tests/test_pretrained_model.py
python/paddle/tests/test_pretrained_model.py
+3
-2
python/paddle/tests/test_vision_models.py
python/paddle/tests/test_vision_models.py
+6
-0
python/paddle/vision/__init__.py
python/paddle/vision/__init__.py
+3
-0
python/paddle/vision/models/__init__.py
python/paddle/vision/models/__init__.py
+6
-0
python/paddle/vision/models/squeezenet.py
python/paddle/vision/models/squeezenet.py
+240
-0
未找到文件。
python/paddle/tests/test_pretrained_model.py
浏览文件 @
e8794250
...
...
@@ -54,8 +54,9 @@ class TestPretrainedModel(unittest.TestCase):
def
test_models
(
self
):
arches
=
[
'mobilenet_v1'
,
'mobilenet_v2'
,
'resnet18'
,
'vgg16'
,
'alexnet'
,
'resnext50_32x4d'
,
'inception_v3'
,
'densenet121'
,
'googlenet'
,
'shufflenet_v2_x0_25'
,
'shufflenet_v2_swish'
'resnext50_32x4d'
,
'inception_v3'
,
'densenet121'
,
'squeezenet1_0'
,
'squeezenet1_1'
,
'googlenet'
,
'shufflenet_v2_x0_25'
,
'shufflenet_v2_swish'
]
for
arch
in
arches
:
self
.
infer
(
arch
)
...
...
python/paddle/tests/test_vision_models.py
浏览文件 @
e8794250
...
...
@@ -85,6 +85,12 @@ class TestVisonModels(unittest.TestCase):
def
test_densenet264
(
self
):
self
.
models_infer
(
'densenet264'
)
def
test_squeezenet1_0
(
self
):
self
.
models_infer
(
'squeezenet1_0'
)
def
test_squeezenet1_1
(
self
):
self
.
models_infer
(
'squeezenet1_1'
)
def
test_alexnet
(
self
):
self
.
models_infer
(
'alexnet'
)
...
...
python/paddle/vision/__init__.py
浏览文件 @
e8794250
...
...
@@ -38,6 +38,9 @@ from .models import MobileNetV1 # noqa: F401
from
.models
import
mobilenet_v1
# noqa: F401
from
.models
import
MobileNetV2
# noqa: F401
from
.models
import
mobilenet_v2
# noqa: F401
from
.models
import
SqueezeNet
# noqa: F401
from
.models
import
squeezenet1_0
# noqa: F401
from
.models
import
squeezenet1_1
# noqa: F401
from
.models
import
VGG
# noqa: F401
from
.models
import
vgg11
# noqa: F401
from
.models
import
vgg13
# noqa: F401
...
...
python/paddle/vision/models/__init__.py
浏览文件 @
e8794250
...
...
@@ -45,6 +45,9 @@ from .resnext import resnext152_32x4d # noqa: F401
from
.resnext
import
resnext152_64x4d
# noqa: F401
from
.inceptionv3
import
InceptionV3
# noqa: F401
from
.inceptionv3
import
inception_v3
# noqa: F401
from
.squeezenet
import
SqueezeNet
# noqa: F401
from
.squeezenet
import
squeezenet1_0
# noqa: F401
from
.squeezenet
import
squeezenet1_1
# noqa: F401
from
.googlenet
import
GoogLeNet
# noqa: F401
from
.googlenet
import
googlenet
# noqa: F401
from
.shufflenetv2
import
ShuffleNetV2
# noqa: F401
...
...
@@ -90,6 +93,9 @@ __all__ = [ #noqa
'resnext152_64x4d'
,
'InceptionV3'
,
'inception_v3'
,
'SqueezeNet'
,
'squeezenet1_0'
,
'squeezenet1_1'
,
'GoogLeNet'
,
'googlenet'
,
'ShuffleNetV2'
,
...
...
python/paddle/vision/models/squeezenet.py
0 → 100644
浏览文件 @
e8794250
# 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
)
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录