From 3393fa6618b682777e94cb397a71abe2cb752b95 Mon Sep 17 00:00:00 2001 From: Quan Tang <73922533+tt8000@users.noreply.github.com> Date: Fri, 8 Oct 2021 21:51:06 +0800 Subject: [PATCH 01/17] Create shufflenetv2.py --- python/paddle/vision/models/shufflenetv2.py | 473 ++++++++++++++++++++ 1 file changed, 473 insertions(+) create mode 100644 python/paddle/vision/models/shufflenetv2.py diff --git a/python/paddle/vision/models/shufflenetv2.py b/python/paddle/vision/models/shufflenetv2.py new file mode 100644 index 0000000000000..eb76933bc9752 --- /dev/null +++ b/python/paddle/vision/models/shufflenetv2.py @@ -0,0 +1,473 @@ +# 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. + +from __future__ import absolute_import +from __future__ import division +from __future__ import print_function + +import paddle +from paddle import ParamAttr, reshape, transpose, concat, split +from paddle.nn import Layer, Conv2D, MaxPool2D, AdaptiveAvgPool2D, BatchNorm, Linear +from paddle.nn.initializer import KaimingNormal +from paddle.nn.functional import swish + +from paddle.utils.download import get_weights_path_from_url + + +MODEL_URLS = { + "ShuffleNetV2_x0_25": + "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/ShuffleNetV2_x0_25_pretrained.pdparams", + "ShuffleNetV2_x0_33": + "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/ShuffleNetV2_x0_33_pretrained.pdparams", + "ShuffleNetV2_x0_5": + "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/ShuffleNetV2_x0_5_pretrained.pdparams", + "ShuffleNetV2_x1_0": + "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/ShuffleNetV2_x1_0_pretrained.pdparams", + "ShuffleNetV2_x1_5": + "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/ShuffleNetV2_x1_5_pretrained.pdparams", + "ShuffleNetV2_x2_0": + "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/ShuffleNetV2_x2_0_pretrained.pdparams", + "ShuffleNetV2_swish": + "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/ShuffleNetV2_swish_pretrained.pdparams" +} + +__all__ = list(MODEL_URLS.keys()) + + +def channel_shuffle(x, groups): + batch_size, num_channels, height, width = x.shape[0:4] + channels_per_group = num_channels // groups + + # reshape + x = reshape( + x=x, shape=[batch_size, groups, channels_per_group, height, width]) + + # transpose + x = transpose(x=x, perm=[0, 2, 1, 3, 4]) + + # flatten + x = reshape(x=x, shape=[batch_size, num_channels, height, width]) + return x + + +class ConvBNLayer(Layer): + def __init__( + self, + in_channels, + out_channels, + kernel_size, + stride, + padding, + groups=1, + act=None, + name=None, ): + super(ConvBNLayer, self).__init__() + self._conv = Conv2D( + in_channels=in_channels, + out_channels=out_channels, + kernel_size=kernel_size, + stride=stride, + padding=padding, + groups=groups, + weight_attr=ParamAttr( + initializer=KaimingNormal(), name=name + "_weights"), + bias_attr=False) + + self._batch_norm = BatchNorm( + out_channels, + param_attr=ParamAttr(name=name + "_bn_scale"), + bias_attr=ParamAttr(name=name + "_bn_offset"), + act=act, + moving_mean_name=name + "_bn_mean", + moving_variance_name=name + "_bn_variance") + + def forward(self, inputs): + y = self._conv(inputs) + y = self._batch_norm(y) + return y + + +class InvertedResidual(Layer): + def __init__(self, + in_channels, + out_channels, + stride, + act="relu", + name=None): + super(InvertedResidual, self).__init__() + self._conv_pw = ConvBNLayer( + in_channels=in_channels // 2, + out_channels=out_channels // 2, + kernel_size=1, + stride=1, + padding=0, + groups=1, + act=act, + name='stage_' + name + '_conv1') + self._conv_dw = ConvBNLayer( + in_channels=out_channels // 2, + out_channels=out_channels // 2, + kernel_size=3, + stride=stride, + padding=1, + groups=out_channels // 2, + act=None, + name='stage_' + name + '_conv2') + self._conv_linear = ConvBNLayer( + in_channels=out_channels // 2, + out_channels=out_channels // 2, + kernel_size=1, + stride=1, + padding=0, + groups=1, + act=act, + name='stage_' + name + '_conv3') + + def forward(self, inputs): + x1, x2 = split( + inputs, + num_or_sections=[inputs.shape[1] // 2, inputs.shape[1] // 2], + axis=1) + x2 = self._conv_pw(x2) + x2 = self._conv_dw(x2) + x2 = self._conv_linear(x2) + out = concat([x1, x2], axis=1) + return channel_shuffle(out, 2) + + +class InvertedResidualDS(Layer): + def __init__(self, + in_channels, + out_channels, + stride, + act="relu", + name=None): + super(InvertedResidualDS, self).__init__() + + # branch1 + self._conv_dw_1 = ConvBNLayer( + in_channels=in_channels, + out_channels=in_channels, + kernel_size=3, + stride=stride, + padding=1, + groups=in_channels, + act=None, + name='stage_' + name + '_conv4') + self._conv_linear_1 = ConvBNLayer( + in_channels=in_channels, + out_channels=out_channels // 2, + kernel_size=1, + stride=1, + padding=0, + groups=1, + act=act, + name='stage_' + name + '_conv5') + # branch2 + self._conv_pw_2 = ConvBNLayer( + in_channels=in_channels, + out_channels=out_channels // 2, + kernel_size=1, + stride=1, + padding=0, + groups=1, + act=act, + name='stage_' + name + '_conv1') + self._conv_dw_2 = ConvBNLayer( + in_channels=out_channels // 2, + out_channels=out_channels // 2, + kernel_size=3, + stride=stride, + padding=1, + groups=out_channels // 2, + act=None, + name='stage_' + name + '_conv2') + self._conv_linear_2 = ConvBNLayer( + in_channels=out_channels // 2, + out_channels=out_channels // 2, + kernel_size=1, + stride=1, + padding=0, + groups=1, + act=act, + name='stage_' + name + '_conv3') + + def forward(self, inputs): + x1 = self._conv_dw_1(inputs) + x1 = self._conv_linear_1(x1) + x2 = self._conv_pw_2(inputs) + x2 = self._conv_dw_2(x2) + x2 = self._conv_linear_2(x2) + out = concat([x1, x2], axis=1) + + return channel_shuffle(out, 2) + + +class ShuffleNetV2(Layer): + """ShuffleNetV2 model from + `"ShuffleNet V2: Practical Guidelines for Ecient CNN Architecture Design" `_ + Args: + num_classes (int): output dim of last fc layer. If num_classes <=0, last fc layer + will not be defined. Default: 1000. + scale (float): network architecture. Default: 1.0. + act (str, optional): Activation to be applied to the output of batch normalization. Default: "relu". + Examples: + .. code-block:: python + from paddle.vision.models import ShuffleNetV2 + + shufflenetv2_x0_25 = ShuffleNetV2(num_classes=1000, scale=0.25, act="relu") + shufflenetv2_swish = ShuffleNetV2(num_classes=1000, scale=1.0, act="swish") + """ + def __init__(self, num_classes=1000, scale=1.0, act="relu"): + super(ShuffleNetV2, self).__init__() + self.scale = scale + self.num_classes = num_classes + stage_repeats = [4, 8, 4] + + if scale == 0.25: + stage_out_channels = [-1, 24, 24, 48, 96, 512] + elif scale == 0.33: + stage_out_channels = [-1, 24, 32, 64, 128, 512] + elif scale == 0.5: + stage_out_channels = [-1, 24, 48, 96, 192, 1024] + elif scale == 1.0: + stage_out_channels = [-1, 24, 116, 232, 464, 1024] + elif scale == 1.5: + stage_out_channels = [-1, 24, 176, 352, 704, 1024] + elif scale == 2.0: + stage_out_channels = [-1, 24, 224, 488, 976, 2048] + else: + raise NotImplementedError("This scale size:[" + str(scale) + + "] is not implemented!") + # 1. conv1 + self._conv1 = ConvBNLayer( + in_channels=3, + out_channels=stage_out_channels[1], + kernel_size=3, + stride=2, + padding=1, + act=act, + name='stage1_conv') + self._max_pool = MaxPool2D(kernel_size=3, stride=2, padding=1) + + # 2. bottleneck sequences + self._block_list = [] + for stage_id, num_repeat in enumerate(stage_repeats): + for i in range(num_repeat): + if i == 0: + block = self.add_sublayer( + name=str(stage_id + 2) + '_' + str(i + 1), + sublayer=InvertedResidualDS( + in_channels=stage_out_channels[stage_id + 1], + out_channels=stage_out_channels[stage_id + 2], + stride=2, + act=act, + name=str(stage_id + 2) + '_' + str(i + 1))) + else: + block = self.add_sublayer( + name=str(stage_id + 2) + '_' + str(i + 1), + sublayer=InvertedResidual( + in_channels=stage_out_channels[stage_id + 2], + out_channels=stage_out_channels[stage_id + 2], + stride=1, + act=act, + name=str(stage_id + 2) + '_' + str(i + 1))) + self._block_list.append(block) + # 3. last_conv + self._last_conv = ConvBNLayer( + in_channels=stage_out_channels[-2], + out_channels=stage_out_channels[-1], + kernel_size=1, + stride=1, + padding=0, + act=act, + name='conv5') + # 4. pool + self._pool2d_avg = AdaptiveAvgPool2D(1) + self._out_c = stage_out_channels[-1] + # 5. fc + self._fc = Linear( + stage_out_channels[-1], + num_classes, + weight_attr=ParamAttr(name='fc6_weights'), + bias_attr=ParamAttr(name='fc6_offset')) + + def forward(self, inputs): + y = self._conv1(inputs) + y = self._max_pool(y) + for inv in self._block_list: + y = inv(y) + y = self._last_conv(y) + y = self._pool2d_avg(y) + y = paddle.flatten(y, start_axis=1, stop_axis=-1) + y = self._fc(y) + return y + + +def shufflenetv2_x0_25(pretrained=False, **kwargs): + """ShuffleNetV2_x0_25 model + + Args: + pretrained (bool): If True, returns a model pre-trained on ImageNet + + Examples: + .. code-block:: python + from paddle.vision.models import shufflenetv2_x0_25 + # build model + model = shufflenetv2_x0_25() + # build model and load imagenet pretrained weight + # model = shufflenetv2_x0_25(pretrained=True) + """ + model = ShuffleNetV2(scale=0.25, **kwargs) + if pretrained: + weight_path = get_weights_path_from_url(MODEL_URLS["ShuffleNetV2_x0_25"]) + param = paddle.load(weight_path) + model.set_dict(param) + return model + + +def shufflenetv2_x0_33(pretrained=False, **kwargs): + """ShuffleNetV2_x0_33 model + + Args: + pretrained (bool): If True, returns a model pre-trained on ImageNet + + Examples: + .. code-block:: python + from paddle.vision.models import shufflenetv2_x0_33 + # build model + model = shufflenetv2_x0_33() + # build model and load imagenet pretrained weight + # model = shufflenetv2_x0_33(pretrained=True) + """ + model = ShuffleNetV2(scale=0.33, **kwargs) + if pretrained: + weight_path = get_weights_path_from_url(MODEL_URLS["ShuffleNetV2_x0_33"]) + param = paddle.load(weight_path) + model.set_dict(param) + return model + + +def shufflenetv2_x0_5(pretrained=False, **kwargs): + """ShuffleNetV2_x0_5 model + + Args: + pretrained (bool): If True, returns a model pre-trained on ImageNet + + Examples: + .. code-block:: python + from paddle.vision.models import shufflenetv2_x0_5 + # build model + model = shufflenetv2_x0_5() + # build model and load imagenet pretrained weight + # model = shufflenetv2_x0_5(pretrained=True) + """ + model = ShuffleNetV2(scale=0.5, **kwargs) + if pretrained: + weight_path = get_weights_path_from_url(MODEL_URLS["ShuffleNetV2_x0_5"]) + param = paddle.load(weight_path) + model.set_dict(param) + return model + + +def shufflenetv2_x1_0(pretrained=False, **kwargs): + """ShuffleNetV2_x1_0 model + + Args: + pretrained (bool): If True, returns a model pre-trained on ImageNet + + Examples: + .. code-block:: python + from paddle.vision.models import shufflenetv2_x1_0 + # build model + model = shufflenetv2_x1_0() + # build model and load imagenet pretrained weight + # model = shufflenetv2_x1_0(pretrained=True) + """ + model = ShuffleNetV2(scale=1.0, **kwargs) + if pretrained: + weight_path = get_weights_path_from_url(MODEL_URLS["ShuffleNetV2_x1_0"]) + param = paddle.load(weight_path) + model.set_dict(param) + + return model + + +def shufflenetv2_x1_5(pretrained=False, **kwargs): + """ShuffleNetV2_x1_5 model + + Args: + pretrained (bool): If True, returns a model pre-trained on ImageNet + + Examples: + .. code-block:: python + from paddle.vision.models import shufflenetv2_x1_5 + # build model + model = shufflenetv2_x1_5() + # build model and load imagenet pretrained weight + # model = shufflenetv2_x1_5(pretrained=True) + """ + model = ShuffleNetV2(scale=1.5, **kwargs) + if pretrained: + weight_path = get_weights_path_from_url(MODEL_URLS["ShuffleNetV2_x1_5"]) + param = paddle.load(weight_path) + model.set_dict(param) + + return model + + +def shufflenetv2_x2_0(pretrained=False, **kwargs): + """ShuffleNetV2_x2_0 model + + Args: + pretrained (bool): If True, returns a model pre-trained on ImageNet + + Examples: + .. code-block:: python + from paddle.vision.models import shufflenetv2_x2_0 + # build model + model = shufflenetv2_x2_0() + # build model and load imagenet pretrained weight + # model = shufflenetv2_x2_0(pretrained=True) + """ + model = ShuffleNetV2(scale=2.0, **kwargs) + if pretrained: + weight_path = get_weights_path_from_url(MODEL_URLS["ShuffleNetV2_x2_0"]) + param = paddle.load(weight_path) + model.set_dict(param) + + return model + + +def shufflenetv2_swish(pretrained=False, **kwargs): + """ShuffleNetV2_swish model + + Args: + pretrained (bool): If True, returns a model pre-trained on ImageNet + + Examples: + .. code-block:: python + from paddle.vision.models import shufflenetv2_swish + # build model + model = shufflenetv2_swish() + # build model and load imagenet pretrained weight + # model = shufflenetv2_swish(pretrained=True) + """ + model = ShuffleNetV2(scale=1.0, act="swish", **kwargs) + if pretrained: + weight_path = get_weights_path_from_url(MODEL_URLS["ShuffleNetV2_swish"]) + param = paddle.load(weight_path) + model.set_dict(param) + + return model From d9dcd184841de007285d1674e649f3d0f39b0807 Mon Sep 17 00:00:00 2001 From: Quan Tang <73922533+tt8000@users.noreply.github.com> Date: Fri, 8 Oct 2021 21:56:01 +0800 Subject: [PATCH 02/17] Create __init__.py --- python/paddle/vision/models/__init__.py | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/python/paddle/vision/models/__init__.py b/python/paddle/vision/models/__init__.py index d38f3b1722ee8..5d99e506d6e21 100644 --- a/python/paddle/vision/models/__init__.py +++ b/python/paddle/vision/models/__init__.py @@ -28,6 +28,15 @@ from .vgg import vgg16 # noqa: F401 from .vgg import vgg19 # noqa: F401 from .lenet import LeNet # noqa: F401 +from .shufflenetv2 import ShuffleNetV2 +from .shufflenetv2 import shufflenetv2_x0_25 +from .shufflenetv2 import shufflenetv2_x0_33 +from .shufflenetv2 import shufflenetv2_x0_5 +from .shufflenetv2 import shufflenetv2_x1_0 +from .shufflenetv2 import shufflenetv2_x1_5 +from .shufflenetv2 import shufflenetv2_x2_0 +from .shufflenetv2 import shufflenetv2_swish + __all__ = [ #noqa 'ResNet', @@ -45,5 +54,13 @@ 'mobilenet_v1', 'MobileNetV2', 'mobilenet_v2', - 'LeNet' + 'LeNet', + 'ShuffleNetV2', + 'shufflenetv2_x0_25', + 'shufflenetv2_x0_33', + 'shufflenetv2_x0_5', + 'shufflenetv2_x1_0', + 'shufflenetv2_x1_5', + 'shufflenetv2_x2_0', + 'shufflenetv2_swish' ] From 3a65412d20792ace8326f36d806759443f1b1aae Mon Sep 17 00:00:00 2001 From: Quan Tang <73922533+tt8000@users.noreply.github.com> Date: Fri, 8 Oct 2021 21:57:44 +0800 Subject: [PATCH 03/17] Update __init__.py --- python/paddle/vision/models/__init__.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/python/paddle/vision/models/__init__.py b/python/paddle/vision/models/__init__.py index 5d99e506d6e21..712d66380824f 100644 --- a/python/paddle/vision/models/__init__.py +++ b/python/paddle/vision/models/__init__.py @@ -28,14 +28,14 @@ from .vgg import vgg16 # noqa: F401 from .vgg import vgg19 # noqa: F401 from .lenet import LeNet # noqa: F401 -from .shufflenetv2 import ShuffleNetV2 -from .shufflenetv2 import shufflenetv2_x0_25 -from .shufflenetv2 import shufflenetv2_x0_33 -from .shufflenetv2 import shufflenetv2_x0_5 -from .shufflenetv2 import shufflenetv2_x1_0 -from .shufflenetv2 import shufflenetv2_x1_5 -from .shufflenetv2 import shufflenetv2_x2_0 -from .shufflenetv2 import shufflenetv2_swish +from .shufflenetv2 import ShuffleNetV2 # noqa: F401 +from .shufflenetv2 import shufflenetv2_x0_25 # noqa: F401 +from .shufflenetv2 import shufflenetv2_x0_33 # noqa: F401 +from .shufflenetv2 import shufflenetv2_x0_5 # noqa: F401 +from .shufflenetv2 import shufflenetv2_x1_0 # noqa: F401 +from .shufflenetv2 import shufflenetv2_x1_5 # noqa: F401 +from .shufflenetv2 import shufflenetv2_x2_0 # noqa: F401 +from .shufflenetv2 import shufflenetv2_swish # noqa: F401 __all__ = [ #noqa From 5ba9bb2220e42522d573150eacc5aaaf1bf12b50 Mon Sep 17 00:00:00 2001 From: Quan Tang <73922533+tt8000@users.noreply.github.com> Date: Fri, 8 Oct 2021 22:08:03 +0800 Subject: [PATCH 04/17] Update shufflenetv2.py --- python/paddle/vision/models/shufflenetv2.py | 66 ++++++--------------- 1 file changed, 18 insertions(+), 48 deletions(-) diff --git a/python/paddle/vision/models/shufflenetv2.py b/python/paddle/vision/models/shufflenetv2.py index eb76933bc9752..73600743bfbc7 100644 --- a/python/paddle/vision/models/shufflenetv2.py +++ b/python/paddle/vision/models/shufflenetv2.py @@ -315,6 +315,15 @@ def forward(self, inputs): return y +def _shufflenetv2(arch, scale, pretrained=False, **kwargs): + model = ShuffleNetV2(scale=scale, **kwargs) + if pretrained: + weight_path = get_weights_path_from_url(MODEL_URLS[arch]) + param = paddle.load(weight_path) + model.set_dict(param) + return model + + def shufflenetv2_x0_25(pretrained=False, **kwargs): """ShuffleNetV2_x0_25 model @@ -328,13 +337,8 @@ def shufflenetv2_x0_25(pretrained=False, **kwargs): model = shufflenetv2_x0_25() # build model and load imagenet pretrained weight # model = shufflenetv2_x0_25(pretrained=True) - """ - model = ShuffleNetV2(scale=0.25, **kwargs) - if pretrained: - weight_path = get_weights_path_from_url(MODEL_URLS["ShuffleNetV2_x0_25"]) - param = paddle.load(weight_path) - model.set_dict(param) - return model + """ + return _shufflenetv2("ShuffleNetV2_x0_25", 0.25, pretrained, **kwargs) def shufflenetv2_x0_33(pretrained=False, **kwargs): @@ -351,12 +355,7 @@ def shufflenetv2_x0_33(pretrained=False, **kwargs): # build model and load imagenet pretrained weight # model = shufflenetv2_x0_33(pretrained=True) """ - model = ShuffleNetV2(scale=0.33, **kwargs) - if pretrained: - weight_path = get_weights_path_from_url(MODEL_URLS["ShuffleNetV2_x0_33"]) - param = paddle.load(weight_path) - model.set_dict(param) - return model + return _shufflenetv2("ShuffleNetV2_x0_33", 0.33, pretrained, **kwargs) def shufflenetv2_x0_5(pretrained=False, **kwargs): @@ -372,13 +371,8 @@ def shufflenetv2_x0_5(pretrained=False, **kwargs): model = shufflenetv2_x0_5() # build model and load imagenet pretrained weight # model = shufflenetv2_x0_5(pretrained=True) - """ - model = ShuffleNetV2(scale=0.5, **kwargs) - if pretrained: - weight_path = get_weights_path_from_url(MODEL_URLS["ShuffleNetV2_x0_5"]) - param = paddle.load(weight_path) - model.set_dict(param) - return model + """ + return _shufflenetv2("ShuffleNetV2_x0_5", 0.5, pretrained, **kwargs) def shufflenetv2_x1_0(pretrained=False, **kwargs): @@ -395,13 +389,7 @@ def shufflenetv2_x1_0(pretrained=False, **kwargs): # build model and load imagenet pretrained weight # model = shufflenetv2_x1_0(pretrained=True) """ - model = ShuffleNetV2(scale=1.0, **kwargs) - if pretrained: - weight_path = get_weights_path_from_url(MODEL_URLS["ShuffleNetV2_x1_0"]) - param = paddle.load(weight_path) - model.set_dict(param) - - return model + return _shufflenetv2("ShuffleNetV2_x1_0", 1.0, pretrained, **kwargs) def shufflenetv2_x1_5(pretrained=False, **kwargs): @@ -418,13 +406,7 @@ def shufflenetv2_x1_5(pretrained=False, **kwargs): # build model and load imagenet pretrained weight # model = shufflenetv2_x1_5(pretrained=True) """ - model = ShuffleNetV2(scale=1.5, **kwargs) - if pretrained: - weight_path = get_weights_path_from_url(MODEL_URLS["ShuffleNetV2_x1_5"]) - param = paddle.load(weight_path) - model.set_dict(param) - - return model + return _shufflenetv2("ShuffleNetV2_x1_5", 1.5, pretrained, **kwargs) def shufflenetv2_x2_0(pretrained=False, **kwargs): @@ -441,13 +423,7 @@ def shufflenetv2_x2_0(pretrained=False, **kwargs): # build model and load imagenet pretrained weight # model = shufflenetv2_x2_0(pretrained=True) """ - model = ShuffleNetV2(scale=2.0, **kwargs) - if pretrained: - weight_path = get_weights_path_from_url(MODEL_URLS["ShuffleNetV2_x2_0"]) - param = paddle.load(weight_path) - model.set_dict(param) - - return model + return _shufflenetv2("ShuffleNetV2_x2_0", 2.0, pretrained, **kwargs) def shufflenetv2_swish(pretrained=False, **kwargs): @@ -464,10 +440,4 @@ def shufflenetv2_swish(pretrained=False, **kwargs): # build model and load imagenet pretrained weight # model = shufflenetv2_swish(pretrained=True) """ - model = ShuffleNetV2(scale=1.0, act="swish", **kwargs) - if pretrained: - weight_path = get_weights_path_from_url(MODEL_URLS["ShuffleNetV2_swish"]) - param = paddle.load(weight_path) - model.set_dict(param) - - return model + return _shufflenetv2("ShuffleNetV2_swish", 1.0, pretrained, act="swish", **kwargs) From 9c2f5d44c4d6220d6d3f696db26ed950b1771e5e Mon Sep 17 00:00:00 2001 From: Quan Tang <73922533+tt8000@users.noreply.github.com> Date: Fri, 8 Oct 2021 22:38:21 +0800 Subject: [PATCH 05/17] Update shufflenetv2.py --- python/paddle/vision/models/shufflenetv2.py | 59 ++++++++------------- 1 file changed, 21 insertions(+), 38 deletions(-) diff --git a/python/paddle/vision/models/shufflenetv2.py b/python/paddle/vision/models/shufflenetv2.py index 73600743bfbc7..d740566795f88 100644 --- a/python/paddle/vision/models/shufflenetv2.py +++ b/python/paddle/vision/models/shufflenetv2.py @@ -70,8 +70,7 @@ def __init__( stride, padding, groups=1, - act=None, - name=None, ): + act=None): super(ConvBNLayer, self).__init__() self._conv = Conv2D( in_channels=in_channels, @@ -81,16 +80,14 @@ def __init__( padding=padding, groups=groups, weight_attr=ParamAttr( - initializer=KaimingNormal(), name=name + "_weights"), + initializer=KaimingNormal()), bias_attr=False) self._batch_norm = BatchNorm( out_channels, - param_attr=ParamAttr(name=name + "_bn_scale"), - bias_attr=ParamAttr(name=name + "_bn_offset"), - act=act, - moving_mean_name=name + "_bn_mean", - moving_variance_name=name + "_bn_variance") + param_attr=ParamAttr(), + bias_attr=ParamAttr(), + act=act) def forward(self, inputs): y = self._conv(inputs) @@ -103,8 +100,7 @@ def __init__(self, in_channels, out_channels, stride, - act="relu", - name=None): + act="relu"): super(InvertedResidual, self).__init__() self._conv_pw = ConvBNLayer( in_channels=in_channels // 2, @@ -113,8 +109,7 @@ def __init__(self, stride=1, padding=0, groups=1, - act=act, - name='stage_' + name + '_conv1') + act=act) self._conv_dw = ConvBNLayer( in_channels=out_channels // 2, out_channels=out_channels // 2, @@ -122,8 +117,7 @@ def __init__(self, stride=stride, padding=1, groups=out_channels // 2, - act=None, - name='stage_' + name + '_conv2') + act=None) self._conv_linear = ConvBNLayer( in_channels=out_channels // 2, out_channels=out_channels // 2, @@ -131,8 +125,7 @@ def __init__(self, stride=1, padding=0, groups=1, - act=act, - name='stage_' + name + '_conv3') + act=act) def forward(self, inputs): x1, x2 = split( @@ -151,8 +144,7 @@ def __init__(self, in_channels, out_channels, stride, - act="relu", - name=None): + act="relu"): super(InvertedResidualDS, self).__init__() # branch1 @@ -163,8 +155,7 @@ def __init__(self, stride=stride, padding=1, groups=in_channels, - act=None, - name='stage_' + name + '_conv4') + act=None) self._conv_linear_1 = ConvBNLayer( in_channels=in_channels, out_channels=out_channels // 2, @@ -172,8 +163,7 @@ def __init__(self, stride=1, padding=0, groups=1, - act=act, - name='stage_' + name + '_conv5') + act=act) # branch2 self._conv_pw_2 = ConvBNLayer( in_channels=in_channels, @@ -182,8 +172,7 @@ def __init__(self, stride=1, padding=0, groups=1, - act=act, - name='stage_' + name + '_conv1') + act=act) self._conv_dw_2 = ConvBNLayer( in_channels=out_channels // 2, out_channels=out_channels // 2, @@ -191,8 +180,7 @@ def __init__(self, stride=stride, padding=1, groups=out_channels // 2, - act=None, - name='stage_' + name + '_conv2') + act=None) self._conv_linear_2 = ConvBNLayer( in_channels=out_channels // 2, out_channels=out_channels // 2, @@ -200,8 +188,7 @@ def __init__(self, stride=1, padding=0, groups=1, - act=act, - name='stage_' + name + '_conv3') + act=act) def forward(self, inputs): x1 = self._conv_dw_1(inputs) @@ -257,8 +244,7 @@ def __init__(self, num_classes=1000, scale=1.0, act="relu"): kernel_size=3, stride=2, padding=1, - act=act, - name='stage1_conv') + act=act) self._max_pool = MaxPool2D(kernel_size=3, stride=2, padding=1) # 2. bottleneck sequences @@ -272,8 +258,7 @@ def __init__(self, num_classes=1000, scale=1.0, act="relu"): in_channels=stage_out_channels[stage_id + 1], out_channels=stage_out_channels[stage_id + 2], stride=2, - act=act, - name=str(stage_id + 2) + '_' + str(i + 1))) + act=act)) else: block = self.add_sublayer( name=str(stage_id + 2) + '_' + str(i + 1), @@ -281,8 +266,7 @@ def __init__(self, num_classes=1000, scale=1.0, act="relu"): in_channels=stage_out_channels[stage_id + 2], out_channels=stage_out_channels[stage_id + 2], stride=1, - act=act, - name=str(stage_id + 2) + '_' + str(i + 1))) + act=act)) self._block_list.append(block) # 3. last_conv self._last_conv = ConvBNLayer( @@ -291,8 +275,7 @@ def __init__(self, num_classes=1000, scale=1.0, act="relu"): kernel_size=1, stride=1, padding=0, - act=act, - name='conv5') + act=act) # 4. pool self._pool2d_avg = AdaptiveAvgPool2D(1) self._out_c = stage_out_channels[-1] @@ -300,8 +283,8 @@ def __init__(self, num_classes=1000, scale=1.0, act="relu"): self._fc = Linear( stage_out_channels[-1], num_classes, - weight_attr=ParamAttr(name='fc6_weights'), - bias_attr=ParamAttr(name='fc6_offset')) + weight_attr=ParamAttr(), + bias_attr=ParamAttr()) def forward(self, inputs): y = self._conv1(inputs) From 3c02d5a0ef17ce4509d02a1d1a38062951a198bc Mon Sep 17 00:00:00 2001 From: Quan Tang <73922533+tt8000@users.noreply.github.com> Date: Fri, 8 Oct 2021 22:42:10 +0800 Subject: [PATCH 06/17] Update test_pretrained_model.py --- python/paddle/tests/test_pretrained_model.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/paddle/tests/test_pretrained_model.py b/python/paddle/tests/test_pretrained_model.py index b24b51555c581..c0bbc8ff49dd5 100644 --- a/python/paddle/tests/test_pretrained_model.py +++ b/python/paddle/tests/test_pretrained_model.py @@ -52,7 +52,7 @@ def infer(self, arch): np.testing.assert_allclose(res['dygraph'], res['static']) def test_models(self): - arches = ['mobilenet_v1', 'mobilenet_v2', 'resnet18', 'vgg16'] + arches = ['mobilenet_v1', 'mobilenet_v2', 'resnet18', 'vgg16', 'shufflenetv2_swish'] for arch in arches: self.infer(arch) From 1ef363efe12ddca5d6687f626519c3331422e521 Mon Sep 17 00:00:00 2001 From: Quan Tang <73922533+tt8000@users.noreply.github.com> Date: Fri, 8 Oct 2021 22:47:48 +0800 Subject: [PATCH 07/17] Update test_vision_models.py --- python/paddle/tests/test_vision_models.py | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/python/paddle/tests/test_vision_models.py b/python/paddle/tests/test_vision_models.py index a25a8f373c29c..a6c88075f974c 100644 --- a/python/paddle/tests/test_vision_models.py +++ b/python/paddle/tests/test_vision_models.py @@ -81,7 +81,27 @@ def test_lenet(self): x = np.array(np.random.random((2, 1, 28, 28)), dtype=np.float32) lenet.predict_batch(x) - + + def test_shufflenetv2_x0_25(self): + self.models_infer('shufflenetv2_x0_25') + + def test_shufflenetv2_x0_33(self): + self.models_infer('shufflenetv2_x0_33') + + def test_shufflenetv2_x0_5(self): + self.models_infer('shufflenetv2_x0_5') + + def test_shufflenetv2_x1_0(self): + self.models_infer('shufflenetv2_x1_0') + + def test_shufflenetv2_x1_5(self): + self.models_infer('shufflenetv2_x1_5') + + def test_shufflenetv2_x2_0(self): + self.models_infer('shufflenetv2_x2_0') + + def test_shufflenetv2_swish(self): + self.models_infer('shufflenetv2_swish') if __name__ == '__main__': unittest.main() From 861fe5172fe6fa29f68d4ea602f7cd0457b4e96f Mon Sep 17 00:00:00 2001 From: Quan Tang <73922533+tt8000@users.noreply.github.com> Date: Sat, 9 Oct 2021 14:13:28 +0800 Subject: [PATCH 08/17] Update __init__.py --- python/paddle/vision/__init__.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/python/paddle/vision/__init__.py b/python/paddle/vision/__init__.py index 76393865ded04..4aff778ba0871 100644 --- a/python/paddle/vision/__init__.py +++ b/python/paddle/vision/__init__.py @@ -44,6 +44,14 @@ from .models import vgg16 # noqa: F401 from .models import vgg19 # noqa: F401 from .models import LeNet # noqa: F401 +from .models import ShuffleNetV2 # noqa: F401 +from .models import shufflenetv2_x0_25 # noqa: F401 +from .models import shufflenetv2_x0_33 # noqa: F401 +from .models import shufflenetv2_x0_5 # noqa: F401 +from .models import shufflenetv2_x1_0 # noqa: F401 +from .models import shufflenetv2_x1_5 # noqa: F401 +from .models import shufflenetv2_x2_0 # noqa: F401 +from .models import shufflenetv2_swish # noqa: F401 from .transforms import BaseTransform # noqa: F401 from .transforms import Compose # noqa: F401 from .transforms import Resize # noqa: F401 From 87b6c74dfacde0bb19c218572088d6900b3caae6 Mon Sep 17 00:00:00 2001 From: Quan Tang <73922533+tt8000@users.noreply.github.com> Date: Sat, 9 Oct 2021 15:03:12 +0800 Subject: [PATCH 09/17] Update shufflenetv2.py --- python/paddle/vision/models/shufflenetv2.py | 23 +++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/python/paddle/vision/models/shufflenetv2.py b/python/paddle/vision/models/shufflenetv2.py index d740566795f88..3d328b51f7f9b 100644 --- a/python/paddle/vision/models/shufflenetv2.py +++ b/python/paddle/vision/models/shufflenetv2.py @@ -17,7 +17,8 @@ from __future__ import print_function import paddle -from paddle import ParamAttr, reshape, transpose, concat, split +from paddle.fluid.layers import reshape, transpose, concat, split +from paddle.fluid.param_attr import ParamAttr from paddle.nn import Layer, Conv2D, MaxPool2D, AdaptiveAvgPool2D, BatchNorm, Linear from paddle.nn.initializer import KaimingNormal from paddle.nn.functional import swish @@ -131,7 +132,7 @@ def forward(self, inputs): x1, x2 = split( inputs, num_or_sections=[inputs.shape[1] // 2, inputs.shape[1] // 2], - axis=1) + dim=1) x2 = self._conv_pw(x2) x2 = self._conv_dw(x2) x2 = self._conv_linear(x2) @@ -204,17 +205,21 @@ def forward(self, inputs): class ShuffleNetV2(Layer): """ShuffleNetV2 model from `"ShuffleNet V2: Practical Guidelines for Ecient CNN Architecture Design" `_ + Args: num_classes (int): output dim of last fc layer. If num_classes <=0, last fc layer will not be defined. Default: 1000. scale (float): network architecture. Default: 1.0. act (str, optional): Activation to be applied to the output of batch normalization. Default: "relu". + Examples: .. code-block:: python + from paddle.vision.models import ShuffleNetV2 shufflenetv2_x0_25 = ShuffleNetV2(num_classes=1000, scale=0.25, act="relu") shufflenetv2_swish = ShuffleNetV2(num_classes=1000, scale=1.0, act="swish") + """ def __init__(self, num_classes=1000, scale=1.0, act="relu"): super(ShuffleNetV2, self).__init__() @@ -315,11 +320,13 @@ def shufflenetv2_x0_25(pretrained=False, **kwargs): Examples: .. code-block:: python + from paddle.vision.models import shufflenetv2_x0_25 # build model model = shufflenetv2_x0_25() # build model and load imagenet pretrained weight # model = shufflenetv2_x0_25(pretrained=True) + """ return _shufflenetv2("ShuffleNetV2_x0_25", 0.25, pretrained, **kwargs) @@ -332,11 +339,13 @@ def shufflenetv2_x0_33(pretrained=False, **kwargs): Examples: .. code-block:: python + from paddle.vision.models import shufflenetv2_x0_33 # build model model = shufflenetv2_x0_33() # build model and load imagenet pretrained weight # model = shufflenetv2_x0_33(pretrained=True) + """ return _shufflenetv2("ShuffleNetV2_x0_33", 0.33, pretrained, **kwargs) @@ -349,11 +358,13 @@ def shufflenetv2_x0_5(pretrained=False, **kwargs): Examples: .. code-block:: python + from paddle.vision.models import shufflenetv2_x0_5 # build model model = shufflenetv2_x0_5() # build model and load imagenet pretrained weight # model = shufflenetv2_x0_5(pretrained=True) + """ return _shufflenetv2("ShuffleNetV2_x0_5", 0.5, pretrained, **kwargs) @@ -366,11 +377,13 @@ def shufflenetv2_x1_0(pretrained=False, **kwargs): Examples: .. code-block:: python + from paddle.vision.models import shufflenetv2_x1_0 # build model model = shufflenetv2_x1_0() # build model and load imagenet pretrained weight # model = shufflenetv2_x1_0(pretrained=True) + """ return _shufflenetv2("ShuffleNetV2_x1_0", 1.0, pretrained, **kwargs) @@ -383,11 +396,13 @@ def shufflenetv2_x1_5(pretrained=False, **kwargs): Examples: .. code-block:: python + from paddle.vision.models import shufflenetv2_x1_5 # build model model = shufflenetv2_x1_5() # build model and load imagenet pretrained weight # model = shufflenetv2_x1_5(pretrained=True) + """ return _shufflenetv2("ShuffleNetV2_x1_5", 1.5, pretrained, **kwargs) @@ -400,11 +415,13 @@ def shufflenetv2_x2_0(pretrained=False, **kwargs): Examples: .. code-block:: python + from paddle.vision.models import shufflenetv2_x2_0 # build model model = shufflenetv2_x2_0() # build model and load imagenet pretrained weight # model = shufflenetv2_x2_0(pretrained=True) + """ return _shufflenetv2("ShuffleNetV2_x2_0", 2.0, pretrained, **kwargs) @@ -417,10 +434,12 @@ def shufflenetv2_swish(pretrained=False, **kwargs): Examples: .. code-block:: python + from paddle.vision.models import shufflenetv2_swish # build model model = shufflenetv2_swish() # build model and load imagenet pretrained weight # model = shufflenetv2_swish(pretrained=True) + """ return _shufflenetv2("ShuffleNetV2_swish", 1.0, pretrained, act="swish", **kwargs) From be4c2a9a4b5d29a0274954ec7cea8a093483734e Mon Sep 17 00:00:00 2001 From: Quan Tang <73922533+tt8000@users.noreply.github.com> Date: Sat, 9 Oct 2021 16:23:07 +0800 Subject: [PATCH 10/17] Update shufflenetv2.py --- python/paddle/vision/models/shufflenetv2.py | 40 ++++++++++----------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/python/paddle/vision/models/shufflenetv2.py b/python/paddle/vision/models/shufflenetv2.py index 3d328b51f7f9b..aceb511c97ad8 100644 --- a/python/paddle/vision/models/shufflenetv2.py +++ b/python/paddle/vision/models/shufflenetv2.py @@ -27,19 +27,19 @@ MODEL_URLS = { - "ShuffleNetV2_x0_25": + "shufflenetv2_x0_25": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/ShuffleNetV2_x0_25_pretrained.pdparams", - "ShuffleNetV2_x0_33": + "shufflenetv2_x0_33": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/ShuffleNetV2_x0_33_pretrained.pdparams", - "ShuffleNetV2_x0_5": + "shufflenetv2_x0_5": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/ShuffleNetV2_x0_5_pretrained.pdparams", - "ShuffleNetV2_x1_0": + "shufflenetv2_x1_0": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/ShuffleNetV2_x1_0_pretrained.pdparams", - "ShuffleNetV2_x1_5": + "shufflenetv2_x1_5": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/ShuffleNetV2_x1_5_pretrained.pdparams", - "ShuffleNetV2_x2_0": + "shufflenetv2_x2_0": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/ShuffleNetV2_x2_0_pretrained.pdparams", - "ShuffleNetV2_swish": + "shufflenetv2_swish": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/ShuffleNetV2_swish_pretrained.pdparams" } @@ -313,7 +313,7 @@ def _shufflenetv2(arch, scale, pretrained=False, **kwargs): def shufflenetv2_x0_25(pretrained=False, **kwargs): - """ShuffleNetV2_x0_25 model + """shufflenetv2_x0_25 model Args: pretrained (bool): If True, returns a model pre-trained on ImageNet @@ -328,11 +328,11 @@ def shufflenetv2_x0_25(pretrained=False, **kwargs): # model = shufflenetv2_x0_25(pretrained=True) """ - return _shufflenetv2("ShuffleNetV2_x0_25", 0.25, pretrained, **kwargs) + return _shufflenetv2("shufflenetv2_x0_25", 0.25, pretrained, **kwargs) def shufflenetv2_x0_33(pretrained=False, **kwargs): - """ShuffleNetV2_x0_33 model + """shufflenetv2_x0_33 model Args: pretrained (bool): If True, returns a model pre-trained on ImageNet @@ -351,7 +351,7 @@ def shufflenetv2_x0_33(pretrained=False, **kwargs): def shufflenetv2_x0_5(pretrained=False, **kwargs): - """ShuffleNetV2_x0_5 model + """shufflenetv2_x0_5 model Args: pretrained (bool): If True, returns a model pre-trained on ImageNet @@ -366,11 +366,11 @@ def shufflenetv2_x0_5(pretrained=False, **kwargs): # model = shufflenetv2_x0_5(pretrained=True) """ - return _shufflenetv2("ShuffleNetV2_x0_5", 0.5, pretrained, **kwargs) + return _shufflenetv2("shufflenetv2_x0_5", 0.5, pretrained, **kwargs) def shufflenetv2_x1_0(pretrained=False, **kwargs): - """ShuffleNetV2_x1_0 model + """shufflenetv2_x1_0 model Args: pretrained (bool): If True, returns a model pre-trained on ImageNet @@ -385,11 +385,11 @@ def shufflenetv2_x1_0(pretrained=False, **kwargs): # model = shufflenetv2_x1_0(pretrained=True) """ - return _shufflenetv2("ShuffleNetV2_x1_0", 1.0, pretrained, **kwargs) + return _shufflenetv2("shufflenetv2_x1_0", 1.0, pretrained, **kwargs) def shufflenetv2_x1_5(pretrained=False, **kwargs): - """ShuffleNetV2_x1_5 model + """shufflenetv2_x1_5 model Args: pretrained (bool): If True, returns a model pre-trained on ImageNet @@ -404,11 +404,11 @@ def shufflenetv2_x1_5(pretrained=False, **kwargs): # model = shufflenetv2_x1_5(pretrained=True) """ - return _shufflenetv2("ShuffleNetV2_x1_5", 1.5, pretrained, **kwargs) + return _shufflenetv2("shufflenetv2_x1_5", 1.5, pretrained, **kwargs) def shufflenetv2_x2_0(pretrained=False, **kwargs): - """ShuffleNetV2_x2_0 model + """shufflenetv2_x2_0 model Args: pretrained (bool): If True, returns a model pre-trained on ImageNet @@ -423,11 +423,11 @@ def shufflenetv2_x2_0(pretrained=False, **kwargs): # model = shufflenetv2_x2_0(pretrained=True) """ - return _shufflenetv2("ShuffleNetV2_x2_0", 2.0, pretrained, **kwargs) + return _shufflenetv2("shufflenetv2_x2_0", 2.0, pretrained, **kwargs) def shufflenetv2_swish(pretrained=False, **kwargs): - """ShuffleNetV2_swish model + """shufflenetv2_swish model Args: pretrained (bool): If True, returns a model pre-trained on ImageNet @@ -442,4 +442,4 @@ def shufflenetv2_swish(pretrained=False, **kwargs): # model = shufflenetv2_swish(pretrained=True) """ - return _shufflenetv2("ShuffleNetV2_swish", 1.0, pretrained, act="swish", **kwargs) + return _shufflenetv2("shufflenetv2_swish", 1.0, pretrained, act="swish", **kwargs) From 87cc10bd19db1aa629959cbe01a513b86319aec3 Mon Sep 17 00:00:00 2001 From: Quan Tang <73922533+tt8000@users.noreply.github.com> Date: Sat, 9 Oct 2021 16:47:29 +0800 Subject: [PATCH 11/17] Update test_pretrained_model.py --- python/paddle/tests/test_pretrained_model.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/python/paddle/tests/test_pretrained_model.py b/python/paddle/tests/test_pretrained_model.py index c0bbc8ff49dd5..eb3e102a832c3 100644 --- a/python/paddle/tests/test_pretrained_model.py +++ b/python/paddle/tests/test_pretrained_model.py @@ -52,7 +52,9 @@ def infer(self, arch): np.testing.assert_allclose(res['dygraph'], res['static']) def test_models(self): - arches = ['mobilenet_v1', 'mobilenet_v2', 'resnet18', 'vgg16', 'shufflenetv2_swish'] + arches = ['mobilenet_v1', 'mobilenet_v2', 'resnet18', 'vgg16', + 'shufflenetv2_swish' + ] for arch in arches: self.infer(arch) From d1d76d32dee6d085a44c950e88113c0f572d1578 Mon Sep 17 00:00:00 2001 From: Quan Tang <73922533+tt8000@users.noreply.github.com> Date: Sat, 9 Oct 2021 16:56:24 +0800 Subject: [PATCH 12/17] Create shufflenetv2.py --- python/paddle/vision/models/shufflenetv2.py | 28 ++++++++++++++++++--- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/python/paddle/vision/models/shufflenetv2.py b/python/paddle/vision/models/shufflenetv2.py index aceb511c97ad8..6ad5d9c982379 100644 --- a/python/paddle/vision/models/shufflenetv2.py +++ b/python/paddle/vision/models/shufflenetv2.py @@ -21,7 +21,6 @@ from paddle.fluid.param_attr import ParamAttr from paddle.nn import Layer, Conv2D, MaxPool2D, AdaptiveAvgPool2D, BatchNorm, Linear from paddle.nn.initializer import KaimingNormal -from paddle.nn.functional import swish from paddle.utils.download import get_weights_path_from_url @@ -207,9 +206,9 @@ class ShuffleNetV2(Layer): `"ShuffleNet V2: Practical Guidelines for Ecient CNN Architecture Design" `_ Args: - num_classes (int): output dim of last fc layer. If num_classes <=0, last fc layer + num_classes (int, optional): output dim of last fc layer. If num_classes <=0, last fc layer will not be defined. Default: 1000. - scale (float): network architecture. Default: 1.0. + scale (float, optional): network architecture. Default: 1.0. act (str, optional): Activation to be applied to the output of batch normalization. Default: "relu". Examples: @@ -217,8 +216,8 @@ class ShuffleNetV2(Layer): from paddle.vision.models import ShuffleNetV2 + shufflenetv2_x0_25 = ShuffleNetV2(num_classes=1000, scale=0.25, act="relu") - shufflenetv2_swish = ShuffleNetV2(num_classes=1000, scale=1.0, act="swish") """ def __init__(self, num_classes=1000, scale=1.0, act="relu"): @@ -322,8 +321,11 @@ def shufflenetv2_x0_25(pretrained=False, **kwargs): .. code-block:: python from paddle.vision.models import shufflenetv2_x0_25 + + # build model model = shufflenetv2_x0_25() + # build model and load imagenet pretrained weight # model = shufflenetv2_x0_25(pretrained=True) @@ -341,8 +343,11 @@ def shufflenetv2_x0_33(pretrained=False, **kwargs): .. code-block:: python from paddle.vision.models import shufflenetv2_x0_33 + + # build model model = shufflenetv2_x0_33() + # build model and load imagenet pretrained weight # model = shufflenetv2_x0_33(pretrained=True) @@ -360,8 +365,11 @@ def shufflenetv2_x0_5(pretrained=False, **kwargs): .. code-block:: python from paddle.vision.models import shufflenetv2_x0_5 + + # build model model = shufflenetv2_x0_5() + # build model and load imagenet pretrained weight # model = shufflenetv2_x0_5(pretrained=True) @@ -379,8 +387,11 @@ def shufflenetv2_x1_0(pretrained=False, **kwargs): .. code-block:: python from paddle.vision.models import shufflenetv2_x1_0 + + # build model model = shufflenetv2_x1_0() + # build model and load imagenet pretrained weight # model = shufflenetv2_x1_0(pretrained=True) @@ -398,8 +409,11 @@ def shufflenetv2_x1_5(pretrained=False, **kwargs): .. code-block:: python from paddle.vision.models import shufflenetv2_x1_5 + + # build model model = shufflenetv2_x1_5() + # build model and load imagenet pretrained weight # model = shufflenetv2_x1_5(pretrained=True) @@ -417,8 +431,11 @@ def shufflenetv2_x2_0(pretrained=False, **kwargs): .. code-block:: python from paddle.vision.models import shufflenetv2_x2_0 + + # build model model = shufflenetv2_x2_0() + # build model and load imagenet pretrained weight # model = shufflenetv2_x2_0(pretrained=True) @@ -436,8 +453,11 @@ def shufflenetv2_swish(pretrained=False, **kwargs): .. code-block:: python from paddle.vision.models import shufflenetv2_swish + + # build model model = shufflenetv2_swish() + # build model and load imagenet pretrained weight # model = shufflenetv2_swish(pretrained=True) From e7a5f6a263607bea8491440399dc42ac20864ac4 Mon Sep 17 00:00:00 2001 From: Quan Tang <73922533+tt8000@users.noreply.github.com> Date: Sat, 9 Oct 2021 20:06:09 +0800 Subject: [PATCH 13/17] Update shufflenetv2.py --- python/paddle/vision/models/shufflenetv2.py | 32 +++++++-------------- 1 file changed, 10 insertions(+), 22 deletions(-) diff --git a/python/paddle/vision/models/shufflenetv2.py b/python/paddle/vision/models/shufflenetv2.py index 6ad5d9c982379..64a2f0cb6b573 100644 --- a/python/paddle/vision/models/shufflenetv2.py +++ b/python/paddle/vision/models/shufflenetv2.py @@ -26,20 +26,13 @@ MODEL_URLS = { - "shufflenetv2_x0_25": - "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/ShuffleNetV2_x0_25_pretrained.pdparams", - "shufflenetv2_x0_33": - "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/ShuffleNetV2_x0_33_pretrained.pdparams", - "shufflenetv2_x0_5": - "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/ShuffleNetV2_x0_5_pretrained.pdparams", - "shufflenetv2_x1_0": - "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/ShuffleNetV2_x1_0_pretrained.pdparams", - "shufflenetv2_x1_5": - "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/ShuffleNetV2_x1_5_pretrained.pdparams", - "shufflenetv2_x2_0": - "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/ShuffleNetV2_x2_0_pretrained.pdparams", - "shufflenetv2_swish": - "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/ShuffleNetV2_swish_pretrained.pdparams" + "shufflenetv2_x0_25": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/ShuffleNetV2_x0_25_pretrained.pdparams", + "shufflenetv2_x0_33": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/ShuffleNetV2_x0_33_pretrained.pdparams", + "shufflenetv2_x0_5": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/ShuffleNetV2_x0_5_pretrained.pdparams", + "shufflenetv2_x1_0": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/ShuffleNetV2_x1_0_pretrained.pdparams", + "shufflenetv2_x1_5": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/ShuffleNetV2_x1_5_pretrained.pdparams", + "shufflenetv2_x2_0": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/ShuffleNetV2_x2_0_pretrained.pdparams", + "shufflenetv2_swish": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/ShuffleNetV2_swish_pretrained.pdparams" } __all__ = list(MODEL_URLS.keys()) @@ -306,8 +299,10 @@ def _shufflenetv2(arch, scale, pretrained=False, **kwargs): model = ShuffleNetV2(scale=scale, **kwargs) if pretrained: weight_path = get_weights_path_from_url(MODEL_URLS[arch]) + param = paddle.load(weight_path) - model.set_dict(param) + model.set_dict(param) + return model @@ -322,7 +317,6 @@ def shufflenetv2_x0_25(pretrained=False, **kwargs): from paddle.vision.models import shufflenetv2_x0_25 - # build model model = shufflenetv2_x0_25() @@ -344,7 +338,6 @@ def shufflenetv2_x0_33(pretrained=False, **kwargs): from paddle.vision.models import shufflenetv2_x0_33 - # build model model = shufflenetv2_x0_33() @@ -366,7 +359,6 @@ def shufflenetv2_x0_5(pretrained=False, **kwargs): from paddle.vision.models import shufflenetv2_x0_5 - # build model model = shufflenetv2_x0_5() @@ -388,7 +380,6 @@ def shufflenetv2_x1_0(pretrained=False, **kwargs): from paddle.vision.models import shufflenetv2_x1_0 - # build model model = shufflenetv2_x1_0() @@ -410,7 +401,6 @@ def shufflenetv2_x1_5(pretrained=False, **kwargs): from paddle.vision.models import shufflenetv2_x1_5 - # build model model = shufflenetv2_x1_5() @@ -432,7 +422,6 @@ def shufflenetv2_x2_0(pretrained=False, **kwargs): from paddle.vision.models import shufflenetv2_x2_0 - # build model model = shufflenetv2_x2_0() @@ -454,7 +443,6 @@ def shufflenetv2_swish(pretrained=False, **kwargs): from paddle.vision.models import shufflenetv2_swish - # build model model = shufflenetv2_swish() From 99681129755af6cc349a6664b0b3c4ced976a732 Mon Sep 17 00:00:00 2001 From: Quan Tang <73922533+tt8000@users.noreply.github.com> Date: Sat, 9 Oct 2021 20:19:51 +0800 Subject: [PATCH 14/17] Update shufflenetv2.py --- python/paddle/vision/models/shufflenetv2.py | 48 ++++++++------------- 1 file changed, 18 insertions(+), 30 deletions(-) diff --git a/python/paddle/vision/models/shufflenetv2.py b/python/paddle/vision/models/shufflenetv2.py index 64a2f0cb6b573..fd2db721fca13 100644 --- a/python/paddle/vision/models/shufflenetv2.py +++ b/python/paddle/vision/models/shufflenetv2.py @@ -55,15 +55,15 @@ def channel_shuffle(x, groups): class ConvBNLayer(Layer): - def __init__( - self, - in_channels, - out_channels, - kernel_size, - stride, - padding, - groups=1, - act=None): + + def __init__(self, + in_channels, + out_channels, + kernel_size, + stride, + padding, + groups=1, + act=None): super(ConvBNLayer, self).__init__() self._conv = Conv2D( in_channels=in_channels, @@ -72,8 +72,7 @@ def __init__( stride=stride, padding=padding, groups=groups, - weight_attr=ParamAttr( - initializer=KaimingNormal()), + weight_attr=ParamAttr(initializer=KaimingNormal()), bias_attr=False) self._batch_norm = BatchNorm( @@ -89,11 +88,8 @@ def forward(self, inputs): class InvertedResidual(Layer): - def __init__(self, - in_channels, - out_channels, - stride, - act="relu"): + + def __init__(self, in_channels, out_channels, stride, act="relu"): super(InvertedResidual, self).__init__() self._conv_pw = ConvBNLayer( in_channels=in_channels // 2, @@ -133,11 +129,8 @@ def forward(self, inputs): class InvertedResidualDS(Layer): - def __init__(self, - in_channels, - out_channels, - stride, - act="relu"): + + def __init__(self, in_channels, out_channels, stride, act="relu"): super(InvertedResidualDS, self).__init__() # branch1 @@ -213,6 +206,7 @@ class ShuffleNetV2(Layer): shufflenetv2_x0_25 = ShuffleNetV2(num_classes=1000, scale=0.25, act="relu") """ + def __init__(self, num_classes=1000, scale=1.0, act="relu"): super(ShuffleNetV2, self).__init__() self.scale = scale @@ -322,7 +316,6 @@ def shufflenetv2_x0_25(pretrained=False, **kwargs): # build model and load imagenet pretrained weight # model = shufflenetv2_x0_25(pretrained=True) - """ return _shufflenetv2("shufflenetv2_x0_25", 0.25, pretrained, **kwargs) @@ -343,7 +336,6 @@ def shufflenetv2_x0_33(pretrained=False, **kwargs): # build model and load imagenet pretrained weight # model = shufflenetv2_x0_33(pretrained=True) - """ return _shufflenetv2("ShuffleNetV2_x0_33", 0.33, pretrained, **kwargs) @@ -364,8 +356,7 @@ def shufflenetv2_x0_5(pretrained=False, **kwargs): # build model and load imagenet pretrained weight # model = shufflenetv2_x0_5(pretrained=True) - - """ + """ return _shufflenetv2("shufflenetv2_x0_5", 0.5, pretrained, **kwargs) @@ -385,7 +376,6 @@ def shufflenetv2_x1_0(pretrained=False, **kwargs): # build model and load imagenet pretrained weight # model = shufflenetv2_x1_0(pretrained=True) - """ return _shufflenetv2("shufflenetv2_x1_0", 1.0, pretrained, **kwargs) @@ -406,7 +396,6 @@ def shufflenetv2_x1_5(pretrained=False, **kwargs): # build model and load imagenet pretrained weight # model = shufflenetv2_x1_5(pretrained=True) - """ return _shufflenetv2("shufflenetv2_x1_5", 1.5, pretrained, **kwargs) @@ -427,7 +416,6 @@ def shufflenetv2_x2_0(pretrained=False, **kwargs): # build model and load imagenet pretrained weight # model = shufflenetv2_x2_0(pretrained=True) - """ return _shufflenetv2("shufflenetv2_x2_0", 2.0, pretrained, **kwargs) @@ -448,6 +436,6 @@ def shufflenetv2_swish(pretrained=False, **kwargs): # build model and load imagenet pretrained weight # model = shufflenetv2_swish(pretrained=True) - """ - return _shufflenetv2("shufflenetv2_swish", 1.0, pretrained, act="swish", **kwargs) + return _shufflenetv2( + "shufflenetv2_swish", 1.0, pretrained, act="swish", **kwargs) From e037ef53a2c695e3a25c87c4158b315bbcd7b3d1 Mon Sep 17 00:00:00 2001 From: Quan Tang <73922533+tt8000@users.noreply.github.com> Date: Sat, 9 Oct 2021 20:21:24 +0800 Subject: [PATCH 15/17] Update test_pretrained_model.py --- python/paddle/tests/test_pretrained_model.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/python/paddle/tests/test_pretrained_model.py b/python/paddle/tests/test_pretrained_model.py index eb3e102a832c3..1cc9e1f2d3455 100644 --- a/python/paddle/tests/test_pretrained_model.py +++ b/python/paddle/tests/test_pretrained_model.py @@ -52,8 +52,9 @@ def infer(self, arch): np.testing.assert_allclose(res['dygraph'], res['static']) def test_models(self): - arches = ['mobilenet_v1', 'mobilenet_v2', 'resnet18', 'vgg16', - 'shufflenetv2_swish' + arches = [ + 'mobilenet_v1', 'mobilenet_v2', 'resnet18', 'vgg16', + 'shufflenetv2_swish' ] for arch in arches: self.infer(arch) From 571535f49ef53337ba4acd70557ecf5b521db567 Mon Sep 17 00:00:00 2001 From: Quan Tang <73922533+tt8000@users.noreply.github.com> Date: Sat, 9 Oct 2021 20:25:32 +0800 Subject: [PATCH 16/17] Update test_vision_models.py --- python/paddle/tests/test_vision_models.py | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/python/paddle/tests/test_vision_models.py b/python/paddle/tests/test_vision_models.py index a6c88075f974c..851553b2bb001 100644 --- a/python/paddle/tests/test_vision_models.py +++ b/python/paddle/tests/test_vision_models.py @@ -70,17 +70,6 @@ def test_resnet101(self): def test_resnet152(self): self.models_infer('resnet152') - - def test_vgg16_num_classes(self): - vgg16 = models.__dict__['vgg16'](pretrained=False, num_classes=10) - - def test_lenet(self): - input = InputSpec([None, 1, 28, 28], 'float32', 'x') - lenet = paddle.Model(models.__dict__['LeNet'](), input) - lenet.prepare() - - x = np.array(np.random.random((2, 1, 28, 28)), dtype=np.float32) - lenet.predict_batch(x) def test_shufflenetv2_x0_25(self): self.models_infer('shufflenetv2_x0_25') @@ -103,5 +92,17 @@ def test_shufflenetv2_x2_0(self): def test_shufflenetv2_swish(self): self.models_infer('shufflenetv2_swish') + def test_vgg16_num_classes(self): + vgg16 = models.__dict__['vgg16'](pretrained=False, num_classes=10) + + def test_lenet(self): + input = InputSpec([None, 1, 28, 28], 'float32', 'x') + lenet = paddle.Model(models.__dict__['LeNet'](), input) + lenet.prepare() + + x = np.array(np.random.random((2, 1, 28, 28)), dtype=np.float32) + lenet.predict_batch(x) + + if __name__ == '__main__': unittest.main() From 17b660c0827bb7c2a24d1a17fe18349ad6702513 Mon Sep 17 00:00:00 2001 From: Quan Tang <73922533+tt8000@users.noreply.github.com> Date: Sat, 9 Oct 2021 21:10:39 +0800 Subject: [PATCH 17/17] Update shufflenetv2.py --- python/paddle/vision/models/shufflenetv2.py | 24 ++++++++++++--------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/python/paddle/vision/models/shufflenetv2.py b/python/paddle/vision/models/shufflenetv2.py index fd2db721fca13..498e6e35fef53 100644 --- a/python/paddle/vision/models/shufflenetv2.py +++ b/python/paddle/vision/models/shufflenetv2.py @@ -26,13 +26,20 @@ MODEL_URLS = { - "shufflenetv2_x0_25": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/ShuffleNetV2_x0_25_pretrained.pdparams", - "shufflenetv2_x0_33": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/ShuffleNetV2_x0_33_pretrained.pdparams", - "shufflenetv2_x0_5": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/ShuffleNetV2_x0_5_pretrained.pdparams", - "shufflenetv2_x1_0": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/ShuffleNetV2_x1_0_pretrained.pdparams", - "shufflenetv2_x1_5": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/ShuffleNetV2_x1_5_pretrained.pdparams", - "shufflenetv2_x2_0": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/ShuffleNetV2_x2_0_pretrained.pdparams", - "shufflenetv2_swish": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/ShuffleNetV2_swish_pretrained.pdparams" + "shufflenetv2_x0_25": + "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/ShuffleNetV2_x0_25_pretrained.pdparams", + "shufflenetv2_x0_33": + "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/ShuffleNetV2_x0_33_pretrained.pdparams", + "shufflenetv2_x0_5": + "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/ShuffleNetV2_x0_5_pretrained.pdparams", + "shufflenetv2_x1_0": + "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/ShuffleNetV2_x1_0_pretrained.pdparams", + "shufflenetv2_x1_5": + "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/ShuffleNetV2_x1_5_pretrained.pdparams", + "shufflenetv2_x2_0": + "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/ShuffleNetV2_x2_0_pretrained.pdparams", + "shufflenetv2_swish": + "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/ShuffleNetV2_swish_pretrained.pdparams" } __all__ = list(MODEL_URLS.keys()) @@ -55,7 +62,6 @@ def channel_shuffle(x, groups): class ConvBNLayer(Layer): - def __init__(self, in_channels, out_channels, @@ -88,7 +94,6 @@ def forward(self, inputs): class InvertedResidual(Layer): - def __init__(self, in_channels, out_channels, stride, act="relu"): super(InvertedResidual, self).__init__() self._conv_pw = ConvBNLayer( @@ -129,7 +134,6 @@ def forward(self, inputs): class InvertedResidualDS(Layer): - def __init__(self, in_channels, out_channels, stride, act="relu"): super(InvertedResidualDS, self).__init__()