diff --git a/ppcls/arch/backbone/__init__.py b/ppcls/arch/backbone/__init__.py index 4b5b08af22151266d12ed712d40abc3fc503be10..a519811bdb9c56b5f5610e592add4d9d4f6c52b9 100644 --- a/ppcls/arch/backbone/__init__.py +++ b/ppcls/arch/backbone/__init__.py @@ -19,11 +19,12 @@ from ppcls.arch.backbone.legendary_models.vgg import VGG11, VGG13, VGG16, VGG19 from ppcls.arch.backbone.legendary_models.inception_v3 import InceptionV3 from ppcls.arch.backbone.legendary_models.hrnet import HRNet_W18_C, HRNet_W30_C, HRNet_W32_C, HRNet_W40_C, HRNet_W44_C, HRNet_W48_C, HRNet_W60_C, HRNet_W64_C, SE_HRNet_W64_C -from ppcls.arch.backbone.model_zoo.resnet_vc import ResNet18_vc, ResNet34_vc, ResNet50_vc, ResNet101_vc, ResNet152_vc +from ppcls.arch.backbone.model_zoo.resnet_vc import ResNet50_vc from ppcls.arch.backbone.model_zoo.resnext import ResNeXt50_32x4d, ResNeXt50_64x4d, ResNeXt101_32x4d, ResNeXt101_64x4d, ResNeXt152_32x4d, ResNeXt152_64x4d -from ppcls.arch.backbone.model_zoo.res2net import Res2Net50_48w_2s, Res2Net50_26w_4s, Res2Net50_14w_8s, Res2Net50_48w_2s, Res2Net50_26w_6s, Res2Net50_26w_8s, Res2Net101_26w_4s, Res2Net152_26w_4s, Res2Net200_26w_4s -from ppcls.arch.backbone.model_zoo.res2net_vd import Res2Net50_vd_48w_2s, Res2Net50_vd_26w_4s, Res2Net50_vd_14w_8s, Res2Net50_vd_48w_2s, Res2Net50_vd_26w_6s, Res2Net50_vd_26w_8s, Res2Net101_vd_26w_4s, Res2Net152_vd_26w_4s, Res2Net200_vd_26w_4s -from ppcls.arch.backbone.model_zoo.se_resnet_vd import SE_ResNet18_vd, SE_ResNet34_vd, SE_ResNet50_vd, SE_ResNet101_vd, SE_ResNet152_vd, SE_ResNet200_vd +from ppcls.arch.backbone.model_zoo.resnext_vd import ResNeXt50_vd_32x4d, ResNeXt50_vd_64x4d, ResNeXt101_vd_32x4d, ResNeXt101_vd_64x4d, ResNeXt152_vd_32x4d, ResNeXt152_vd_64x4d +from ppcls.arch.backbone.model_zoo.res2net import Res2Net50_26w_4s, Res2Net50_14w_8s +from ppcls.arch.backbone.model_zoo.res2net_vd import Res2Net50_vd_26w_4s, Res2Net101_vd_26w_4s, Res2Net200_vd_26w_4s +from ppcls.arch.backbone.model_zoo.se_resnet_vd import SE_ResNet18_vd, SE_ResNet34_vd, SE_ResNet50_vd from ppcls.arch.backbone.model_zoo.se_resnext_vd import SE_ResNeXt50_vd_32x4d, SE_ResNeXt50_vd_32x4d, SENet154_vd from ppcls.arch.backbone.model_zoo.se_resnext import SE_ResNeXt50_32x4d, SE_ResNeXt101_32x4d, SE_ResNeXt152_64x4d from ppcls.arch.backbone.model_zoo.dpn import DPN68, DPN92, DPN98, DPN107, DPN131 @@ -33,10 +34,11 @@ from ppcls.arch.backbone.model_zoo.resnest import ResNeSt50_fast_1s1x64d, ResNeS from ppcls.arch.backbone.model_zoo.googlenet import GoogLeNet from ppcls.arch.backbone.model_zoo.mobilenet_v2 import MobileNetV2_x0_25, MobileNetV2_x0_5, MobileNetV2_x0_75, MobileNetV2, MobileNetV2_x1_5, MobileNetV2_x2_0 from ppcls.arch.backbone.model_zoo.shufflenet_v2 import ShuffleNetV2_x0_25, ShuffleNetV2_x0_33, ShuffleNetV2_x0_5, ShuffleNetV2_x1_0, ShuffleNetV2_x1_5, ShuffleNetV2_x2_0, ShuffleNetV2_swish +from ppcls.arch.backbone.model_zoo.ghostnet import GhostNet_x0_5, GhostNet_x1_0, GhostNet_x1_3 from ppcls.arch.backbone.model_zoo.alexnet import AlexNet from ppcls.arch.backbone.model_zoo.inception_v4 import InceptionV4 from ppcls.arch.backbone.model_zoo.xception import Xception41, Xception65, Xception71 -from ppcls.arch.backbone.model_zoo.xception_deeplab import Xception41_deeplab, Xception65_deeplab, Xception71_deeplab +from ppcls.arch.backbone.model_zoo.xception_deeplab import Xception41_deeplab, Xception65_deeplab from ppcls.arch.backbone.model_zoo.resnext101_wsl import ResNeXt101_32x8d_wsl, ResNeXt101_32x16d_wsl, ResNeXt101_32x32d_wsl, ResNeXt101_32x48d_wsl from ppcls.arch.backbone.model_zoo.squeezenet import SqueezeNet1_0, SqueezeNet1_1 from ppcls.arch.backbone.model_zoo.darknet import DarkNet53 diff --git a/ppcls/arch/backbone/model_zoo/alexnet.py b/ppcls/arch/backbone/model_zoo/alexnet.py index a9155ca4a650f9412bfd983acbc20a8d741fdd9a..3e1d1aa526565b6b18b0e192128961ceaf053074 100644 --- a/ppcls/arch/backbone/model_zoo/alexnet.py +++ b/ppcls/arch/backbone/model_zoo/alexnet.py @@ -1,3 +1,17 @@ +# 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. + import paddle from paddle import ParamAttr import paddle.nn as nn @@ -7,8 +21,11 @@ from paddle.nn import AdaptiveAvgPool2D, MaxPool2D, AvgPool2D from paddle.nn.initializer import Uniform import math -__all__ = ["AlexNet"] +from ppcls.utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url + +MODEL_URLS = {"AlexNet": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/AlexNet_pretrained.pdparams"} +__all__ = list(MODEL_URLS.keys()) class ConvPoolLayer(nn.Layer): def __init__(self, @@ -126,7 +143,19 @@ class AlexNetDY(nn.Layer): x = self._fc8(x) return x +def _load_pretrained(pretrained, model, model_url, use_ssld=False): + if pretrained is False: + pass + elif pretrained is True: + load_dygraph_pretrain_from_url(model, model_url, use_ssld=use_ssld) + elif isinstance(pretrained, str): + load_dygraph_pretrain(model, pretrained) + else: + raise RuntimeError( + "pretrained type is not available. Please use `string` or `boolean` type." + ) -def AlexNet(**args): - model = AlexNetDY(**args) +def AlexNet(pretrained=False, use_ssld=False, **kwargs): + model = AlexNetDY(**kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["AlexNet"], use_ssld=use_ssld) return model diff --git a/ppcls/arch/backbone/model_zoo/darknet.py b/ppcls/arch/backbone/model_zoo/darknet.py index 5aef16d7ccf81483b458ba63c68938ed1531e9f2..16b4b8600866d2bf78e45a1e1d3c4747a398c346 100644 --- a/ppcls/arch/backbone/model_zoo/darknet.py +++ b/ppcls/arch/backbone/model_zoo/darknet.py @@ -1,3 +1,17 @@ +# 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. + import paddle from paddle import ParamAttr import paddle.nn as nn @@ -7,8 +21,11 @@ from paddle.nn import AdaptiveAvgPool2D, MaxPool2D, AvgPool2D from paddle.nn.initializer import Uniform import math -__all__ = ["DarkNet53"] +from ppcls.utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url + +MODEL_URLS = {"DarkNet53": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/DarkNet53_pretrained.pdparams"} +__all__ = list(MODEL_URLS.keys()) class ConvBNLayer(nn.Layer): def __init__(self, @@ -155,7 +172,19 @@ class DarkNet(nn.Layer): x = self._out(x) return x - -def DarkNet53(**args): - model = DarkNet(**args) +def _load_pretrained(pretrained, model, model_url, use_ssld=False): + if pretrained is False: + pass + elif pretrained is True: + load_dygraph_pretrain_from_url(model, model_url, use_ssld=use_ssld) + elif isinstance(pretrained, str): + load_dygraph_pretrain(model, pretrained) + else: + raise RuntimeError( + "pretrained type is not available. Please use `string` or `boolean` type." + ) + +def DarkNet53(pretrained=False, use_ssld=False, **kwargs): + model = DarkNet(**kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["DarkNet53"], use_ssld=use_ssld) return model diff --git a/ppcls/arch/backbone/model_zoo/densenet.py b/ppcls/arch/backbone/model_zoo/densenet.py index df58f3551b3907b6896e3ba7c4d46c89edbe1c26..190959b80827abe86aa413afc83cc6af41eba5e9 100644 --- a/ppcls/arch/backbone/model_zoo/densenet.py +++ b/ppcls/arch/backbone/model_zoo/densenet.py @@ -1,4 +1,4 @@ -# copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve. +# 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. @@ -26,9 +26,16 @@ from paddle.nn.initializer import Uniform import math -__all__ = [ - "DenseNet121", "DenseNet161", "DenseNet169", "DenseNet201", "DenseNet264" -] +from ppcls.utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url + +MODEL_URLS = {"DenseNet121": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/DenseNet121_pretrained.pdparams", + "DenseNet161": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/DenseNet161_pretrained.pdparams", + "DenseNet169": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/DenseNet169_pretrained.pdparams", + "DenseNet201": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/DenseNet201_pretrained.pdparams", + "DenseNet264": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/DenseNet264_pretrained.pdparams", + } + +__all__ = list(MODEL_URLS.keys()) class BNACConvLayer(nn.Layer): @@ -282,27 +289,43 @@ class DenseNet(nn.Layer): y = self.out(y) return y - -def DenseNet121(**args): - model = DenseNet(layers=121, **args) +def _load_pretrained(pretrained, model, model_url, use_ssld=False): + if pretrained is False: + pass + elif pretrained is True: + load_dygraph_pretrain_from_url(model, model_url, use_ssld=use_ssld) + elif isinstance(pretrained, str): + load_dygraph_pretrain(model, pretrained) + else: + raise RuntimeError( + "pretrained type is not available. Please use `string` or `boolean` type." + ) + +def DenseNet121(pretrained=False, use_ssld=False, **kwargs): + model = DenseNet(layers=121, **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["DenseNet121"], use_ssld=use_ssld) return model -def DenseNet161(**args): - model = DenseNet(layers=161, **args) +def DenseNet161(pretrained=False, use_ssld=False, **kwargs): + model = DenseNet(layers=161, **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["DenseNet161"], use_ssld=use_ssld) return model -def DenseNet169(**args): - model = DenseNet(layers=169, **args) +def DenseNet169(pretrained=False, use_ssld=False, **kwargs): + model = DenseNet(layers=169, **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["DenseNet169"], use_ssld=use_ssld) return model -def DenseNet201(**args): - model = DenseNet(layers=201, **args) +def DenseNet201(pretrained=False, use_ssld=False, **kwargs): + model = DenseNet(layers=201, **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["DenseNet201"], use_ssld=use_ssld) return model -def DenseNet264(**args): - model = DenseNet(layers=264, **args) +def DenseNet264(pretrained=False, use_ssld=False, **kwargs): + model = DenseNet(layers=264, **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["DenseNet264"], use_ssld=use_ssld) return model diff --git a/ppcls/arch/backbone/model_zoo/distilled_vision_transformer.py b/ppcls/arch/backbone/model_zoo/distilled_vision_transformer.py index 48fd25050629aab56b7ec59aeef06f77c0da7bea..b7c36192c1043a7a81fb7034b98636bea4c8cda8 100644 --- a/ppcls/arch/backbone/model_zoo/distilled_vision_transformer.py +++ b/ppcls/arch/backbone/model_zoo/distilled_vision_transformer.py @@ -16,12 +16,20 @@ import paddle import paddle.nn as nn from .vision_transformer import VisionTransformer, Identity, trunc_normal_, zeros_ -__all__ = [ - 'DeiT_tiny_patch16_224', 'DeiT_small_patch16_224', 'DeiT_base_patch16_224', - 'DeiT_tiny_distilled_patch16_224', 'DeiT_small_distilled_patch16_224', - 'DeiT_base_distilled_patch16_224', 'DeiT_base_patch16_384', - 'DeiT_base_distilled_patch16_384' -] +from ppcls.utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url + +MODEL_URLS = { + "DeiT_tiny_patch16_224": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/DeiT_tiny_patch16_224_pretrained.pdparams", + "DeiT_small_patch16_224": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/DeiT_small_patch16_224_pretrained.pdparams", + "DeiT_base_patch16_224": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/DeiT_base_patch16_224_pretrained.pdparams", + "DeiT_tiny_distilled_patch16_224": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/DeiT_tiny_distilled_patch16_224_pretrained.pdparams", + "DeiT_small_distilled_patch16_224": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/DeiT_small_distilled_patch16_224_pretrained.pdparams", + "DeiT_base_distilled_patch16_224": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/DeiT_base_distilled_patch16_224_pretrained.pdparams", + "DeiT_base_patch16_384": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/DeiT_base_patch16_384_pretrained.pdparams", + "DeiT_base_distilled_patch16_384": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/DeiT_base_distilled_patch16_384_pretrained.pdparams", + } + +__all__ = list(MODEL_URLS.keys()) class DistilledVisionTransformer(VisionTransformer): @@ -90,7 +98,20 @@ class DistilledVisionTransformer(VisionTransformer): return (x + x_dist) / 2 -def DeiT_tiny_patch16_224(**kwargs): +def _load_pretrained(pretrained, model, model_url, use_ssld=False): + if pretrained is False: + pass + elif pretrained is True: + load_dygraph_pretrain_from_url(model, model_url, use_ssld=use_ssld) + elif isinstance(pretrained, str): + load_dygraph_pretrain(model, pretrained) + else: + raise RuntimeError( + "pretrained type is not available. Please use `string` or `boolean` type." + ) + + +def DeiT_tiny_patch16_224(pretrained=False, use_ssld=False, **kwargs): model = VisionTransformer( patch_size=16, embed_dim=192, @@ -100,10 +121,11 @@ def DeiT_tiny_patch16_224(**kwargs): qkv_bias=True, epsilon=1e-6, **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["DeiT_tiny_patch16_224"], use_ssld=use_ssld) return model -def DeiT_small_patch16_224(**kwargs): +def DeiT_small_patch16_224(pretrained=False, use_ssld=False, **kwargs): model = VisionTransformer( patch_size=16, embed_dim=384, @@ -113,10 +135,11 @@ def DeiT_small_patch16_224(**kwargs): qkv_bias=True, epsilon=1e-6, **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["DeiT_small_patch16_224"], use_ssld=use_ssld) return model -def DeiT_base_patch16_224(**kwargs): +def DeiT_base_patch16_224(pretrained=False, use_ssld=False, **kwargs): model = VisionTransformer( patch_size=16, embed_dim=768, @@ -126,10 +149,11 @@ def DeiT_base_patch16_224(**kwargs): qkv_bias=True, epsilon=1e-6, **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["DeiT_base_patch16_224"], use_ssld=use_ssld) return model -def DeiT_tiny_distilled_patch16_224(**kwargs): +def DeiT_tiny_distilled_patch16_224(pretrained=False, use_ssld=False, **kwargs): model = DistilledVisionTransformer( patch_size=16, embed_dim=192, @@ -139,10 +163,11 @@ def DeiT_tiny_distilled_patch16_224(**kwargs): qkv_bias=True, epsilon=1e-6, **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["DeiT_tiny_distilled_patch16_224"], use_ssld=use_ssld) return model -def DeiT_small_distilled_patch16_224(**kwargs): +def DeiT_small_distilled_patch16_224(pretrained=False, use_ssld=False, **kwargs): model = DistilledVisionTransformer( patch_size=16, embed_dim=384, @@ -152,10 +177,11 @@ def DeiT_small_distilled_patch16_224(**kwargs): qkv_bias=True, epsilon=1e-6, **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["DeiT_small_distilled_patch16_224"], use_ssld=use_ssld) return model -def DeiT_base_distilled_patch16_224(**kwargs): +def DeiT_base_distilled_patch16_224(pretrained=False, use_ssld=False, **kwargs): model = DistilledVisionTransformer( patch_size=16, embed_dim=768, @@ -165,10 +191,11 @@ def DeiT_base_distilled_patch16_224(**kwargs): qkv_bias=True, epsilon=1e-6, **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["DeiT_base_distilled_patch16_224"], use_ssld=use_ssld) return model -def DeiT_base_patch16_384(**kwargs): +def DeiT_base_patch16_384(pretrained=False, use_ssld=False, **kwargs): model = VisionTransformer( img_size=384, patch_size=16, @@ -179,10 +206,11 @@ def DeiT_base_patch16_384(**kwargs): qkv_bias=True, epsilon=1e-6, **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["DeiT_base_patch16_384"], use_ssld=use_ssld) return model -def DeiT_base_distilled_patch16_384(**kwargs): +def DeiT_base_distilled_patch16_384(pretrained=False, use_ssld=False, **kwargs): model = DistilledVisionTransformer( img_size=384, patch_size=16, @@ -193,4 +221,5 @@ def DeiT_base_distilled_patch16_384(**kwargs): qkv_bias=True, epsilon=1e-6, **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["DeiT_base_distilled_patch16_384"], use_ssld=use_ssld) return model diff --git a/ppcls/arch/backbone/model_zoo/dla.py b/ppcls/arch/backbone/model_zoo/dla.py index 8d914c17f2195de380e3a49871c87a319033fba5..51151710ebb48c7fdd625e3a2b5d9086b1d57f9e 100644 --- a/ppcls/arch/backbone/model_zoo/dla.py +++ b/ppcls/arch/backbone/model_zoo/dla.py @@ -26,25 +26,25 @@ from ppcls.utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_f MODEL_URLS = { "DLA34": - "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/model_zoo/DLA34_pretrained.pdparams", + "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/DLA34_pretrained.pdparams", "DLA46_c": - "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/model_zoo/DLA46_c_pretrained.pdparams", + "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/DLA46_c_pretrained.pdparams", "DLA46x_c": - "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/model_zoo/DLA46x_c_pretrained.pdparams", + "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/DLA46x_c_pretrained.pdparams", "DLA60": - "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/model_zoo/DLA60_pretrained.pdparams", + "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/DLA60_pretrained.pdparams", "DLA60x": - "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/model_zoo/DLA60x_pretrained.pdparams", + "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/DLA60x_pretrained.pdparams", "DLA60x_c": - "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/model_zoo/DLA60x_c_pretrained.pdparams", + "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/DLA60x_c_pretrained.pdparams", "DLA102": - "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/model_zoo/DLA102_pretrained.pdparams", + "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/DLA102_pretrained.pdparams", "DLA102x": - "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/model_zoo/DLA102x_pretrained.pdparams", + "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/DLA102x_pretrained.pdparams", "DLA102x2": - "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/model_zoo/DLA102x2_pretrained.pdparams", + "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/DLA102x2_pretrained.pdparams", "DLA169": - "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/model_zoo/DLA169_pretrained.pdparams" + "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/DLA169_pretrained.pdparams" } diff --git a/ppcls/arch/backbone/model_zoo/dpn.py b/ppcls/arch/backbone/model_zoo/dpn.py index becdc8ca60d9f49b3528c765ac30e65051ab0603..7741eb7ce7fdc9b3fcb58d60fbacd9e1dcd86e24 100644 --- a/ppcls/arch/backbone/model_zoo/dpn.py +++ b/ppcls/arch/backbone/model_zoo/dpn.py @@ -27,14 +27,16 @@ from paddle.nn.initializer import Uniform import math -__all__ = [ - "DPN", - "DPN68", - "DPN92", - "DPN98", - "DPN107", - "DPN131", -] +from ppcls.utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url + +MODEL_URLS = {"DPN68": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/DPN68_pretrained.pdparams", + "DPN92": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/DPN92_pretrained.pdparams", + "DPN98": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/DPN98_pretrained.pdparams", + "DPN107": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/DPN107_pretrained.pdparams", + "DPN131": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/DPN131_pretrained.pdparams", + } + +__all__ = list(MODEL_URLS.keys()) class ConvBNLayer(nn.Layer): @@ -398,28 +400,45 @@ class DPN(nn.Layer): net_arg['init_padding'] = init_padding return net_arg - - -def DPN68(**args): - model = DPN(layers=68, **args) + +def _load_pretrained(pretrained, model, model_url, use_ssld=False): + if pretrained is False: + pass + elif pretrained is True: + load_dygraph_pretrain_from_url(model, model_url, use_ssld=use_ssld) + elif isinstance(pretrained, str): + load_dygraph_pretrain(model, pretrained) + else: + raise RuntimeError( + "pretrained type is not available. Please use `string` or `boolean` type." + ) + + +def DPN68(pretrained=False, use_ssld=False, **kwargs): + model = DPN(layers=68, **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["DPN68"]) return model -def DPN92(**args): - model = DPN(layers=92, **args) +def DPN92(pretrained=False, use_ssld=False, **kwargs): + model = DPN(layers=92, **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["DPN92"]) return model -def DPN98(**args): - model = DPN(layers=98, **args) +def DPN98(pretrained=False, use_ssld=False, **kwargs): + model = DPN(layers=98, **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["DPN98"]) return model -def DPN107(**args): - model = DPN(layers=107, **args) +def DPN107(pretrained=False, use_ssld=False, **kwargs): + model = DPN(layers=107, **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["DPN107"]) return model -def DPN131(**args): - model = DPN(layers=131, **args) - return model +def DPN131(pretrained=False, use_ssld=False, **kwargs): + model = DPN(layers=131, **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["DPN131"]) + return model \ No newline at end of file diff --git a/ppcls/arch/backbone/model_zoo/efficientnet.py b/ppcls/arch/backbone/model_zoo/efficientnet.py index dd7ef86e1588ad746534be71e473067671c9783d..de2d5245902d0f4e96a0f9ed72d5a112df125704 100644 --- a/ppcls/arch/backbone/model_zoo/efficientnet.py +++ b/ppcls/arch/backbone/model_zoo/efficientnet.py @@ -9,11 +9,20 @@ import collections import re import copy -__all__ = [ - 'EfficientNet', 'EfficientNetB0_small', 'EfficientNetB0', 'EfficientNetB1', - 'EfficientNetB2', 'EfficientNetB3', 'EfficientNetB4', 'EfficientNetB5', - 'EfficientNetB6', 'EfficientNetB7' -] +from ppcls.utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url + +MODEL_URLS = {"EfficientNetB0_small": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/EfficientNetB0_small_pretrained.pdparams", + "EfficientNetB0": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/EfficientNetB0_pretrained.pdparams", + "EfficientNetB1": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/EfficientNetB1_pretrained.pdparams", + "EfficientNetB2": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/EfficientNetB2_pretrained.pdparams", + "EfficientNetB3": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/EfficientNetB3_pretrained.pdparams", + "EfficientNetB4": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/EfficientNetB4_pretrained.pdparams", + "EfficientNetB5": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/EfficientNetB5_pretrained.pdparams", + "EfficientNetB6": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/EfficientNetB6_pretrained.pdparams", + "EfficientNetB7": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/EfficientNetB7_pretrained.pdparams", + } + +__all__ = list(MODEL_URLS.keys()) GlobalParams = collections.namedtuple('GlobalParams', [ 'batch_norm_momentum', @@ -783,119 +792,159 @@ class EfficientNet(nn.Layer): x = self._fc(x) return x + +def _load_pretrained(pretrained, model, model_url, use_ssld=False): + if pretrained is False: + pass + elif pretrained is True: + load_dygraph_pretrain_from_url(model, model_url, use_ssld=use_ssld) + elif isinstance(pretrained, str): + load_dygraph_pretrain(model, pretrained) + else: + raise RuntimeError( + "pretrained type is not available. Please use `string` or `boolean` type." + ) + def EfficientNetB0_small(padding_type='DYNAMIC', override_params=None, use_se=False, - **args): + pretrained=False, + use_ssld=False, + **kwargs): model = EfficientNet( name='b0', padding_type=padding_type, override_params=override_params, use_se=use_se, - **args) + **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["EfficientNetB0_small"]) return model def EfficientNetB0(padding_type='SAME', override_params=None, use_se=True, - **args): + pretrained=False, + use_ssld=False, + **kwargs): model = EfficientNet( name='b0', padding_type=padding_type, override_params=override_params, use_se=use_se, - **args) + **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["EfficientNetB0"]) return model def EfficientNetB1(padding_type='SAME', override_params=None, use_se=True, - **args): + pretrained=False, + use_ssld=False, + **kwargs): model = EfficientNet( name='b1', padding_type=padding_type, override_params=override_params, use_se=use_se, - **args) + **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["EfficientNetB1"]) return model def EfficientNetB2(padding_type='SAME', override_params=None, use_se=True, - **args): + pretrained=False, + use_ssld=False, + **kwargs): model = EfficientNet( name='b2', padding_type=padding_type, override_params=override_params, use_se=use_se, - **args) + **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["EfficientNetB2"]) return model def EfficientNetB3(padding_type='SAME', override_params=None, use_se=True, - **args): + pretrained=False, + use_ssld=False, + **kwargs): model = EfficientNet( name='b3', padding_type=padding_type, override_params=override_params, use_se=use_se, - **args) + **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["EfficientNetB3"]) return model def EfficientNetB4(padding_type='SAME', override_params=None, use_se=True, - **args): + pretrained=False, + use_ssld=False, + **kwargs): model = EfficientNet( name='b4', padding_type=padding_type, override_params=override_params, use_se=use_se, - **args) + **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["EfficientNetB4"]) return model def EfficientNetB5(padding_type='SAME', override_params=None, use_se=True, - **args): + pretrained=False, + use_ssld=False, + **kwargs): model = EfficientNet( name='b5', padding_type=padding_type, override_params=override_params, use_se=use_se, - **args) + **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["EfficientNetB5"]) return model def EfficientNetB6(padding_type='SAME', override_params=None, use_se=True, - **args): + pretrained=False, + use_ssld=False, + **kwargs): model = EfficientNet( name='b6', padding_type=padding_type, override_params=override_params, use_se=use_se, - **args) + **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["EfficientNetB6"]) return model def EfficientNetB7(padding_type='SAME', override_params=None, use_se=True, - **args): + pretrained=False, + use_ssld=False, + **kwargs): model = EfficientNet( name='b7', padding_type=padding_type, override_params=override_params, use_se=use_se, - **args) - return model + **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["EfficientNetB7"]) + return model \ No newline at end of file diff --git a/ppcls/arch/backbone/model_zoo/ghostnet.py b/ppcls/arch/backbone/model_zoo/ghostnet.py index 0a47bc274ef794baf94038a374bd010f13eefde4..e557e0f9fa13929fd686ae3036010d066811b946 100644 --- a/ppcls/arch/backbone/model_zoo/ghostnet.py +++ b/ppcls/arch/backbone/model_zoo/ghostnet.py @@ -1,4 +1,4 @@ -# copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve. +# 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. @@ -21,7 +21,14 @@ from paddle.nn import Conv2D, BatchNorm, AdaptiveAvgPool2D, Linear from paddle.regularizer import L2Decay from paddle.nn.initializer import Uniform, KaimingNormal -__all__ = ["GhostNet_x0_5", "GhostNet_x1_0", "GhostNet_x1_3"] +from ppcls.utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url + +MODEL_URLS = {"GhostNet_x0_5": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/GhostNet_x0_5_pretrained.pdparams", + "GhostNet_x1_0": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/GhostNet_x1_0_pretrained.pdparams", + "GhostNet_x1_3": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/GhostNet_x1_3_pretrained.pdparams", + } + +__all__ = list(MODEL_URLS.keys()) class ConvBNLayer(nn.Layer): @@ -315,17 +322,33 @@ class GhostNet(nn.Layer): new_v += divisor return new_v + +def _load_pretrained(pretrained, model, model_url, use_ssld=False): + if pretrained is False: + pass + elif pretrained is True: + load_dygraph_pretrain_from_url(model, model_url, use_ssld=use_ssld) + elif isinstance(pretrained, str): + load_dygraph_pretrain(model, pretrained) + else: + raise RuntimeError( + "pretrained type is not available. Please use `string` or `boolean` type." + ) + -def GhostNet_x0_5(**args): - model = GhostNet(scale=0.5) +def GhostNet_x0_5(pretrained=False, use_ssld=False, **kwargs): + model = GhostNet(scale=0.5, **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["GhostNet_x0_5"], use_ssld=use_ssld) return model -def GhostNet_x1_0(**args): - model = GhostNet(scale=1.0) +def GhostNet_x1_0(pretrained=False, use_ssld=False, **kwargs): + model = GhostNet(scale=1.0, **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["GhostNet_x1_0"], use_ssld=use_ssld) return model -def GhostNet_x1_3(**args): - model = GhostNet(scale=1.3) +def GhostNet_x1_3(pretrained=False, use_ssld=False, **kwargs): + model = GhostNet(scale=1.3, **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["GhostNet_x1_3"], use_ssld=use_ssld) return model diff --git a/ppcls/arch/backbone/model_zoo/googlenet.py b/ppcls/arch/backbone/model_zoo/googlenet.py index 534c6ff01515b01bdf58b1aa8a32f8168f3b87e2..7ef35a9649fdbadf0a7c3c39dfec25376ff94e7b 100644 --- a/ppcls/arch/backbone/model_zoo/googlenet.py +++ b/ppcls/arch/backbone/model_zoo/googlenet.py @@ -8,7 +8,12 @@ from paddle.nn.initializer import Uniform import math -__all__ = ['GoogLeNet'] +from ppcls.utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url + +MODEL_URLS = {"GoogLeNet": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/GoogLeNet_pretrained.pdparams", + } + +__all__ = list(MODEL_URLS.keys()) def xavier(channels, filter_size, name): @@ -200,8 +205,22 @@ class GoogLeNetDY(nn.Layer): x = self._drop_o2(x) out2 = self._out2(x) return [out, out1, out2] - - -def GoogLeNet(**args): - model = GoogLeNetDY(**args) + + +def _load_pretrained(pretrained, model, model_url, use_ssld=False): + if pretrained is False: + pass + elif pretrained is True: + load_dygraph_pretrain_from_url(model, model_url, use_ssld=use_ssld) + elif isinstance(pretrained, str): + load_dygraph_pretrain(model, pretrained) + else: + raise RuntimeError( + "pretrained type is not available. Please use `string` or `boolean` type." + ) + + +def GoogLeNet(pretrained=False, use_ssld=False, **kwargs): + model = GoogLeNetDY(**kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["GoogLeNet"], use_ssld=use_ssld) return model diff --git a/ppcls/arch/backbone/model_zoo/gvt.py b/ppcls/arch/backbone/model_zoo/gvt.py index bcfdfead63c86ad4e6029edd51c2b955c540a58c..659be4964cac2136a4778195fdbb27d725123c58 100644 --- a/ppcls/arch/backbone/model_zoo/gvt.py +++ b/ppcls/arch/backbone/model_zoo/gvt.py @@ -22,10 +22,19 @@ from paddle.regularizer import L2Decay from .vision_transformer import trunc_normal_, normal_, zeros_, ones_, to_2tuple, DropPath, Identity, Mlp from .vision_transformer import Block as ViTBlock -__all__ = [ - "CPVTV2", "PCPVT", "ALTGVT", "pcpvt_small", "pcpvt_base", "pcpvt_large", - "alt_gvt_small", "alt_gvt_base", "alt_gvt_large" -] +from ppcls.utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url + +MODEL_URLS = { + "pcpvt_small": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/pcpvt_small_pretrained.pdparams", + "pcpvt_base": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/pcpvt_base_pretrained.pdparams", + "pcpvt_large": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/pcpvt_large_pretrained.pdparams", + "alt_gvt_small": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/alt_gvt_small_pretrained.pdparams", + "alt_gvt_base": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/alt_gvt_base_pretrained.pdparams", + "alt_gvt_large": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/alt_gvt_large_pretrained.pdparams" + } + +__all__ = list(MODEL_URLS.keys()) + class GroupAttention(nn.Layer): @@ -559,8 +568,20 @@ class ALTGVT(PCPVT): cur += depths[k] self.apply(self._init_weights) +def _load_pretrained(pretrained, model, model_url, use_ssld=False): + if pretrained is False: + pass + elif pretrained is True: + load_dygraph_pretrain_from_url(model, model_url, use_ssld=use_ssld) + elif isinstance(pretrained, str): + load_dygraph_pretrain(model, pretrained) + else: + raise RuntimeError( + "pretrained type is not available. Please use `string` or `boolean` type." + ) -def pcpvt_small(pretrained=False, **kwargs): + +def pcpvt_small(pretrained=False, use_ssld=False, **kwargs): model = CPVTV2( patch_size=4, embed_dims=[64, 128, 320, 512], @@ -572,11 +593,11 @@ def pcpvt_small(pretrained=False, **kwargs): depths=[3, 4, 6, 3], sr_ratios=[8, 4, 2, 1], **kwargs) - + _load_pretrained(pretrained, model, MODEL_URLS["pcpvt_small"], use_ssld=use_ssld) return model -def pcpvt_base(pretrained=False, **kwargs): +def pcpvt_base(pretrained=False, use_ssld=False, **kwargs): model = CPVTV2( patch_size=4, embed_dims=[64, 128, 320, 512], @@ -588,11 +609,11 @@ def pcpvt_base(pretrained=False, **kwargs): depths=[3, 4, 18, 3], sr_ratios=[8, 4, 2, 1], **kwargs) - + _load_pretrained(pretrained, model, MODEL_URLS["pcpvt_base"], use_ssld=use_ssld) return model -def pcpvt_large(pretrained=False, **kwargs): +def pcpvt_large(pretrained=False, use_ssld=False, **kwargs): model = CPVTV2( patch_size=4, embed_dims=[64, 128, 320, 512], @@ -604,11 +625,11 @@ def pcpvt_large(pretrained=False, **kwargs): depths=[3, 8, 27, 3], sr_ratios=[8, 4, 2, 1], **kwargs) - + _load_pretrained(pretrained, model, MODEL_URLS["pcpvt_large"], use_ssld=use_ssld) return model -def alt_gvt_small(pretrained=False, **kwargs): +def alt_gvt_small(pretrained=False, use_ssld=False, **kwargs): model = ALTGVT( patch_size=4, embed_dims=[64, 128, 256, 512], @@ -621,11 +642,11 @@ def alt_gvt_small(pretrained=False, **kwargs): wss=[7, 7, 7, 7], sr_ratios=[8, 4, 2, 1], **kwargs) - + _load_pretrained(pretrained, model, MODEL_URLS["alt_gvt_small"], use_ssld=use_ssld) return model -def alt_gvt_base(pretrained=False, **args): +def alt_gvt_base(pretrained=False, use_ssld=False, **kwargs): model = ALTGVT( patch_size=4, embed_dims=[96, 192, 384, 768], @@ -637,12 +658,12 @@ def alt_gvt_base(pretrained=False, **args): depths=[2, 2, 18, 2], wss=[7, 7, 7, 7], sr_ratios=[8, 4, 2, 1], - **args) - + **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["alt_gvt_base"], use_ssld=use_ssld) return model -def alt_gvt_large(pretrained=False, **kwargs): +def alt_gvt_large(pretrained=False, use_ssld=False, **kwargs): model = ALTGVT( patch_size=4, embed_dims=[128, 256, 512, 1024], @@ -655,5 +676,5 @@ def alt_gvt_large(pretrained=False, **kwargs): wss=[7, 7, 7, 7], sr_ratios=[8, 4, 2, 1], **kwargs) - + _load_pretrained(pretrained, model, MODEL_URLS["alt_gvt_large"], use_ssld=use_ssld) return model diff --git a/ppcls/arch/backbone/model_zoo/hardnet.py b/ppcls/arch/backbone/model_zoo/hardnet.py index 34b31a1e01ee78482c7b7967f0dff693e455ad4d..b3d5f9a45ce315e003fd17e11fa5a8db2700f2b1 100644 --- a/ppcls/arch/backbone/model_zoo/hardnet.py +++ b/ppcls/arch/backbone/model_zoo/hardnet.py @@ -20,13 +20,13 @@ from ppcls.utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_f MODEL_URLS = { 'HarDNet39_ds': - 'https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/model_zoo/HarDNet39_ds_pretrained.pdparams', + 'https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/HarDNet39_ds_pretrained.pdparams', 'HarDNet68_ds': - 'https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/model_zoo/HarDNet68_ds_pretrained.pdparams', + 'https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/HarDNet68_ds_pretrained.pdparams', 'HarDNet68': - 'https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/model_zoo/HarDNet68_pretrained.pdparams', + 'https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/HarDNet68_pretrained.pdparams', 'HarDNet85': - 'https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/model_zoo/HarDNet85_pretrained.pdparams' + 'https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/HarDNet85_pretrained.pdparams' } diff --git a/ppcls/arch/backbone/model_zoo/hrnet.py b/ppcls/arch/backbone/model_zoo/hrnet.py index e69827666cf658e98a13f4ef12edd2224d5938da..1566a00a823f10aa2c4901355f051b62d54d8d16 100644 --- a/ppcls/arch/backbone/model_zoo/hrnet.py +++ b/ppcls/arch/backbone/model_zoo/hrnet.py @@ -27,24 +27,18 @@ from paddle.nn.initializer import Uniform import math -__all__ = [ - "HRNet_W18_C", - "HRNet_W30_C", - "HRNet_W32_C", - "HRNet_W40_C", - "HRNet_W44_C", - "HRNet_W48_C", - "HRNet_W60_C", - "HRNet_W64_C", - "SE_HRNet_W18_C", - "SE_HRNet_W30_C", - "SE_HRNet_W32_C", - "SE_HRNet_W40_C", - "SE_HRNet_W44_C", - "SE_HRNet_W48_C", - "SE_HRNet_W60_C", - "SE_HRNet_W64_C", -] +from ppcls.utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url + +MODEL_URLS = {"HRNet_W18_C": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/HRNet_W18_C_pretrained.pdparams", + "HRNet_W30_C": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/HRNet_W30_C_pretrained.pdparams", + "HRNet_W32_C": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/HRNet_W32_C_pretrained.pdparams", + "HRNet_W40_C": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/HRNet_W40_C_pretrained.pdparams", + "HRNet_W44_C": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/HRNet_W44_C_pretrained.pdparams", + "HRNet_W48_C": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/HRNet_W48_C_pretrained.pdparams", + "HRNet_W64_C": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/HRNet_W64_C_pretrained.pdparams", + } + +__all__ = list(MODEL_URLS.keys()) class ConvBNLayer(nn.Layer): @@ -661,82 +655,62 @@ class HRNet(nn.Layer): y = self.out(y) return y - -def HRNet_W18_C(**args): - model = HRNet(width=18, **args) - return model - - -def HRNet_W30_C(**args): - model = HRNet(width=30, **args) - return model - - -def HRNet_W32_C(**args): - model = HRNet(width=32, **args) - return model - - -def HRNet_W40_C(**args): - model = HRNet(width=40, **args) - return model - - -def HRNet_W44_C(**args): - model = HRNet(width=44, **args) - return model - - -def HRNet_W48_C(**args): - model = HRNet(width=48, **args) - return model - - -def HRNet_W60_C(**args): - model = HRNet(width=60, **args) - return model - - -def HRNet_W64_C(**args): - model = HRNet(width=64, **args) - return model - - -def SE_HRNet_W18_C(**args): - model = HRNet(width=18, has_se=True, **args) +def _load_pretrained(pretrained, model, model_url, use_ssld=False): + if pretrained is False: + pass + elif pretrained is True: + load_dygraph_pretrain_from_url(model, model_url, use_ssld=use_ssld) + elif isinstance(pretrained, str): + load_dygraph_pretrain(model, pretrained) + else: + raise RuntimeError( + "pretrained type is not available. Please use `string` or `boolean` type." + ) + + +def HRNet_W18_C(pretrained=False, use_ssld=False, **kwarg): + model = HRNet(width=18, **kwarg) + _load_pretrained(pretrained, model, MODEL_URLS["HRNet_W18_C"], use_ssld=use_ssld) return model -def SE_HRNet_W30_C(**args): - model = HRNet(width=30, has_se=True, **args) +def HRNet_W30_C(pretrained=False, use_ssld=False, **kwarg): + model = HRNet(width=30, **kwarg) + _load_pretrained(pretrained, model, MODEL_URLS["HRNet_W30_C"], use_ssld=use_ssld) return model -def SE_HRNet_W32_C(**args): - model = HRNet(width=32, has_se=True, **args) +def HRNet_W32_C(pretrained=False, use_ssld=False, **kwarg): + model = HRNet(width=32, **kwarg) + _load_pretrained(pretrained, model, MODEL_URLS["HRNet_W32_C"], use_ssld=use_ssld) return model -def SE_HRNet_W40_C(**args): - model = HRNet(width=40, has_se=True, **args) +def HRNet_W40_C(pretrained=False, use_ssld=False, **kwarg): + model = HRNet(width=40, **kwarg) + _load_pretrained(pretrained, model, MODEL_URLS["HRNet_W40_C"], use_ssld=use_ssld) return model -def SE_HRNet_W44_C(**args): - model = HRNet(width=44, has_se=True, **args) +def HRNet_W44_C(pretrained=False, use_ssld=False, **kwarg): + model = HRNet(width=44, **kwarg) + _load_pretrained(pretrained, model, MODEL_URLS["HRNet_W44_C"], use_ssld=use_ssld) return model -def SE_HRNet_W48_C(**args): - model = HRNet(width=48, has_se=True, **args) +def HRNet_W48_C(pretrained=False, use_ssld=False, **kwarg): + model = HRNet(width=48, **kwarg) + _load_pretrained(pretrained, model, MODEL_URLS["HRNet_W48_C"], use_ssld=use_ssld) return model -def SE_HRNet_W60_C(**args): - model = HRNet(width=60, has_se=True, **args) +def HRNet_W64_C(pretrained=False, use_ssld=False, **kwarg): + model = HRNet(width=64, **kwarg) + _load_pretrained(pretrained, model, MODEL_URLS["HRNet_W64_C"], use_ssld=use_ssld) return model -def SE_HRNet_W64_C(**args): - model = HRNet(width=64, has_se=True, **args) +def SE_HRNet_W64_C(pretrained=False, use_ssld=False, **kwarg): + model = HRNet(width=64, **kwarg) + _load_pretrained(pretrained, model, MODEL_URLS["SE_HRNet_W64_C"], use_ssld=use_ssld) return model diff --git a/ppcls/arch/backbone/model_zoo/inception_v3.py b/ppcls/arch/backbone/model_zoo/inception_v3.py index 8f93ca68371a939bdc2db47a0a37a0a991485deb..d8a9f1d8867df1016dc4947f7db1ddc386980f27 100644 --- a/ppcls/arch/backbone/model_zoo/inception_v3.py +++ b/ppcls/arch/backbone/model_zoo/inception_v3.py @@ -1,4 +1,4 @@ -# copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve. +# 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. @@ -26,7 +26,11 @@ from paddle.nn import AdaptiveAvgPool2D, MaxPool2D, AvgPool2D from paddle.nn.initializer import Uniform import math -__all__ = ["InceptionV3"] +from ppcls.utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url + +MODEL_URLS = {"InceptionV3": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/InceptionV3_pretrained.pdparams"} + +__all__ = list(MODEL_URLS.keys()) class ConvBNLayer(nn.Layer): @@ -425,9 +429,9 @@ class InceptionE(nn.Layer): return outputs -class InceptionV3(nn.Layer): +class Inception_V3(nn.Layer): def __init__(self, class_dim=1000): - super(InceptionV3, self).__init__() + super(Inception_V3, self).__init__() self.inception_a_list = [[192, 256, 288], [32, 64, 64]] self.inception_c_list = [[768, 768, 768, 768], [128, 160, 160, 192]] @@ -472,10 +476,28 @@ class InceptionV3(nn.Layer): def forward(self, x): y = self.inception_stem(x) for inception_block in self.inception_block_list: - y = inception_block(y) + y = inception_block(y) y = self.gap(y) y = paddle.reshape(y, shape=[-1, 2048]) y = self.drop(y) y = self.out(y) return y + +def _load_pretrained(pretrained, model, model_url, use_ssld=False): + if pretrained is False: + pass + elif pretrained is True: + load_dygraph_pretrain_from_url(model, model_url, use_ssld=use_ssld) + elif isinstance(pretrained, str): + load_dygraph_pretrain(model, pretrained) + else: + raise RuntimeError( + "pretrained type is not available. Please use `string` or `boolean` type." + ) + +def InceptionV3(pretrained=False, use_ssld=False, **kwargs): + model = Inception_V3(**kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["InceptionV3"], use_ssld=use_ssld) + return model + diff --git a/ppcls/arch/backbone/model_zoo/inception_v4.py b/ppcls/arch/backbone/model_zoo/inception_v4.py index b8ba098490ce8c705ddb0efda0452b3a0900e401..37cef5c20f6b444d078b0f3a69ce45357c093c5c 100644 --- a/ppcls/arch/backbone/model_zoo/inception_v4.py +++ b/ppcls/arch/backbone/model_zoo/inception_v4.py @@ -21,7 +21,11 @@ from paddle.nn import AdaptiveAvgPool2D, MaxPool2D, AvgPool2D from paddle.nn.initializer import Uniform import math -__all__ = ["InceptionV4"] +from ppcls.utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url + +MODEL_URLS = {"InceptionV4": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/InceptionV4_pretrained.pdparams"} + +__all__ = list(MODEL_URLS.keys()) class ConvBNLayer(nn.Layer): @@ -450,6 +454,19 @@ class InceptionV4DY(nn.Layer): return x -def InceptionV4(**args): - model = InceptionV4DY(**args) +def _load_pretrained(pretrained, model, model_url, use_ssld=False): + if pretrained is False: + pass + elif pretrained is True: + load_dygraph_pretrain_from_url(model, model_url, use_ssld=use_ssld) + elif isinstance(pretrained, str): + load_dygraph_pretrain(model, pretrained) + else: + raise RuntimeError( + "pretrained type is not available. Please use `string` or `boolean` type." + ) + +def InceptionV4(pretrained=False, use_ssld=False, **kwargs): + model = InceptionV4DY(**kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["InceptionV4"], use_ssld=use_ssld) return model diff --git a/ppcls/arch/backbone/model_zoo/levit.py b/ppcls/arch/backbone/model_zoo/levit.py index 459afcd6f13c43b87156a81fe0913efa437b565c..bb74e00c63f7f77d8cef2ac22b8fecc46549e816 100644 --- a/ppcls/arch/backbone/model_zoo/levit.py +++ b/ppcls/arch/backbone/model_zoo/levit.py @@ -24,7 +24,17 @@ from paddle.regularizer import L2Decay from .vision_transformer import trunc_normal_, zeros_, ones_, Identity -__all__ = ["LeViT_128S", "LeViT_128", "LeViT_192", "LeViT_256", "LeViT_384"] +from ppcls.utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url + +MODEL_URLS = { + "LeViT_128S": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/LeViT_128S_pretrained.pdparams", + "LeViT_128": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/LeViT_128_pretrained.pdparams", + "LeViT_192": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/LeViT_192_pretrained.pdparams", + "LeViT_256": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/LeViT_256_pretrained.pdparams", + "LeViT_384": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/LeViT_384_pretrained.pdparams", + } + +__all__ = list(MODEL_URLS.keys()) def cal_attention_biases(attention_biases, attention_bias_idxs): @@ -479,37 +489,59 @@ specification = { }, } - -def LeViT_128S(class_dim=1000, distillation=True, pretrained=False): - return model_factory( +def _load_pretrained(pretrained, model, model_url, use_ssld=False): + if pretrained is False: + pass + elif pretrained is True: + load_dygraph_pretrain_from_url(model, model_url, use_ssld=use_ssld) + elif isinstance(pretrained, str): + load_dygraph_pretrain(model, pretrained) + else: + raise RuntimeError( + "pretrained type is not available. Please use `string` or `boolean` type." + ) + + +def LeViT_128S(pretrained=False, use_ssld=False, class_dim=1000, distillation=False, **kwargs): + model = model_factory( **specification['LeViT_128S'], class_dim=class_dim, distillation=distillation) + _load_pretrained(pretrained, model, MODEL_URLS["LeViT_128S"], use_ssld=use_ssld) + return model -def LeViT_128(class_dim=1000, distillation=True): - return model_factory( +def LeViT_128(pretrained=False, use_ssld=False, class_dim=1000, distillation=False, **kwargs): + model = model_factory( **specification['LeViT_128'], class_dim=class_dim, distillation=distillation) + _load_pretrained(pretrained, model, MODEL_URLS["LeViT_128"], use_ssld=use_ssld) + return model -def LeViT_192(class_dim=1000, distillation=True): - return model_factory( +def LeViT_192(pretrained=False, use_ssld=False, class_dim=1000, distillation=False, **kwargs): + model = model_factory( **specification['LeViT_192'], class_dim=class_dim, distillation=distillation) + _load_pretrained(pretrained, model, MODEL_URLS["LeViT_192"], use_ssld=use_ssld) + return model -def LeViT_256(class_dim=1000, distillation=False): - return model_factory( +def LeViT_256(pretrained=False, use_ssld=False, class_dim=1000, distillation=False, **kwargs): + model = model_factory( **specification['LeViT_256'], class_dim=class_dim, distillation=distillation) + _load_pretrained(pretrained, model, MODEL_URLS["LeViT_256"], use_ssld=use_ssld) + return model -def LeViT_384(class_dim=1000, distillation=True): - return model_factory( +def LeViT_384(pretrained=False, use_ssld=False, class_dim=1000, distillation=False, **kwargs): + model = model_factory( **specification['LeViT_384'], class_dim=class_dim, distillation=distillation) + _load_pretrained(pretrained, model, MODEL_URLS["LeViT_384"], use_ssld=use_ssld) + return model diff --git a/ppcls/arch/backbone/model_zoo/mixnet.py b/ppcls/arch/backbone/model_zoo/mixnet.py index f195694741292977e07b084a3c8efa8303f810a1..13582acb861cbf29f0964ae7dbf103e3a3b19b80 100644 --- a/ppcls/arch/backbone/model_zoo/mixnet.py +++ b/ppcls/arch/backbone/model_zoo/mixnet.py @@ -17,14 +17,20 @@ https://arxiv.org/abs/1907.09595. """ -__all__ = ['MixNet_S', 'MixNet_M', 'MixNet_L'] - import os from inspect import isfunction from functools import reduce import paddle import paddle.nn as nn +from ppcls.utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url + +MODEL_URLS = {"MixNet_S": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/MixNet_S_pretrained.pdparams", + "MixNet_M": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/MixNet_M_pretrained.pdparams", + "MixNet_L": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/MixNet_L_pretrained.pdparams"} + +__all__ = list(MODEL_URLS.keys()) + class Identity(nn.Layer): """ @@ -755,13 +761,33 @@ def get_mixnet(version, width_scale, model_name=None, **kwargs): return net +def _load_pretrained(pretrained, model, model_url, use_ssld=False): + if pretrained is False: + pass + elif pretrained is True: + load_dygraph_pretrain_from_url(model, model_url, use_ssld=use_ssld) + elif isinstance(pretrained, str): + load_dygraph_pretrain(model, pretrained) + else: + raise RuntimeError( + "pretrained type is not available. Please use `string` or `boolean` type." + ) + +def MixNet_S(pretrained=False, use_ssld=False, **kwargs): + model = InceptionV4DY(**kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["InceptionV4"], use_ssld=use_ssld) + return model + + def MixNet_S(**kwargs): """ MixNet-S model from 'MixConv: Mixed Depthwise Convolutional Kernels,' https://arxiv.org/abs/1907.09595. """ - return get_mixnet( + model = get_mixnet( version="s", width_scale=1.0, model_name="MixNet_S", **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["MixNet_S"], use_ssld=use_ssld) + return model def MixNet_M(**kwargs): @@ -769,14 +795,19 @@ def MixNet_M(**kwargs): MixNet-M model from 'MixConv: Mixed Depthwise Convolutional Kernels,' https://arxiv.org/abs/1907.09595. """ - return get_mixnet( + model = get_mixnet( version="m", width_scale=1.0, model_name="MixNet_M", **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["MixNet_M"], use_ssld=use_ssld) + return model def MixNet_L(**kwargs): """ - MixNet-L model from 'MixConv: Mixed Depthwise Convolutional Kernels,' + MixNet-S model from 'MixConv: Mixed Depthwise Convolutional Kernels,' https://arxiv.org/abs/1907.09595. """ - return get_mixnet( + model = get_mixnet( version="m", width_scale=1.3, model_name="MixNet_L", **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["MixNet_L"], use_ssld=use_ssld) + return model + diff --git a/ppcls/arch/backbone/model_zoo/mobilenet_v1.py b/ppcls/arch/backbone/model_zoo/mobilenet_v1.py index 751ca552aace540d3fc6099ef9dafcc137322a9d..575dbf26d8519fe8e3dcb10c81f8b76d436b8b44 100644 --- a/ppcls/arch/backbone/model_zoo/mobilenet_v1.py +++ b/ppcls/arch/backbone/model_zoo/mobilenet_v1.py @@ -26,9 +26,14 @@ from paddle.nn import AdaptiveAvgPool2D, MaxPool2D, AvgPool2D from paddle.nn.initializer import KaimingNormal import math -__all__ = [ - "MobileNetV1_x0_25", "MobileNetV1_x0_5", "MobileNetV1_x0_75", "MobileNetV1" -] +from ppcls.utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url + +MODEL_URLS = {"MobileNetV1_x0_25": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/MobileNetV1_x0_25_pretrained.pdparams", + "MobileNetV1_x0_5": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/MobileNetV1_x0_5_pretrained.pdparams", + "MobileNetV1_x0_75": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/MobileNetV1_x0_75_pretrained.pdparams", + "MobileNetV1": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/MobileNetV1_pretrained.pdparams"} + +__all__ = list(MODEL_URLS.keys()) class ConvBNLayer(nn.Layer): @@ -245,22 +250,39 @@ class MobileNet(nn.Layer): y = self.out(y) return y - -def MobileNetV1_x0_25(**args): - model = MobileNet(scale=0.25, **args) + +def _load_pretrained(pretrained, model, model_url, use_ssld=False): + if pretrained is False: + pass + elif pretrained is True: + load_dygraph_pretrain_from_url(model, model_url, use_ssld=use_ssld) + elif isinstance(pretrained, str): + load_dygraph_pretrain(model, pretrained) + else: + raise RuntimeError( + "pretrained type is not available. Please use `string` or `boolean` type." + ) + + +def MobileNetV1_x0_25(pretrained=False, use_ssld=False, **kwargs): + model = MobileNet(scale=0.25, **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["MobileNetV1_x0_25"], use_ssld=use_ssld) return model -def MobileNetV1_x0_5(**args): - model = MobileNet(scale=0.5, **args) +def MobileNetV1_x0_5(pretrained=False, use_ssld=False, **kwargs): + model = MobileNet(scale=0.5, **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["MobileNetV1_x0_5"], use_ssld=use_ssld) return model -def MobileNetV1_x0_75(**args): - model = MobileNet(scale=0.75, **args) +def MobileNetV1_x0_75(pretrained=False, use_ssld=False, **kwargs): + model = MobileNet(scale=0.75, **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["MobileNetV1_x0_75"], use_ssld=use_ssld) return model -def MobileNetV1(**args): - model = MobileNet(scale=1.0, **args) - return model +def MobileNetV1(pretrained=False, use_ssld=False, **kwargs): + model = MobileNet(scale=1.0, **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["MobileNetV1"], use_ssld=use_ssld) + return model \ No newline at end of file diff --git a/ppcls/arch/backbone/model_zoo/mobilenet_v2.py b/ppcls/arch/backbone/model_zoo/mobilenet_v2.py index e2f0043b5d3d263d0d5839e40cfdb779d9673452..4cafd1461d51eea724655a38eb18c056d7944526 100644 --- a/ppcls/arch/backbone/model_zoo/mobilenet_v2.py +++ b/ppcls/arch/backbone/model_zoo/mobilenet_v2.py @@ -26,10 +26,16 @@ from paddle.nn import AdaptiveAvgPool2D, MaxPool2D, AvgPool2D import math -__all__ = [ - "MobileNetV2_x0_25", "MobileNetV2_x0_5", "MobileNetV2_x0_75", - "MobileNetV2", "MobileNetV2_x1_5", "MobileNetV2_x2_0" -] +from ppcls.utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url + +MODEL_URLS = {"MobileNetV2_x0_25": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/MobileNetV2_x0_25_pretrained.pdparams", + "MobileNetV2_x0_5": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/MobileNetV2_x0_5_pretrained.pdparams", + "MobileNetV2_x0_75": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/MobileNetV2_x0_75_pretrained.pdparams", + "MobileNetV2": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/MobileNetV2_pretrained.pdparams", + "MobileNetV2_x1_5": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/MobileNetV2_x1_5_pretrained.pdparams", + "MobileNetV2_x2_0": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/MobileNetV2_x2_0_pretrained.pdparams"} + +__all__ = list(MODEL_URLS.keys()) class ConvBNLayer(nn.Layer): @@ -149,7 +155,7 @@ class InvresiBlocks(nn.Layer): class MobileNet(nn.Layer): - def __init__(self, class_dim=1000, scale=1.0, prefix_name="", **args): + def __init__(self, class_dim=1000, scale=1.0, prefix_name=""): super(MobileNet, self).__init__() self.scale = scale self.class_dim = class_dim @@ -216,33 +222,52 @@ class MobileNet(nn.Layer): y = paddle.flatten(y, start_axis=1, stop_axis=-1) y = self.out(y) return y - - -def MobileNetV2_x0_25(**args): - model = MobileNet(scale=0.25, **args) + + +def _load_pretrained(pretrained, model, model_url, use_ssld=False): + if pretrained is False: + pass + elif pretrained is True: + load_dygraph_pretrain_from_url(model, model_url, use_ssld=use_ssld) + elif isinstance(pretrained, str): + load_dygraph_pretrain(model, pretrained) + else: + raise RuntimeError( + "pretrained type is not available. Please use `string` or `boolean` type." + ) + + +def MobileNetV2_x0_25(pretrained=False, use_ssld=False, **kwargs): + model = MobileNet(scale=0.25, **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["MobileNetV2_x0_25"], use_ssld=use_ssld) return model -def MobileNetV2_x0_5(**args): - model = MobileNet(scale=0.5, **args) +def MobileNetV2_x0_5(pretrained=False, use_ssld=False, **kwargs): + model = MobileNet(scale=0.5, **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["MobileNetV2_x0_5"], use_ssld=use_ssld) return model -def MobileNetV2_x0_75(**args): - model = MobileNet(scale=0.75, **args) +def MobileNetV2_x0_75(pretrained=False, use_ssld=False, **kwargs): + model = MobileNet(scale=0.75, **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["MobileNetV2_x0_75"], use_ssld=use_ssld) return model -def MobileNetV2(**args): - model = MobileNet(scale=1.0, **args) +def MobileNetV2(pretrained=False, use_ssld=False, **kwargs): + model = MobileNet(scale=1.0, **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["MobileNetV2"], use_ssld=use_ssld) return model -def MobileNetV2_x1_5(**args): - model = MobileNet(scale=1.5, **args) +def MobileNetV2_x1_5(pretrained=False, use_ssld=False, **kwargs): + model = MobileNet(scale=1.5, **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["MobileNetV2_x1_5"], use_ssld=use_ssld) return model -def MobileNetV2_x2_0(**args): - model = MobileNet(scale=2.0, **args) +def MobileNetV2_x2_0(pretrained=False, use_ssld=False, **kwargs): + model = MobileNet(scale=2.0, **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["MobileNetV2_x2_0"], use_ssld=use_ssld) return model diff --git a/ppcls/arch/backbone/model_zoo/mobilenet_v3.py b/ppcls/arch/backbone/model_zoo/mobilenet_v3.py index 42954c8e889002390378e3905e6d8ea19a1d97ec..2169f7a2093390e537a7d39f2e414e5dd52a1c03 100644 --- a/ppcls/arch/backbone/model_zoo/mobilenet_v3.py +++ b/ppcls/arch/backbone/model_zoo/mobilenet_v3.py @@ -28,13 +28,20 @@ from paddle.regularizer import L2Decay import math -__all__ = [ - "MobileNetV3_small_x0_35", "MobileNetV3_small_x0_5", - "MobileNetV3_small_x0_75", "MobileNetV3_small_x1_0", - "MobileNetV3_small_x1_25", "MobileNetV3_large_x0_35", - "MobileNetV3_large_x0_5", "MobileNetV3_large_x0_75", - "MobileNetV3_large_x1_0", "MobileNetV3_large_x1_25" -] +from ppcls.utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url + +MODEL_URLS = {"MobileNetV3_small_x0_35": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/MobileNetV3_small_x0_35_pretrained.pdparams", + "MobileNetV3_small_x0_5": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/MobileNetV3_small_x0_5_pretrained.pdparams", + "MobileNetV3_small_x0_75": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/MobileNetV3_small_x0_75_pretrained.pdparams", + "MobileNetV3_small_x1_0": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/MobileNetV3_small_x1_0_pretrained.pdparams", + "MobileNetV3_small_x1_25": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/MobileNetV3_small_x1_25_pretrained.pdparams", + "MobileNetV3_large_x0_35": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/MobileNetV3_large_x0_35_pretrained.pdparams", + "MobileNetV3_large_x0_5": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/MobileNetV3_large_x0_5_pretrained.pdparams", + "MobileNetV3_large_x0_75": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/MobileNetV3_large_x0_75_pretrained.pdparams", + "MobileNetV3_large_x1_0": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/MobileNetV3_large_x1_0_pretrained.pdparams", + "MobileNetV3_large_x1_25": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/MobileNetV3_large_x1_25_pretrained.pdparams"} + +__all__ = list(MODEL_URLS.keys()) def make_divisible(v, divisor=8, min_value=None): @@ -308,52 +315,75 @@ class SEModule(nn.Layer): outputs = hardsigmoid(outputs, slope=0.2, offset=0.5) return paddle.multiply(x=inputs, y=outputs) - -def MobileNetV3_small_x0_35(**args): - model = MobileNetV3(model_name="small", scale=0.35, **args) + +def _load_pretrained(pretrained, model, model_url, use_ssld=False): + if pretrained is False: + pass + elif pretrained is True: + load_dygraph_pretrain_from_url(model, model_url, use_ssld=use_ssld) + elif isinstance(pretrained, str): + load_dygraph_pretrain(model, pretrained) + else: + raise RuntimeError( + "pretrained type is not available. Please use `string` or `boolean` type." + ) + + +def MobileNetV3_small_x0_35(pretrained=False, use_ssld=False, **kwargs): + model = MobileNetV3(model_name="small", scale=0.35, **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["MobileNetV3_small_x0_35"], use_ssld=use_ssld) return model -def MobileNetV3_small_x0_5(**args): - model = MobileNetV3(model_name="small", scale=0.5, **args) +def MobileNetV3_small_x0_5(pretrained=False, use_ssld=False, **kwargs): + model = MobileNetV3(model_name="small", scale=0.5, **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["MobileNetV3_small_x0_5"], use_ssld=use_ssld) return model -def MobileNetV3_small_x0_75(**args): - model = MobileNetV3(model_name="small", scale=0.75, **args) +def MobileNetV3_small_x0_75(pretrained=False, use_ssld=False, **kwargs): + model = MobileNetV3(model_name="small", scale=0.75, **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["MobileNetV3_small_x0_75"], use_ssld=use_ssld) return model -def MobileNetV3_small_x1_0(**args): - model = MobileNetV3(model_name="small", scale=1.0, **args) +def MobileNetV3_small_x1_0(pretrained=False, use_ssld=False, **kwargs): + model = MobileNetV3(model_name="small", scale=1.0, **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["MobileNetV3_small_x1_0"], use_ssld=use_ssld) return model -def MobileNetV3_small_x1_25(**args): - model = MobileNetV3(model_name="small", scale=1.25, **args) +def MobileNetV3_small_x1_25(pretrained=False, use_ssld=False, **kwargs): + model = MobileNetV3(model_name="small", scale=1.25, **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["MobileNetV3_small_x1_25"], use_ssld=use_ssld) return model - -def MobileNetV3_large_x0_35(**args): - model = MobileNetV3(model_name="large", scale=0.35, **args) + +def MobileNetV3_large_x0_35(pretrained=False, use_ssld=False, **kwargs): + model = MobileNetV3(model_name="large", scale=0.35, **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["MobileNetV3_large_x0_35"], use_ssld=use_ssld) return model -def MobileNetV3_large_x0_5(**args): - model = MobileNetV3(model_name="large", scale=0.5, **args) +def MobileNetV3_large_x0_5(pretrained=False, use_ssld=False, **kwargs): + model = MobileNetV3(model_name="large", scale=0.5, **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["MobileNetV3_large_x0_5"], use_ssld=use_ssld) return model -def MobileNetV3_large_x0_75(**args): - model = MobileNetV3(model_name="large", scale=0.75, **args) +def MobileNetV3_large_x0_75(pretrained=False, use_ssld=False, **kwargs): + model = MobileNetV3(model_name="large", scale=0.75, **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["MobileNetV3_large_x0_75"], use_ssld=use_ssld) return model -def MobileNetV3_large_x1_0(**args): - model = MobileNetV3(model_name="large", scale=1.0, **args) +def MobileNetV3_large_x1_0(pretrained=False, use_ssld=False, **kwargs): + model = MobileNetV3(model_name="large", scale=1.0, **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["MobileNetV3_large_x1_0"], use_ssld=use_ssld) return model -def MobileNetV3_large_x1_25(**args): - model = MobileNetV3(model_name="large", scale=1.25, **args) +def MobileNetV3_large_x1_25(pretrained=False, use_ssld=False, **kwargs): + model = MobileNetV3(model_name="large", scale=1.25, **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["MobileNetV3_large_x1_25"], use_ssld=use_ssld) return model diff --git a/ppcls/arch/backbone/model_zoo/rednet.py b/ppcls/arch/backbone/model_zoo/rednet.py index e801879271e92cb0e73a563958055acb787540cb..a113a32aca5e6d000cb95157690a3b56f85c7cb4 100644 --- a/ppcls/arch/backbone/model_zoo/rednet.py +++ b/ppcls/arch/backbone/model_zoo/rednet.py @@ -22,15 +22,15 @@ from ppcls.utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_f MODEL_URLS = { "RedNet26": - "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/model_zoo/RedNet26_pretrained.pdparams", + "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/RedNet26_pretrained.pdparams", "RedNet38": - "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/model_zoo/RedNet38_pretrained.pdparams", + "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/RedNet38_pretrained.pdparams", "RedNet50": - "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/model_zoo/RedNet50_pretrained.pdparams", + "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/RedNet50_pretrained.pdparams", "RedNet101": - "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/model_zoo/RedNet101_pretrained.pdparams", + "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/RedNet101_pretrained.pdparams", "RedNet152": - "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/model_zoo/RedNet152_pretrained.pdparams" + "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/RedNet152_pretrained.pdparams" } diff --git a/ppcls/arch/backbone/model_zoo/regnet.py b/ppcls/arch/backbone/model_zoo/regnet.py index 19ddaaad1b1ed3ebcc6557bcb19c753f8f33c50f..86802ee7e5736356925ecb2293b29dcf2d4b539a 100644 --- a/ppcls/arch/backbone/model_zoo/regnet.py +++ b/ppcls/arch/backbone/model_zoo/regnet.py @@ -26,10 +26,17 @@ from paddle.nn import AdaptiveAvgPool2D, MaxPool2D, AvgPool2D from paddle.nn.initializer import Uniform import math -__all__ = [ - "RegNetX_200MF", "RegNetX_4GF", "RegNetX_32GF", "RegNetY_200MF", - "RegNetY_4GF", "RegNetY_32GF" -] +from ppcls.utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url + +MODEL_URLS = {"RegNetX_200MF": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/RegNetX_200MF_pretrained.pdparams", + "RegNetX_4GF": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/RegNetX_4GF_pretrained.pdparams", + "RegNetX_32GF": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/RegNetX_32GF_pretrained.pdparams", + "RegNetY_200MF": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/RegNetY_200MF_pretrained.pdparams", + "RegNetY_4GF": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/RegNetY_4GF_pretrained.pdparams", + "RegNetY_32GF": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/RegNetY_32GF_pretrained.pdparams", + } + +__all__ = list(MODEL_URLS.keys()) def quantize_float(f, q): @@ -308,14 +315,28 @@ class RegNet(nn.Layer): y = self.out(y) return y - -def RegNetX_200MF(**args): + +def _load_pretrained(pretrained, model, model_url, use_ssld=False): + if pretrained is False: + pass + elif pretrained is True: + load_dygraph_pretrain_from_url(model, model_url, use_ssld=use_ssld) + elif isinstance(pretrained, str): + load_dygraph_pretrain(model, pretrained) + else: + raise RuntimeError( + "pretrained type is not available. Please use `string` or `boolean` type." + ) + + +def RegNetX_200MF(pretrained=False, use_ssld=False, **kwargs): model = RegNet( - w_a=36.44, w_0=24, w_m=2.49, d=13, group_w=8, bot_mul=1.0, q=8, **args) + w_a=36.44, w_0=24, w_m=2.49, d=13, group_w=8, bot_mul=1.0, q=8, **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["RegNetX_200MF"], use_ssld=use_ssld) return model -def RegNetX_4GF(**args): +def RegNetX_4GF(pretrained=False, use_ssld=False, **kwargs): model = RegNet( w_a=38.65, w_0=96, @@ -324,11 +345,12 @@ def RegNetX_4GF(**args): group_w=40, bot_mul=1.0, q=8, - **args) + **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["RegNetX_4GF"], use_ssld=use_ssld) return model -def RegNetX_32GF(**args): +def RegNetX_32GF(pretrained=False, use_ssld=False, **kwargs): model = RegNet( w_a=69.86, w_0=320, @@ -337,11 +359,12 @@ def RegNetX_32GF(**args): group_w=168, bot_mul=1.0, q=8, - **args) + **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["RegNetX_32GF"], use_ssld=use_ssld) return model -def RegNetY_200MF(**args): +def RegNetY_200MF(pretrained=False, use_ssld=False, **kwargs): model = RegNet( w_a=36.44, w_0=24, @@ -351,11 +374,12 @@ def RegNetY_200MF(**args): bot_mul=1.0, q=8, se_on=True, - **args) + **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["RegNetX_32GF"], use_ssld=use_ssld) return model -def RegNetY_4GF(**args): +def RegNetY_4GF(pretrained=False, use_ssld=False, **kwargs): model = RegNet( w_a=31.41, w_0=96, @@ -365,11 +389,12 @@ def RegNetY_4GF(**args): bot_mul=1.0, q=8, se_on=True, - **args) + **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["RegNetX_32GF"], use_ssld=use_ssld) return model -def RegNetY_32GF(**args): +def RegNetY_32GF(pretrained=False, use_ssld=False, **kwargs): model = RegNet( w_a=115.89, w_0=232, @@ -379,5 +404,6 @@ def RegNetY_32GF(**args): bot_mul=1.0, q=8, se_on=True, - **args) + **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["RegNetX_32GF"], use_ssld=use_ssld) return model diff --git a/ppcls/arch/backbone/model_zoo/repvgg.py b/ppcls/arch/backbone/model_zoo/repvgg.py index ec43d21d9c917859816fbd073a4fe7521cc08947..2447fbe251e9baa35cc186618422fd710366dcc8 100644 --- a/ppcls/arch/backbone/model_zoo/repvgg.py +++ b/ppcls/arch/backbone/model_zoo/repvgg.py @@ -2,22 +2,29 @@ import paddle.nn as nn import paddle import numpy as np -__all__ = [ - 'RepVGG', - 'RepVGG_A0', - 'RepVGG_A1', - 'RepVGG_A2', - 'RepVGG_B0', - 'RepVGG_B1', - 'RepVGG_B2', - 'RepVGG_B3', - 'RepVGG_B1g2', - 'RepVGG_B1g4', - 'RepVGG_B2g2', - 'RepVGG_B2g4', - 'RepVGG_B3g2', - 'RepVGG_B3g4', -] +from ppcls.utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url + +MODEL_URLS = {"RepVGG_A0": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/RepVGG_A0_pretrained.pdparams", + "RepVGG_A1": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/RepVGG_A1_pretrained.pdparams", + "RepVGG_A2": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/RepVGG_A2_pretrained.pdparams", + "RepVGG_B0": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/RepVGG_B0_pretrained.pdparams", + "RepVGG_B1": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/RepVGG_B1_pretrained.pdparams", + "RepVGG_B2": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/RepVGG_B2_pretrained.pdparams", + "RepVGG_B3": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/RepVGG_B3_pretrained.pdparams", + "RepVGG_B1g2": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/RepVGG_B1g2_pretrained.pdparams", + "RepVGG_B1g4": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/RepVGG_B1g4_pretrained.pdparams", + "RepVGG_B2g2": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/RepVGG_B2g2_pretrained.pdparams", + "RepVGG_B2g4": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/RepVGG_B2g4_pretrained.pdparams", + "RepVGG_B3g2": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/RepVGG_B3g2_pretrained.pdparams", + "RepVGG_B3g4": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/RepVGG_B3g4_pretrained.pdparams", + } + +__all__ = list(MODEL_URLS.keys()) + + +optional_groupwise_layers = [2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26] +g2_map = {l: 2 for l in optional_groupwise_layers} +g4_map = {l: 4 for l in optional_groupwise_layers} class ConvBN(nn.Layer): @@ -230,110 +237,144 @@ class RepVGG(nn.Layer): return out -optional_groupwise_layers = [2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26] -g2_map = {l: 2 for l in optional_groupwise_layers} -g4_map = {l: 4 for l in optional_groupwise_layers} - - -def RepVGG_A0(**kwargs): - return RepVGG( +def _load_pretrained(pretrained, model, model_url, use_ssld=False): + if pretrained is False: + pass + elif pretrained is True: + load_dygraph_pretrain_from_url(model, model_url, use_ssld=use_ssld) + elif isinstance(pretrained, str): + load_dygraph_pretrain(model, pretrained) + else: + raise RuntimeError( + "pretrained type is not available. Please use `string` or `boolean` type." + ) + + +def RepVGG_A0(pretrained=False, use_ssld=False, **kwargs): + model = RepVGG( num_blocks=[2, 4, 14, 1], width_multiplier=[0.75, 0.75, 0.75, 2.5], override_groups_map=None, **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["RepVGG_A0"], use_ssld=use_ssld) + return model -def RepVGG_A1(**kwargs): - return RepVGG( +def RepVGG_A1(pretrained=False, use_ssld=False, **kwargs): + model = RepVGG( num_blocks=[2, 4, 14, 1], width_multiplier=[1, 1, 1, 2.5], override_groups_map=None, **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["RepVGG_A1"], use_ssld=use_ssld) + return model -def RepVGG_A2(**kwargs): - return RepVGG( +def RepVGG_A2(pretrained=False, use_ssld=False, **kwargs): + model = RepVGG( num_blocks=[2, 4, 14, 1], width_multiplier=[1.5, 1.5, 1.5, 2.75], override_groups_map=None, **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["RepVGG_A2"], use_ssld=use_ssld) + return model -def RepVGG_B0(**kwargs): - return RepVGG( +def RepVGG_B0(pretrained=False, use_ssld=False, **kwargs): + model = RepVGG( num_blocks=[4, 6, 16, 1], width_multiplier=[1, 1, 1, 2.5], override_groups_map=None, **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["RepVGG_B0"], use_ssld=use_ssld) + return model -def RepVGG_B1(**kwargs): - return RepVGG( +def RepVGG_B1(pretrained=False, use_ssld=False, **kwargs): + model = RepVGG( num_blocks=[4, 6, 16, 1], width_multiplier=[2, 2, 2, 4], override_groups_map=None, **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["RepVGG_B1"], use_ssld=use_ssld) + return model -def RepVGG_B1g2(**kwargs): - return RepVGG( +def RepVGG_B1g2(pretrained=False, use_ssld=False, **kwargs): + model = RepVGG( num_blocks=[4, 6, 16, 1], width_multiplier=[2, 2, 2, 4], override_groups_map=g2_map, **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["RepVGG_B1g2"], use_ssld=use_ssld) + return model -def RepVGG_B1g4(**kwargs): - return RepVGG( +def RepVGG_B1g4(pretrained=False, use_ssld=False, **kwargs): + model = RepVGG( num_blocks=[4, 6, 16, 1], width_multiplier=[2, 2, 2, 4], override_groups_map=g4_map, **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["RepVGG_B1g4"], use_ssld=use_ssld) + return model -def RepVGG_B2(**kwargs): - return RepVGG( +def RepVGG_B2(pretrained=False, use_ssld=False, **kwargs): + model = RepVGG( num_blocks=[4, 6, 16, 1], width_multiplier=[2.5, 2.5, 2.5, 5], override_groups_map=None, **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["RepVGG_B2"], use_ssld=use_ssld) + return model -def RepVGG_B2g2(**kwargs): - return RepVGG( +def RepVGG_B2g2(pretrained=False, use_ssld=False, **kwargs): + model = RepVGG( num_blocks=[4, 6, 16, 1], width_multiplier=[2.5, 2.5, 2.5, 5], override_groups_map=g2_map, **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["RepVGG_B2g2"], use_ssld=use_ssld) + return model -def RepVGG_B2g4(**kwargs): - return RepVGG( +def RepVGG_B2g4(pretrained=False, use_ssld=False, **kwargs): + model = RepVGG( num_blocks=[4, 6, 16, 1], width_multiplier=[2.5, 2.5, 2.5, 5], override_groups_map=g4_map, **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["RepVGG_B2g4"], use_ssld=use_ssld) + return model -def RepVGG_B3(**kwargs): - return RepVGG( +def RepVGG_B3(pretrained=False, use_ssld=False, **kwargs): + model = RepVGG( num_blocks=[4, 6, 16, 1], width_multiplier=[3, 3, 3, 5], override_groups_map=None, **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["RepVGG_B3"], use_ssld=use_ssld) + return model -def RepVGG_B3g2(**kwargs): - return RepVGG( +def RepVGG_B3g2(pretrained=False, use_ssld=False, **kwargs): + model = RepVGG( num_blocks=[4, 6, 16, 1], width_multiplier=[3, 3, 3, 5], override_groups_map=g2_map, **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["RepVGG_B3g2"], use_ssld=use_ssld) + return model -def RepVGG_B3g4(**kwargs): - return RepVGG( +def RepVGG_B3g4(pretrained=False, use_ssld=False, **kwargs): + model = RepVGG( num_blocks=[4, 6, 16, 1], width_multiplier=[3, 3, 3, 5], override_groups_map=g4_map, **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["RepVGG_B3g4"], use_ssld=use_ssld) + return model diff --git a/ppcls/arch/backbone/model_zoo/res2net.py b/ppcls/arch/backbone/model_zoo/res2net.py index c03d34ea8bdc34b201dac047a9a0e777530e85ba..15a9427c24ce23d27d97178a83beb074635ab86d 100644 --- a/ppcls/arch/backbone/model_zoo/res2net.py +++ b/ppcls/arch/backbone/model_zoo/res2net.py @@ -27,11 +27,13 @@ from paddle.nn.initializer import Uniform import math -__all__ = [ - "Res2Net50_48w_2s", "Res2Net50_26w_4s", "Res2Net50_14w_8s", - "Res2Net50_48w_2s", "Res2Net50_26w_6s", "Res2Net50_26w_8s", - "Res2Net101_26w_4s", "Res2Net152_26w_4s", "Res2Net200_26w_4s" -] +from ppcls.utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url + +MODEL_URLS = {"Res2Net50_26w_4s": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/Res2Net50_26w_4s_pretrained.pdparams", + "Res2Net50_14w_8s": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/Res2Net50_14w_8s_pretrained.pdparams", + } + +__all__ = list(MODEL_URLS.keys()) class ConvBNLayer(nn.Layer): @@ -232,41 +234,26 @@ class Res2Net(nn.Layer): return y -def Res2Net50_48w_2s(**args): - model = Res2Net(layers=50, scales=2, width=48, **args) - return model - - -def Res2Net50_26w_4s(**args): - model = Res2Net(layers=50, scales=4, width=26, **args) - return model - - -def Res2Net50_14w_8s(**args): - model = Res2Net(layers=50, scales=8, width=14, **args) - return model - - -def Res2Net50_26w_6s(**args): - model = Res2Net(layers=50, scales=6, width=26, **args) +def _load_pretrained(pretrained, model, model_url, use_ssld=False): + if pretrained is False: + pass + elif pretrained is True: + load_dygraph_pretrain_from_url(model, model_url, use_ssld=use_ssld) + elif isinstance(pretrained, str): + load_dygraph_pretrain(model, pretrained) + else: + raise RuntimeError( + "pretrained type is not available. Please use `string` or `boolean` type." + ) + + +def Res2Net50_26w_4s(pretrained=False, use_ssld=False, **kwargs): + model = Res2Net(layers=50, scales=4, width=26, **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["Res2Net50_26w_4s"], use_ssld=use_ssld) return model -def Res2Net50_26w_8s(**args): - model = Res2Net(layers=50, scales=8, width=26, **args) - return model - - -def Res2Net101_26w_4s(**args): - model = Res2Net(layers=101, scales=4, width=26, **args) - return model - - -def Res2Net152_26w_4s(**args): - model = Res2Net(layers=152, scales=4, width=26, **args) - return model - - -def Res2Net200_26w_4s(**args): - model = Res2Net(layers=200, scales=4, width=26, **args) - return model +def Res2Net50_14w_8s(pretrained=False, use_ssld=False, **kwargs): + model = Res2Net(layers=50, scales=8, width=14, **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["Res2Net50_14w_8s"], use_ssld=use_ssld) + return model \ No newline at end of file diff --git a/ppcls/arch/backbone/model_zoo/res2net_vd.py b/ppcls/arch/backbone/model_zoo/res2net_vd.py index 9961d520f4544126e94f925654a3d4a0b9e686ee..28ab03a01c1986997d750ab3183c6add6ec7019a 100644 --- a/ppcls/arch/backbone/model_zoo/res2net_vd.py +++ b/ppcls/arch/backbone/model_zoo/res2net_vd.py @@ -27,11 +27,14 @@ from paddle.nn.initializer import Uniform import math -__all__ = [ - "Res2Net50_vd_48w_2s", "Res2Net50_vd_26w_4s", "Res2Net50_vd_14w_8s", - "Res2Net50_vd_48w_2s", "Res2Net50_vd_26w_6s", "Res2Net50_vd_26w_8s", - "Res2Net101_vd_26w_4s", "Res2Net152_vd_26w_4s", "Res2Net200_vd_26w_4s" -] +from ppcls.utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url + +MODEL_URLS = {"Res2Net50_vd_26w_4s": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/Res2Net50_vd_26w_4s_pretrained.pdparams", + "Res2Net101_vd_26w_4s": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/Res2Net101_vd_26w_4s_pretrained.pdparams", + "Res2Net200_vd_26w_4s": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/Res2Net200_vd_26w_4s_pretrained.pdparams", + } + +__all__ = list(MODEL_URLS.keys()) class ConvBNLayer(nn.Layer): @@ -255,41 +258,32 @@ class Res2Net_vd(nn.Layer): return y -def Res2Net50_vd_48w_2s(**args): - model = Res2Net_vd(layers=50, scales=2, width=48, **args) - return model - - -def Res2Net50_vd_26w_4s(**args): - model = Res2Net_vd(layers=50, scales=4, width=26, **args) - return model - - -def Res2Net50_vd_14w_8s(**args): - model = Res2Net_vd(layers=50, scales=8, width=14, **args) - return model - - -def Res2Net50_vd_26w_6s(**args): - model = Res2Net_vd(layers=50, scales=6, width=26, **args) - return model +def _load_pretrained(pretrained, model, model_url, use_ssld=False): + if pretrained is False: + pass + elif pretrained is True: + load_dygraph_pretrain_from_url(model, model_url, use_ssld=use_ssld) + elif isinstance(pretrained, str): + load_dygraph_pretrain(model, pretrained) + else: + raise RuntimeError( + "pretrained type is not available. Please use `string` or `boolean` type." + ) -def Res2Net50_vd_26w_8s(**args): - model = Res2Net_vd(layers=50, scales=8, width=26, **args) +def Res2Net50_vd_26w_4s(pretrained=False, use_ssld=False, **kwargs): + model = Res2Net_vd(layers=50, scales=4, width=26, **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["Res2Net50_vd_26w_4s"], use_ssld=use_ssld) return model -def Res2Net101_vd_26w_4s(**args): - model = Res2Net_vd(layers=101, scales=4, width=26, **args) +def Res2Net101_vd_26w_4s(pretrained=False, use_ssld=False, **kwargs): + model = Res2Net_vd(layers=101, scales=4, width=26, **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["Res2Net101_vd_26w_4s"], use_ssld=use_ssld) return model -def Res2Net152_vd_26w_4s(**args): - model = Res2Net_vd(layers=152, scales=4, width=26, **args) - return model - - -def Res2Net200_vd_26w_4s(**args): - model = Res2Net_vd(layers=200, scales=4, width=26, **args) - return model +def Res2Net200_vd_26w_4s(pretrained=False, use_ssld=False, **kwargs): + model = Res2Net_vd(layers=200, scales=4, width=26, **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["Res2Net200_vd_26w_4s"], use_ssld=use_ssld) + return model \ No newline at end of file diff --git a/ppcls/arch/backbone/model_zoo/resnest.py b/ppcls/arch/backbone/model_zoo/resnest.py index 4eeefddf44cc1ca89ed896e9556f191a13470b4c..3160095ef52a13b0db12b1da437a4bcd9c68c6e4 100644 --- a/ppcls/arch/backbone/model_zoo/resnest.py +++ b/ppcls/arch/backbone/model_zoo/resnest.py @@ -27,7 +27,14 @@ from paddle.nn import Conv2D, BatchNorm, Linear, Dropout from paddle.nn import AdaptiveAvgPool2D, MaxPool2D, AvgPool2D from paddle.regularizer import L2Decay -__all__ = ["ResNeSt50_fast_1s1x64d", "ResNeSt50", "ResNeSt101"] +from ppcls.utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url + +MODEL_URLS = {"ResNeSt50_fast_1s1x64d": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/ResNeSt50_fast_1s1x64d_pretrained.pdparams", + "ResNeSt50": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/ResNeSt50_pretrained.pdparams", + "ResNeSt101": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/ResNeSt101_pretrained.pdparams", + } + +__all__ = list(MODEL_URLS.keys()) class ConvBNLayer(nn.Layer): @@ -656,8 +663,21 @@ class ResNeSt(nn.Layer): x = self.out(x) return x - -def ResNeSt50_fast_1s1x64d(**args): + +def _load_pretrained(pretrained, model, model_url, use_ssld=False): + if pretrained is False: + pass + elif pretrained is True: + load_dygraph_pretrain_from_url(model, model_url, use_ssld=use_ssld) + elif isinstance(pretrained, str): + load_dygraph_pretrain(model, pretrained) + else: + raise RuntimeError( + "pretrained type is not available. Please use `string` or `boolean` type." + ) + + +def ResNeSt50_fast_1s1x64d(pretrained=False, use_ssld=False, **kwargs): model = ResNeSt( layers=[3, 4, 6, 3], radix=1, @@ -669,11 +689,12 @@ def ResNeSt50_fast_1s1x64d(**args): avd=True, avd_first=True, final_drop=0.0, - **args) + **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["ResNeSt50_fast_1s1x64d"], use_ssld=use_ssld) return model -def ResNeSt50(**args): +def ResNeSt50(pretrained=False, use_ssld=False, **kwargs): model = ResNeSt( layers=[3, 4, 6, 3], radix=2, @@ -685,11 +706,12 @@ def ResNeSt50(**args): avd=True, avd_first=False, final_drop=0.0, - **args) + **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["ResNeSt50"], use_ssld=use_ssld) return model -def ResNeSt101(**args): +def ResNeSt101(pretrained=False, use_ssld=False, **kwargs): model = ResNeSt( layers=[3, 4, 23, 3], radix=2, @@ -701,5 +723,6 @@ def ResNeSt101(**args): avd=True, avd_first=False, final_drop=0.0, - **args) + **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["ResNeSt101"], use_ssld=use_ssld) return model diff --git a/ppcls/arch/backbone/model_zoo/resnet.py b/ppcls/arch/backbone/model_zoo/resnet.py index 879bbd35509d8f9a9382114ee55d26ea7ebf29ef..5f95de99f164e5a5583a987c58a41e57c9c60295 100644 --- a/ppcls/arch/backbone/model_zoo/resnet.py +++ b/ppcls/arch/backbone/model_zoo/resnet.py @@ -27,7 +27,16 @@ from paddle.nn.initializer import Uniform import math -__all__ = ["ResNet18", "ResNet34", "ResNet50", "ResNet101", "ResNet152"] +from ppcls.utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url + +MODEL_URLS = {"ResNet18": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/ResNet18_pretrained.pdparams", + "ResNet34": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/ResNet34_pretrained.pdparams", + "ResNet50": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/ResNet50_pretrained.pdparams", + "ResNet101": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/ResNet101_pretrained.pdparams", + "ResNet152": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/ResNet152_pretrained.pdparams", + } + +__all__ = list(MODEL_URLS.keys()) class ConvBNLayer(nn.Layer): @@ -290,27 +299,45 @@ class ResNet(nn.Layer): y = self.out(y) return y - -def ResNet18(**args): - model = ResNet(layers=18, **args) + +def _load_pretrained(pretrained, model, model_url, use_ssld=False): + if pretrained is False: + pass + elif pretrained is True: + load_dygraph_pretrain_from_url(model, model_url, use_ssld=use_ssld) + elif isinstance(pretrained, str): + load_dygraph_pretrain(model, pretrained) + else: + raise RuntimeError( + "pretrained type is not available. Please use `string` or `boolean` type." + ) + + +def ResNet18(pretrained=False, use_ssld=False, **kwargs): + model = ResNet(layers=18, **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["ResNet18"], use_ssld=use_ssld) return model -def ResNet34(**args): - model = ResNet(layers=34, **args) +def ResNet34(pretrained=False, use_ssld=False, **kwargs): + model = ResNet(layers=34, **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["ResNet34"], use_ssld=use_ssld) return model -def ResNet50(**args): - model = ResNet(layers=50, **args) +def ResNet50(pretrained=False, use_ssld=False, **kwargs): + model = ResNet(layers=50, **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["ResNet50"], use_ssld=use_ssld) return model -def ResNet101(**args): - model = ResNet(layers=101, **args) +def ResNet101(pretrained=False, use_ssld=False, **kwargs): + model = ResNet(layers=101, **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["ResNet101"], use_ssld=use_ssld) return model -def ResNet152(**args): - model = ResNet(layers=152, **args) +def ResNet152(pretrained=False, use_ssld=False, **kwargs): + model = ResNet(layers=152, **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["ResNet152"], use_ssld=use_ssld) return model diff --git a/ppcls/arch/backbone/model_zoo/resnet_vc.py b/ppcls/arch/backbone/model_zoo/resnet_vc.py index 5a9374c1584649dc886f194338c534cc3278c1cc..53b9f8d5ebb3a44330389bbe0484235371c4f160 100644 --- a/ppcls/arch/backbone/model_zoo/resnet_vc.py +++ b/ppcls/arch/backbone/model_zoo/resnet_vc.py @@ -27,9 +27,13 @@ from paddle.nn.initializer import Uniform import math -__all__ = [ - "ResNet18_vc", "ResNet34_vc", "ResNet50_vc", "ResNet101_vc", "ResNet152_vc" -] +from ppcls.utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url + +MODEL_URLS = { + "ResNet50_vc": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/ResNet50_vc_pretrained.pdparams", + } + +__all__ = list(MODEL_URLS.keys()) class ConvBNLayer(nn.Layer): @@ -283,27 +287,22 @@ class ResNet_vc(nn.Layer): y = self.out(y) return y - -def ResNet18_vc(**args): - model = ResNet_vc(layers=18, **args) - return model - - -def ResNet34_vc(**args): - model = ResNet_vc(layers=34, **args) - return model - - -def ResNet50_vc(**args): - model = ResNet_vc(layers=50, **args) + +def _load_pretrained(pretrained, model, model_url, use_ssld=False): + if pretrained is False: + pass + elif pretrained is True: + load_dygraph_pretrain_from_url(model, model_url, use_ssld=use_ssld) + elif isinstance(pretrained, str): + load_dygraph_pretrain(model, pretrained) + else: + raise RuntimeError( + "pretrained type is not available. Please use `string` or `boolean` type." + ) + + +def ResNet50_vc(pretrained=False, use_ssld=False, **kwargs): + model = ResNet_vc(layers=50, **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["ResNet50_vc"], use_ssld=use_ssld) return model - -def ResNet101_vc(**args): - model = ResNet_vc(layers=101, **args) - return model - - -def ResNet152_vc(**args): - model = ResNet_vc(layers=152, **args) - return model diff --git a/ppcls/arch/backbone/model_zoo/resnet_vd.py b/ppcls/arch/backbone/model_zoo/resnet_vd.py index 5867261df341d00df15fe4c2e4a20584b6b0388d..cc491ad9dad0125fb17bbcc4722cf39cb45adee8 100644 --- a/ppcls/arch/backbone/model_zoo/resnet_vd.py +++ b/ppcls/arch/backbone/model_zoo/resnet_vd.py @@ -27,9 +27,18 @@ from paddle.nn.initializer import Uniform import math -__all__ = [ - "ResNet18_vd", "ResNet34_vd", "ResNet50_vd", "ResNet101_vd", "ResNet152_vd" -] +from ppcls.utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url + +MODEL_URLS = { + "ResNet18_vd": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/ResNet18_vd_pretrained.pdparams", + "ResNet34_vd": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/ResNet34_vd_pretrained.pdparams", + "ResNet50_vd": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/ResNet50_vd_pretrained.pdparams", + "ResNet101_vd": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/ResNet101_vd_pretrained.pdparams", + "ResNet152_vd": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/ResNet152_vd_pretrained.pdparams", + "ResNet200_vd": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/ResNet200_vd_pretrained.pdparams", + } + +__all__ = list(MODEL_URLS.keys()) class ConvBNLayer(nn.Layer): @@ -324,31 +333,50 @@ class ResNet_vd(nn.Layer): return y -def ResNet18_vd(**args): - model = ResNet_vd(layers=18, **args) +def _load_pretrained(pretrained, model, model_url, use_ssld=False): + if pretrained is False: + pass + elif pretrained is True: + load_dygraph_pretrain_from_url(model, model_url, use_ssld=use_ssld) + elif isinstance(pretrained, str): + load_dygraph_pretrain(model, pretrained) + else: + raise RuntimeError( + "pretrained type is not available. Please use `string` or `boolean` type." + ) + + +def ResNet18_vd(pretrained=False, use_ssld=False, **kwargs): + model = ResNet_vd(layers=18, **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["ResNet18_vd"], use_ssld=use_ssld) return model -def ResNet34_vd(**args): - model = ResNet_vd(layers=34, **args) +def ResNet34_vd(pretrained=False, use_ssld=False, **kwargs): + model = ResNet_vd(layers=34, **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["ResNet34_vd"], use_ssld=use_ssld) return model -def ResNet50_vd(**args): - model = ResNet_vd(layers=50, **args) +def ResNet50_vd(pretrained=False, use_ssld=False, **kwargs): + model = ResNet_vd(layers=50, **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["ResNet50_vd"], use_ssld=use_ssld) return model -def ResNet101_vd(**args): - model = ResNet_vd(layers=101, **args) +def ResNet101_vd(pretrained=False, use_ssld=False, **kwargs): + model = ResNet_vd(layers=101, **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["ResNet101_vd"], use_ssld=use_ssld) return model -def ResNet152_vd(**args): - model = ResNet_vd(layers=152, **args) +def ResNet152_vd(pretrained=False, use_ssld=False, **kwargs): + model = ResNet_vd(layers=152, **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["ResNet152_vd"], use_ssld=use_ssld) return model -def ResNet200_vd(**args): - model = ResNet_vd(layers=200, **args) +def ResNet200_vd(pretrained=False, use_ssld=False, **kwargs): + model = ResNet_vd(layers=200, **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["ResNet200_vd"], use_ssld=use_ssld) return model diff --git a/ppcls/arch/backbone/model_zoo/resnext.py b/ppcls/arch/backbone/model_zoo/resnext.py index 66ab40f9b4634413ce1d14d0fede8536d3905bf8..5104b4cba9cb45e866a222a5e981de6f04627f20 100644 --- a/ppcls/arch/backbone/model_zoo/resnext.py +++ b/ppcls/arch/backbone/model_zoo/resnext.py @@ -27,10 +27,18 @@ from paddle.nn.initializer import Uniform import math -__all__ = [ - "ResNeXt50_32x4d", "ResNeXt50_64x4d", "ResNeXt101_32x4d", - "ResNeXt101_64x4d", "ResNeXt152_32x4d", "ResNeXt152_64x4d" -] +from ppcls.utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url + +MODEL_URLS = { + "ResNeXt50_32x4d": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/ResNeXt50_32x4d_pretrained.pdparams", + "ResNeXt50_64x4d": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/ResNeXt50_64x4d_pretrained.pdparams", + "ResNeXt101_32x4d": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/ResNeXt101_32x4d_pretrained.pdparams", + "ResNeXt101_64x4d": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/ResNeXt101_64x4d_pretrained.pdparams", + "ResNeXt152_32x4d": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/ResNeXt152_32x4d_pretrained.pdparams", + "ResNeXt152_64x4d": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/ResNeXt152_64x4d_pretrained.pdparams", + } + +__all__ = list(MODEL_URLS.keys()) class ConvBNLayer(nn.Layer): @@ -222,32 +230,51 @@ class ResNeXt(nn.Layer): y = self.out(y) return y - -def ResNeXt50_32x4d(**args): - model = ResNeXt(layers=50, cardinality=32, **args) + +def _load_pretrained(pretrained, model, model_url, use_ssld=False): + if pretrained is False: + pass + elif pretrained is True: + load_dygraph_pretrain_from_url(model, model_url, use_ssld=use_ssld) + elif isinstance(pretrained, str): + load_dygraph_pretrain(model, pretrained) + else: + raise RuntimeError( + "pretrained type is not available. Please use `string` or `boolean` type." + ) + + +def ResNeXt50_32x4d(pretrained=False, use_ssld=False, **kwargs): + model = ResNeXt(layers=50, cardinality=32, **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["ResNeXt50_32x4d"], use_ssld=use_ssld) return model -def ResNeXt50_64x4d(**args): - model = ResNeXt(layers=50, cardinality=64, **args) +def ResNeXt50_64x4d(pretrained=False, use_ssld=False, **kwargs): + model = ResNeXt(layers=50, cardinality=64, **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["ResNeXt50_64x4d"], use_ssld=use_ssld) return model -def ResNeXt101_32x4d(**args): - model = ResNeXt(layers=101, cardinality=32, **args) +def ResNeXt101_32x4d(pretrained=False, use_ssld=False, **kwargs): + model = ResNeXt(layers=101, cardinality=32, **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["ResNeXt101_32x4d"], use_ssld=use_ssld) return model -def ResNeXt101_64x4d(**args): - model = ResNeXt(layers=101, cardinality=64, **args) +def ResNeXt101_64x4d(pretrained=False, use_ssld=False, **kwargs): + model = ResNeXt(layers=101, cardinality=64, **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["ResNeXt101_64x4d"], use_ssld=use_ssld) return model -def ResNeXt152_32x4d(**args): - model = ResNeXt(layers=152, cardinality=32, **args) +def ResNeXt152_32x4d(pretrained=False, use_ssld=False, **kwargs): + model = ResNeXt(layers=152, cardinality=32, **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["ResNeXt152_32x4d"], use_ssld=use_ssld) return model -def ResNeXt152_64x4d(**args): - model = ResNeXt(layers=152, cardinality=64, **args) +def ResNeXt152_64x4d(pretrained=False, use_ssld=False, **kwargs): + model = ResNeXt(layers=152, cardinality=64, **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["ResNeXt152_64x4d"], use_ssld=use_ssld) return model diff --git a/ppcls/arch/backbone/model_zoo/resnext101_wsl.py b/ppcls/arch/backbone/model_zoo/resnext101_wsl.py index 7130812b1092c6974e0da6042d46e0c82be57e6f..e530a9a2b750ba547ea6f1fafb520c050da5b63f 100644 --- a/ppcls/arch/backbone/model_zoo/resnext101_wsl.py +++ b/ppcls/arch/backbone/model_zoo/resnext101_wsl.py @@ -6,10 +6,18 @@ from paddle.nn import Conv2D, BatchNorm, Linear, Dropout from paddle.nn import AdaptiveAvgPool2D, MaxPool2D, AvgPool2D from paddle.nn.initializer import Uniform -__all__ = [ - "ResNeXt101_32x8d_wsl", "ResNeXt101_32x16d_wsl", "ResNeXt101_32x32d_wsl", - "ResNeXt101_32x48d_wsl" -] +from ppcls.utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url + +MODEL_URLS = { + "ResNeXt101_32x8d_wsl": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/ResNeXt101_32x8d_wsl_pretrained.pdparams", + "ResNeXt101_32x16d_wsl": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/ResNeXt101_32x816_wsl_pretrained.pdparams", + "ResNeXt101_32x32d_wsl": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/ResNeXt101_32x32d_wsl_pretrained.pdparams", + "ResNeXt101_32x48d_wsl": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/ResNeXt101_32x48d_wsl_pretrained.pdparams", + + } + +__all__ = list(MODEL_URLS.keys()) + class ConvBNLayer(nn.Layer): @@ -426,22 +434,39 @@ class ResNeXt101WSL(nn.Layer): x = self._out(x) return x - -def ResNeXt101_32x8d_wsl(**args): - model = ResNeXt101WSL(cardinality=32, width=8, **args) + +def _load_pretrained(pretrained, model, model_url, use_ssld=False): + if pretrained is False: + pass + elif pretrained is True: + load_dygraph_pretrain_from_url(model, model_url, use_ssld=use_ssld) + elif isinstance(pretrained, str): + load_dygraph_pretrain(model, pretrained) + else: + raise RuntimeError( + "pretrained type is not available. Please use `string` or `boolean` type." + ) + + +def ResNeXt101_32x8d_wsl(pretrained=False, use_ssld=False, **kwargs): + model = ResNeXt101WSL(cardinality=32, width=8, **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["ResNeXt101_32x8d_wsl"], use_ssld=use_ssld) return model def ResNeXt101_32x16d_wsl(**args): - model = ResNeXt101WSL(cardinality=32, width=16, **args) + model = ResNeXt101WSL(cardinality=32, width=16, **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["ResNeXt101_32x16d_ws"], use_ssld=use_ssld) return model def ResNeXt101_32x32d_wsl(**args): - model = ResNeXt101WSL(cardinality=32, width=32, **args) + model = ResNeXt101WSL(cardinality=32, width=32, **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["ResNeXt101_32x32d_wsl"], use_ssld=use_ssld) return model def ResNeXt101_32x48d_wsl(**args): - model = ResNeXt101WSL(cardinality=32, width=48, **args) + model = ResNeXt101WSL(cardinality=32, width=48, **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["ResNeXt101_32x48d_wsl"], use_ssld=use_ssld) return model diff --git a/ppcls/arch/backbone/model_zoo/resnext_vd.py b/ppcls/arch/backbone/model_zoo/resnext_vd.py index a05b645232f788b3810205724c347c40073a7e8f..b14e265e909a3ac54c6db222063869316c5697e5 100644 --- a/ppcls/arch/backbone/model_zoo/resnext_vd.py +++ b/ppcls/arch/backbone/model_zoo/resnext_vd.py @@ -27,11 +27,18 @@ from paddle.nn.initializer import Uniform import math -__all__ = [ - "ResNeXt50_vd_32x4d", "ResNeXt50_vd_64x4d", "ResNeXt101_vd_32x4d", - "ResNeXt101_vd_64x4d", "ResNeXt152_vd_32x4d", "ResNeXt152_vd_64x4d" -] +from ppcls.utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url +MODEL_URLS = { + "ResNeXt50_vd_32x4d": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/ResNeXt50_vd_32x4d_pretrained.pdparams", + "ResNeXt50_vd_64x4d": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/ResNeXt50_vd_64x4d_pretrained.pdparams", + "ResNeXt101_vd_32x4d": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/ResNeXt101_vd_32x4d_pretrained.pdparams", + "ResNeXt101_vd_64x4d": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/ResNeXt101_vd_64x4d_pretrained.pdparams", + "ResNeXt152_vd_32x4d": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/ResNeXt152_vd_32x4d_pretrained.pdparams", + "ResNeXt152_vd_64x4d": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/ResNeXt152_vd_64x4d_pretrained.pdparams", + } + +__all__ = list(MODEL_URLS.keys()) class ConvBNLayer(nn.Layer): def __init__( @@ -235,32 +242,50 @@ class ResNeXt(nn.Layer): y = self.out(y) return y - -def ResNeXt50_vd_32x4d(**args): - model = ResNeXt(layers=50, cardinality=32, **args) +def _load_pretrained(pretrained, model, model_url, use_ssld=False): + if pretrained is False: + pass + elif pretrained is True: + load_dygraph_pretrain_from_url(model, model_url, use_ssld=use_ssld) + elif isinstance(pretrained, str): + load_dygraph_pretrain(model, pretrained) + else: + raise RuntimeError( + "pretrained type is not available. Please use `string` or `boolean` type." + ) + + +def ResNeXt50_vd_32x4d(pretrained=False, use_ssld=False, **kwargs): + model = ResNeXt(layers=50, cardinality=32, **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["ResNeXt50_vd_32x4d"], use_ssld=use_ssld) return model -def ResNeXt50_vd_64x4d(**args): - model = ResNeXt(layers=50, cardinality=64, **args) +def ResNeXt50_vd_64x4d(pretrained=False, use_ssld=False, **kwargs): + model = ResNeXt(layers=50, cardinality=64, **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["ResNeXt50_vd_64x4d"], use_ssld=use_ssld) return model -def ResNeXt101_vd_32x4d(**args): - model = ResNeXt(layers=101, cardinality=32, **args) +def ResNeXt101_vd_32x4d(pretrained=False, use_ssld=False, **kwargs): + model = ResNeXt(layers=101, cardinality=32, **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["ResNeXt101_vd_32x4d"], use_ssld=use_ssld) return model -def ResNeXt101_vd_64x4d(**args): - model = ResNeXt(layers=101, cardinality=64, **args) +def ResNeXt101_vd_64x4d(pretrained=False, use_ssld=False, **kwargs): + model = ResNeXt(layers=101, cardinality=64, **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["ResNeXt101_vd_64x4d"], use_ssld=use_ssld) return model -def ResNeXt152_vd_32x4d(**args): - model = ResNeXt(layers=152, cardinality=32, **args) +def ResNeXt152_vd_32x4d(pretrained=False, use_ssld=False, **kwargs): + model = ResNeXt(layers=152, cardinality=32, **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["ResNeXt152_vd_32x4d"], use_ssld=use_ssld) return model -def ResNeXt152_vd_64x4d(**args): - model = ResNeXt(layers=152, cardinality=64, **args) +def ResNeXt152_vd_64x4d(pretrained=False, use_ssld=False, **kwargs): + model = ResNeXt(layers=152, cardinality=64, **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["ResNeXt152_vd_64x4d"], use_ssld=use_ssld) return model diff --git a/ppcls/arch/backbone/model_zoo/rexnet.py b/ppcls/arch/backbone/model_zoo/rexnet.py index ac0382374f8f7d95799e7bd30828ed60246dc240..799826c9443ac4aace47143408652b83659fec28 100644 --- a/ppcls/arch/backbone/model_zoo/rexnet.py +++ b/ppcls/arch/backbone/model_zoo/rexnet.py @@ -22,9 +22,17 @@ from paddle import ParamAttr import paddle.nn as nn from math import ceil -__all__ = [ - "ReXNet_1_0", "ReXNet_1_3", "ReXNet_1_5", "ReXNet_2_0", "ReXNet_3_0" -] +from ppcls.utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url + +MODEL_URLS = { + "ReXNet_1_0": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/ReXNet_1_0_pretrained.pdparams", + "ReXNet_1_3": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/ReXNet_1_3_pretrained.pdparams", + "ReXNet_1_5": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/ReXNet_1_5_32x4d_pretrained.pdparams", + "ReXNet_2_0": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/ReXNet_2_0_pretrained.pdparams", + "ReXNet_3_0": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/ReXNet_3_0_pretrained.pdparams", + } + +__all__ = list(MODEL_URLS.keys()) def conv_bn_act(out, @@ -220,21 +228,44 @@ class ReXNetV1(nn.Layer): return x -def ReXNet_1_0(**args): - return ReXNetV1(width_mult=1.0, **args) +def _load_pretrained(pretrained, model, model_url, use_ssld=False): + if pretrained is False: + pass + elif pretrained is True: + load_dygraph_pretrain_from_url(model, model_url, use_ssld=use_ssld) + elif isinstance(pretrained, str): + load_dygraph_pretrain(model, pretrained) + else: + raise RuntimeError( + "pretrained type is not available. Please use `string` or `boolean` type." + ) + + +def ReXNet_1_0(pretrained=False, use_ssld=False, **kwargs): + model = ReXNetV1(width_mult=1.0, **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["ReXNet_1_0"], use_ssld=use_ssld) + return model -def ReXNet_1_3(**args): - return ReXNetV1(width_mult=1.3, **args) +def ReXNet_1_3(pretrained=False, use_ssld=False, **kwargs): + model = ReXNetV1(width_mult=1.3, **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["ReXNet_1_3"], use_ssld=use_ssld) + return model -def ReXNet_1_5(**args): - return ReXNetV1(width_mult=1.5, **args) +def ReXNet_1_5(pretrained=False, use_ssld=False, **kwargs): + model = ReXNetV1(width_mult=1.5, **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["ReXNet_1_5"], use_ssld=use_ssld) + return model -def ReXNet_2_0(**args): - return ReXNetV1(width_mult=2.0, **args) +def ReXNet_2_0(pretrained=False, use_ssld=False, **kwargs): + model = ReXNetV1(width_mult=2.0, **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["ReXNet_2_0"], use_ssld=use_ssld) + return model -def ReXNet_3_0(**args): - return ReXNetV1(width_mult=3.0, **args) +def ReXNet_3_0(pretrained=False, use_ssld=False, **kwargs): + model = ReXNetV1(width_mult=3.0, **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["ReXNet_3_0"], use_ssld=use_ssld) + return model \ No newline at end of file diff --git a/ppcls/arch/backbone/model_zoo/se_resnet_vd.py b/ppcls/arch/backbone/model_zoo/se_resnet_vd.py index 5b7a587cdc5cd910dd47ef035c35ea70de166fb5..cc48f8d362f609ec8a236c5845171eb1aff43e88 100644 --- a/ppcls/arch/backbone/model_zoo/se_resnet_vd.py +++ b/ppcls/arch/backbone/model_zoo/se_resnet_vd.py @@ -26,10 +26,16 @@ from paddle.nn.initializer import Uniform import math -__all__ = [ - "SE_ResNet18_vd", "SE_ResNet34_vd", "SE_ResNet50_vd", "SE_ResNet101_vd", - "SE_ResNet152_vd", "SE_ResNet200_vd" -] +from ppcls.utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url + +MODEL_URLS = { + "SE_ResNet18_vd": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/SE_ResNet18_vd_pretrained.pdparams", + "SE_ResNet34_vd": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/SE_ResNet34_vd_pretrained.pdparams", + "SE_ResNet50_vd": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/SE_ResNet50_vd_pretrained.pdparams", + + } + +__all__ = list(MODEL_URLS.keys()) class ConvBNLayer(nn.Layer): @@ -347,32 +353,33 @@ class SE_ResNet_vd(nn.Layer): y = self.out(y) return y - -def SE_ResNet18_vd(**args): - model = SE_ResNet_vd(layers=18, **args) - return model - - -def SE_ResNet34_vd(**args): - model = SE_ResNet_vd(layers=34, **args) - return model - - -def SE_ResNet50_vd(**args): - model = SE_ResNet_vd(layers=50, **args) - return model - - -def SE_ResNet101_vd(**args): - model = SE_ResNet_vd(layers=101, **args) + +def _load_pretrained(pretrained, model, model_url, use_ssld=False): + if pretrained is False: + pass + elif pretrained is True: + load_dygraph_pretrain_from_url(model, model_url, use_ssld=use_ssld) + elif isinstance(pretrained, str): + load_dygraph_pretrain(model, pretrained) + else: + raise RuntimeError( + "pretrained type is not available. Please use `string` or `boolean` type." + ) + + +def SE_ResNet18_vd(pretrained=False, use_ssld=False, **kwargs): + model = SE_ResNet_vd(layers=18, **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["SE_ResNet18_vd"], use_ssld=use_ssld) return model -def SE_ResNet152_vd(**args): - model = SE_ResNet_vd(layers=152, **args) +def SE_ResNet34_vd(pretrained=False, use_ssld=False, **kwargs): + model = SE_ResNet_vd(layers=34, **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["SE_ResNet34_vd"], use_ssld=use_ssld) return model -def SE_ResNet200_vd(**args): - model = SE_ResNet_vd(layers=200, **args) +def SE_ResNet50_vd(pretrained=False, use_ssld=False, **kwargs): + model = SE_ResNet_vd(layers=50, **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["SE_ResNet50_vd"], use_ssld=use_ssld) return model diff --git a/ppcls/arch/backbone/model_zoo/se_resnext.py b/ppcls/arch/backbone/model_zoo/se_resnext.py index df7cb021efdfb6365604d51df7cec3115c254fef..d873d81111cc48c1055dc203f6cab674ffb91b88 100644 --- a/ppcls/arch/backbone/model_zoo/se_resnext.py +++ b/ppcls/arch/backbone/model_zoo/se_resnext.py @@ -27,7 +27,16 @@ from paddle.nn.initializer import Uniform import math -__all__ = ["SE_ResNeXt50_32x4d", "SE_ResNeXt101_32x4d", "SE_ResNeXt152_64x4d"] +from ppcls.utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url + +MODEL_URLS = { + "SE_ResNeXt50_32x4d": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/SE_ResNeXt50_32x4d_pretrained.pdparams", + "SE_ResNeXt101_32x4d": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/SE_ResNeXt101_32x4d_pretrained.pdparams", + "SE_ResNeXt152_64x4d": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/SE_ResNeXt152_64x4d_pretrained.pdparams", + + } + +__all__ = list(MODEL_URLS.keys()) class ConvBNLayer(nn.Layer): @@ -301,17 +310,33 @@ class ResNeXt(nn.Layer): y = self.out(y) return y - -def SE_ResNeXt50_32x4d(**args): - model = ResNeXt(layers=50, cardinality=32, **args) + +def _load_pretrained(pretrained, model, model_url, use_ssld=False): + if pretrained is False: + pass + elif pretrained is True: + load_dygraph_pretrain_from_url(model, model_url, use_ssld=use_ssld) + elif isinstance(pretrained, str): + load_dygraph_pretrain(model, pretrained) + else: + raise RuntimeError( + "pretrained type is not available. Please use `string` or `boolean` type." + ) + + +def SE_ResNeXt50_32x4d(pretrained=False, use_ssld=False, **kwargs): + model = ResNeXt(layers=50, cardinality=32, **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["SE_ResNeXt50_32x4d"], use_ssld=use_ssld) return model -def SE_ResNeXt101_32x4d(**args): - model = ResNeXt(layers=101, cardinality=32, **args) +def SE_ResNeXt101_32x4d(pretrained=False, use_ssld=False, **kwargs): + model = ResNeXt(layers=101, cardinality=32, **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["SE_ResNeXt101_32x4d"], use_ssld=use_ssld) return model -def SE_ResNeXt152_64x4d(**args): - model = ResNeXt(layers=152, cardinality=64, **args) +def SE_ResNeXt152_64x4d(pretrained=False, use_ssld=False, **kwargs): + model = ResNeXt(layers=152, cardinality=64, **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["SE_ResNeXt152_64x4d"], use_ssld=use_ssld) return model diff --git a/ppcls/arch/backbone/model_zoo/se_resnext_vd.py b/ppcls/arch/backbone/model_zoo/se_resnext_vd.py index 56cbe83d24901f6a37a9a17a872462eadc3dc854..5e840f83d3c279e004782400388f662b3f589d39 100644 --- a/ppcls/arch/backbone/model_zoo/se_resnext_vd.py +++ b/ppcls/arch/backbone/model_zoo/se_resnext_vd.py @@ -27,7 +27,16 @@ from paddle.nn.initializer import Uniform import math -__all__ = ["SE_ResNeXt50_vd_32x4d", "SE_ResNeXt50_vd_32x4d", "SENet154_vd"] +from ppcls.utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url + +MODEL_URLS = { + "SE_ResNeXt50_vd_32x4d": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/SE_ResNeXt50_vd_32x4d_pretrained.pdparams", + "SE_ResNeXt50_vd_32x4d": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/SE_ResNeXt50_vd_32x4d_pretrained.pdparams", + "SENet154_vd": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/SENet154_vd_pretrained.pdparams", + + } + +__all__ = list(MODEL_URLS.keys()) class ConvBNLayer(nn.Layer): @@ -269,17 +278,33 @@ class ResNeXt(nn.Layer): y = self.out(y) return y - -def SE_ResNeXt50_vd_32x4d(**args): - model = ResNeXt(layers=50, cardinality=32, **args) + +def _load_pretrained(pretrained, model, model_url, use_ssld=False): + if pretrained is False: + pass + elif pretrained is True: + load_dygraph_pretrain_from_url(model, model_url, use_ssld=use_ssld) + elif isinstance(pretrained, str): + load_dygraph_pretrain(model, pretrained) + else: + raise RuntimeError( + "pretrained type is not available. Please use `string` or `boolean` type." + ) + + +def SE_ResNeXt50_vd_32x4d(pretrained=False, use_ssld=False, **kwargs): + model = ResNeXt(layers=50, cardinality=32, **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["SE_ResNeXt50_vd_32x4d"], use_ssld=use_ssld) return model -def SE_ResNeXt101_vd_32x4d(**args): - model = ResNeXt(layers=101, cardinality=32, **args) +def SE_ResNeXt101_vd_32x4d(pretrained=False, use_ssld=False, **kwargs): + model = ResNeXt(layers=101, cardinality=32, **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["SE_ResNeXt101_vd_32x4d"], use_ssld=use_ssld) return model -def SENet154_vd(**args): - model = ResNeXt(layers=152, cardinality=64, **args) +def SENet154_vd(pretrained=False, use_ssld=False, **kwargs): + model = ResNeXt(layers=152, cardinality=64, **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["SENet154_vd"], use_ssld=use_ssld) return model diff --git a/ppcls/arch/backbone/model_zoo/shufflenet_v2.py b/ppcls/arch/backbone/model_zoo/shufflenet_v2.py index ac8898f6cbbdd0569e2163b1eb1aa0b64fff445d..29abad66ef750dafdebf5cf91607f34743756e58 100644 --- a/ppcls/arch/backbone/model_zoo/shufflenet_v2.py +++ b/ppcls/arch/backbone/model_zoo/shufflenet_v2.py @@ -22,11 +22,19 @@ from paddle.nn import Layer, Conv2D, MaxPool2D, AdaptiveAvgPool2D, BatchNorm, Li from paddle.nn.initializer import KaimingNormal from paddle.nn.functional import swish -__all__ = [ - "ShuffleNetV2_x0_25", "ShuffleNetV2_x0_33", "ShuffleNetV2_x0_5", - "ShuffleNetV2_x1_0", "ShuffleNetV2_x1_5", "ShuffleNetV2_x2_0", - "ShuffleNetV2_swish" -] +from ppcls.utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_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): @@ -285,36 +293,56 @@ class ShuffleNet(Layer): return y -def ShuffleNetV2_x0_25(**args): - model = ShuffleNet(scale=0.25, **args) +def _load_pretrained(pretrained, model, model_url, use_ssld=False): + if pretrained is False: + pass + elif pretrained is True: + load_dygraph_pretrain_from_url(model, model_url, use_ssld=use_ssld) + elif isinstance(pretrained, str): + load_dygraph_pretrain(model, pretrained) + else: + raise RuntimeError( + "pretrained type is not available. Please use `string` or `boolean` type." + ) + + +def ShuffleNetV2_x0_25(pretrained=False, use_ssld=False, **kwargs): + model = ShuffleNet(scale=0.25, **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["ShuffleNetV2_x0_25"], use_ssld=use_ssld) return model -def ShuffleNetV2_x0_33(**args): - model = ShuffleNet(scale=0.33, **args) +def ShuffleNetV2_x0_33(pretrained=False, use_ssld=False, **kwargs): + model = ShuffleNet(scale=0.33, **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["ShuffleNetV2_x0_33"], use_ssld=use_ssld) return model -def ShuffleNetV2_x0_5(**args): - model = ShuffleNet(scale=0.5, **args) +def ShuffleNetV2_x0_5(pretrained=False, use_ssld=False, **kwargs): + model = ShuffleNet(scale=0.5, **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["ShuffleNetV2_x0_5"], use_ssld=use_ssld) return model -def ShuffleNetV2_x1_0(**args): - model = ShuffleNet(scale=1.0, **args) +def ShuffleNetV2_x1_0(pretrained=False, use_ssld=False, **kwargs): + model = ShuffleNet(scale=1.0, **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["ShuffleNetV2_x1_0"], use_ssld=use_ssld) return model -def ShuffleNetV2_x1_5(**args): - model = ShuffleNet(scale=1.5, **args) +def ShuffleNetV2_x1_5(pretrained=False, use_ssld=False, **kwargs): + model = ShuffleNet(scale=1.5, **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["ShuffleNetV2_x1_5"], use_ssld=use_ssld) return model -def ShuffleNetV2_x2_0(**args): - model = ShuffleNet(scale=2.0, **args) +def ShuffleNetV2_x2_0(pretrained=False, use_ssld=False, **kwargs): + model = ShuffleNet(scale=2.0, **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["ShuffleNetV2_x2_0"], use_ssld=use_ssld) return model -def ShuffleNetV2_swish(**args): - model = ShuffleNet(scale=1.0, act="swish", **args) +def ShuffleNetV2_swish(pretrained=False, use_ssld=False, **kwargs): + model = ShuffleNet(scale=1.0, act="swish", **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["ShuffleNetV2_swish"], use_ssld=use_ssld) return model diff --git a/ppcls/arch/backbone/model_zoo/squeezenet.py b/ppcls/arch/backbone/model_zoo/squeezenet.py index eb380f252ace998cc14072117911e7e1e8b0cc3a..a88a1bcff6acb4bc62289b739b571996892fc57d 100644 --- a/ppcls/arch/backbone/model_zoo/squeezenet.py +++ b/ppcls/arch/backbone/model_zoo/squeezenet.py @@ -1,3 +1,17 @@ +# 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 from paddle import ParamAttr import paddle.nn as nn @@ -5,7 +19,14 @@ import paddle.nn.functional as F from paddle.nn import Conv2D, BatchNorm, Linear, Dropout from paddle.nn import AdaptiveAvgPool2D, MaxPool2D, AvgPool2D -__all__ = ["SqueezeNet1_0", "SqueezeNet1_1"] +from ppcls.utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url + +MODEL_URLS = { + "SqueezeNet1_0": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/SqueezeNet1_0_pretrained.pdparams", + "SqueezeNet1_1": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/SqueezeNet1_1_pretrained.pdparams", + } + +__all__ = list(MODEL_URLS.keys()) class MakeFireConv(nn.Layer): @@ -143,12 +164,26 @@ class SqueezeNet(nn.Layer): x = paddle.squeeze(x, axis=[2, 3]) return x - -def SqueezeNet1_0(**args): - model = SqueezeNet(version="1.0", **args) +def _load_pretrained(pretrained, model, model_url, use_ssld=False): + if pretrained is False: + pass + elif pretrained is True: + load_dygraph_pretrain_from_url(model, model_url, use_ssld=use_ssld) + elif isinstance(pretrained, str): + load_dygraph_pretrain(model, pretrained) + else: + raise RuntimeError( + "pretrained type is not available. Please use `string` or `boolean` type." + ) + + +def SqueezeNet1_0(pretrained=False, use_ssld=False, **kwargs): + model = SqueezeNet(version="1.0", **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["SqueezeNet1_0"], use_ssld=use_ssld) return model -def SqueezeNet1_1(**args): - model = SqueezeNet(version="1.1", **args) +def SqueezeNet1_1(pretrained=False, use_ssld=False, **kwargs): + model = SqueezeNet(version="1.1", **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["SqueezeNet1_1"], use_ssld=use_ssld) return model diff --git a/ppcls/arch/backbone/model_zoo/swin_transformer.py b/ppcls/arch/backbone/model_zoo/swin_transformer.py index 97efbd1f341d69fd601bfb86697d4f1cb06ca022..a33bf588838ac51a4f52124cbb48bbc5fda4886d 100644 --- a/ppcls/arch/backbone/model_zoo/swin_transformer.py +++ b/ppcls/arch/backbone/model_zoo/swin_transformer.py @@ -21,6 +21,19 @@ from paddle.nn.initializer import TruncatedNormal, Constant from .vision_transformer import trunc_normal_, zeros_, ones_, to_2tuple, DropPath, Identity +from ppcls.utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url + +MODEL_URLS = { + "SwinTransformer_tiny_patch4_window7_224": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/SwinTransformer_tiny_patch4_window7_224_pretrained.pdparams", + "SwinTransformer_small_patch4_window7_224": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/SwinTransformer_small_patch4_window7_224_pretrained.pdparams", + "SwinTransformer_base_patch4_window7_224": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/SwinTransformer_base_patch4_window7_224_pretrained.pdparams", + "SwinTransformer_base_patch4_window12_384": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/SwinTransformer_base_patch4_window12_384_pretrained.pdparams", + "SwinTransformer_large_patch4_window7_224": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/SwinTransformer_large_patch4_window7_224_pretrained.pdparams", + "SwinTransformer_large_patch4_window12_384": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/SwinTransformer_large_patch4_window12_384_pretrained.pdparams", + } + +__all__ = list(MODEL_URLS.keys()) + class Mlp(nn.Layer): def __init__(self, @@ -716,40 +729,56 @@ class SwinTransformer(nn.Layer): flops += self.num_features * self.num_classes return flops - -def SwinTransformer_tiny_patch4_window7_224(**args): + +def _load_pretrained(pretrained, model, model_url, use_ssld=False): + if pretrained is False: + pass + elif pretrained is True: + load_dygraph_pretrain_from_url(model, model_url, use_ssld=use_ssld) + elif isinstance(pretrained, str): + load_dygraph_pretrain(model, pretrained) + else: + raise RuntimeError( + "pretrained type is not available. Please use `string` or `boolean` type." + ) + + +def SwinTransformer_tiny_patch4_window7_224(pretrained=False, use_ssld=False, **kwargs): model = SwinTransformer( embed_dim=96, depths=[2, 2, 6, 2], num_heads=[3, 6, 12, 24], window_size=7, drop_path_rate=0.2, - **args) + **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["SwinTransformer_tiny_patch4_window7_224"], use_ssld=use_ssld) return model -def SwinTransformer_small_patch4_window7_224(**args): +def SwinTransformer_small_patch4_window7_224(pretrained=False, use_ssld=False, **kwargs): model = SwinTransformer( embed_dim=96, depths=[2, 2, 18, 2], num_heads=[3, 6, 12, 24], window_size=7, - **args) + **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["SwinTransformer_small_patch4_window7_224"], use_ssld=use_ssld) return model -def SwinTransformer_base_patch4_window7_224(**args): +def SwinTransformer_base_patch4_window7_224(pretrained=False, use_ssld=False, **kwargs): model = SwinTransformer( embed_dim=128, depths=[2, 2, 18, 2], num_heads=[4, 8, 16, 32], window_size=7, drop_path_rate=0.5, - **args) + **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["SwinTransformer_base_patch4_window7_224"], use_ssld=use_ssld) return model -def SwinTransformer_base_patch4_window12_384(**args): +def SwinTransformer_base_patch4_window12_384(pretrained=False, use_ssld=False, **kwargs): model = SwinTransformer( img_size=384, embed_dim=128, @@ -757,26 +786,29 @@ def SwinTransformer_base_patch4_window12_384(**args): num_heads=[4, 8, 16, 32], window_size=12, drop_path_rate=0.5, # NOTE: do not appear in offical code - **args) + **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["SwinTransformer_base_patch4_window12_384"], use_ssld=use_ssld) return model -def SwinTransformer_large_patch4_window7_224(**args): +def SwinTransformer_large_patch4_window7_224(pretrained=False, use_ssld=False, **kwargs): model = SwinTransformer( embed_dim=192, depths=[2, 2, 18, 2], num_heads=[6, 12, 24, 48], window_size=7, - **args) + **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["SwinTransformer_large_patch4_window7_224"], use_ssld=use_ssld) return model -def SwinTransformer_large_patch4_window12_384(**args): +def SwinTransformer_large_patch4_window12_384(pretrained=False, use_ssld=False, **kwargs): model = SwinTransformer( img_size=384, embed_dim=192, depths=[2, 2, 18, 2], num_heads=[6, 12, 24, 48], window_size=12, - **args) + **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["SwinTransformer_large_patch4_window12_384"], use_ssld=use_ssld) return model diff --git a/ppcls/arch/backbone/model_zoo/tnt.py b/ppcls/arch/backbone/model_zoo/tnt.py index fd8cec842381f47907c7ce771a878a8175cb798f..61f1083e4cadd268ca2022a7908935db33138797 100644 --- a/ppcls/arch/backbone/model_zoo/tnt.py +++ b/ppcls/arch/backbone/model_zoo/tnt.py @@ -26,7 +26,7 @@ from ppcls.utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_f MODEL_URLS = { "TNT_small": - "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/model_zoo/TNT_small_pretrained.pdparams" + "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/TNT_small_pretrained.pdparams" } diff --git a/ppcls/arch/backbone/model_zoo/vgg.py b/ppcls/arch/backbone/model_zoo/vgg.py index fe00d9feead07d2775f7736df4e1fc92a2c4d57f..9e737590d2e0d821126745276248317177b2239d 100644 --- a/ppcls/arch/backbone/model_zoo/vgg.py +++ b/ppcls/arch/backbone/model_zoo/vgg.py @@ -5,7 +5,16 @@ import paddle.nn.functional as F from paddle.nn import Conv2D, BatchNorm, Linear, Dropout from paddle.nn import AdaptiveAvgPool2D, MaxPool2D, AvgPool2D -__all__ = ["VGG11", "VGG13", "VGG16", "VGG19"] +from ppcls.utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url + +MODEL_URLS = { + "VGG11": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/VGG11_pretrained.pdparams", + "VGG13": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/VGG13_pretrained.pdparams", + "VGG16": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/VGG16_pretrained.pdparams", + "VGG19": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/VGG19_pretrained.pdparams", + } + +__all__ = list(MODEL_URLS.keys()) class ConvBlock(nn.Layer): @@ -131,22 +140,40 @@ class VGGNet(nn.Layer): x = self._out(x) return x - -def VGG11(**args): - model = VGGNet(layers=11, **args) + + +def _load_pretrained(pretrained, model, model_url, use_ssld=False): + if pretrained is False: + pass + elif pretrained is True: + load_dygraph_pretrain_from_url(model, model_url, use_ssld=use_ssld) + elif isinstance(pretrained, str): + load_dygraph_pretrain(model, pretrained) + else: + raise RuntimeError( + "pretrained type is not available. Please use `string` or `boolean` type." + ) + + +def VGG11(pretrained, model, model_url, use_ssld=False, **kwargs): + model = VGGNet(layers=11, **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["VGG11"], use_ssld=use_ssld) return model -def VGG13(**args): - model = VGGNet(layers=13, **args) +def VGG13(pretrained, model, model_url, use_ssld=False, **kwargs): + model = VGGNet(layers=13, **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["VGG13"], use_ssld=use_ssld) return model -def VGG16(**args): - model = VGGNet(layers=16, **args) +def VGG16(pretrained, model, model_url, use_ssld=False, **kwargs): + model = VGGNet(layers=16, **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["VGG16"], use_ssld=use_ssld) return model -def VGG19(**args): - model = VGGNet(layers=19, **args) +def VGG19(pretrained, model, model_url, use_ssld=False, **kwargs): + model = VGGNet(layers=19, **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["VGG19"], use_ssld=use_ssld) return model diff --git a/ppcls/arch/backbone/model_zoo/vision_transformer.py b/ppcls/arch/backbone/model_zoo/vision_transformer.py index 22cc0ad2790a15aeef4a34c107b8eb27308aebb4..4bb39890f79fa407619475332475e79f09322b15 100644 --- a/ppcls/arch/backbone/model_zoo/vision_transformer.py +++ b/ppcls/arch/backbone/model_zoo/vision_transformer.py @@ -19,12 +19,22 @@ import paddle import paddle.nn as nn from paddle.nn.initializer import TruncatedNormal, Constant, Normal -__all__ = [ - "VisionTransformer", "ViT_small_patch16_224", "ViT_base_patch16_224", - "ViT_base_patch16_384", "ViT_base_patch32_384", "ViT_large_patch16_224", - "ViT_large_patch16_384", "ViT_large_patch32_384", "ViT_huge_patch16_224", - "ViT_huge_patch32_384" -] +from ppcls.utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url + +MODEL_URLS = { + "ViT_small_patch16_224": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/ViT_small_patch16_224_pretrained.pdparams", + "ViT_base_patch16_224": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/ViT_base_patch16_224_pretrained.pdparams", + "ViT_base_patch16_384": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/ViT_base_patch16_384_pretrained.pdparams", + "ViT_base_patch32_384": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/ViT_base_patch32_384_pretrained.pdparams", + "ViT_large_patch16_224": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/ViT_large_patch16_224_pretrained.pdparams", + "ViT_large_patch16_384": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/ViT_large_patch16_384_pretrained.pdparams", + "ViT_large_patch32_384": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/ViT_large_patch32_384_pretrained.pdparams", + "ViT_huge_patch16_224": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/ViT_huge_patch16_224_pretrained.pdparams", + "ViT_huge_patch32_384": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/ViT_huge_patch32_384_pretrained.pdparams" + } + +__all__ = list(MODEL_URLS.keys()) + trunc_normal_ = TruncatedNormal(std=.02) normal_ = Normal @@ -300,7 +310,21 @@ class VisionTransformer(nn.Layer): return x -def ViT_small_patch16_224(**kwargs): +def _load_pretrained(pretrained, model, model_url, use_ssld=False): + if pretrained is False: + pass + elif pretrained is True: + load_dygraph_pretrain_from_url(model, model_url, use_ssld=use_ssld) + elif isinstance(pretrained, str): + load_dygraph_pretrain(model, pretrained) + else: + raise RuntimeError( + "pretrained type is not available. Please use `string` or `boolean` type." + ) + + + +def ViT_small_patch16_224(pretrained, model, model_url, use_ssld=False, **kwargs): model = VisionTransformer( patch_size=16, embed_dim=768, @@ -309,10 +333,12 @@ def ViT_small_patch16_224(**kwargs): mlp_ratio=3, qk_scale=768**-0.5, **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["ViT_small_patch16_224"], use_ssld=use_ssld) return model -def ViT_base_patch16_224(**kwargs): + +def ViT_base_patch16_224(pretrained, model, model_url, use_ssld=False, **kwargs): model = VisionTransformer( patch_size=16, embed_dim=768, @@ -322,10 +348,11 @@ def ViT_base_patch16_224(**kwargs): qkv_bias=True, epsilon=1e-6, **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["ViT_base_patch16_224"], use_ssld=use_ssld) return model -def ViT_base_patch16_384(**kwargs): +def ViT_base_patch16_384(pretrained, model, model_url, use_ssld=False, **kwargs): model = VisionTransformer( img_size=384, patch_size=16, @@ -336,10 +363,11 @@ def ViT_base_patch16_384(**kwargs): qkv_bias=True, epsilon=1e-6, **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["ViT_base_patch16_384"], use_ssld=use_ssld) return model -def ViT_base_patch32_384(**kwargs): +def ViT_base_patch32_384(pretrained, model, model_url, use_ssld=False, **kwargs): model = VisionTransformer( img_size=384, patch_size=32, @@ -350,10 +378,11 @@ def ViT_base_patch32_384(**kwargs): qkv_bias=True, epsilon=1e-6, **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["ViT_base_patch32_384"], use_ssld=use_ssld) return model -def ViT_large_patch16_224(**kwargs): +def ViT_large_patch16_224(pretrained, model, model_url, use_ssld=False, **kwargs): model = VisionTransformer( patch_size=16, embed_dim=1024, @@ -363,10 +392,11 @@ def ViT_large_patch16_224(**kwargs): qkv_bias=True, epsilon=1e-6, **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["ViT_large_patch16_224"], use_ssld=use_ssld) return model -def ViT_large_patch16_384(**kwargs): +def ViT_large_patch16_384(pretrained, model, model_url, use_ssld=False, **kwargs): model = VisionTransformer( img_size=384, patch_size=16, @@ -377,10 +407,11 @@ def ViT_large_patch16_384(**kwargs): qkv_bias=True, epsilon=1e-6, **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["ViT_large_patch16_384"], use_ssld=use_ssld) return model -def ViT_large_patch32_384(**kwargs): +def ViT_large_patch32_384(pretrained, model, model_url, use_ssld=False, **kwargs): model = VisionTransformer( img_size=384, patch_size=32, @@ -391,10 +422,11 @@ def ViT_large_patch32_384(**kwargs): qkv_bias=True, epsilon=1e-6, **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["ViT_large_patch32_384"], use_ssld=use_ssld) return model -def ViT_huge_patch16_224(**kwargs): +def ViT_huge_patch16_224(pretrained, model, model_url, use_ssld=False, **kwargs): model = VisionTransformer( patch_size=16, embed_dim=1280, @@ -402,10 +434,11 @@ def ViT_huge_patch16_224(**kwargs): num_heads=16, mlp_ratio=4, **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["ViT_huge_patch16_224"], use_ssld=use_ssld) return model -def ViT_huge_patch32_384(**kwargs): +def ViT_huge_patch32_384(pretrained, model, model_url, use_ssld=False, **kwargs): model = VisionTransformer( img_size=384, patch_size=32, @@ -414,4 +447,5 @@ def ViT_huge_patch32_384(**kwargs): num_heads=16, mlp_ratio=4, **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["ViT_huge_patch32_384"], use_ssld=use_ssld) return model diff --git a/ppcls/arch/backbone/model_zoo/xception.py b/ppcls/arch/backbone/model_zoo/xception.py index e7ff6c5225a33c5dbe95e89de0c6b6d05907fbdf..126c3dfdb34771c7668b8494223839b2c00742f0 100644 --- a/ppcls/arch/backbone/model_zoo/xception.py +++ b/ppcls/arch/backbone/model_zoo/xception.py @@ -8,7 +8,16 @@ from paddle.nn.initializer import Uniform import math import sys -__all__ = ['Xception41', 'Xception65', 'Xception71'] + +from ppcls.utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url + +MODEL_URLS = { + "Xception41": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/Xception41_pretrained.pdparams", + "Xception65": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/Xception65_pretrained.pdparams", + "Xception71": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/Xception71_pretrained.pdparams" + } + +__all__ = list(MODEL_URLS.keys()) class ConvBNLayer(nn.Layer): @@ -329,17 +338,32 @@ class Xception(nn.Layer): x = self._exit_flow(x) return x - -def Xception41(**args): - model = Xception(entry_flow_block_num=3, middle_flow_block_num=8, **args) +def _load_pretrained(pretrained, model, model_url, use_ssld=False): + if pretrained is False: + pass + elif pretrained is True: + load_dygraph_pretrain_from_url(model, model_url, use_ssld=use_ssld) + elif isinstance(pretrained, str): + load_dygraph_pretrain(model, pretrained) + else: + raise RuntimeError( + "pretrained type is not available. Please use `string` or `boolean` type." + ) + + +def Xception41(pretrained=False, use_ssld=False, **kwargs): + model = Xception(entry_flow_block_num=3, middle_flow_block_num=8, **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["Xception41"], use_ssld=use_ssld) return model -def Xception65(**args): - model = Xception(entry_flow_block_num=3, middle_flow_block_num=16, **args) +def Xception65(pretrained=False, use_ssld=False, **kwargs): + model = Xception(entry_flow_block_num=3, middle_flow_block_num=16, **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["Xception65"], use_ssld=use_ssld) return model -def Xception71(**args): - model = Xception(entry_flow_block_num=5, middle_flow_block_num=16, **args) +def Xception71(pretrained=False, use_ssld=False, **kwargs): + model = Xception(entry_flow_block_num=5, middle_flow_block_num=16, **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["Xception71"], use_ssld=use_ssld) return model diff --git a/ppcls/arch/backbone/model_zoo/xception_deeplab.py b/ppcls/arch/backbone/model_zoo/xception_deeplab.py index e3e39a4fb5d1aa88945615cd9de0efa5d207103a..f2424823fd90f37646063692b48726a6e4186e70 100644 --- a/ppcls/arch/backbone/model_zoo/xception_deeplab.py +++ b/ppcls/arch/backbone/model_zoo/xception_deeplab.py @@ -1,3 +1,17 @@ +# 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. + import paddle from paddle import ParamAttr import paddle.nn as nn @@ -5,7 +19,12 @@ import paddle.nn.functional as F from paddle.nn import Conv2D, BatchNorm, Linear, Dropout from paddle.nn import AdaptiveAvgPool2D, MaxPool2D, AvgPool2D -__all__ = ["Xception41_deeplab", "Xception65_deeplab", "Xception71_deeplab"] +from ppcls.utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url + +MODEL_URLS = {"Xception41_deeplab": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/Xception41_deeplab_pretrained.pdparams", + "Xception65_deeplab": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/Xception41_deeplab_pretrained.pdparams"} + +__all__ = list(MODEL_URLS.keys()) def check_data(data, number): @@ -369,18 +388,28 @@ class XceptionDeeplab(nn.Layer): x = paddle.squeeze(x, axis=[2, 3]) x = self._fc(x) return x + + +def _load_pretrained(pretrained, model, model_url, use_ssld=False): + if pretrained is False: + pass + elif pretrained is True: + load_dygraph_pretrain_from_url(model, model_url, use_ssld=use_ssld) + elif isinstance(pretrained, str): + load_dygraph_pretrain(model, pretrained) + else: + raise RuntimeError( + "pretrained type is not available. Please use `string` or `boolean` type." + ) -def Xception41_deeplab(**args): - model = XceptionDeeplab('xception_41', **args) - return model - - -def Xception65_deeplab(**args): - model = XceptionDeeplab("xception_65", **args) +def Xception41_deeplab(pretrained=False, use_ssld=False, **kwargs): + model = XceptionDeeplab('xception_41', **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["Xception41_deeplab"], use_ssld=use_ssld) return model -def Xception71_deeplab(**args): - model = XceptionDeeplab("xception_71", **args) +def Xception65_deeplab(pretrained=False, use_ssld=False, **kwargs): + model = XceptionDeeplab("xception_65", **kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["Xception65_deeplab"], use_ssld=use_ssld) return model