• Stars
    star
    183
  • Rank 210,154 (Top 5 %)
  • Language
    Python
  • Created over 2 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

[NeurIPS2022] Deep Model Reassembly

Deep Model Reassembly ๐Ÿญ -> ๐Ÿงฑ -> ๐Ÿญ

๐Ÿ˜Ž Introduction

This repository contains the offical implementation for our paper

Deep Model Reassembly (NeurIPS2022)

[arxiv] [project page] [code]

Xingyi Yang, Daquan Zhou, Songhua Liu, Jingwen Ye, Xinchao Wang

In this work, we explore a novel knowledge-transfer task, termed as Deep Model Reassembly (DeRy), for general-purpose model reuse. DeRy first dissect each model into distinctive building blocks, and then selectively reassemble the derived blocks to produce customized networks under both the hardware resource and performance constraints.

pipeline

  • 2022/12/07 Code Updated for better usage.

๐Ÿ“š File Orgnization

DeRy/
โ”œโ”€โ”€ blocklize/block_meta.py         [Meta Information & Node Defnition]

โ”œโ”€โ”€ similarity/
โ”‚   โ”œโ”€โ”€ get_rep.py                  [Compute and save the feature embeddings]
โ”‚   โ”œโ”€โ”€ get_sim.py                  [Compute representation similarity given the saved features]
|   โ”œโ”€โ”€ partition.py                [Network partition by cover set problem]
|   โ”œโ”€โ”€ zeroshot_reassembly.py      [Network reassembly by solving integer program]

โ”œโ”€โ”€ configs/
|   โ”œโ”€โ”€ compute_sim/                [Model configs in the model zoo to compute the feature similarity]
|   โ”œโ”€โ”€ dery/XXX/$ModelSize_$DataSet_$BatchSize_$TrainTime_dery_$Optimizor.py   [Config files for transfer experiments]

โ”œโ”€โ”€ mmcls_addon/
|   โ”œโ”€โ”€ datasets/                   [Dataset definitions]
|   โ”œโ”€โ”€ models/backbones/dery.py    [DeRy backbone definition]

โ”œโ”€โ”€ third_package/timm              [Modified timm package]

๐Ÿ›  Installation

The model training part is based on mmclassification. Some of the pre-trained weights are from timm.

# Create python env
conda create -n open-mmlab python=3.8 pytorch=1.10 cudatoolkit=11.3 torchvision -c pytorch -y
conda activate open-mmlab

# Install mmcv and mmcls
pip3 install openmim
mim install mmcv-full==1.4.8
mim install mmcls==0.18.0

# Install timm
pip3 install timm

Note: Our code needs torch.fx to support the computational graph extraction from the torch model. Therefore, please install the torch > 1.10.

๐Ÿš€ Getting Started

To run the code for DeRy, we need to go through 4 steps

  1. [Model Zoo Preparation] Compute the model feature embeddings and representation similarity. We first write model configuration and its weight path, and run the configs in configs/compute_sim

     PYTHONPATH="$PWD" python simlarity/get_rep.py \
     $Config_file \              # configs in `configs/compute_sim`
     --out $Feature_path \       # Save feature embeddings in *.pth* files
     [--checkpoint $Checkpoint]  # download checkpoint if any
    

    All feature embeddings need to be saved in .pth files in the same $Feat_dictionary. We then load them and compute the feature similarity. Similarity will be saved as net1.net2.pkl files.

     PYTHONPATH="$PWD" python simlarity/compute_sim.py /
     --feat_path $Feat_dictionary /
     --sim_func $Similarity_function [cka, rbf_cka, lr]
    

    Pre-computed similarity on ImageNet for Linear CKA and Linear Regression.

    We also need to compute the feature size (input-output feature dimensions). It can be done by running

     PYTHONPATH="$PWD" python simlarity/count_inout_size.py /
     --root $Feat_dictionary
    

    The results is a json file containing the input-output shape for all network layers, like MODEL_INOUT_SHAPE.json.

  2. [Network Partition] Solve the cover set optimization to get the network partition. The results is an assignment file in .pkl.

     PYTHONPATH="$PWD" python simlarity/partition.py /
     --sim_path $Feat_similarity_path /
     --K        $Num_partition /             # default=4
     --trial    $Num_repeat_runs /           # default=200
     --eps      $Size_ratio_each_block /     # default=0.2
     --num_iter $Maximum_num_iter_eachrun    # default=200
    
  3. [Reassemby] Reassemble the partitioned building blocks into a full model, by solving a integer program with training-free proxy. The results are a series of model configs in .py.

     PYTHONPATH="$PWD" python simlarity/zeroshot_reassembly.py \
     --path          $Block_partition_file [Saved in the partition step] \
     --C             $Maximum_parameter_num \
     --minC          $Minimum_parameter_num \
     --flop_C        $Maximum_FLOPs_num \
     --minflop_C     $Minimum_FLOPs_num \
     --num_batch     $Number_batch_average_to_compute_score \
     --batch_size    $Number_sample_each_batch \
     --trial         $Search_time \
     --zero_proxy    $Type_train_free_proxy [Default NASWOT] \
     --data_config   $Config_target_data
    
  4. [Fune-tuning] Train the reassembled model on target data. You may refers to mmclassification for the model training.

