PaddleSlim icon indicating copy to clipboard operation
PaddleSlim copied to clipboard

Pruning models in PaddleClas dynamic graph

Open wanghaoshuang opened this issue 3 years ago • 3 comments

安装PaddleSlim develop分支,或PaddleSlim2.1版本(预计5月20日前发布)。

在PaddleClas路径下执行以下命令,验证PaddleClas中所有模型的剪裁效果(仅计算FLOPs, 不重训模型):

find configs/ -name *.yaml | xargs -i python tools/prune.py -c {}

以上命令生成测试结果在文件PaddleClas/test_result中。

PaddleClas/tools/prune.py代码如下:

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import argparse
import os
import sys
__dir__ = os.path.dirname(os.path.abspath(__file__))
sys.path.append(__dir__)
sys.path.append(os.path.abspath(os.path.join(__dir__, '..')))

import paddle

from ppcls.data import Reader
from ppcls.utils.config import get_config
from ppcls.utils.save_load import init_model, save_model
from ppcls.utils import logger
import program
from paddleslim.dygraph import L1NormFilterPruner


def parse_args():
    parser = argparse.ArgumentParser("PaddleClas train script")
    parser.add_argument(
        '-c',
        '--config',
        type=str,
        default='configs/ResNet/ResNet50.yaml',
        help='config file path')
    parser.add_argument(
        '-o',
        '--override',
        action='append',
        default=[],
        help='config options to be overridden')
    args = parser.parse_args()
    return args


def main(args):
    config = get_config(args.config, overrides=args.override, show=True)
    net = program.create_model(config.ARCHITECTURE, config.classes_num)
    params = {}
    for param in net.parameters():
        if len(param.shape) == 4:
            params[param.name] = 0.5

    pruner = L1NormFilterPruner(net, [1, 3, 32, 32])
    b_FLOPs = paddle.flops(net, input_size=[1, 3, 32, 32])
    plan = pruner.prune_vars(params, axis=0)
    a_FLOPs = paddle.flops(net, input_size=[1, 3, 32, 32])
    ret = (float(b_FLOPs - a_FLOPs) / (b_FLOPs)) * 100
    ret = "model: {}; FLOPs -{:.1f}%\n".format(args.config, ret)
    with open("./test_result", 'a') as f:
        f.write(ret)
    print(ret)

if __name__ == '__main__':
    args = parse_args()
    main(args)

wanghaoshuang avatar Apr 20 '21 08:04 wanghaoshuang

