โ https://github.com/fangvv/MTACP
This is the source code for our paper: Deep Reinforcement Learning based Multi-task Automated Channel Pruning for DNNs. A brief introduction of this work is as follows:
Model compression is a key technique that enables deploying Deep Neural Networks (DNNs) on Internet-of-Things (IoT) devices with constrained computing resources and limited power budgets. Channel pruning has become one of the representative compression approaches, but how to determine the compression ratio for different layers of a model still remains as a challenging task. Current automated pruning solutions address this issue by searching for an optimal strategy according to the target compression ratio. Nevertheless, when given a series of tasks with multiple compression ratios and different training datasets, these approaches have to carry out the pruning process repeatedly, which is inefficient and time-consuming. In this paper, we propose a Multi-Task Automated Channel Pruning (MTACP) framework, which can simultaneously generate a number of feasible compressed models satisfying different task demands for a target DNN model. To learn MTACP, the layer-by-layer multi-task channel pruning process is transformed into a Markov Decision Process (MDP), which seeks to solve a series of decision-making problems. Based on this MDP, we propose an actor-critic-based multi-task Reinforcement Learning (RL) algorithm to learn the optimal policy, working based on the IMPortance weighted Actor-Learner Architectures (IMPALA). IMPALA is known as a distributed RL architecture, in which the learner can learn from a set of actors that continuously generate trajectories of experience in their own environments. Extensive experiments on CIFAR10/100 and FLOWER102 datasets for MTACP demonstrate its unique capability for multi-task settings, as well as its superior performance over state-of-the-art solutions.
ๆจกๅๅ็ผฉๆฏๅจ่ฎก็ฎ่ตๆบๅ้ใๅ่้ข็ฎๆ้็็ฉ่็ฝ๏ผIoT๏ผ่ฎพๅคไธ้จ็ฝฒๆทฑๅบฆ็ฅ็ป็ฝ็ป๏ผDNN๏ผ็ๅ ณ้ฎๆๆฏใ้้ๅชๆๅทฒๆไธบไปฃ่กจๆงๅ็ผฉๆนๆณไนไธ๏ผไฝๅฆไฝ็กฎๅฎๆจกๅไธญไธๅๅฑ็ๅ็ผฉๆฏไพไปๆฏไธไธชๅ ทๆๆๆๆง็ไปปๅกใๅฝๅ่ชๅจๅๅชๆๆนๆก้่ฟๆ นๆฎ็ฎๆ ๅ็ผฉๆฏๆ็ดขๆไผ็ญ็ฅๆฅ่งฃๅณ่ฟไธ้ฎ้ขใ็ถ่๏ผๅฝ้ขๅฏนๅ ทๆๅคไธชๅ็ผฉๆฏไพๅไธๅ่ฎญ็ปๆฐๆฎ้็็ณปๅไปปๅกๆถ๏ผ่ฟไบๆนๆณ้่ฆ้ๅคๆง่กๅชๆ่ฟ็จ๏ผๆ็ไฝไธไธ่ๆถใๆฌๆๆๅบๅคไปปๅก่ชๅจ้้ๅชๆ๏ผMTACP๏ผๆกๆถ๏ผ่ฝๅคไธบ็ฎๆ DNNๆจกๅๅๆถ็ๆๅคไธชๆปก่ถณไธๅไปปๅก้ๆฑ็ๅฏ่กๅ็ผฉๆจกๅใไธบๅญฆไน MTACP๏ผๆไปฌๅฐ้ๅฑๅคไปปๅก้้ๅชๆ่ฟ็จ่ฝฌๅไธบ้ฉฌๅฐๅฏๅคซๅณ็ญ่ฟ็จ๏ผMDP๏ผ๏ผๆจๅจ่งฃๅณไธ็ณปๅๅณ็ญ้ฎ้ขใๅบไบๆญคMDP๏ผๆไปฌๆๅบไธ็งๅบไบ่กๅจ่ -่ฏ่ฎบๅฎถๆถๆ็ๅคไปปๅกๅผบๅๅญฆไน ๏ผRL๏ผ็ฎๆณ๏ผไพๆ้่ฆๆงๅ ๆ่กๅจ่ -ๅญฆไน ่ ๆถๆ๏ผIMPALA๏ผๆฅๅญฆไน ๆไผ็ญ็ฅใIMPALAๆฏไธ็งๅๅธๅผๅผบๅๅญฆไน ๆถๆ๏ผๅ ถๅญฆไน ่ ๅฏไปไธ็ปๅจๅ่ช็ฏๅขไธญๆ็ปญ็ๆ็ป้ช่ฝจ่ฟน็่กๅจ่ ไธญ่ฟ่กๅญฆไน ใๅจCIFAR10/100ๅFLOWER102ๆฐๆฎ้ไธ่ฟ่ก็ๅคง้ๅฎ้ช่กจๆ๏ผMTACPไธไป ๅ ทๆๅคไปปๅกๅบๆฏไธ็็ฌ็น่ฝๅ๏ผๅ ถๆง่ฝไนไผไบๆๅ ่ฟ็่งฃๅณๆนๆกใ
This work will be published by IJCNN 2023. Click here for our paper online, or you can find the camera ready PDF in this repo link.
-
Python 3.7+
-
PyTorch 1.9
-
Torchvision 0.2.1
-
NumPy
-
scikit-learn
-
ptflops
-
TensorboardX
MTACP/
โโโ config/ # JSON configs for each (model, dataset) task
โ โโโ auto_prune_impala_cifar10.json
โ โโโ auto_prune_impala_cifar100*.json
โ โโโ auto_prune_impala_flower*.json
โ โโโ ...
โโโ core/ # IMPALA core: V-trace, env wrapper, profiling
โ โโโ environment.py # Gym-style env wrapper for the pruning environment
โ โโโ file_writer.py
โ โโโ prof.py # Profiling utilities
โ โโโ vtrace.py # V-trace off-policy correction
โโโ env/ # Channel pruning environments & reward functions
โ โโโ channel_pruning_env_mobilenet.py # Pruning env for MobileNet
โ โโโ channel_pruning_env_vgg16.py # Pruning env for VGG-16
โ โโโ rewards.py # Reward: acc_reward, acc_flops_reward, etc.
โ โโโ rewards_mxd.py
โโโ lib/ # Shared libs: DDPG agent, data loaders, utils
โ โโโ agent.py # Actor / Critic networks, DDPG agent
โ โโโ data.py # Dataset loaders & splits
โ โโโ memory.py # Experience replay buffer
โ โโโ net_measure.py # FLOPs / params measurement
โ โโโ thop/ # FLOPs counter (vendored)
โ โโโ utils.py
โโโ models/ # DNN architectures used in the experiments
โ โโโ mobilenet.py
โ โโโ mobilenet_v2.py
โ โโโ resnet.py
โ โโโ vgg_cifar.py
โโโ scripts/ # Ready-to-run shell scripts
โ โโโ search_mobilenet_0.5flops.sh # Run multi-task search
โ โโโ finetune_mobilenet_0.5flops.sh # Fine-tune the searched model
โ โโโ export_mobilenet_0.5flops.sh # Export the pruned model
โโโ logs/ # Training logs, checkpoints, exported models
โโโ amc_search.py # Entry: AMC baseline search (DDPG-based)
โโโ amc_fine_tune.py # Entry: Fine-tune the searched pruned model
โโโ cacp_search.py # Entry: CACP baseline search
โโโ eval_mobilenet.py # Entry: Evaluate a pruned MobileNet
โโโ impala_auto_pruning.py # Entry: Main MTACP algorithm (IMPALA-based multi-task pruning)
โโโ atari_wrappers.py # Atari wrappers reused by the IMPALA pipeline
โโโ utils.py # Top-level helpers (model loading, config, etc.)
โโโ conda_requirements.txt
โโโ pip_requirement.txt
โโโ README.md
The environment class that models the layer-by-layer channel pruning process as an MDP. Key attributes:
| Attribute | Description |
|---|---|
lbound / rbound |
Lower / upper bound of the per-layer preserve ratio (e.g. 0.2 / 1.0) |
preserve_ratio |
Target overall FLOPs/params compression ratio for the task |
channel_round |
Round the number of kept channels to a multiple of this value (e.g. 8) |
n_calibration_batches |
Batches used to estimate channel importance |
n_points_per_layer |
Number of importance samples per layer |
prunable_layer_types |
Layer types eligible for pruning: Conv2d, Linear |
acc_metric |
Accuracy metric: acc1 or acc5 |
use_real_val |
If true, use the real validation set to compute accuracy |
State space (continuous): the state encodes the current DNN to be pruned, including the preserve ratio of the next layer, the FLOPs/params of the next prunable layer, the input/output channel counts of that layer, and cumulative statistics over previous layers.
Action space (continuous, in [lbound, rbound]): the preserve ratio of the next prunable layer. The agent decides how aggressively to prune the current layer.
Key methods:
-
reset()โ Restore the model from the backup, reset the layer pointer, and return the initial state. -
step(action)โ Apply the chosen preserve ratio to the current layer, advance the layer pointer, and return(next_state, reward, done, info). When all prunable layers are processed, the environment validates the model, computes the reward, and resets. -
_validate()โ Evaluate the current pruned model on the validation set and return top-1 (or top-5) accuracy. -
_get_reward()โ Call intoenv.rewardsto convert(acc, flops)into a scalar reward.
Reward: configurable in env/rewards.py:
acc_rewardโacc * 0.01(maximize accuracy).acc_flops_rewardโ-((100 - acc) / 100) * log(flops)(trade-off accuracy and FLOPs).acc_reward_mxdโ Centered accuracy reward used in the multi-task setting.
The main MTACP algorithm. Multiple actors run in parallel, each holding a different (model, dataset, target_ratio) task. They generate trajectories of pruning actions; a central learner updates the policy using V-trace off-policy correction.
Network architecture (Actor / Critic):
| Network | Layers | Output Activation |
|---|---|---|
| Actor | state โ 400(relu) โ 300(relu) โ action |
sigmoid (action in [0, 1], mapped to [lbound, rbound]) |
| Critic | state(400) + action(400) โ 300(relu) โ 1 |
linear (value) |
The Actor outputs a continuous Gaussian policy (mean & std) per layer, sampled to produce the preserve ratio.
Key hyperparameters:
| Parameter | Value | Description |
|---|---|---|
num_actors |
1 (default) |
Number of parallel actor processes |
total_steps |
10000 (default) |
Total environment steps for training |
batch_size |
32 |
Training batch size per env |
discount |
0.99 |
Discount factor ฮณ |
entropy_cost |
0.01 |
Entropy bonus for exploration |
baseline_cost |
0.5 |
Critic loss weight |
learning_rate |
1e-3 |
RMSProp learning rate |
clip_rho_threshold |
1.0 |
V-trace importance-weight clip |
clip_pg_rho_threshold |
1.0 |
V-trace policy-gradient clip |
Key methods:
-
train()โ Spawn actors and learner, batch trajectories, apply V-trace, and update the network with RMSProp. -
act()โ Sample a continuous action from the current policy and executeenv.step(). -
compute_vtrace()โ Apply V-trace to off-policy trajectories to obtain corrected advantages and value targets. -
file_writerโ Stream TensorBoard logs (loss, reward, mean episode return) tologs/.
Implements the V-trace off-policy correction used by IMPALA. It computes:
vsโ the value targets for the value function,pg_advantagesโ the policy-gradient advantages,log_rhosโ the clipped importance sampling log-ratios.
Two variants are provided: from_logits (continuous Gaussian policy) and from_importance_weights (discrete policy).
Each JSON file describes a single (model, dataset, target_ratio) task. Sample auto_prune_impala_cifar10.json:
{
"dataset": "cifar10",
"preserve_ratio": 0.5,
"model": "mobilenet",
"n_data_worker": 8,
"lbound": 0.2,
"rbound": 1,
"reward": "acc_reward",
"data_root": "/../dataset/cifar10",
"ckpt_path": "./checkpoints/mobilenetamc_cifar10.pth",
"seed": 2022,
"batch_size": 32,
"channel_round": 8,
"n_points_per_layer": 10,
"n_calibration_batches": 60
}Multiple tasks (different datasets, ratios) are launched together to form the multi-task MDP.
- AMC (
amc_search.py) โ DDPG-based single-task automated channel pruning. - CACP (
cacp_search.py) โ AutoPrune-style continuous-action search. - Fine-tune (
amc_fine_tune.py) โ Standard training of the searched pruned model. - Evaluate (
eval_mobilenet.py) โ Load a pruned checkpoint and report top-1 / top-5 accuracy and FLOPs.
# Clone the repository
git clone https://github.com/fangvv/MTACP.git
cd MTACP
# Create a new conda environment from the lock file
conda create --name mtacp --file conda_requirements.txt
conda activate mtacp
# Install pip dependencies
pip install -r pip_requirement.txt# Run the main MTACP algorithm (multi-task IMPALA pruning)
python impala_auto_pruning.py --env AutoPrune-v0 --mode train
# Run the AMC baseline (single-task DDPG)
python amc_search.py --job train --model mobilenet --dataset cifar10 \
--preserve_ratio 0.5 --lbound 0.2 --rbound 1 --reward acc_reward \
--data_root ./dataset/cifar10/ --ckpt_path ./checkpoints/mobilenetamc_cifar10.pth --seed 2022
# Run the CACP baseline
python cacp_search.py
# Fine-tune the searched pruned model
python -W ignore amc_fine_tune.py --model mobilenet_0.5flops --dataset cifar10 \
--lr 0.05 --n_gpu 1 --batch_size 256 --n_worker 8 --lr_type cos --n_epoch 150 \
--data_root ./dataset/cifar10/ --ckpt_path ./checkpoints/mobilenet_cifar10_0.5flops_export.pth.tar
# Evaluate a pruned MobileNet
python eval_mobilenet.pySee scripts/search_mobilenet_0.5flops.sh, scripts/finetune_mobilenet_0.5flops.sh, and scripts/export_mobilenet_0.5flops.sh for ready-to-run examples.
If you find MTACP useful or relevant to your project and research, please kindly cite our paper:
@inproceedings{ma2023deep,
title={Deep Reinforcement Learning Based Multi-Task Automated Channel Pruning for DNNs},
author={Ma, Xiaodong and Fang, Weiwei},
booktitle={2023 International Joint Conference on Neural Networks (IJCNN)},
pages={1--9},
year={2023},
organization={IEEE}
}
We have another work on UAV-DDPG and VN-MADDPG for your reference.
Xiaodong Ma (maxdzzu@gmail.com)
Please note that the open source code in this repository was mainly completed by the graduate student author during his master's degree study. Since the author did not continue to engage in scientific research work after graduation, it is difficult to continue to maintain and update these codes. We sincerely apologize that these codes are for reference only.