Note: Partitioning and reassembling results may not be identical because of the algorithmatic stochaticity. It may slightly affect the performance.

๐Ÿš› Other Resources

  1. We use several pre-trained models not included in timm and mmcls, listed in Pre-trained.

Thanks for the support

Stargazers repo roster for @Adamdad/DeRy

โœ Citation

@article{yang2022dery,
    author    = {Xingyi Yang, Daquan Zhou, Songhua Liu, Jingwen Ye, Xinchao Wang},
    title     = {Deep Model Reassembly},
    journal   = {NeurIPS},
    year      = {2022},
}

Extensions

  1. Extension on Efficienr and Parallel Training: Deep-Incubation
  2. Extension for Efficient Model Zoo Training: Stitchable Neural Networks

More Repositories

1

keras-YOLOv3-mobilenet

I transfer the backend of yolov3 into Mobilenetv1๏ผŒVGG16๏ผŒResNet101 and ResNeXt101
Python
565
star
2

ConsistentTeacher

[CVPR2023 Highlight] Consistent-Teacher: Towards Reducing Inconsistent Pseudo-targets in Semi-supervised Object Detection
Python
290
star
3

Awesome-ComposableAI

A curated list of Composable AI methods: Building AI system by composing modules.
147
star
4

KnowledgeFactor

[ECCV2022] Factorizing Knowledge in Neural Networks
Python
67
star
5

Repfusion

Python
52
star
6

CT-Lung-Segmentation

Pytorch implementation of Lung CT image segmentation Using U-net
Jupyter Notebook
15
star
7

Awesome-metrics-learning

13
star
8

Orthogonal-Matching-Pursuit

This is a implementation of Orthogonal Matching Pursuit algorithm for sparse approximation
Python
11
star
9

Filter-Gradient-Decent

In this paper, we propose Filter Gradient Decent (FGD), an efficient stochastic optimization algorithm that makes a consistent estimation of the local gradient by solving an adaptive filtering problem with different designs of filters.
Python
10
star
10

Samesame

An Tensorflow.keras implementation of Same, Same But Different - Recovering Neural Network Quantization Error Through Weight Factorization(https://arxiv.org/pdf/1902.01917.pdf) ---ICML2019
Python
10
star
11

EE2211_Python

Jupyter Notebook
3
star
12

adamdad

2
star
13

Pop

CVPR 2006 Best Paper: Putting Objects in Perspective, Derek Hoiem, Alexei Efros, Martial Hebert
MATLAB
2
star
14

ECE-271

MATLAB
1
star
15

EE5907

NUS ECE Pattern Recognition
Python
1
star
16

DouBan_book_spider

beautiful soup based scraper for douban book
Python
1
star
17

Mips32-Pipline-CPU

A Mips32 CPU with 57+1 instructions and a pipelines structure
Verilog
1
star