|
| 1 | +Step-by-Step |
| 2 | +============ |
| 3 | + |
| 4 | +This document is used to list steps of reproducing TensorFlow Object Detection models tuning results. This example can run on Intel CPUs and GPUs. |
| 5 | + |
| 6 | +# Prerequisite |
| 7 | + |
| 8 | + |
| 9 | +## 1. Environment |
| 10 | +Recommend python 3.6 or higher version. |
| 11 | + |
| 12 | +### Install Intel® Neural Compressor |
| 13 | +```shell |
| 14 | +pip install neural-compressor |
| 15 | +``` |
| 16 | + |
| 17 | +### Install Intel Tensorflow |
| 18 | +```shell |
| 19 | +pip install intel-tensorflow |
| 20 | +``` |
| 21 | +> Note: Validated TensorFlow [Version](/docs/source/installation_guide.md#validated-software-environment). |
| 22 | +
|
| 23 | +### Installation Dependency packages |
| 24 | +```shell |
| 25 | +cd examples\tensorflow\graph_networks\graphsage\quantization\ptq |
| 26 | +pip install -r requirements.txt |
| 27 | +``` |
| 28 | + |
| 29 | +### Install Intel Extension for Tensorflow |
| 30 | + |
| 31 | +#### Quantizing the model on Intel GPU(Mandatory to install ITEX) |
| 32 | +Intel Extension for Tensorflow is mandatory to be installed for quantizing the model on Intel GPUs. |
| 33 | + |
| 34 | +```shell |
| 35 | +pip install --upgrade intel-extension-for-tensorflow[xpu] |
| 36 | +``` |
| 37 | +For any more details, please follow the procedure in [install-gpu-drivers](https://github.com/intel/intel-extension-for-tensorflow/blob/main/docs/install/install_for_xpu.md#install-gpu-drivers) |
| 38 | + |
| 39 | +#### Quantizing the model on Intel CPU(Optional to install ITEX) |
| 40 | +Intel Extension for Tensorflow for Intel CPUs is experimental currently. It's not mandatory for quantizing the model on Intel CPUs. |
| 41 | + |
| 42 | +```shell |
| 43 | +pip install --upgrade intel-extension-for-tensorflow[cpu] |
| 44 | +``` |
| 45 | + |
| 46 | +> **Note**: |
| 47 | +> The version compatibility of stock Tensorflow and ITEX can be checked [here](https://github.com/intel/intel-extension-for-tensorflow#compatibility-table). Please make sure you have installed compatible Tensorflow and ITEX. |
| 48 | +
|
| 49 | +## 2. Prepare Model |
| 50 | +Download Frozen graph: |
| 51 | +```shell |
| 52 | +wget https://storage.googleapis.com/intel-optimized-tensorflow/models/2_12_0/graphsage_frozen_model.pb |
| 53 | +``` |
| 54 | + |
| 55 | +## 3. Prepare Dataset |
| 56 | + |
| 57 | +```shell |
| 58 | +wget https://snap.stanford.edu/graphsage/ppi.zip |
| 59 | +unzip ppi.zip |
| 60 | +``` |
| 61 | + |
| 62 | +# Run |
| 63 | + |
| 64 | +## 1. Quantization |
| 65 | + |
| 66 | + ```shell |
| 67 | + # The cmd of running faster_rcnn_resnet50 |
| 68 | + bash run_quant.sh --input_model=./graphsage_frozen_model.pb --output_model=./nc_graphsage_int8_model.pb --dataset_location=./ppi |
| 69 | + ``` |
| 70 | + |
| 71 | +## 2. Benchmark |
| 72 | + ```shell |
| 73 | + bash run_benchmark.sh --input_model=./nc_graphsage_int8_model.pb --dataset_location=./ppi --mode=performance |
| 74 | + ``` |
| 75 | + |
| 76 | +Details of enabling Intel® Neural Compressor on graphsage for Tensorflow. |
| 77 | +========================= |
| 78 | + |
| 79 | +This is a tutorial of how to enable graphsage model with Intel® Neural Compressor. |
| 80 | +## User Code Analysis |
| 81 | +User specifies fp32 *model*, calibration dataset *calib_dataloader* and a custom *eval_func* which encapsulates the evaluation dataset and metric by itself. |
| 82 | + |
| 83 | +For graphsage, we applied the latter one because our philosophy is to enable the model with minimal changes. Hence we need to make two changes on the original code. The first one is to implement the q_dataloader and make necessary changes to *eval_func*. |
| 84 | + |
| 85 | +### Code update |
| 86 | + |
| 87 | +After prepare step is done, we just need update main.py like below. |
| 88 | +```python |
| 89 | + if args.tune: |
| 90 | + from neural_compressor.tensorflow import StaticQuantConfig, quantize_model |
| 91 | + from neural_compressor.tensorflow.utils import BaseDataLoader |
| 92 | + |
| 93 | + dataset = CustomDataset() |
| 94 | + calib_dataloader = BaseDataLoader(dataset=dataset, batch_size=1, collate_fn=collate_function) |
| 95 | + quant_config = StaticQuantConfig() |
| 96 | + q_model = quantize_model(args.input_graph, quant_config, calib_dataloader) |
| 97 | + q_model.save(args.output_graph) |
| 98 | + |
| 99 | + if args.benchmark: |
| 100 | + if args.mode == 'performance': |
| 101 | + evaluate(args.input_graph) |
| 102 | + elif args.mode == 'accuracy': |
| 103 | + acc_result = evaluate(args.input_graph) |
| 104 | + print("Batch size = %d" % args.batch_size) |
| 105 | + print("Accuracy: %.5f" % acc_result) |
| 106 | + |
| 107 | +``` |
| 108 | + |
| 109 | +The quantization.fit() function will return a best quantized model during timeout constrain. |
0 commit comments