Skip to content

Commit 529aae1

Browse files
author
qibinhang
committed
first commit
0 parents  commit 529aae1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+6047
-0
lines changed

README.md

+175
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
# Reusing Deep Neural Network Models through Model Re-engineering
2+
## Abstract
3+
Training deep neural network (DNN) models, which has become an important task in today's software development, is often costly in terms of computational resources and time. With the inspiration of software reuse, building DNN models through reusing existing ones has gained increasing attention recently. Prior approaches to DNN model reuse have two main limitations: 1) reusing the entire model, while only a small part of the model's functionalities (labels) are required, would cause much overhead (e.g., computational and time costs for inference), and 2) model reuse would inherit the defects and weaknesses of the reused model, and hence put the new system under threats of security attack. To solve the above problem, we propose SeaM, a tool that re-engineers a trained DNN model to improve its reusability. Specifically, given a target problem and a trained model, SeaM utilizes a gradient-based search method to search for the model's weights that are relevant to the target problem. The re-engineered model that only retains the relevant weights is then reused to solve the target problem. Evaluation results on widely-used models show that the re-engineered models produced by SeaM only contain 10.11% weights of the original models, resulting 42.41% reduction in terms of inference time. For the target problem, the re-engineered models even outperform the original models in classification accuracy by 5.85%. Moreover, reusing the re-engineered models inherits an average of 57% fewer defects than reusing the entire model. We believe our approach to reducing reuse overhead and defect inheritance is one important step forward for practical model reuse.
4+
5+
6+
## Requirements
7+
+ advertorch 0.2.3<br>
8+
+ fvcore 0.1.5.post20220512<br>
9+
+ matplotlib 3.4.2<br>
10+
+ numpy 1.19.2<br>
11+
+ python 3.8.10<br>
12+
+ pytorch 1.8.1<br>
13+
+ torchvision 0.9.0<br>
14+
+ tqdm 4.61.0<br>
15+
+ GPU with CUDA support is also needed
16+
17+
<br>
18+
19+
## Structure of the directories
20+
21+
```powershell
22+
|--- README.md : user guidance
23+
|--- data/ : experimental data
24+
|--- src/ : source code of our work
25+
|------ global_config.py : setting the path
26+
|------ binary_class/ : direct reuse on binary classification problems
27+
|--------- model_reengineering.py : re-engineering a trained model and then reuse the re-engineered model
28+
|--------- calculate_flop.py : calculating the number of FLOPs required by reusing the re-engineered and original models
29+
|--------- calculate_time_cost.py : calculating the inference time required by reusing the re-engineered and original models
30+
|--------- ......
31+
|------ multi_class/ : direct reuse on multi-class classification problems
32+
|--------- ......
33+
|------ defect_inherit/ : indirect reuse
34+
|--------- reengineering_finetune.py : re-engineering a trained model and then fine-tuning the re-engineered model
35+
|--------- standard_finetune.py : using standard fine-tuning approach to fine-tune a trained model
36+
|--------- eval_robustness.py : calculating the defect inheritance rate
37+
|--------- ......
38+
```
39+
40+
41+
<br>
42+
43+
The following sections describe how to reproduce the experimental results in our paper.
44+
45+
## Downloading experimental data
46+
1. We provide the trained models and datasets used in the experiments, as well as the corresponding re-engineered models.<br>
47+
One can download `data/` from [here](https://mega.nz/file/tX91ACpR#CSbQ2Xariha7_HLavE_6pKg4FoO5axOPemlv5J0JYwY) and then move it to `SeaM/`.<br>
48+
The trained models will be downloaded automatically by PyTorch when running our project. If the download fails, please move our provided trained models to the folder according to the failure information given by PyTorch.<br>
49+
Due to the huge size of ImageNet, please download it from its [webpage](https://www.image-net.org/).
50+
2. Modify `self.root_dir` in `src/global_config.py`.
51+
52+
## Direct model reuse
53+
### Re-engineering on binary classification problems
54+
1. Go to the directory of experiments related to the binary classification problems.
55+
```commandline
56+
cd src/binary_class
57+
```
58+
2. Re-engineer VGG16-CIFAR10 on a binary classification problem.
59+
```commandline
60+
python model_reengineering.py --model vgg16 --dataset cifar10 --target_class 0 --lr_mask 0.01 --alpha 1
61+
```
62+
3. Compute the number of FLOPs required by the original and re-engineered VGG16-CIFAR10, respectively. This command also presents the accuracy of models.
63+
```commandline
64+
python calculate_flop.py --model vgg16 --dataset cifar10 --target_class 0 --lr_mask 0.01 --alpha 1
65+
```
66+
4. Compute the time cost for inference required by the original and re-engineered VGG16-CIFAR10, respectively. This command also presents the number of a model's weights.
67+
```commandline
68+
python calculate_time_cost.py --model vgg16 --dataset cifar10 --target_class 0 --lr_mask 0.01 --alpha 1
69+
```
70+
71+
### Re-engineering on multi-class classification problems
72+
1. Go to the directory of experiments related to the multi-class classification problems.
73+
```commandline
74+
cd src/multi_class
75+
```
76+
2. Re-engineer ResNet20-CIFAR100 on a multi-class classification problem.
77+
```commandline
78+
python model_reengineering.py --model resnet20 --dataset cifar100 --target_superclass_idx 0 --lr_mask 0.1 --alpha 2
79+
```
80+
3. Compute the number of FLOPs required by the original and re-engineered ResNet20-CIFAR100, respectively. This command also presents the accuracy of models.
81+
```commandline
82+
python calculate_flop.py --model resnet20 --dataset cifar100 --target_superclass_idx 0 --lr_mask 0.1 --alpha 2
83+
```
84+
4. Compute the time cost for inference required by the original and re-engineered ResNet20-CIFAR100, respectively. This command also presents the number of a model's weights.
85+
```commandline
86+
python calculate_time_cost.py --model resnet20 --dataset cifar100 --target_superclass 0 --lr_mask 0.1 --alpha 2
87+
```
88+
89+
***NOTE***: When computing the time cost for inference, DeepSparse runs a model on several CPUs.
90+
The inference process would be interfered with other active processes, leading to fluctuations in inference time cost.
91+
In our experiments, we manually kill as many other processes as possible and enable the inference process to occupy the CPUs exclusively.
92+
93+
## Indirect model reuse
94+
1. Go to the directory of experiments related to the defect inheritance.
95+
```commandline
96+
cd src/defect_inherit
97+
```
98+
2. Re-engineer ResNet18-ImageNet and then fine-tune the re-engineered model on the target dataset Scenes.
99+
```commandline
100+
python reengineering_finetune.py --model resnet18 --dataset mit67 --lr_mask 0.05 --alpha 0.5 --prune_threshold 0.6
101+
```
102+
3. Compute the defect inheritance rate of fine-tuned re-engineered ResNet18-Scenes.
103+
```commandline
104+
python eval_robustness.py --model resnet18 --dataset mit67 --eval seam --lr_mask 0.05 --alpha 0.5 --prune_threshold 0.6
105+
```
106+
4. Fine-tune the original ResNet18-ImageNet on the target dataset Scenes.
107+
```commandline
108+
python standard_finetune.py --model resnet18 --dataset mit67
109+
```
110+
5. Compute the defect inheritance rate of fine-tuned original ResNet18-Scenes.
111+
```commandline
112+
python eval_robustness.py --model resnet18 --dataset mit67 --eval standard
113+
```
114+
115+
116+
## Supplementary experimental results
117+
### The values of major parameters
118+
The following table shows the default hyperparameters. The details of settings for re-engineering each trained model on each target problem can be obtained according to the experimental result files. <br>
119+
For instance, the values of *learning rate* and *alpha* for the re-engineered model file `SeaM/data/multi_class_classification/resnet20_cifar100/predefined/tsc_0/lr_head_mask_0.1_0.05_alpha_1.0.pth` are 0.05 and 1.0, respectively.
120+
<table>
121+
<thead>
122+
<tr>
123+
<th>Target Problem</th>
124+
<th>Model Name</th>
125+
<th>Learning Rate</th>
126+
<th>Alpha</th>
127+
</tr>
128+
</thead>
129+
<tbody>
130+
<tr>
131+
<td rowspan="4">Binary<br>Classification</td>
132+
<td>VGG16-CIFAR10</td>
133+
<td>0.01</td>
134+
<td>1.00</td>
135+
</tr>
136+
<tr>
137+
<td>VGG16-CIFAR100</td>
138+
<td>0.05</td>
139+
<td>1.50</td>
140+
</tr>
141+
<tr>
142+
<td>ResNet20-CIFAR10</td>
143+
<td>0.05</td>
144+
<td>1.00</td>
145+
</tr>
146+
<tr>
147+
<td>ResNet20-CIFAR100</td>
148+
<td>0.12</td>
149+
<td>1.50</td>
150+
</tr>
151+
<tr>
152+
<td rowspan="2">Multi-class<br>Classification</td>
153+
<td>ResNet20-CIFAR100</td>
154+
<td>0.10</td>
155+
<td>2.00</td>
156+
</tr>
157+
<tr>
158+
<td>ResNet50-ImageNet</td>
159+
<td>0.05</td>
160+
<td>2.00</td>
161+
</tr>
162+
</tbody>
163+
</table>
164+
165+
<br>
166+
167+
168+
### The impact of reducing the number of weights on ACC and DIR. (for RQ3)
169+
We investigate the impact of the reduction in the number of weights on the ACC and DIR.
170+
A threshold $t$ is used to early stop model re-engineering when the rate of removed weights reaches the threshold.
171+
The following figure shows the ACC and DIR of the fine-tuned ResNet18 with different values of $t$, where $t=0.3, 0.4, 0.5, 0.6, 0.7$.
172+
We find that, as the number of weights reduces, the DIR reduces significantly, while the ACC is stable overall.
173+
The results demonstrate the effectiveness of model re-engineering in reducing the DIR and the robustness of SeaM.
174+
175+
![img](https://github.com/qibinhang/SeaM/blob/main/src/defect_inherit/prune_ratio.png)

data/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
The experimental data in our paper can be downloaded from [here](https://mega.nz/file/tX91ACpR#CSbQ2Xariha7_HLavE_6pKg4FoO5axOPemlv5J0JYwY).

0 commit comments

Comments
 (0)