-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
272 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# MobileNet v1 training and pruning | ||
|
||
The following uses MobileNet v1 on the ImageNet dataset to illustrate how to use the model optimizer to achieve | ||
model traing, and pruning | ||
|
||
## 1 Prepare data | ||
|
||
### 1.1 Generate training and test data sets | ||
|
||
You may follow the data preparation guide [here](https://github.com/tensorflow/models/tree/v1.13.0/research/inception) | ||
to download the full dataset and convert it into TFRecord files. By default, when the script finishes, you will find | ||
1024 training files and 128 validation files in the DATA_DIR. The file will match the patterns | ||
train-?????-of-01024 and validation-?????-of-0128, respectively. | ||
|
||
## 2 Train | ||
|
||
Enter the examples directory and execute | ||
|
||
```shell | ||
cd examples | ||
horovodrun -np 8 -H localhost:8 python movilenet_v1_imagenet_train.py | ||
``` | ||
|
||
After execution, the default checkpoint file will be generated in ./models_ckpt/mobilenet_v1_imagenet, and the | ||
inference checkpoint file will be generated in ./models_eval_ckpt/mobilenet_v1_imagenet. You can also modify the | ||
checkpoint_path and checkpoint_eval_path of the mobilenet_v1_imagenet_train.py file to change the generated file path. | ||
|
||
## 3 Prune | ||
|
||
Here, you can use a full trained model or the model in training process as a initial model to prune. The following | ||
uses specified pruning strategy as an example. | ||
|
||
If you have a well trained model, for example, named checkpoint-120.h5 in directory ./models_ckpt/mobilenet_v1_imagenet. | ||
You can copy it to the ./models_ckpt/mobilenet_v1_imagenet_specified_pruned directory, and then perform pruning. Enter | ||
the examples diretory and execute | ||
|
||
```shell | ||
cd examples | ||
cp ./models_ckpt/mobilenet_v1_imagenet/checkpoint-120.h5 ./models_ckpt/mobilenet_v1_imagenet_specified_pruned/ | ||
horovodrun -np 8 -H localhost:8 python mobilenet_v1_imagenet_prune.py | ||
``` | ||
|
||
Or you can start a training and pruning process from scratch | ||
|
||
```shell | ||
cd examples | ||
horovodrun -np 8 -H localhost:8 python mobilenet_v1_imagenet_prune.py | ||
``` | ||
|
||
After execution, the default checkpoint file weill be generated in ./models_ckpt/mobilenet_v1_imagenet_specified_pruned, | ||
and the inference checkpoint file will be generated in ./models_eval_ckpt/mobilenet_v1_imagenet_specified_pruned. You | ||
can also modify the checkpoint_path and checkpoint_eval_path of the mobilenet_v1_imagenet_prune.py file to change the | ||
generated file path. | ||
|
||
## 4 Quantize | ||
|
||
To be continue. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
src/model_optimizer/pruner/scheduler/uniform_auto/mobilenet_v1_imagenet.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
version: 1 | ||
pruners: | ||
prune_func1: | ||
criterion: l1_norm | ||
prune_type: auto_prune | ||
ratio: 0.30 | ||
|
||
lr_schedulers: | ||
# Learning rate | ||
- name: warmup_lr | ||
class: LearningRateWarmupCallback | ||
warmup_epochs: 5 | ||
verbose: 0 | ||
- name: lr_multiply_1 | ||
class: LearningRateScheduleCallback | ||
start_epoch: 5 | ||
end_epoch: 30 | ||
multiplier: 1.0 | ||
- name: lr_multiply_0.1 | ||
class: LearningRateScheduleCallback | ||
start_epoch: 30 | ||
end_epoch: 80 | ||
multiplier: 1e-1 | ||
- name: lr_multiply_0.01 | ||
class: LearningRateScheduleCallback | ||
start_epoch: 80 | ||
end_epoch: 120 | ||
multiplier: 1e-2 | ||
- name: lr_multiply_0.001 | ||
class: LearningRateScheduleCallback | ||
start_epoch: 120 | ||
end_epoch: 140 | ||
multiplier: 1e-3 | ||
- name: lr_multiply_0.0001 | ||
class: LearningRateScheduleCallback | ||
start_epoch: 140 | ||
end_epoch: 200 | ||
multiplier: 1e-4 | ||
|
||
prune_schedulers: | ||
- pruner: | ||
func_name: prune_func1 | ||
epochs: [50] | ||
|
52 changes: 52 additions & 0 deletions
52
src/model_optimizer/pruner/scheduler/uniform_specified_layer/mobilenet_v1_imagenet.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
version: 1 | ||
pruners: | ||
prune_func1: | ||
criterion: mean_l1_norm | ||
prune_type: specified_layer_prune | ||
ratio: 0.35 | ||
layers_to_be_pruned: [ | ||
conv_pw_1, | ||
conv_pw_2, | ||
conv_pw_4, | ||
conv_pw_5, | ||
conv_pw_6, | ||
conv_pw_7, | ||
conv_pw_8, | ||
conv_pw_9, | ||
conv_pw_10, | ||
conv_pw_11, | ||
conv_pw_12, | ||
conv_pw_13 | ||
] | ||
|
||
lr_schedulers: | ||
# Learning rate | ||
- name: warmup_lr | ||
class: LearningRateWarmupCallback | ||
warmup_epochs: 5 | ||
verbose: 0 | ||
- name: lr_multiply_1 | ||
class: LearningRateScheduleCallback | ||
start_epoch: 5 | ||
end_epoch: 30 | ||
multiplier: 1.0 | ||
- name: lr_multiply_0.1 | ||
class: LearningRateScheduleCallback | ||
start_epoch: 30 | ||
end_epoch: 50 | ||
multiplier: 1e-1 | ||
- name: lr_multiply_0.01 | ||
class: LearningRateScheduleCallback | ||
start_epoch: 50 | ||
end_epoch: 70 | ||
multiplier: 1e-2 | ||
- name: lr_multiply_0.001 | ||
class: LearningRateScheduleCallback | ||
start_epoch: 70 | ||
end_epoch : 90 | ||
multiplier: 1e-3 | ||
|
||
prune_schedulers: | ||
- pruner: | ||
func_name: prune_func1 | ||
epochs: [5] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters