Skip to content

Commit 26db4ab

Browse files
xin3hechensuyue
andauthored
add two export examples to validate accuracy and performance of export API. (#563)
Signed-off-by: Xin He <[email protected]> Co-authored-by: chensuyue <[email protected]>
1 parent b445adb commit 26db4ab

File tree

16 files changed

+2013
-9
lines changed

16 files changed

+2013
-9
lines changed

docs/source/export.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ int8_onnx_config = Torch2ONNXConfig(
6767
)
6868
q_model.export('int8-model.onnx', int8_onnx_config)
6969
```
70+
> **Note**: Two export examples covering computer vision and natural language processing tasks exist in examples. Users can leverage them to verify the accuracy and performance of the exported ONNX model.
71+
- [Image recognition](/examples/pytorch/image_recognition/torchvision_models/export/fx/)
72+
- [Text classification](/examples/pytorch/nlp/huggingface_models/text-classification/export/fx/)
7073

7174
# Appendix
7275

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"pt2onnx": {
3+
"resnet18": {
4+
"model_src_dir": "image_recognition/torchvision_models/export/fx",
5+
"source_model_dataset": "/tf_dataset/pytorch/ImageNet/raw",
6+
"target_model_dataset": "/tf_dataset2/datasets/imagenet/ImagenetRaw/ImagenetRaw_small_5000",
7+
"input_model": "resnet18",
8+
"main_script": "main.py",
9+
"batch_size": 100
10+
},
11+
"resnet50": {
12+
"model_src_dir": "image_recognition/torchvision_models/export/fx",
13+
"source_model_dataset": "/tf_dataset/pytorch/ImageNet/raw",
14+
"target_model_dataset": "/tf_dataset2/datasets/imagenet/ImagenetRaw/ImagenetRaw_small_5000",
15+
"input_model": "resnet50",
16+
"main_script": "main.py",
17+
"batch_size": 100
18+
},
19+
"bert_base_MRPC": {
20+
"model_src_dir": "nlp/huggingface_models/text-classification/export/fx",
21+
"source_model_dataset": "mrpc",
22+
"target_model_dataset": "mrpc",
23+
"input_model": "/tf_dataset/pytorch/glue_data/base_weights/bert_MRPC_output",
24+
"main_script": "run_glue.py",
25+
"batch_size": 64
26+
},
27+
"bert_large_MRPC": {
28+
"model_src_dir": "nlp/huggingface_models/text-classification/export/fx",
29+
"source_model_dataset": "mrpc",
30+
"target_model_dataset": "mrpc",
31+
"input_model": "/tf_dataset/pytorch/glue_data/weights/bert_MRPC_output",
32+
"main_script": "run_glue.py",
33+
"batch_size": 64
34+
}
35+
}
36+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
Step-by-Step
2+
============
3+
4+
This document describes the step-by-step instructions for reproducing PyTorch tuning results with Intel® Neural Compressor.
5+
6+
# Prerequisite
7+
8+
## 1. Environment
9+
10+
PyTorch 1.8 or higher version is needed with pytorch_fx backend.
11+
12+
```shell
13+
cd examples/pytorch/image_recognition/torchvision_models/quantization/ptq/cpu/fx
14+
pip install -r requirements.txt
15+
```
16+
> Note: Validated PyTorch [Version](/docs/source/installation_guide.md#validated-software-environment).
17+
18+
## 2. Prepare Dataset
19+
20+
Download [ImageNet](http://www.image-net.org/) Raw image to dir: /path/to/imagenet. The dir include below folder:
21+
22+
```bash
23+
ls /path/to/pytorch-imagenet
24+
train val
25+
ls /path/to/onnx-imagenet-validation
26+
ILSVRC2012_img_val val.txt
27+
```
28+
29+
# Run
30+
### 1. To get the exported model:
31+
32+
Run run_export.sh to get ONNX model from PyTorch model.
33+
```bash
34+
# export fp32 model
35+
bash run_export.sh --input_model=resnet50 --dtype=fp32 --dataset_location=/path/to/pytorch-imagenet --output_model=resnet50-fp32.onnx
36+
# export int8 model
37+
bash run_export.sh --input_model=resnet50 --dtype=int8 --quant_format=[QDQ|QLinear] --dataset_location=/path/to/pytorch-imagenet --output_model=resnet50-int8.onnx
38+
```
39+
40+
### 2. To get the benchmark of exported and tuned models, includes Batch_size and Throughput:
41+
Run run_benchmark.sh to benchmark the accuracy and performance of ONNX models and PyTorch model.
42+
```bash
43+
# benchmark ONNX model
44+
bash run_benchmark.sh --input_model=[resnet50-fp32.onnx|resnet50-int8.onnx] --dataset_location=/path/to/onnx-imagenet-validation --mode=[accuracy|performance] --batch_size=[16]
45+
# benchmark PyTorch model
46+
bash run_benchmark.sh --input_model=[resnet50|/path/to/saved_results] --dataset_location=/path/to/pytorch-imagenet --mode=[accuracy|performance] --int8=[true|false] --batch_size=[16]
47+
```
48+
49+
> Note: All torchvision model names can be passed as long as they are included in `torchvision.models`, below are some examples.

0 commit comments

Comments
 (0)