From 6cb17cfe44548fc4e8a9e86047bf6f3f2a707771 Mon Sep 17 00:00:00 2001 From: root Date: Wed, 17 Aug 2022 14:34:06 +0000 Subject: [PATCH] fix: change to relative import --- __init__.py | 2 +- ppcls/arch/__init__.py | 10 +- ppcls/arch/backbone/__init__.py | 110 ++++---- ppcls/arch/backbone/base/theseus_layer.py | 2 +- ppcls/arch/backbone/legendary_models/esnet.py | 4 +- ppcls/arch/backbone/legendary_models/hrnet.py | 4 +- .../backbone/legendary_models/inception_v3.py | 4 +- .../backbone/legendary_models/mobilenet_v1.py | 4 +- .../backbone/legendary_models/mobilenet_v3.py | 5 +- .../backbone/legendary_models/pp_hgnet.py | 7 +- .../backbone/legendary_models/pp_lcnet.py | 40 ++- .../backbone/legendary_models/pp_lcnet_v2.py | 5 +- .../arch/backbone/legendary_models/resnet.py | 8 +- .../legendary_models/swin_transformer.py | 6 +- ppcls/arch/backbone/legendary_models/vgg.py | 4 +- ppcls/arch/backbone/model_zoo/alexnet.py | 2 +- ppcls/arch/backbone/model_zoo/convnext.py | 232 +++++++++++++++ ppcls/arch/backbone/model_zoo/cspnet.py | 2 +- .../backbone/model_zoo/cswin_transformer.py | 2 +- ppcls/arch/backbone/model_zoo/darknet.py | 2 +- ppcls/arch/backbone/model_zoo/densenet.py | 2 +- .../model_zoo/distilled_vision_transformer.py | 2 +- ppcls/arch/backbone/model_zoo/dla.py | 4 +- ppcls/arch/backbone/model_zoo/dpn.py | 2 +- ppcls/arch/backbone/model_zoo/efficientnet.py | 2 +- ppcls/arch/backbone/model_zoo/ghostnet.py | 2 +- ppcls/arch/backbone/model_zoo/googlenet.py | 2 +- ppcls/arch/backbone/model_zoo/gvt.py | 2 +- ppcls/arch/backbone/model_zoo/hardnet.py | 2 +- ppcls/arch/backbone/model_zoo/inception_v4.py | 2 +- ppcls/arch/backbone/model_zoo/levit.py | 2 +- ppcls/arch/backbone/model_zoo/mixnet.py | 2 +- ppcls/arch/backbone/model_zoo/mobilenet_v2.py | 2 +- ppcls/arch/backbone/model_zoo/mobilevit.py | 2 +- ppcls/arch/backbone/model_zoo/peleenet.py | 263 ++++++++++++++++++ ppcls/arch/backbone/model_zoo/pvt_v2.py | 2 +- ppcls/arch/backbone/model_zoo/rednet.py | 2 +- ppcls/arch/backbone/model_zoo/regnet.py | 2 +- ppcls/arch/backbone/model_zoo/repvgg.py | 2 +- ppcls/arch/backbone/model_zoo/res2net.py | 2 +- ppcls/arch/backbone/model_zoo/res2net_vd.py | 2 +- ppcls/arch/backbone/model_zoo/resnest.py | 2 +- ppcls/arch/backbone/model_zoo/resnet_vc.py | 2 +- ppcls/arch/backbone/model_zoo/resnext.py | 2 +- .../arch/backbone/model_zoo/resnext101_wsl.py | 2 +- ppcls/arch/backbone/model_zoo/resnext_vd.py | 2 +- ppcls/arch/backbone/model_zoo/rexnet.py | 2 +- ppcls/arch/backbone/model_zoo/se_resnet_vd.py | 2 +- ppcls/arch/backbone/model_zoo/se_resnext.py | 2 +- .../arch/backbone/model_zoo/se_resnext_vd.py | 2 +- .../arch/backbone/model_zoo/shufflenet_v2.py | 2 +- ppcls/arch/backbone/model_zoo/squeezenet.py | 2 +- ppcls/arch/backbone/model_zoo/tnt.py | 4 +- ppcls/arch/backbone/model_zoo/van.py | 2 +- .../backbone/model_zoo/vision_transformer.py | 2 +- ppcls/arch/backbone/model_zoo/xception.py | 2 +- .../backbone/model_zoo/xception_deeplab.py | 2 +- .../variant_models/pp_lcnet_variant.py | 2 +- .../backbone/variant_models/resnet_variant.py | 2 +- .../backbone/variant_models/vgg_variant.py | 2 +- ppcls/arch/gears/bnneck.py | 2 +- ppcls/arch/gears/fc.py | 2 +- ppcls/arch/slim/__init__.py | 4 +- ppcls/arch/slim/prune.py | 3 +- ppcls/arch/slim/quant.py | 2 +- ppcls/utils/check.py | 4 +- ppcls/utils/config.py | 5 +- ppcls/utils/download.py | 2 +- ppcls/utils/gallery2fc.py | 12 +- ppcls/utils/model_zoo.py | 4 +- ppcls/utils/save_load.py | 2 +- 71 files changed, 668 insertions(+), 174 deletions(-) create mode 100644 ppcls/arch/backbone/model_zoo/convnext.py create mode 100644 ppcls/arch/backbone/model_zoo/peleenet.py diff --git a/__init__.py b/__init__.py index 2128a6cc..e65204ba 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 40214579..213bef9d 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 e9573584..d272f230 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 9f3f596a..a533cdc7 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 e05e0ceb..d8dc6e6f 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 39913151..a5a3d8bc 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 74a3d1fd..5bec7588 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 3d6caeb0..3e612068 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 3fbf9776..34d497de 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 a5add431..9ca386f0 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 a4fe6fad..017592d3 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 459d8427..40264092 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 705511f5..2a792dde 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 9500a941..b72035ef 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 b7124961..3e79815e 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 90e1d7e1..f389f0fb 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 00000000..b2745ff1 --- /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 bf4e061e..e2dc45ab 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 429edbe1..0d5d729d 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 34ca9a2e..17a6b43e 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 d658a18f..14886576 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 e816e804..2ccd3c7b 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 d5ee822c..81a24df6 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 2eb2647e..828e8dfd 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 9ddb90fb..dec40d37 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 5cfa56eb..1fa6d155 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 1461b1ba..468320eb 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 cf1affea..2e1ae0fe 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 ea24cdfc..adf351a7 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 73b3c70b..8f191bf9 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 a5cb534f..45a00bda 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 38bb7c3c..a3702218 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 ab2a89bb..8df54b94 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 7c0de0e6..7cfaf345 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 00000000..eef8139e --- /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 e6b5ff27..7ca8cca9 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 eb52621c..809122ff 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 a8c29420..d55163e9 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 12f65549..b3009827 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 87187075..5684e568 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 2139e198..9d474ca2 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 b4a978cc..c9f7850f 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 1cbd0e9c..99093e62 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 3ab21399..4d9d9a5f 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 bcf2bf54..d1c86ecf 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 d57e71e3..e2e31f9d 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 d2f32af3..4ab20eea 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 bded11fb..8b1799a2 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 d1b985b4..ea5046fd 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 78582f25..f83bf8c4 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 c769afdd..58be569d 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 3e8ea5d2..cac9b752 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 c463ef20..b9d83270 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 17ae5d69..c4d73392 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 35796e5e..acecf61f 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 966092b9..2be02a19 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 2a80d173..7294ab66 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 dc9747af..e4c25c4c 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 0219344b..7c14407e 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 c1f75ba9..36ce4776 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 c2f10c79..916a612a 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 279c5496..622b0d37 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 3733059c..734bfefa 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 c0c9d220..59cd411a 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 3e7b4a24..659dc3c3 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 59255124..8b743335 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 1c2f5fdb..3241efcf 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 51d45438..e9f9847e 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 e654522c..a3980fb8 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 fce9f464..e9ab5992 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 04486cc2..721a327e 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'] -- GitLab