diff --git a/__init__.py b/__init__.py index 2128a6cc7fa5684767dfefe4348ebcc107613b2d..e65204ba653350ac6a499be7edecd3a97a5695ec 100644 --- a/__init__.py +++ b/__init__.py @@ -14,4 +14,4 @@ __all__ = ['PaddleClas'] from .paddleclas import PaddleClas -from ppcls.arch.backbone import * +from .ppcls.arch.backbone import * diff --git a/ppcls/arch/__init__.py b/ppcls/arch/__init__.py index 4021457961ad9013df79b05594e8424d1c312b10..213bef9d81198534fc076ab3447fe9a638fd848e 100644 --- a/ppcls/arch/__init__.py +++ b/ppcls/arch/__init__.py @@ -23,11 +23,11 @@ from . import backbone, gears from .backbone import * from .gears import build_gear from .utils import * -from ppcls.arch.backbone.base.theseus_layer import TheseusLayer -from ppcls.utils import logger -from ppcls.utils.save_load import load_dygraph_pretrain -from ppcls.arch.slim import prune_model, quantize_model -from ppcls.arch.distill.afd_attention import LinearTransformStudent, LinearTransformTeacher +from .backbone.base.theseus_layer import TheseusLayer +from ..utils import logger +from ..utils.save_load import load_dygraph_pretrain +from .slim import prune_model, quantize_model +from .distill.afd_attention import LinearTransformStudent, LinearTransformTeacher __all__ = ["build_model", "RecModel", "DistillationModel", "AttentionModel"] diff --git a/ppcls/arch/backbone/__init__.py b/ppcls/arch/backbone/__init__.py index e957358479cb98d8bde3dac0d4b2785b8965c7bf..d272f230b63170c69a016059262d50a15b8e9c39 100644 --- a/ppcls/arch/backbone/__init__.py +++ b/ppcls/arch/backbone/__init__.py @@ -15,62 +15,62 @@ import sys import inspect -from ppcls.arch.backbone.legendary_models.mobilenet_v1 import MobileNetV1_x0_25, MobileNetV1_x0_5, MobileNetV1_x0_75, MobileNetV1 -from ppcls.arch.backbone.legendary_models.mobilenet_v3 import 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.arch.backbone.legendary_models.resnet import ResNet18, ResNet18_vd, ResNet34, ResNet34_vd, ResNet50, ResNet50_vd, ResNet101, ResNet101_vd, ResNet152, ResNet152_vd, ResNet200_vd -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.legendary_models.pp_lcnet import PPLCNet_x0_25, PPLCNet_x0_35, PPLCNet_x0_5, PPLCNet_x0_75, PPLCNet_x1_0, PPLCNet_x1_5, PPLCNet_x2_0, PPLCNet_x2_5 -from ppcls.arch.backbone.legendary_models.pp_lcnet_v2 import PPLCNetV2_base -from ppcls.arch.backbone.legendary_models.esnet import ESNet_x0_25, ESNet_x0_5, ESNet_x0_75, ESNet_x1_0 -from ppcls.arch.backbone.legendary_models.pp_hgnet import PPHGNet_tiny, PPHGNet_small, PPHGNet_base +from .legendary_models.mobilenet_v1 import MobileNetV1_x0_25, MobileNetV1_x0_5, MobileNetV1_x0_75, MobileNetV1 +from .legendary_models.mobilenet_v3 import 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 .legendary_models.resnet import ResNet18, ResNet18_vd, ResNet34, ResNet34_vd, ResNet50, ResNet50_vd, ResNet101, ResNet101_vd, ResNet152, ResNet152_vd, ResNet200_vd +from .legendary_models.vgg import VGG11, VGG13, VGG16, VGG19 +from .legendary_models.inception_v3 import InceptionV3 +from .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 .legendary_models.pp_lcnet import PPLCNet_x0_25, PPLCNet_x0_35, PPLCNet_x0_5, PPLCNet_x0_75, PPLCNet_x1_0, PPLCNet_x1_5, PPLCNet_x2_0, PPLCNet_x2_5 +from .legendary_models.pp_lcnet_v2 import PPLCNetV2_base +from .legendary_models.esnet import ESNet_x0_25, ESNet_x0_5, ESNet_x0_75, ESNet_x1_0 +from .legendary_models.pp_hgnet import PPHGNet_tiny, PPHGNet_small, PPHGNet_base -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.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 -from ppcls.arch.backbone.model_zoo.densenet import DenseNet121, DenseNet161, DenseNet169, DenseNet201, DenseNet264 -from ppcls.arch.backbone.model_zoo.efficientnet import EfficientNetB0, EfficientNetB1, EfficientNetB2, EfficientNetB3, EfficientNetB4, EfficientNetB5, EfficientNetB6, EfficientNetB7, EfficientNetB0_small -from ppcls.arch.backbone.model_zoo.resnest import ResNeSt50_fast_1s1x64d, ResNeSt50, ResNeSt101 -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 -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 -from ppcls.arch.backbone.model_zoo.regnet import RegNetX_200MF, RegNetX_4GF, RegNetX_32GF, RegNetY_200MF, RegNetY_4GF, RegNetY_32GF -from ppcls.arch.backbone.model_zoo.vision_transformer import 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 -from ppcls.arch.backbone.model_zoo.distilled_vision_transformer import 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.arch.backbone.legendary_models.swin_transformer import SwinTransformer_tiny_patch4_window7_224, SwinTransformer_small_patch4_window7_224, SwinTransformer_base_patch4_window7_224, SwinTransformer_base_patch4_window12_384, SwinTransformer_large_patch4_window7_224, SwinTransformer_large_patch4_window12_384 -from ppcls.arch.backbone.model_zoo.cswin_transformer import CSWinTransformer_tiny_224, CSWinTransformer_small_224, CSWinTransformer_base_224, CSWinTransformer_large_224, CSWinTransformer_base_384, CSWinTransformer_large_384 -from ppcls.arch.backbone.model_zoo.mixnet import MixNet_S, MixNet_M, MixNet_L -from ppcls.arch.backbone.model_zoo.rexnet import ReXNet_1_0, ReXNet_1_3, ReXNet_1_5, ReXNet_2_0, ReXNet_3_0 -from ppcls.arch.backbone.model_zoo.gvt import pcpvt_small, pcpvt_base, pcpvt_large, alt_gvt_small, alt_gvt_base, alt_gvt_large -from ppcls.arch.backbone.model_zoo.levit import LeViT_128S, LeViT_128, LeViT_192, LeViT_256, LeViT_384 -from ppcls.arch.backbone.model_zoo.dla import DLA34, DLA46_c, DLA46x_c, DLA60, DLA60x, DLA60x_c, DLA102, DLA102x, DLA102x2, DLA169 -from ppcls.arch.backbone.model_zoo.rednet import RedNet26, RedNet38, RedNet50, RedNet101, RedNet152 -from ppcls.arch.backbone.model_zoo.tnt import TNT_small -from ppcls.arch.backbone.model_zoo.hardnet import HarDNet68, HarDNet85, HarDNet39_ds, HarDNet68_ds -from ppcls.arch.backbone.model_zoo.cspnet import CSPDarkNet53 -from ppcls.arch.backbone.model_zoo.pvt_v2 import PVT_V2_B0, PVT_V2_B1, PVT_V2_B2_Linear, PVT_V2_B2, PVT_V2_B3, PVT_V2_B4, PVT_V2_B5 -from ppcls.arch.backbone.model_zoo.mobilevit import MobileViT_XXS, MobileViT_XS, MobileViT_S -from ppcls.arch.backbone.model_zoo.repvgg import RepVGG_A0, RepVGG_A1, RepVGG_A2, RepVGG_B0, RepVGG_B1, RepVGG_B2, RepVGG_B1g2, RepVGG_B1g4, RepVGG_B2g4, RepVGG_B3g4 -from ppcls.arch.backbone.model_zoo.van import VAN_tiny -from ppcls.arch.backbone.variant_models.resnet_variant import ResNet50_last_stage_stride1 -from ppcls.arch.backbone.variant_models.vgg_variant import VGG19Sigmoid -from ppcls.arch.backbone.variant_models.pp_lcnet_variant import PPLCNet_x2_5_Tanh -from ppcls.arch.backbone.model_zoo.adaface_ir_net import AdaFace_IR_18, AdaFace_IR_34, AdaFace_IR_50, AdaFace_IR_101, AdaFace_IR_152, AdaFace_IR_SE_50, AdaFace_IR_SE_101, AdaFace_IR_SE_152, AdaFace_IR_SE_200 +from .model_zoo.resnet_vc import ResNet50_vc +from .model_zoo.resnext import ResNeXt50_32x4d, ResNeXt50_64x4d, ResNeXt101_32x4d, ResNeXt101_64x4d, ResNeXt152_32x4d, ResNeXt152_64x4d +from .model_zoo.resnext_vd import ResNeXt50_vd_32x4d, ResNeXt50_vd_64x4d, ResNeXt101_vd_32x4d, ResNeXt101_vd_64x4d, ResNeXt152_vd_32x4d, ResNeXt152_vd_64x4d +from .model_zoo.res2net import Res2Net50_26w_4s, Res2Net50_14w_8s +from .model_zoo.res2net_vd import Res2Net50_vd_26w_4s, Res2Net101_vd_26w_4s, Res2Net200_vd_26w_4s +from .model_zoo.se_resnet_vd import SE_ResNet18_vd, SE_ResNet34_vd, SE_ResNet50_vd +from .model_zoo.se_resnext_vd import SE_ResNeXt50_vd_32x4d, SE_ResNeXt50_vd_32x4d, SENet154_vd +from .model_zoo.se_resnext import SE_ResNeXt50_32x4d, SE_ResNeXt101_32x4d, SE_ResNeXt152_64x4d +from .model_zoo.dpn import DPN68, DPN92, DPN98, DPN107, DPN131 +from .model_zoo.densenet import DenseNet121, DenseNet161, DenseNet169, DenseNet201, DenseNet264 +from .model_zoo.efficientnet import EfficientNetB0, EfficientNetB1, EfficientNetB2, EfficientNetB3, EfficientNetB4, EfficientNetB5, EfficientNetB6, EfficientNetB7, EfficientNetB0_small +from .model_zoo.resnest import ResNeSt50_fast_1s1x64d, ResNeSt50, ResNeSt101 +from .model_zoo.googlenet import GoogLeNet +from .model_zoo.mobilenet_v2 import MobileNetV2_x0_25, MobileNetV2_x0_5, MobileNetV2_x0_75, MobileNetV2, MobileNetV2_x1_5, MobileNetV2_x2_0 +from .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 .model_zoo.ghostnet import GhostNet_x0_5, GhostNet_x1_0, GhostNet_x1_3 +from .model_zoo.alexnet import AlexNet +from .model_zoo.inception_v4 import InceptionV4 +from .model_zoo.xception import Xception41, Xception65, Xception71 +from .model_zoo.xception_deeplab import Xception41_deeplab, Xception65_deeplab +from .model_zoo.resnext101_wsl import ResNeXt101_32x8d_wsl, ResNeXt101_32x16d_wsl, ResNeXt101_32x32d_wsl, ResNeXt101_32x48d_wsl +from .model_zoo.squeezenet import SqueezeNet1_0, SqueezeNet1_1 +from .model_zoo.darknet import DarkNet53 +from .model_zoo.regnet import RegNetX_200MF, RegNetX_4GF, RegNetX_32GF, RegNetY_200MF, RegNetY_4GF, RegNetY_32GF +from .model_zoo.vision_transformer import 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 +from .model_zoo.distilled_vision_transformer import 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 .legendary_models.swin_transformer import SwinTransformer_tiny_patch4_window7_224, SwinTransformer_small_patch4_window7_224, SwinTransformer_base_patch4_window7_224, SwinTransformer_base_patch4_window12_384, SwinTransformer_large_patch4_window7_224, SwinTransformer_large_patch4_window12_384 +from .model_zoo.cswin_transformer import CSWinTransformer_tiny_224, CSWinTransformer_small_224, CSWinTransformer_base_224, CSWinTransformer_large_224, CSWinTransformer_base_384, CSWinTransformer_large_384 +from .model_zoo.mixnet import MixNet_S, MixNet_M, MixNet_L +from .model_zoo.rexnet import ReXNet_1_0, ReXNet_1_3, ReXNet_1_5, ReXNet_2_0, ReXNet_3_0 +from .model_zoo.gvt import pcpvt_small, pcpvt_base, pcpvt_large, alt_gvt_small, alt_gvt_base, alt_gvt_large +from .model_zoo.levit import LeViT_128S, LeViT_128, LeViT_192, LeViT_256, LeViT_384 +from .model_zoo.dla import DLA34, DLA46_c, DLA46x_c, DLA60, DLA60x, DLA60x_c, DLA102, DLA102x, DLA102x2, DLA169 +from .model_zoo.rednet import RedNet26, RedNet38, RedNet50, RedNet101, RedNet152 +from .model_zoo.tnt import TNT_small +from .model_zoo.hardnet import HarDNet68, HarDNet85, HarDNet39_ds, HarDNet68_ds +from .model_zoo.cspnet import CSPDarkNet53 +from .model_zoo.pvt_v2 import PVT_V2_B0, PVT_V2_B1, PVT_V2_B2_Linear, PVT_V2_B2, PVT_V2_B3, PVT_V2_B4, PVT_V2_B5 +from .model_zoo.mobilevit import MobileViT_XXS, MobileViT_XS, MobileViT_S +from .model_zoo.repvgg import RepVGG_A0, RepVGG_A1, RepVGG_A2, RepVGG_B0, RepVGG_B1, RepVGG_B2, RepVGG_B1g2, RepVGG_B1g4, RepVGG_B2g4, RepVGG_B3g4 +from .model_zoo.van import VAN_tiny +from .variant_models.resnet_variant import ResNet50_last_stage_stride1 +from .variant_models.vgg_variant import VGG19Sigmoid +from .variant_models.pp_lcnet_variant import PPLCNet_x2_5_Tanh +from .model_zoo.adaface_ir_net import AdaFace_IR_18, AdaFace_IR_34, AdaFace_IR_50, AdaFace_IR_101, AdaFace_IR_152, AdaFace_IR_SE_50, AdaFace_IR_SE_101, AdaFace_IR_SE_152, AdaFace_IR_SE_200 # help whl get all the models' api (class type) and components' api (func type) diff --git a/ppcls/arch/backbone/base/theseus_layer.py b/ppcls/arch/backbone/base/theseus_layer.py index 9f3f596a85a4ca7336779b88e77053bf19458c7d..a533cdc77bd1a124030fde66184144bdd3025f37 100644 --- a/ppcls/arch/backbone/base/theseus_layer.py +++ b/ppcls/arch/backbone/base/theseus_layer.py @@ -15,7 +15,7 @@ from typing import Tuple, List, Dict, Union, Callable, Any from paddle import nn -from ppcls.utils import logger +from ....utils import logger class Identity(nn.Layer): diff --git a/ppcls/arch/backbone/legendary_models/esnet.py b/ppcls/arch/backbone/legendary_models/esnet.py index e05e0ceb3223d7ddb41f8f8fda7acb11d2eb6267..d8dc6e6fa00825b0908703ea686a3d56bd9e46cb 100644 --- a/ppcls/arch/backbone/legendary_models/esnet.py +++ b/ppcls/arch/backbone/legendary_models/esnet.py @@ -22,8 +22,8 @@ from paddle.nn import AdaptiveAvgPool2D, MaxPool2D from paddle.nn.initializer import KaimingNormal from paddle.regularizer import L2Decay -from ppcls.arch.backbone.base.theseus_layer import TheseusLayer -from ppcls.utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url +from ..base.theseus_layer import TheseusLayer +from ....utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url MODEL_URLS = { "ESNet_x0_25": diff --git a/ppcls/arch/backbone/legendary_models/hrnet.py b/ppcls/arch/backbone/legendary_models/hrnet.py index 399131513028cf43fd4360c859829278531ccd10..a5a3d8bc6332035a3b0a6b00f85310cba266b004 100644 --- a/ppcls/arch/backbone/legendary_models/hrnet.py +++ b/ppcls/arch/backbone/legendary_models/hrnet.py @@ -25,8 +25,8 @@ from paddle import ParamAttr from paddle.nn.functional import upsample from paddle.nn.initializer import Uniform -from ppcls.arch.backbone.base.theseus_layer import TheseusLayer, Identity -from ppcls.utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url +from ..base.theseus_layer import TheseusLayer, Identity +from ....utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url MODEL_URLS = { "HRNet_W18_C": diff --git a/ppcls/arch/backbone/legendary_models/inception_v3.py b/ppcls/arch/backbone/legendary_models/inception_v3.py index 74a3d1fd210aa34d5e5615b6529b9c02d0dfc410..5bec7588dc8ef206962b59eecf4187ff4cd5200e 100644 --- a/ppcls/arch/backbone/legendary_models/inception_v3.py +++ b/ppcls/arch/backbone/legendary_models/inception_v3.py @@ -23,8 +23,8 @@ from paddle.nn import Conv2D, BatchNorm, Linear, Dropout from paddle.nn import AdaptiveAvgPool2D, MaxPool2D, AvgPool2D from paddle.nn.initializer import Uniform -from ppcls.arch.backbone.base.theseus_layer import TheseusLayer -from ppcls.utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url +from ..base.theseus_layer import TheseusLayer +from ....utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url MODEL_URLS = { "InceptionV3": diff --git a/ppcls/arch/backbone/legendary_models/mobilenet_v1.py b/ppcls/arch/backbone/legendary_models/mobilenet_v1.py index 3d6caeb0f049e86997d2da281208e3963e2d28a2..3e612068399b1a08ae17db8161fad1931774012e 100644 --- a/ppcls/arch/backbone/legendary_models/mobilenet_v1.py +++ b/ppcls/arch/backbone/legendary_models/mobilenet_v1.py @@ -22,8 +22,8 @@ from paddle.nn import Conv2D, BatchNorm, Linear, ReLU, Flatten from paddle.nn import AdaptiveAvgPool2D from paddle.nn.initializer import KaimingNormal -from ppcls.arch.backbone.base.theseus_layer import TheseusLayer -from ppcls.utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url +from ..base.theseus_layer import TheseusLayer +from ....utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url MODEL_URLS = { "MobileNetV1_x0_25": diff --git a/ppcls/arch/backbone/legendary_models/mobilenet_v3.py b/ppcls/arch/backbone/legendary_models/mobilenet_v3.py index 3fbf9776bc4f39a5667b01623b7950d362203e9c..34d497de895ad69dfd4c2770553297256879f2aa 100644 --- a/ppcls/arch/backbone/legendary_models/mobilenet_v3.py +++ b/ppcls/arch/backbone/legendary_models/mobilenet_v3.py @@ -21,8 +21,9 @@ import paddle.nn as nn from paddle import ParamAttr from paddle.nn import AdaptiveAvgPool2D, BatchNorm, Conv2D, Dropout, Linear from paddle.regularizer import L2Decay -from ppcls.arch.backbone.base.theseus_layer import TheseusLayer -from ppcls.utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url + +from ..base.theseus_layer import TheseusLayer +from ....utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url MODEL_URLS = { "MobileNetV3_small_x0_35": diff --git a/ppcls/arch/backbone/legendary_models/pp_hgnet.py b/ppcls/arch/backbone/legendary_models/pp_hgnet.py index a5add431b025d9b97f0564a671a531d5ab7cd72d..9ca386f0694cdf50772703543d89c4fb9cd5444f 100644 --- a/ppcls/arch/backbone/legendary_models/pp_hgnet.py +++ b/ppcls/arch/backbone/legendary_models/pp_hgnet.py @@ -20,8 +20,8 @@ from paddle.nn import Conv2D, BatchNorm2D, ReLU, AdaptiveAvgPool2D, MaxPool2D from paddle.regularizer import L2Decay from paddle import ParamAttr -from ppcls.arch.backbone.base.theseus_layer import TheseusLayer -from ppcls.utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url +from ..base.theseus_layer import TheseusLayer +from ....utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url MODEL_URLS = { "PPHGNet_tiny": @@ -199,6 +199,7 @@ class PPHGNet(TheseusLayer): Returns: model: nn.Layer. Specific PPHGNet model depends on args. """ + def __init__(self, stem_channels, stage_config, @@ -230,7 +231,7 @@ class PPHGNet(TheseusLayer): k] self.stages.append( HG_Stage(in_channels, mid_channels, out_channels, block_num, - layer_num, downsample)) + layer_num, downsample)) self.avg_pool = AdaptiveAvgPool2D(1) if self.use_last_conv: diff --git a/ppcls/arch/backbone/legendary_models/pp_lcnet.py b/ppcls/arch/backbone/legendary_models/pp_lcnet.py index a4fe6fadb53b19176d03e529a00200a1570c1eed..017592d3f2cbfedf9bc64f3827c14790ff2c0ed0 100644 --- a/ppcls/arch/backbone/legendary_models/pp_lcnet.py +++ b/ppcls/arch/backbone/legendary_models/pp_lcnet.py @@ -20,8 +20,9 @@ from paddle import ParamAttr from paddle.nn import AdaptiveAvgPool2D, BatchNorm2D, Conv2D, Dropout, Linear from paddle.regularizer import L2Decay from paddle.nn.initializer import KaimingNormal -from ppcls.arch.backbone.base.theseus_layer import TheseusLayer -from ppcls.utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url + +from ..base.theseus_layer import TheseusLayer +from ....utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url MODEL_URLS = { "PPLCNet_x0_25": @@ -229,64 +230,59 @@ class PPLCNet(TheseusLayer): stride=stride_list[0], lr_mult=self.lr_mult_list[0]) - self.blocks2 = nn.Sequential(*[ + self.blocks2 = nn.Sequential(* [ DepthwiseSeparable( num_channels=make_divisible(in_c * scale), num_filters=make_divisible(out_c * scale), dw_size=k, stride=s, use_se=se, - lr_mult=self.lr_mult_list[1]) - for i, (k, in_c, out_c, s, se - ) in enumerate(self.net_config["blocks2"]) + lr_mult=self.lr_mult_list[1]) for i, (k, in_c, out_c, s, se) in + enumerate(self.net_config["blocks2"]) ]) - self.blocks3 = nn.Sequential(*[ + self.blocks3 = nn.Sequential(* [ DepthwiseSeparable( num_channels=make_divisible(in_c * scale), num_filters=make_divisible(out_c * scale), dw_size=k, stride=s, use_se=se, - lr_mult=self.lr_mult_list[2]) - for i, (k, in_c, out_c, s, se - ) in enumerate(self.net_config["blocks3"]) + lr_mult=self.lr_mult_list[2]) for i, (k, in_c, out_c, s, se) in + enumerate(self.net_config["blocks3"]) ]) - self.blocks4 = nn.Sequential(*[ + self.blocks4 = nn.Sequential(* [ DepthwiseSeparable( num_channels=make_divisible(in_c * scale), num_filters=make_divisible(out_c * scale), dw_size=k, stride=s, use_se=se, - lr_mult=self.lr_mult_list[3]) - for i, (k, in_c, out_c, s, se - ) in enumerate(self.net_config["blocks4"]) + lr_mult=self.lr_mult_list[3]) for i, (k, in_c, out_c, s, se) in + enumerate(self.net_config["blocks4"]) ]) - self.blocks5 = nn.Sequential(*[ + self.blocks5 = nn.Sequential(* [ DepthwiseSeparable( num_channels=make_divisible(in_c * scale), num_filters=make_divisible(out_c * scale), dw_size=k, stride=s, use_se=se, - lr_mult=self.lr_mult_list[4]) - for i, (k, in_c, out_c, s, se - ) in enumerate(self.net_config["blocks5"]) + lr_mult=self.lr_mult_list[4]) for i, (k, in_c, out_c, s, se) in + enumerate(self.net_config["blocks5"]) ]) - self.blocks6 = nn.Sequential(*[ + self.blocks6 = nn.Sequential(* [ DepthwiseSeparable( num_channels=make_divisible(in_c * scale), num_filters=make_divisible(out_c * scale), dw_size=k, stride=s, use_se=se, - lr_mult=self.lr_mult_list[5]) - for i, (k, in_c, out_c, s, se - ) in enumerate(self.net_config["blocks6"]) + lr_mult=self.lr_mult_list[5]) for i, (k, in_c, out_c, s, se) in + enumerate(self.net_config["blocks6"]) ]) self.avg_pool = AdaptiveAvgPool2D(1) diff --git a/ppcls/arch/backbone/legendary_models/pp_lcnet_v2.py b/ppcls/arch/backbone/legendary_models/pp_lcnet_v2.py index 459d84275ac63af54fb9ad10af2bcf2f7759052d..40264092a47deb1e11ed11d2edbda7135f0b5a75 100644 --- a/ppcls/arch/backbone/legendary_models/pp_lcnet_v2.py +++ b/ppcls/arch/backbone/legendary_models/pp_lcnet_v2.py @@ -21,8 +21,9 @@ from paddle import ParamAttr from paddle.nn import AdaptiveAvgPool2D, BatchNorm2D, Conv2D, Dropout, Linear from paddle.regularizer import L2Decay from paddle.nn.initializer import KaimingNormal -from ppcls.arch.backbone.base.theseus_layer import TheseusLayer -from ppcls.utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url + +from ..base.theseus_layer import TheseusLayer +from ....utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url MODEL_URLS = { "PPLCNetV2_base": diff --git a/ppcls/arch/backbone/legendary_models/resnet.py b/ppcls/arch/backbone/legendary_models/resnet.py index 705511f5b5a8ed5aac45636dddb3598aefd4276a..2a792ddec415df3f7f6ac4e098df7090c39f9975 100644 --- a/ppcls/arch/backbone/legendary_models/resnet.py +++ b/ppcls/arch/backbone/legendary_models/resnet.py @@ -26,9 +26,9 @@ from paddle.nn.initializer import Uniform from paddle.regularizer import L2Decay import math -from ppcls.utils import logger -from ppcls.arch.backbone.base.theseus_layer import TheseusLayer -from ppcls.utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url +from ....utils import logger +from ..base.theseus_layer import TheseusLayer +from ....utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url MODEL_URLS = { "ResNet18": @@ -328,7 +328,7 @@ class ResNet(TheseusLayer): [32, 32, 3, 1], [32, 64, 3, 1]] } - self.stem = nn.Sequential(*[ + self.stem = nn.Sequential(* [ ConvBNLayer( num_channels=in_c, num_filters=out_c, diff --git a/ppcls/arch/backbone/legendary_models/swin_transformer.py b/ppcls/arch/backbone/legendary_models/swin_transformer.py index 9500a94134910c14c689d0aa1d8975e2c2bf0974..b72035ef9f90804b3543815e51848d30be8863b8 100644 --- a/ppcls/arch/backbone/legendary_models/swin_transformer.py +++ b/ppcls/arch/backbone/legendary_models/swin_transformer.py @@ -21,9 +21,9 @@ import paddle.nn as nn import paddle.nn.functional as F from paddle.nn.initializer import TruncatedNormal, Constant -from ppcls.arch.backbone.base.theseus_layer import TheseusLayer -from ppcls.arch.backbone.model_zoo.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 +from ..model_zoo.vision_transformer import trunc_normal_, zeros_, ones_, to_2tuple, DropPath, Identity +from ..base.theseus_layer import TheseusLayer +from ....utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url MODEL_URLS = { "SwinTransformer_tiny_patch4_window7_224": diff --git a/ppcls/arch/backbone/legendary_models/vgg.py b/ppcls/arch/backbone/legendary_models/vgg.py index b71249616456008c77818b2d0a16b1f7a6337143..3e79815e1a8fb5083ae8a21b929822e1891cc77f 100644 --- a/ppcls/arch/backbone/legendary_models/vgg.py +++ b/ppcls/arch/backbone/legendary_models/vgg.py @@ -20,8 +20,8 @@ import paddle.nn as nn from paddle.nn import Conv2D, BatchNorm, Linear, Dropout from paddle.nn import MaxPool2D -from ppcls.arch.backbone.base.theseus_layer import TheseusLayer -from ppcls.utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url +from ..base.theseus_layer import TheseusLayer +from ....utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url MODEL_URLS = { "VGG11": diff --git a/ppcls/arch/backbone/model_zoo/alexnet.py b/ppcls/arch/backbone/model_zoo/alexnet.py index 90e1d7e1dbff4f4f9331a37c3cffe3872997da07..f389f0fb20f89dc7ffd094efa86fc09d5a53036a 100644 --- a/ppcls/arch/backbone/model_zoo/alexnet.py +++ b/ppcls/arch/backbone/model_zoo/alexnet.py @@ -23,7 +23,7 @@ from paddle.nn import AdaptiveAvgPool2D, MaxPool2D, AvgPool2D from paddle.nn.initializer import Uniform import math -from ppcls.utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url +from ....utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url MODEL_URLS = { "AlexNet": diff --git a/ppcls/arch/backbone/model_zoo/convnext.py b/ppcls/arch/backbone/model_zoo/convnext.py new file mode 100644 index 0000000000000000000000000000000000000000..b2745ff14d942ecd3b67238abb07c0985e147edc --- /dev/null +++ b/ppcls/arch/backbone/model_zoo/convnext.py @@ -0,0 +1,232 @@ +# copyright (c) 2022 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. +# +# Code was heavily based on https://github.com/facebookresearch/ConvNeXt + +import paddle +import paddle.nn as nn +from paddle.nn.initializer import TruncatedNormal, Constant + +from ....utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url + +MODEL_URLS = { + "ConvNeXt_tiny": "", # TODO +} + +__all__ = list(MODEL_URLS.keys()) + +trunc_normal_ = TruncatedNormal(std=.02) +zeros_ = Constant(value=0.) +ones_ = Constant(value=1.) + + +def drop_path(x, drop_prob=0., training=False): + """Drop paths (Stochastic Depth) per sample (when applied in main path of residual blocks). + the original name is misleading as 'Drop Connect' is a different form of dropout in a separate paper... + See discussion: https://github.com/tensorflow/tpu/issues/494#issuecomment-532968956 ... + """ + if drop_prob == 0. or not training: + return x + keep_prob = paddle.to_tensor(1 - drop_prob) + shape = (paddle.shape(x)[0], ) + (1, ) * (x.ndim - 1) + random_tensor = keep_prob + paddle.rand(shape, dtype=x.dtype) + random_tensor = paddle.floor(random_tensor) # binarize + output = x.divide(keep_prob) * random_tensor + return output + + +class DropPath(nn.Layer): + """Drop paths (Stochastic Depth) per sample (when applied in main path of residual blocks). + """ + + def __init__(self, drop_prob=None): + super(DropPath, self).__init__() + self.drop_prob = drop_prob + + def forward(self, x): + return drop_path(x, self.drop_prob, self.training) + + +class ChannelsFirstLayerNorm(nn.Layer): + r""" LayerNorm that supports two data formats: channels_last (default) or channels_first. + The ordering of the dimensions in the inputs. channels_last corresponds to inputs with + shape (batch_size, height, width, channels) while channels_first corresponds to inputs + with shape (batch_size, channels, height, width). + """ + + def __init__(self, normalized_shape, epsilon=1e-5): + super().__init__() + self.weight = self.create_parameter( + shape=[normalized_shape], default_initializer=ones_) + self.bias = self.create_parameter( + shape=[normalized_shape], default_initializer=zeros_) + self.epsilon = epsilon + self.normalized_shape = [normalized_shape] + + def forward(self, x): + u = x.mean(1, keepdim=True) + s = (x - u).pow(2).mean(1, keepdim=True) + x = (x - u) / paddle.sqrt(s + self.epsilon) + x = self.weight[:, None, None] * x + self.bias[:, None, None] + return x + + +class Block(nn.Layer): + r""" ConvNeXt Block. There are two equivalent implementations: + (1) DwConv -> LayerNorm (channels_first) -> 1x1 Conv -> GELU -> 1x1 Conv; all in (N, C, H, W) + (2) DwConv -> Permute to (N, H, W, C); LayerNorm (channels_last) -> Linear -> GELU -> Linear; Permute back + We use (2) as we find it slightly faster in PyTorch + + Args: + dim (int): Number of input channels. + drop_path (float): Stochastic depth rate. Default: 0.0 + layer_scale_init_value (float): Init value for Layer Scale. Default: 1e-6. + """ + + def __init__(self, dim, drop_path=0., layer_scale_init_value=1e-6): + super().__init__() + self.dwconv = nn.Conv2D( + dim, dim, 7, padding=3, groups=dim) # depthwise conv + self.norm = nn.LayerNorm(dim, epsilon=1e-6) + # pointwise/1x1 convs, implemented with linear layers + self.pwconv1 = nn.Linear(dim, 4 * dim) + self.act = nn.GELU() + self.pwconv2 = nn.Linear(4 * dim, dim) + if layer_scale_init_value > 0: + self.gamma = self.create_parameter( + shape=[dim], + default_initializer=Constant(value=layer_scale_init_value)) + else: + self.gamma = None + self.drop_path = DropPath( + drop_path) if drop_path > 0. else nn.Identity() + + def forward(self, x): + input = x + x = self.dwconv(x) + x = x.transpose([0, 2, 3, 1]) # (N, C, H, W) -> (N, H, W, C) + x = self.norm(x) + x = self.pwconv1(x) + x = self.act(x) + x = self.pwconv2(x) + if self.gamma is not None: + x = self.gamma * x + x = x.transpose([0, 3, 1, 2]) # (N, H, W, C) -> (N, C, H, W) + + x = input + self.drop_path(x) + return x + + +class ConvNeXt(nn.Layer): + r""" ConvNeXt + A PaddlePaddle impl of : `A ConvNet for the 2020s` - + https://arxiv.org/pdf/2201.03545.pdf + + Args: + in_chans (int): Number of input image channels. Default: 3 + class_num (int): Number of classes for classification head. Default: 1000 + depths (tuple(int)): Number of blocks at each stage. Default: [3, 3, 9, 3] + dims (int): Feature dimension at each stage. Default: [96, 192, 384, 768] + drop_path_rate (float): Stochastic depth rate. Default: 0. + layer_scale_init_value (float): Init value for Layer Scale. Default: 1e-6. + head_init_scale (float): Init scaling value for classifier weights and biases. Default: 1. + """ + + def __init__(self, + in_chans=3, + class_num=1000, + depths=[3, 3, 9, 3], + dims=[96, 192, 384, 768], + drop_path_rate=0., + layer_scale_init_value=1e-6, + head_init_scale=1.): + super().__init__() + + # stem and 3 intermediate downsampling conv layers + self.downsample_layers = nn.LayerList() + stem = nn.Sequential( + nn.Conv2D( + in_chans, dims[0], 4, stride=4), + ChannelsFirstLayerNorm( + dims[0], epsilon=1e-6)) + self.downsample_layers.append(stem) + for i in range(3): + downsample_layer = nn.Sequential( + ChannelsFirstLayerNorm( + dims[i], epsilon=1e-6), + nn.Conv2D( + dims[i], dims[i + 1], 2, stride=2), ) + self.downsample_layers.append(downsample_layer) + + # 4 feature resolution stages, each consisting of multiple residual blocks + self.stages = nn.LayerList() + dp_rates = [ + x.item() for x in paddle.linspace(0, drop_path_rate, sum(depths)) + ] + cur = 0 + for i in range(4): + stage = nn.Sequential(* [ + Block( + dim=dims[i], + drop_path=dp_rates[cur + j], + layer_scale_init_value=layer_scale_init_value) + for j in range(depths[i]) + ]) + self.stages.append(stage) + cur += depths[i] + + self.norm = nn.LayerNorm(dims[-1], epsilon=1e-6) # final norm layer + self.head = nn.Linear(dims[-1], class_num) + + self.apply(self._init_weights) + self.head.weight.set_value(self.head.weight * head_init_scale) + self.head.bias.set_value(self.head.bias * head_init_scale) + + def _init_weights(self, m): + if isinstance(m, (nn.Conv2D, nn.Linear)): + trunc_normal_(m.weight) + if m.bias is not None: + zeros_(m.bias) + + def forward_features(self, x): + for i in range(4): + x = self.downsample_layers[i](x) + x = self.stages[i](x) + # global average pooling, (N, C, H, W) -> (N, C) + return self.norm(x.mean([-2, -1])) + + def forward(self, x): + x = self.forward_features(x) + x = self.head(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 ConvNeXt_tiny(pretrained=False, use_ssld=False, **kwargs): + model = ConvNeXt(depths=[3, 3, 9, 3], dims=[96, 192, 384, 768], **kwargs) + _load_pretrained( + pretrained, model, MODEL_URLS["ConvNeXt_tiny"], use_ssld=use_ssld) + return model diff --git a/ppcls/arch/backbone/model_zoo/cspnet.py b/ppcls/arch/backbone/model_zoo/cspnet.py index bf4e061e3835d2f07c84baa5e8af97d2192a6d1e..e2dc45ab1be543ec1ff8df57c980a8ab93dbd8fe 100644 --- a/ppcls/arch/backbone/model_zoo/cspnet.py +++ b/ppcls/arch/backbone/model_zoo/cspnet.py @@ -20,7 +20,7 @@ import paddle.nn as nn import paddle.nn.functional as F from paddle import ParamAttr -from ppcls.utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url +from ....utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url MODEL_URLS = { "CSPDarkNet53": diff --git a/ppcls/arch/backbone/model_zoo/cswin_transformer.py b/ppcls/arch/backbone/model_zoo/cswin_transformer.py index 429edbe1aa18bc3704781ab1cb9f1b4b31bca69f..0d5d729dca2f37e9b941d275bbd861d74aa32420 100644 --- a/ppcls/arch/backbone/model_zoo/cswin_transformer.py +++ b/ppcls/arch/backbone/model_zoo/cswin_transformer.py @@ -21,7 +21,7 @@ import paddle import paddle.nn as nn 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 +from ....utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url MODEL_URLS = { "CSWinTransformer_tiny_224": diff --git a/ppcls/arch/backbone/model_zoo/darknet.py b/ppcls/arch/backbone/model_zoo/darknet.py index 34ca9a2e71dbaee94431853a94495f639f4e53cf..17a6b43eb5f8341f00e8f5040f3ba867061e9876 100644 --- a/ppcls/arch/backbone/model_zoo/darknet.py +++ b/ppcls/arch/backbone/model_zoo/darknet.py @@ -23,7 +23,7 @@ from paddle.nn import AdaptiveAvgPool2D, MaxPool2D, AvgPool2D from paddle.nn.initializer import Uniform import math -from ppcls.utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url +from ....utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url MODEL_URLS = { "DarkNet53": diff --git a/ppcls/arch/backbone/model_zoo/densenet.py b/ppcls/arch/backbone/model_zoo/densenet.py index d658a18f8f18faea6faece1fa1f56ffc61680f68..14886576ffb21735cb2f580141f64a6b26c2a0d8 100644 --- a/ppcls/arch/backbone/model_zoo/densenet.py +++ b/ppcls/arch/backbone/model_zoo/densenet.py @@ -28,7 +28,7 @@ from paddle.nn.initializer import Uniform import math -from ppcls.utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url +from ....utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url MODEL_URLS = { "DenseNet121": diff --git a/ppcls/arch/backbone/model_zoo/distilled_vision_transformer.py b/ppcls/arch/backbone/model_zoo/distilled_vision_transformer.py index e816e804204235d0bd3ab08e8e4b687787e5cd94..2ccd3c7bee9d55fb0337dd67b187108e4451cb8e 100644 --- a/ppcls/arch/backbone/model_zoo/distilled_vision_transformer.py +++ b/ppcls/arch/backbone/model_zoo/distilled_vision_transformer.py @@ -19,7 +19,7 @@ import paddle import paddle.nn as nn from .vision_transformer import VisionTransformer, Identity, trunc_normal_, zeros_ -from ppcls.utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url +from ....utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url MODEL_URLS = { "DeiT_tiny_patch16_224": diff --git a/ppcls/arch/backbone/model_zoo/dla.py b/ppcls/arch/backbone/model_zoo/dla.py index d5ee822c480fa5c88e18dca2288c2f5b5cff2f97..81a24df65946d98c55c3631a7da38a26bea097f9 100644 --- a/ppcls/arch/backbone/model_zoo/dla.py +++ b/ppcls/arch/backbone/model_zoo/dla.py @@ -23,8 +23,8 @@ import paddle.nn.functional as F from paddle.nn.initializer import Normal, Constant -from ppcls.arch.backbone.base.theseus_layer import Identity -from ppcls.utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url +from ..base.theseus_layer import Identity +from ....utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url MODEL_URLS = { "DLA34": diff --git a/ppcls/arch/backbone/model_zoo/dpn.py b/ppcls/arch/backbone/model_zoo/dpn.py index 2eb2647e88d5f3ec7a319f1b68367c189ac8ee11..828e8dfd4f169efb00dff374e210bcb138173c88 100644 --- a/ppcls/arch/backbone/model_zoo/dpn.py +++ b/ppcls/arch/backbone/model_zoo/dpn.py @@ -29,7 +29,7 @@ from paddle.nn.initializer import Uniform import math -from ppcls.utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url +from ....utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url MODEL_URLS = { "DPN68": diff --git a/ppcls/arch/backbone/model_zoo/efficientnet.py b/ppcls/arch/backbone/model_zoo/efficientnet.py index 9ddb90fbb077622f86f0624bdd30b8a3a9297ac9..dec40d37eb9bea9e839781afac03f8454c6b11ca 100644 --- a/ppcls/arch/backbone/model_zoo/efficientnet.py +++ b/ppcls/arch/backbone/model_zoo/efficientnet.py @@ -26,7 +26,7 @@ import collections import re import copy -from ppcls.utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url +from ....utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url MODEL_URLS = { "EfficientNetB0_small": diff --git a/ppcls/arch/backbone/model_zoo/ghostnet.py b/ppcls/arch/backbone/model_zoo/ghostnet.py index 5cfa56ebd5a58c0ae1cd6d2752a7e64a21538507..1fa6d1553d6e2a3dd3f728520df9bb5cc25e8ad3 100644 --- a/ppcls/arch/backbone/model_zoo/ghostnet.py +++ b/ppcls/arch/backbone/model_zoo/ghostnet.py @@ -24,7 +24,7 @@ from paddle.nn import Conv2D, BatchNorm, AdaptiveAvgPool2D, Linear from paddle.regularizer import L2Decay from paddle.nn.initializer import Uniform, KaimingNormal -from ppcls.utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url +from ....utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url MODEL_URLS = { "GhostNet_x0_5": diff --git a/ppcls/arch/backbone/model_zoo/googlenet.py b/ppcls/arch/backbone/model_zoo/googlenet.py index 1461b1ba52fb22d29d87092af995fc8783daf7b6..468320eb1ee0096b51065bb547f77f204f258d72 100644 --- a/ppcls/arch/backbone/model_zoo/googlenet.py +++ b/ppcls/arch/backbone/model_zoo/googlenet.py @@ -24,7 +24,7 @@ from paddle.nn.initializer import Uniform import math -from ppcls.utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url +from ....utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url MODEL_URLS = { "GoogLeNet": diff --git a/ppcls/arch/backbone/model_zoo/gvt.py b/ppcls/arch/backbone/model_zoo/gvt.py index cf1affea80c8572605ab32e0df4cf56788411662..2e1ae0fedb3e594026da1735645822f8b0d9651a 100644 --- a/ppcls/arch/backbone/model_zoo/gvt.py +++ b/ppcls/arch/backbone/model_zoo/gvt.py @@ -25,7 +25,7 @@ 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 -from ppcls.utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url +from ....utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url MODEL_URLS = { "pcpvt_small": diff --git a/ppcls/arch/backbone/model_zoo/hardnet.py b/ppcls/arch/backbone/model_zoo/hardnet.py index ea24cdfce571d051c39dcdb419fe52beefbaf720..adf351a744dd075a02a497db5508c686db9094b0 100644 --- a/ppcls/arch/backbone/model_zoo/hardnet.py +++ b/ppcls/arch/backbone/model_zoo/hardnet.py @@ -18,7 +18,7 @@ import paddle import paddle.nn as nn -from ppcls.utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url +from ....utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url MODEL_URLS = { 'HarDNet39_ds': diff --git a/ppcls/arch/backbone/model_zoo/inception_v4.py b/ppcls/arch/backbone/model_zoo/inception_v4.py index 73b3c70b7df681e9eb131419ee0fd5749d505871..8f191bf999c0d88422f71de99d517cc01d597fd0 100644 --- a/ppcls/arch/backbone/model_zoo/inception_v4.py +++ b/ppcls/arch/backbone/model_zoo/inception_v4.py @@ -23,7 +23,7 @@ from paddle.nn import AdaptiveAvgPool2D, MaxPool2D, AvgPool2D from paddle.nn.initializer import Uniform import math -from ppcls.utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url +from ....utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url MODEL_URLS = { "InceptionV4": diff --git a/ppcls/arch/backbone/model_zoo/levit.py b/ppcls/arch/backbone/model_zoo/levit.py index a5cb534f857bfdd7ed983838ae08cbc3231e011b..45a00bdaabca416bbcb3b691eecfb03dbfdcf454 100644 --- a/ppcls/arch/backbone/model_zoo/levit.py +++ b/ppcls/arch/backbone/model_zoo/levit.py @@ -27,7 +27,7 @@ from paddle.regularizer import L2Decay from .vision_transformer import trunc_normal_, zeros_, ones_, Identity -from ppcls.utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url +from ....utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url MODEL_URLS = { "LeViT_128S": diff --git a/ppcls/arch/backbone/model_zoo/mixnet.py b/ppcls/arch/backbone/model_zoo/mixnet.py index 38bb7c3c6a78b2474026c8c8b07a08255ec97730..a37022187dcbe89688ba3328dec9ca0471dce4b9 100644 --- a/ppcls/arch/backbone/model_zoo/mixnet.py +++ b/ppcls/arch/backbone/model_zoo/mixnet.py @@ -20,7 +20,7 @@ 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 +from ....utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url MODEL_URLS = { "MixNet_S": diff --git a/ppcls/arch/backbone/model_zoo/mobilenet_v2.py b/ppcls/arch/backbone/model_zoo/mobilenet_v2.py index ab2a89bb884b694a895bd284b69121c0d025a09a..8df54b94947165b5aa1fffdbadb447e440f15b4f 100644 --- a/ppcls/arch/backbone/model_zoo/mobilenet_v2.py +++ b/ppcls/arch/backbone/model_zoo/mobilenet_v2.py @@ -28,7 +28,7 @@ from paddle.nn import AdaptiveAvgPool2D, MaxPool2D, AvgPool2D import math -from ppcls.utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url +from ....utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url MODEL_URLS = { "MobileNetV2_x0_25": diff --git a/ppcls/arch/backbone/model_zoo/mobilevit.py b/ppcls/arch/backbone/model_zoo/mobilevit.py index 7c0de0e620362db85085c92e2436f9a0a0a24f47..7cfaf3459021136bf56520326e05f0630d978575 100644 --- a/ppcls/arch/backbone/model_zoo/mobilevit.py +++ b/ppcls/arch/backbone/model_zoo/mobilevit.py @@ -23,7 +23,7 @@ import paddle.nn.functional as F from paddle.nn.initializer import KaimingUniform, TruncatedNormal, Constant import math -from ppcls.utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url +from ....utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url MODEL_URLS = { "MobileViT_XXS": diff --git a/ppcls/arch/backbone/model_zoo/peleenet.py b/ppcls/arch/backbone/model_zoo/peleenet.py new file mode 100644 index 0000000000000000000000000000000000000000..eef8139e6f7d981476109ea6b27f9c06048f8512 --- /dev/null +++ b/ppcls/arch/backbone/model_zoo/peleenet.py @@ -0,0 +1,263 @@ +# copyright (c) 2022 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. +# +# Code was heavily based on https://github.com/Robert-JunWang/PeleeNet +# reference: https://arxiv.org/pdf/1804.06882.pdf + +import math + +import paddle +import paddle.nn as nn +import paddle.nn.functional as F +from paddle.nn.initializer import Normal, Constant + +from ....utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url + +MODEL_URLS = { + "PeleeNet": "" # TODO +} + +__all__ = MODEL_URLS.keys() + +normal_ = lambda x, mean=0, std=1: Normal(mean, std)(x) +constant_ = lambda x, value=0: Constant(value)(x) +zeros_ = Constant(value=0.) +ones_ = Constant(value=1.) + + +class _DenseLayer(nn.Layer): + def __init__(self, num_input_features, growth_rate, bottleneck_width, + drop_rate): + super(_DenseLayer, self).__init__() + + growth_rate = int(growth_rate / 2) + inter_channel = int(growth_rate * bottleneck_width / 4) * 4 + + if inter_channel > num_input_features / 2: + inter_channel = int(num_input_features / 8) * 4 + print('adjust inter_channel to ', inter_channel) + + self.branch1a = BasicConv2D( + num_input_features, inter_channel, kernel_size=1) + self.branch1b = BasicConv2D( + inter_channel, growth_rate, kernel_size=3, padding=1) + + self.branch2a = BasicConv2D( + num_input_features, inter_channel, kernel_size=1) + self.branch2b = BasicConv2D( + inter_channel, growth_rate, kernel_size=3, padding=1) + self.branch2c = BasicConv2D( + growth_rate, growth_rate, kernel_size=3, padding=1) + + def forward(self, x): + branch1 = self.branch1a(x) + branch1 = self.branch1b(branch1) + + branch2 = self.branch2a(x) + branch2 = self.branch2b(branch2) + branch2 = self.branch2c(branch2) + + return paddle.concat([x, branch1, branch2], 1) + + +class _DenseBlock(nn.Sequential): + def __init__(self, num_layers, num_input_features, bn_size, growth_rate, + drop_rate): + super(_DenseBlock, self).__init__() + for i in range(num_layers): + layer = _DenseLayer(num_input_features + i * growth_rate, + growth_rate, bn_size, drop_rate) + setattr(self, 'denselayer%d' % (i + 1), layer) + + +class _StemBlock(nn.Layer): + def __init__(self, num_input_channels, num_init_features): + super(_StemBlock, self).__init__() + + num_stem_features = int(num_init_features / 2) + + self.stem1 = BasicConv2D( + num_input_channels, + num_init_features, + kernel_size=3, + stride=2, + padding=1) + self.stem2a = BasicConv2D( + num_init_features, + num_stem_features, + kernel_size=1, + stride=1, + padding=0) + self.stem2b = BasicConv2D( + num_stem_features, + num_init_features, + kernel_size=3, + stride=2, + padding=1) + self.stem3 = BasicConv2D( + 2 * num_init_features, + num_init_features, + kernel_size=1, + stride=1, + padding=0) + self.pool = nn.MaxPool2D(kernel_size=2, stride=2) + + def forward(self, x): + out = self.stem1(x) + + branch2 = self.stem2a(out) + branch2 = self.stem2b(branch2) + branch1 = self.pool(out) + + out = paddle.concat([branch1, branch2], 1) + out = self.stem3(out) + + return out + + +class BasicConv2D(nn.Layer): + def __init__(self, in_channels, out_channels, activation=True, **kwargs): + super(BasicConv2D, self).__init__() + self.conv = nn.Conv2D( + in_channels, out_channels, bias_attr=False, **kwargs) + self.norm = nn.BatchNorm2D(out_channels) + self.activation = activation + + def forward(self, x): + x = self.conv(x) + x = self.norm(x) + if self.activation: + return F.relu(x) + else: + return x + + +class PeleeNetDY(nn.Layer): + r"""PeleeNet model class, based on + `"Densely Connected Convolutional Networks" and + "Pelee: A Real-Time Object Detection System on Mobile Devices" ` + + Args: + growth_rate (int or list of 4 ints) - how many filters to add each layer (`k` in paper) + block_config (list of 4 ints) - how many layers in each pooling block + num_init_features (int) - the number of filters to learn in the first convolution layer + bottleneck_width (int or list of 4 ints) - multiplicative factor for number of bottle neck layers + (i.e. bn_size * k features in the bottleneck layer) + drop_rate (float) - dropout rate after each dense layer + class_num (int) - number of classification classes + """ + + def __init__(self, + growth_rate=32, + block_config=[3, 4, 8, 6], + num_init_features=32, + bottleneck_width=[1, 2, 4, 4], + drop_rate=0.05, + class_num=1000): + + super(PeleeNetDY, self).__init__() + + self.features = nn.Sequential(* [('stemblock', _StemBlock( + 3, num_init_features)), ]) + + if type(growth_rate) is list: + growth_rates = growth_rate + assert len(growth_rates) == 4, \ + 'The growth rate must be the list and the size must be 4' + else: + growth_rates = [growth_rate] * 4 + + if type(bottleneck_width) is list: + bottleneck_widths = bottleneck_width + assert len(bottleneck_widths) == 4, \ + 'The bottleneck width must be the list and the size must be 4' + else: + bottleneck_widths = [bottleneck_width] * 4 + + # Each denseblock + num_features = num_init_features + for i, num_layers in enumerate(block_config): + block = _DenseBlock( + num_layers=num_layers, + num_input_features=num_features, + bn_size=bottleneck_widths[i], + growth_rate=growth_rates[i], + drop_rate=drop_rate) + setattr(self.features, 'denseblock%d' % (i + 1), block) + num_features = num_features + num_layers * growth_rates[i] + + setattr( + self.features, + 'transition%d' % (i + 1), + BasicConv2D( + num_features, + num_features, + kernel_size=1, + stride=1, + padding=0)) + + if i != len(block_config) - 1: + setattr( + self.features, + 'transition%d_pool' % (i + 1), + nn.AvgPool2D( + kernel_size=2, stride=2)) + num_features = num_features + + # Linear layer + self.classifier = nn.Linear(num_features, class_num) + self.drop_rate = drop_rate + + self.apply(self._initialize_weights) + + def forward(self, x): + features = self.features(x) + out = F.avg_pool2d( + features, kernel_size=features.shape[2:4]).flatten(1) + if self.drop_rate > 0: + out = F.dropout(out, p=self.drop_rate, training=self.training) + out = self.classifier(out) + return out + + def _initialize_weights(self, m): + if isinstance(m, nn.Conv2D): + n = m._kernel_size[0] * m._kernel_size[1] * m._out_channels + normal_(m.weight, std=math.sqrt(2. / n)) + if m.bias is not None: + zeros_(m.bias) + elif isinstance(m, nn.BatchNorm2D): + ones_(m.weight) + zeros_(m.bias) + elif isinstance(m, nn.Linear): + normal_(m.weight, std=0.01) + zeros_(m.bias) + + +def _load_pretrained(pretrained, model, model_url, use_ssld): + 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 PeleeNet(pretrained=False, use_ssld=False, **kwargs): + model = PeleeNetDY(**kwargs) + _load_pretrained(pretrained, model, MODEL_URLS["PeleeNet"], use_ssld) + return model diff --git a/ppcls/arch/backbone/model_zoo/pvt_v2.py b/ppcls/arch/backbone/model_zoo/pvt_v2.py index e6b5ff27f466f12afba198c2b4994eccb87824f6..7ca8cca94bdc5faf9ac91a845e1c93c685db4664 100644 --- a/ppcls/arch/backbone/model_zoo/pvt_v2.py +++ b/ppcls/arch/backbone/model_zoo/pvt_v2.py @@ -24,7 +24,7 @@ from paddle.nn.initializer import TruncatedNormal, Constant from .vision_transformer import trunc_normal_, zeros_, ones_, to_2tuple, DropPath, Identity, drop_path -from ppcls.utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url +from ....utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url MODEL_URLS = { "PVT_V2_B0": diff --git a/ppcls/arch/backbone/model_zoo/rednet.py b/ppcls/arch/backbone/model_zoo/rednet.py index eb52621c3b5d236cf6dcaa17439383a6963469f0..809122ff74be8564942d4d451ef44ff8ec0363ac 100644 --- a/ppcls/arch/backbone/model_zoo/rednet.py +++ b/ppcls/arch/backbone/model_zoo/rednet.py @@ -20,7 +20,7 @@ import paddle.nn as nn from paddle.vision.models import resnet -from ppcls.utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url +from ....utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url MODEL_URLS = { "RedNet26": diff --git a/ppcls/arch/backbone/model_zoo/regnet.py b/ppcls/arch/backbone/model_zoo/regnet.py index a8c2942023eecf4d80ffb6eb9c66081bc628c8c4..d55163e92d59afde9baa92a937c58f3cccd71a91 100644 --- a/ppcls/arch/backbone/model_zoo/regnet.py +++ b/ppcls/arch/backbone/model_zoo/regnet.py @@ -29,7 +29,7 @@ from paddle.nn import AdaptiveAvgPool2D, MaxPool2D, AvgPool2D from paddle.nn.initializer import Uniform import math -from ppcls.utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url +from ....utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url MODEL_URLS = { "RegNetX_200MF": diff --git a/ppcls/arch/backbone/model_zoo/repvgg.py b/ppcls/arch/backbone/model_zoo/repvgg.py index 12f65549fad60adae6a412d8adb05f9846922c81..b30098277b523e50da11108e77127b179345bd34 100644 --- a/ppcls/arch/backbone/model_zoo/repvgg.py +++ b/ppcls/arch/backbone/model_zoo/repvgg.py @@ -19,7 +19,7 @@ import paddle.nn as nn import paddle import numpy as np -from ppcls.utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url +from ....utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url MODEL_URLS = { "RepVGG_A0": diff --git a/ppcls/arch/backbone/model_zoo/res2net.py b/ppcls/arch/backbone/model_zoo/res2net.py index 87187075578ea312c13cd5e56cfa24fe8da0d8ce..5684e56844262dca019f80ea5cfe30be15cb889f 100644 --- a/ppcls/arch/backbone/model_zoo/res2net.py +++ b/ppcls/arch/backbone/model_zoo/res2net.py @@ -29,7 +29,7 @@ from paddle.nn.initializer import Uniform import math -from ppcls.utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url +from ....utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url MODEL_URLS = { "Res2Net50_26w_4s": diff --git a/ppcls/arch/backbone/model_zoo/res2net_vd.py b/ppcls/arch/backbone/model_zoo/res2net_vd.py index 2139e198819c6768b975b339e9373fe7f6334f10..9d474ca2cc5476dc3e4089c8bec5ee85ef04496b 100644 --- a/ppcls/arch/backbone/model_zoo/res2net_vd.py +++ b/ppcls/arch/backbone/model_zoo/res2net_vd.py @@ -29,7 +29,7 @@ from paddle.nn.initializer import Uniform import math -from ppcls.utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url +from ....utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url MODEL_URLS = { "Res2Net50_vd_26w_4s": diff --git a/ppcls/arch/backbone/model_zoo/resnest.py b/ppcls/arch/backbone/model_zoo/resnest.py index b4a978cc9e3fe68180348120be73deff229b77d2..c9f7850fbade0537704efd0cecadb7df07b28b8e 100644 --- a/ppcls/arch/backbone/model_zoo/resnest.py +++ b/ppcls/arch/backbone/model_zoo/resnest.py @@ -30,7 +30,7 @@ from paddle.nn import Conv2D, BatchNorm, Linear, Dropout from paddle.nn import AdaptiveAvgPool2D, MaxPool2D, AvgPool2D from paddle.regularizer import L2Decay -from ppcls.utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url +from ....utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url MODEL_URLS = { "ResNeSt50_fast_1s1x64d": diff --git a/ppcls/arch/backbone/model_zoo/resnet_vc.py b/ppcls/arch/backbone/model_zoo/resnet_vc.py index 1cbd0e9cec423ac71c35bd866f7037326d3350b6..99093e62da60d8f8bc98e98117c29d18cb116fe3 100644 --- a/ppcls/arch/backbone/model_zoo/resnet_vc.py +++ b/ppcls/arch/backbone/model_zoo/resnet_vc.py @@ -29,7 +29,7 @@ from paddle.nn.initializer import Uniform import math -from ppcls.utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url +from ....utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url MODEL_URLS = { "ResNet50_vc": diff --git a/ppcls/arch/backbone/model_zoo/resnext.py b/ppcls/arch/backbone/model_zoo/resnext.py index 3ab21399fc1bd695accfd1c608536c9a2631f0b3..4d9d9a5f0f0e3cb8737ad93911b9ea6e98d2131a 100644 --- a/ppcls/arch/backbone/model_zoo/resnext.py +++ b/ppcls/arch/backbone/model_zoo/resnext.py @@ -29,7 +29,7 @@ from paddle.nn.initializer import Uniform import math -from ppcls.utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url +from ....utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url MODEL_URLS = { "ResNeXt50_32x4d": diff --git a/ppcls/arch/backbone/model_zoo/resnext101_wsl.py b/ppcls/arch/backbone/model_zoo/resnext101_wsl.py index bcf2bf542cf136fa61eda5a4d682ea63adc7f803..d1c86ecfcdbb4dcad7052dc4c67921511059c899 100644 --- a/ppcls/arch/backbone/model_zoo/resnext101_wsl.py +++ b/ppcls/arch/backbone/model_zoo/resnext101_wsl.py @@ -22,7 +22,7 @@ from paddle.nn import Conv2D, BatchNorm, Linear, Dropout from paddle.nn import AdaptiveAvgPool2D, MaxPool2D, AvgPool2D from paddle.nn.initializer import Uniform -from ppcls.utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url +from ....utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url MODEL_URLS = { "ResNeXt101_32x8d_wsl": diff --git a/ppcls/arch/backbone/model_zoo/resnext_vd.py b/ppcls/arch/backbone/model_zoo/resnext_vd.py index d57e71e346514f779c0376541a3570665067a5f3..e2e31f9d1677b0cb0a04b410bb018dfc2ec94e60 100644 --- a/ppcls/arch/backbone/model_zoo/resnext_vd.py +++ b/ppcls/arch/backbone/model_zoo/resnext_vd.py @@ -29,7 +29,7 @@ from paddle.nn.initializer import Uniform import math -from ppcls.utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url +from ....utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url MODEL_URLS = { "ResNeXt50_vd_32x4d": diff --git a/ppcls/arch/backbone/model_zoo/rexnet.py b/ppcls/arch/backbone/model_zoo/rexnet.py index d2f32af395698399d8314cf029429cb9eb641e0a..4ab20eea48eb07500eb93a174bf5ef589183a2db 100644 --- a/ppcls/arch/backbone/model_zoo/rexnet.py +++ b/ppcls/arch/backbone/model_zoo/rexnet.py @@ -24,7 +24,7 @@ from paddle import ParamAttr import paddle.nn as nn from math import ceil -from ppcls.utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url +from ....utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url MODEL_URLS = { "ReXNet_1_0": diff --git a/ppcls/arch/backbone/model_zoo/se_resnet_vd.py b/ppcls/arch/backbone/model_zoo/se_resnet_vd.py index bded11fb9437e6a8d7d43ffc9dd923ca6a244772..8b1799a207b572e7cde87f57c1f2e503768be8a4 100644 --- a/ppcls/arch/backbone/model_zoo/se_resnet_vd.py +++ b/ppcls/arch/backbone/model_zoo/se_resnet_vd.py @@ -28,7 +28,7 @@ from paddle.nn.initializer import Uniform import math -from ppcls.utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url +from ....utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url MODEL_URLS = { "SE_ResNet18_vd": diff --git a/ppcls/arch/backbone/model_zoo/se_resnext.py b/ppcls/arch/backbone/model_zoo/se_resnext.py index d1b985b42b20afeb4427a50d8124ce805e36e434..ea5046fdfed9f7ebef3b6dce1e181c205f0c8332 100644 --- a/ppcls/arch/backbone/model_zoo/se_resnext.py +++ b/ppcls/arch/backbone/model_zoo/se_resnext.py @@ -29,7 +29,7 @@ from paddle.nn.initializer import Uniform import math -from ppcls.utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url +from ....utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url MODEL_URLS = { "SE_ResNeXt50_32x4d": diff --git a/ppcls/arch/backbone/model_zoo/se_resnext_vd.py b/ppcls/arch/backbone/model_zoo/se_resnext_vd.py index 78582f257bc0c890f942f8c5e32374e0de69d934..f83bf8c493299857b84d0336e782eb172e7dd7af 100644 --- a/ppcls/arch/backbone/model_zoo/se_resnext_vd.py +++ b/ppcls/arch/backbone/model_zoo/se_resnext_vd.py @@ -29,7 +29,7 @@ from paddle.nn.initializer import Uniform import math -from ppcls.utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url +from ....utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url MODEL_URLS = { "SE_ResNeXt50_vd_32x4d": diff --git a/ppcls/arch/backbone/model_zoo/shufflenet_v2.py b/ppcls/arch/backbone/model_zoo/shufflenet_v2.py index c769afdd4b238fa0a7b92fdb72c3962645a2ac8f..58be569deca3f868b9f55267bf1e7810dda7d1c7 100644 --- a/ppcls/arch/backbone/model_zoo/shufflenet_v2.py +++ b/ppcls/arch/backbone/model_zoo/shufflenet_v2.py @@ -24,7 +24,7 @@ from paddle.nn import Layer, Conv2D, MaxPool2D, AdaptiveAvgPool2D, BatchNorm, Li from paddle.nn.initializer import KaimingNormal from paddle.nn.functional import swish -from ppcls.utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url +from ....utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url MODEL_URLS = { "ShuffleNetV2_x0_25": diff --git a/ppcls/arch/backbone/model_zoo/squeezenet.py b/ppcls/arch/backbone/model_zoo/squeezenet.py index 3e8ea5d2a7f111c1cecd1374203d85817cecbe49..cac9b752e9e1c94fae35a0f53f9557ab6cf13743 100644 --- a/ppcls/arch/backbone/model_zoo/squeezenet.py +++ b/ppcls/arch/backbone/model_zoo/squeezenet.py @@ -21,7 +21,7 @@ import paddle.nn.functional as F from paddle.nn import Conv2D, BatchNorm, Linear, Dropout from paddle.nn import AdaptiveAvgPool2D, MaxPool2D, AvgPool2D -from ppcls.utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url +from ....utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url MODEL_URLS = { "SqueezeNet1_0": diff --git a/ppcls/arch/backbone/model_zoo/tnt.py b/ppcls/arch/backbone/model_zoo/tnt.py index c463ef2002101ddb3017164a64cbec9782ff2f4c..b9d8327098db724d3514987ffac6cb4091753caa 100644 --- a/ppcls/arch/backbone/model_zoo/tnt.py +++ b/ppcls/arch/backbone/model_zoo/tnt.py @@ -23,8 +23,8 @@ import paddle.nn as nn from paddle.nn.initializer import TruncatedNormal, Constant -from ppcls.arch.backbone.base.theseus_layer import Identity -from ppcls.utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url +from ..base.theseus_layer import Identity +from ....utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url MODEL_URLS = { "TNT_small": diff --git a/ppcls/arch/backbone/model_zoo/van.py b/ppcls/arch/backbone/model_zoo/van.py index 17ae5d69b8862173a84f60d684e2f8cac8c4791a..c4d7339292faf8b11c9a4a637b7ae357b1bae1fd 100644 --- a/ppcls/arch/backbone/model_zoo/van.py +++ b/ppcls/arch/backbone/model_zoo/van.py @@ -21,7 +21,7 @@ import paddle import paddle.nn as nn from paddle.nn.initializer import TruncatedNormal, Constant -from ppcls.utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url +from ....utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url MODEL_URLS = { "VAN_tiny": "", # TODO diff --git a/ppcls/arch/backbone/model_zoo/vision_transformer.py b/ppcls/arch/backbone/model_zoo/vision_transformer.py index 35796e5e9610587d794428dc8284cab5bae3d554..acecf61f1cd7b517a855301b324a7639a6c047ee 100644 --- a/ppcls/arch/backbone/model_zoo/vision_transformer.py +++ b/ppcls/arch/backbone/model_zoo/vision_transformer.py @@ -22,7 +22,7 @@ import paddle import paddle.nn as nn from paddle.nn.initializer import TruncatedNormal, Constant, Normal -from ppcls.utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url +from ....utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url MODEL_URLS = { "ViT_small_patch16_224": diff --git a/ppcls/arch/backbone/model_zoo/xception.py b/ppcls/arch/backbone/model_zoo/xception.py index 966092b98779e1ec60bacd92b060e27549e2724d..2be02a19e6126daf181215fa8532480167602fe3 100644 --- a/ppcls/arch/backbone/model_zoo/xception.py +++ b/ppcls/arch/backbone/model_zoo/xception.py @@ -24,7 +24,7 @@ from paddle.nn.initializer import Uniform import math import sys -from ppcls.utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url +from ....utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url MODEL_URLS = { "Xception41": diff --git a/ppcls/arch/backbone/model_zoo/xception_deeplab.py b/ppcls/arch/backbone/model_zoo/xception_deeplab.py index 2a80d173684ce207211b0173f7bd6c58a7e3e30d..7294ab66f433ab15e55b529202f4a5753e6d9427 100644 --- a/ppcls/arch/backbone/model_zoo/xception_deeplab.py +++ b/ppcls/arch/backbone/model_zoo/xception_deeplab.py @@ -21,7 +21,7 @@ import paddle.nn.functional as F from paddle.nn import Conv2D, BatchNorm, Linear, Dropout from paddle.nn import AdaptiveAvgPool2D, MaxPool2D, AvgPool2D -from ppcls.utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url +from ....utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url MODEL_URLS = { "Xception41_deeplab": diff --git a/ppcls/arch/backbone/variant_models/pp_lcnet_variant.py b/ppcls/arch/backbone/variant_models/pp_lcnet_variant.py index dc9747af0843b276249277a78d46c75da7209c57..e4c25c4c6db2e5867882b5dc441a8bd99447afe1 100644 --- a/ppcls/arch/backbone/variant_models/pp_lcnet_variant.py +++ b/ppcls/arch/backbone/variant_models/pp_lcnet_variant.py @@ -1,7 +1,7 @@ import paddle from paddle.nn import Sigmoid from paddle.nn import Tanh -from ppcls.arch.backbone.legendary_models.pp_lcnet import PPLCNet_x2_5 +from ..legendary_models.pp_lcnet import PPLCNet_x2_5 __all__ = ["PPLCNet_x2_5_Tanh"] diff --git a/ppcls/arch/backbone/variant_models/resnet_variant.py b/ppcls/arch/backbone/variant_models/resnet_variant.py index 0219344b12dfeec3cb864a52fd152855b852e6be..7c14407eacb60196b7794119e3dc71afbd99d968 100644 --- a/ppcls/arch/backbone/variant_models/resnet_variant.py +++ b/ppcls/arch/backbone/variant_models/resnet_variant.py @@ -1,5 +1,5 @@ from paddle.nn import Conv2D -from ppcls.arch.backbone.legendary_models.resnet import ResNet50, MODEL_URLS, _load_pretrained +from ..legendary_models.resnet import ResNet50, MODEL_URLS, _load_pretrained __all__ = ["ResNet50_last_stage_stride1"] diff --git a/ppcls/arch/backbone/variant_models/vgg_variant.py b/ppcls/arch/backbone/variant_models/vgg_variant.py index c1f75ba904a26900df9bd545728d600c1b2be05a..36ce47768e77fce796663c54ab7efa5a98bc6d36 100644 --- a/ppcls/arch/backbone/variant_models/vgg_variant.py +++ b/ppcls/arch/backbone/variant_models/vgg_variant.py @@ -1,6 +1,6 @@ import paddle from paddle.nn import Sigmoid -from ppcls.arch.backbone.legendary_models.vgg import VGG19 +from ..legendary_models.vgg import VGG19 __all__ = ["VGG19Sigmoid"] diff --git a/ppcls/arch/gears/bnneck.py b/ppcls/arch/gears/bnneck.py index c2f10c79f9c3862102f7b425c18018d2c4cce15e..916a612abb27ef7d89b5155496d8d90b3f5ec0c2 100644 --- a/ppcls/arch/gears/bnneck.py +++ b/ppcls/arch/gears/bnneck.py @@ -17,7 +17,7 @@ from __future__ import absolute_import, division, print_function import paddle import paddle.nn as nn -from ppcls.arch.utils import get_param_attr_dict +from ..utils import get_param_attr_dict class BNNeck(nn.Layer): diff --git a/ppcls/arch/gears/fc.py b/ppcls/arch/gears/fc.py index 279c5496e4aeeef86f1ebdafbdbfe7468391fa2d..622b0d37d60d12901ebf434dc16d4f0ef9ed6906 100644 --- a/ppcls/arch/gears/fc.py +++ b/ppcls/arch/gears/fc.py @@ -19,7 +19,7 @@ from __future__ import print_function import paddle import paddle.nn as nn -from ppcls.arch.utils import get_param_attr_dict +from ..utils import get_param_attr_dict class FC(nn.Layer): diff --git a/ppcls/arch/slim/__init__.py b/ppcls/arch/slim/__init__.py index 3733059cee256d305c49ba0fb49e758e8f50d165..734bfefad7b681e15a3425cfad8d1955c6c6494b 100644 --- a/ppcls/arch/slim/__init__.py +++ b/ppcls/arch/slim/__init__.py @@ -12,5 +12,5 @@ # See the License for the specific language governing permissions and # limitations under the License. -from ppcls.arch.slim.prune import prune_model -from ppcls.arch.slim.quant import quantize_model +from .prune import prune_model +from .quant import quantize_model diff --git a/ppcls/arch/slim/prune.py b/ppcls/arch/slim/prune.py index c0c9d220bbd48d409087781b2b9b6709f2acca71..59cd411a1c46739b19b8740bb0ec6fa8d0e0e194 100644 --- a/ppcls/arch/slim/prune.py +++ b/ppcls/arch/slim/prune.py @@ -14,7 +14,7 @@ from __future__ import absolute_import, division, print_function import paddle -from ppcls.utils import logger +from ...utils import logger def prune_model(config, model): @@ -37,7 +37,6 @@ def prune_model(config, model): model.pruner = None - def _prune_model(config, model): from paddleslim.analysis import dygraph_flops as flops logger.info("FLOPs before pruning: {}GFLOPs".format( diff --git a/ppcls/arch/slim/quant.py b/ppcls/arch/slim/quant.py index 3e7b4a242be22d8bca57a36ef3183201068cb0b6..659dc3c34f067d428e1ec6f713d461151a89bc15 100644 --- a/ppcls/arch/slim/quant.py +++ b/ppcls/arch/slim/quant.py @@ -14,7 +14,7 @@ from __future__ import absolute_import, division, print_function import paddle -from ppcls.utils import logger +from ...utils import logger QUANT_CONFIG = { # weight preprocess type, default is None and no preprocessing is performed. diff --git a/ppcls/utils/check.py b/ppcls/utils/check.py index 59255124a6c82a072f351c832ffe16ec6bd269c4..8b743335ea2b68f8afc8cf78782c5997bd6b6b15 100644 --- a/ppcls/utils/check.py +++ b/ppcls/utils/check.py @@ -22,8 +22,8 @@ import sys import paddle from paddle import is_compiled_with_cuda -from ppcls.arch.utils import get_architectures, similar_architectures, get_blacklist_model_in_static_mode -from ppcls.utils import logger +from ..arch.utils import get_architectures, similar_architectures, get_blacklist_model_in_static_mode +from . import logger def check_version(): diff --git a/ppcls/utils/config.py b/ppcls/utils/config.py index 1c2f5fdb8b40a19e7373e6298e3f6b6169e9e8b4..3241efcf9ac0418f981dacabb931d22da95f8967 100644 --- a/ppcls/utils/config.py +++ b/ppcls/utils/config.py @@ -16,8 +16,9 @@ import os import copy import argparse import yaml -from ppcls.utils import logger -from ppcls.utils import check +from . import logger +from . import check + __all__ = ['get_config'] diff --git a/ppcls/utils/download.py b/ppcls/utils/download.py index 51d45438880c940929acf2f8542eec937052f21b..e9f9847e5d5b780daa8dc549c42370e54893f0db 100644 --- a/ppcls/utils/download.py +++ b/ppcls/utils/download.py @@ -28,7 +28,7 @@ import time from collections import OrderedDict from tqdm import tqdm -from ppcls.utils import logger +from . import logger __all__ = ['get_weights_path_from_url'] diff --git a/ppcls/utils/gallery2fc.py b/ppcls/utils/gallery2fc.py index e654522cf5d70eaa162581dd684fb4131d4f91c3..a3980fb85c14109bd58a9920ad77fb99e348be1f 100644 --- a/ppcls/utils/gallery2fc.py +++ b/ppcls/utils/gallery2fc.py @@ -20,12 +20,12 @@ import sys __dir__ = os.path.dirname(os.path.abspath(__file__)) sys.path.append(os.path.abspath(os.path.join(__dir__, '../../'))) -from ppcls.arch import build_model -from ppcls.utils.config import parse_config, parse_args -from ppcls.utils.save_load import load_dygraph_pretrain -from ppcls.utils.logger import init_logger -from ppcls.data import create_operators -from ppcls.arch.slim import quantize_model +from ..arch import build_model +from .config import parse_config, parse_args +from .save_load import load_dygraph_pretrain +from .logger import init_logger +from ..data import create_operators +from ..arch.slim import quantize_model class GalleryLayer(paddle.nn.Layer): diff --git a/ppcls/utils/model_zoo.py b/ppcls/utils/model_zoo.py index fce9f4644ca4b918fdcb07b3d8750dcf45b43e6f..e9ab5992dd58a758c365aa24b9ab5159fc3c7b68 100644 --- a/ppcls/utils/model_zoo.py +++ b/ppcls/utils/model_zoo.py @@ -23,8 +23,8 @@ import tarfile import tqdm import zipfile -from ppcls.arch.utils import similar_architectures -from ppcls.utils import logger +from ..arch.utils import similar_architectures +from . import logger __all__ = ['get'] diff --git a/ppcls/utils/save_load.py b/ppcls/utils/save_load.py index 04486cc273bbfe9e3d9863b4c4ded6a8d283eee3..721a327e5b1d817bec5d297ce4ac468a8f7c07e5 100644 --- a/ppcls/utils/save_load.py +++ b/ppcls/utils/save_load.py @@ -20,7 +20,7 @@ import errno import os import paddle -from ppcls.utils import logger +from . import logger from .download import get_weights_path_from_url __all__ = ['init_model', 'save_model', 'load_dygraph_pretrain']