supported models in PaddleClas

  • [x] ShuffleNetV2_x0_33
  • [x] ShuffleNetV2_x2_0
  • [x] ShuffleNetV2_x1_5
  • [x] ShuffleNetV2_x0_25
  • [x] ShuffleNetV2_swish
  • [x] ShuffleNetV2_x1_0
  • [x] ShuffleNetV2_x0_5
  • [x] ResNet50_Mixup
  • [x] ResNet50_GridMask
  • [x] ResNet50_Cutmix
  • [x] ResNet50_Baseline
  • [x] ResNet50_HideAndSeek
  • [x] ResNet50_AutoAugment
  • [x] ResNet50_RandAugment
  • [x] ResNet50_Cutout
  • [x] ResNet50_RandomErasing
  • [x] HRNet_W64_C
  • [x] HRNet_W30_C
  • [x] HRNet_W48_C
  • [x] HRNet_W40_C
  • [x] HRNet_W44_C
  • [x] HRNet_W32_C
  • [x] HRNet_W18_C
  • [x] ResNet50_vd
  • [x] MobileNetV3_large_x1_0_finetune
  • [x] ResNet50_vd_finetune
  • [x] R50_vd_distill_MV3_large_x1_0
  • [x] VGG16_finetune_kunlun
  • [x] ResNet50_vd_ssld_finetune
  • [x] VGG19_finetune_kunlun
  • [x] ResNet50_vd_finetune_kunlun
  • [x] ResNet50_vd_ssld_random_erasing_finetune
  • [x] HRNet_W18_C_finetune_kunlun
  • [x] Xception41_deeplab
  • [x] Xception65_deeplab
  • [x] Xception65
  • [x] Xception71
  • [x] Xception41
  • [x] GoogLeNet
  • [x] InceptionV4
  • [x] InceptionV3
  • [x] DarkNet53
  • [x] VGG11
  • [x] VGG13
  • [x] VGG16
  • [x] VGG19
  • [x] MobileNetV2_x0_25
  • [x] MobileNetV2_x0_5
  • [x] MobileNetV2_x1_5
  • [x] MobileNetV2_x0_75
  • [x] MobileNetV2_x2_0
  • [x] MobileNetV2
  • [x] ResNeSt50
  • [x] ResNeSt101
  • [x] ResNeSt50_fast_1s1x64d
  • [x] Res2Net50_14w_8s
  • [x] Res2Net50_26w_4s
  • [x] Res2Net50_vd_26w_4s
  • [x] Res2Net200_vd_26w_4s
  • [x] Res2Net101_vd_26w_4s
  • [x] ResNet50_vd
  • [x] ResNet34
  • [x] ResNet101_vd
  • [x] ResNet18_vd
  • [x] ResNet200_vd
  • [x] ResNet50_vc
  • [x] ResNet101
  • [x] ResNet152
  • [x] ResNet18
  • [x] ResNet34_vd
  • [x] ResNet152_vd
  • [x] ResNet50
  • [x] MobileNetV3_large_x1_25
  • [x] MobileNetV3_small_x1_0
  • [x] MobileNetV3_small_x0_5
  • [x] MobileNetV3_large_x0_5
  • [x] MobileNetV3_large_x0_75
  • [x] MobileNetV3_large_x1_0
  • [x] MobileNetV3_small_x0_35
  • [x] MobileNetV3_small_x1_25
  • [x] MobileNetV3_small_x0_75
  • [x] MobileNetV3_large_x0_35
  • [x] AlexNet
  • [x] RegNetX_4GF
  • [x] DPN98
  • [x] DPN107
  • [x] DPN92
  • [x] DPN68
  • [x] DPN131
  • [x] EfficientNetB0
  • [x] MobileNetV1_x0_5
  • [x] MobileNetV1_x0_75
  • [x] MobileNetV1
  • [x] MobileNetV1_x0_25
  • [x] ResX101_32x16d_wsl_distill_R50_vd
  • [x] R50_vd_distill_MV3_large_x1_0
  • [x] SE_ResNet50_vd
  • [x] SE_ResNeXt50_32x4d
  • [x] SE_ResNeXt50_vd_32x4d
  • [x] SE_ResNet18_vd
  • [x] SE_ResNet34_vd
  • [x] SE_ResNeXt101_32x4d
  • [x] SENet154_vd
  • [x] SqueezeNet1_1
  • [x] SqueezeNet1_0
  • [x] ResNeXt101_32x8d_wsl
  • [ ] GhostNet_x1_3
  • [ ] GhostNet_x0_5
  • [ ] GhostNet_x1_0
  • [x] ResNeXt50_64x4d
  • [x] ResNeXt101_vd_32x4d
  • [x] ResNeXt50_vd_32x4d
  • [x] ResNeXt152_vd_64x4d
  • [x] ResNeXt152_64x4d
  • [x] ResNeXt152_vd_32x4d
  • [x] ResNeXt101_32x4d
  • [x] ResNeXt101_vd_64x4d
  • [x] ResNeXt101_64x4d
  • [x] ResNeXt50_32x4d
  • [x] ResNeXt50_vd_64x4d
  • [x] ResNeXt152_32x4d
  • [x] DenseNet264
  • [x] DenseNet201
  • [x] DenseNet121
  • [x] DenseNet161
  • [x] DenseNet169

wanghaoshuang avatar Apr 20 '21 08:04 wanghaoshuang

GhostNet结构如下:

image

wanghaoshuang avatar Apr 21 '21 10:04 wanghaoshuang

https://github.com/PaddlePaddle/PaddleSlim/issues/976 ResNeXt101_vd_32x4d 参考PaddleClas 模板剪枝报错

请大牛看看是怎么回事。十分感谢。

ChrisMengxl avatar Feb 26 '22 11:02 ChrisMengxl