-
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
1 parent
5738a2e
commit 074c81e
Showing
42 changed files
with
619 additions
and
107 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# ResNet-50 Knowledge Distillation | ||
|
||
The following uses ResNet-101 on the ImageNet data set as teacher model to illustrate how to use the model optimizer to | ||
improve the preformance of ResNet-50 by knowledge distillation. | ||
|
||
## 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 data set 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 files will match the patterns train-?????-of-01024 | ||
and validation-?????-of-00128, respectively. | ||
|
||
### 2 Train the teacher model | ||
|
||
Enter the examples directory and execute | ||
|
||
```shell | ||
cd examples | ||
horovodrun -np 8 -H localhost:8 python resnet_101_imagenet_train.py | ||
``` | ||
|
||
After execution, the default checkpoint file will be generated in ./models_ckpt/resnet_101_imagenet, and the inference | ||
checkpoint file will be generated in ./models_eval_ckpt/resnet_101_imagenet. You can also modify the checkpoint_path | ||
and checkpoint_eval_path of the resnet_101_imagenet_train.py file to change the generated file path. | ||
|
||
### 3 Distill | ||
|
||
Enter the examples directory and execute | ||
|
||
```shell | ||
horovodrun -np 8 -H localhost:8 python resnet_50_imagenet_distill.py | ||
``` | ||
|
||
After execution, the default checkpoint file will be generated in ./models_ckpt/resnet_50_imagenet_distill, and | ||
the inference checkpoint file will be generated in ./models_eval_ckpt/resnet_50_imagenet_distill. You can also | ||
modify the checkpoint_path and checkpoint_eval_path of the resnet_50_imagenet_distill.py file to change the generated | ||
file path. | ||
|
||
> Note | ||
> | ||
> > i. The model in the checkpoint_path is not the pure ResNet-50 model. It's the hybird of ResNet-50(student) and | ||
> > ResNet-101(teacher) | ||
> > | ||
> > ii. The model in the checkpoint_eval_path is the distilled model, i.e. pure ResNet-50 model |
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,37 @@ | ||
# Copyright 2019 ZTE corporation. All Rights Reserved. | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
""" | ||
Train a ResNet_101 model on the ImageNet dataset | ||
""" | ||
import os | ||
# If you did not execute the setup.py, uncomment the following four lines | ||
# import sys | ||
# from os.path import abspath, join, dirname | ||
# sys.path.insert(0, join(abspath(dirname(__file__)), '../src')) | ||
# print(sys.path) | ||
|
||
from model_optimizer import prune_model # noqa: E402 | ||
|
||
|
||
def _main(): | ||
base_dir = os.path.dirname(__file__) | ||
request = { | ||
"dataset": "imagenet", | ||
"model_name": "resnet_101", | ||
"data_dir": os.path.join(base_dir, "./data/imagenet"), | ||
"batch_size": 128, | ||
"batch_size_val": 100, | ||
"learning_rate": 0.1, | ||
"epochs": 120, | ||
"checkpoint_path": os.path.join(base_dir, "./models_ckpt/resnet_101_imagenet"), | ||
"checkpoint_save_period": 5, # save a checkpoint every 5 epoch | ||
"checkpoint_eval_path": os.path.join(base_dir, "./models_eval_ckpt/resnet_101_imagenet"), | ||
"scheduler": "train", | ||
"classifier_activation": None # None or "softmax", default is softmax | ||
} | ||
prune_model(request) | ||
|
||
|
||
if __name__ == "__main__": | ||
_main() |
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,38 @@ | ||
# Copyright 2020 ZTE corporation. All Rights Reserved. | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
""" | ||
Distill a ResNet_50 model from a trained ResNet_101 model on the ImageNet dataset | ||
""" | ||
import os | ||
# If you did not execute the setup.py, uncomment the following four lines | ||
# import sys | ||
# from os.path import abspath, join, dirname | ||
# sys.path.insert(0, join(abspath(dirname(__file__)), '../src')) | ||
# print(sys.path) | ||
|
||
from model_optimizer import prune_model # noqa: E402 | ||
|
||
|
||
def _main(): | ||
base_dir = os.path.dirname(__file__) | ||
request = { | ||
"dataset": "imagenet", | ||
"model_name": "resnet_50", | ||
"data_dir": os.path.join(base_dir, "./data/imagenet"), | ||
"batch_size": 256, | ||
"batch_size_val": 100, | ||
"learning_rate": 0.1, | ||
"epochs": 90, | ||
"checkpoint_path": os.path.join(base_dir, "./models_ckpt/resnet_50_imagenet_distill"), | ||
"checkpoint_save_period": 5, # save a checkpoint every 5 epoch | ||
"checkpoint_eval_path": os.path.join(base_dir, "./models_eval_ckpt/resnet_50_imagenet_distill"), | ||
"scheduler": "distill", | ||
"scheduler_file_name": "resnet_50_imagenet_0.3.yaml", | ||
"classifier_activation": None # None or "softmax", default is softmax | ||
} | ||
prune_model(request) | ||
|
||
|
||
if __name__ == "__main__": | ||
_main() |
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
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,2 @@ | ||
# Copyright 2021 ZTE corporation. All Rights Reserved. | ||
# SPDX-License-Identifier: Apache-2.0 |
Oops, something went wrong.