|
| 1 | +This document describes the step-by-step to reproduce Yolo-v5 tuning result with Neural Compressor. This example can run on Intel CPUs and GPUs. |
| 2 | + |
| 3 | +# Prerequisite |
| 4 | + |
| 5 | + |
| 6 | +## 1. Environment |
| 7 | +Recommend python 3.10 or higher version. |
| 8 | + |
| 9 | +### Install Intel® Neural Compressor |
| 10 | +```shell |
| 11 | +pip install neural-compressor |
| 12 | +``` |
| 13 | + |
| 14 | +### Install Tensorflow |
| 15 | +```shell |
| 16 | +pip install tensorflow |
| 17 | +``` |
| 18 | +> Note: Validated TensorFlow [Version](/docs/source/installation_guide.md#validated-software-environment). |
| 19 | +
|
| 20 | +### Installation Dependency packages |
| 21 | +```shell |
| 22 | +cd examples/3.x_api/tensorflow/object_detection/yolo_v5/quantization/ptq |
| 23 | +pip install -r requirements.txt |
| 24 | +``` |
| 25 | + |
| 26 | +### Install Intel Extension for Tensorflow |
| 27 | + |
| 28 | +#### Quantizing the model on Intel GPU(Mandatory to install ITEX) |
| 29 | +Intel Extension for Tensorflow is mandatory to be installed for quantizing the model on Intel GPUs. |
| 30 | + |
| 31 | +```shell |
| 32 | +pip install --upgrade intel-extension-for-tensorflow[xpu] |
| 33 | +``` |
| 34 | +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) |
| 35 | + |
| 36 | +#### Quantizing the model on Intel CPU(Optional to install ITEX) |
| 37 | +Intel Extension for Tensorflow for Intel CPUs is experimental currently. It's not mandatory for quantizing the model on Intel CPUs. |
| 38 | + |
| 39 | +```shell |
| 40 | +pip install --upgrade intel-extension-for-tensorflow[cpu] |
| 41 | +``` |
| 42 | + |
| 43 | +> **Note**: |
| 44 | +> 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. |
| 45 | +
|
| 46 | +## 2. Prepare model |
| 47 | + |
| 48 | +Users can choose to automatically or manually download the model. |
| 49 | +### Automatic download |
| 50 | + |
| 51 | +Run the `prepare_model.sh` script. |
| 52 | +```shell |
| 53 | +. prepare_model.sh |
| 54 | +``` |
| 55 | + |
| 56 | +This script will load yolov5 model to `./yolov5/yolov5s.pb`. |
| 57 | + |
| 58 | +### Manual download |
| 59 | + |
| 60 | +To get a TensorFlow pretrained model, you need to export it from a PyTorch model. Clone the [Ultralytics yolov5 repository](https://github.com/ultralytics/yolov5.git). |
| 61 | +Generate the pretrained PyTorch model and then export to a Tensorflow supported format with the following commands: |
| 62 | +```shell |
| 63 | +python yolov5/models/tf.py --weights yolov5/yolov5s.pt |
| 64 | +python yolov5/export.py --weights yolov5/yolov5s.pt --include pb |
| 65 | +``` |
| 66 | + |
| 67 | +The yolov5 model will be loaded to `./yolov5/yolov5s.pb`. |
| 68 | + |
| 69 | +## 3. Prepare Dataset |
| 70 | + |
| 71 | +Users can choose to automatically or manually download the dataset. |
| 72 | +### Automatic download |
| 73 | + |
| 74 | +Run the `prepare_dataset.sh` script. |
| 75 | +```shell |
| 76 | +. prepare_dataset.sh |
| 77 | +``` |
| 78 | +The validation set of coco2017 will be downloaded into a `./coco` folder. |
| 79 | + |
| 80 | +# Run |
| 81 | + |
| 82 | +## 1. Quantization |
| 83 | +```python |
| 84 | +bash run_quant.sh --input_model=./yolov5/yolov5s.pb --output_model=yolov5s_int8.pb --dataset_location=/path/to/dataset |
| 85 | +``` |
| 86 | + |
| 87 | +## 2. Benchmark |
| 88 | +```python |
| 89 | +# run performance benchmark |
| 90 | +bash run_benchmark.sh --input_model=yolov5s_int8.pb --dataset_location=/path/to/dataset --mode=performance |
| 91 | + |
| 92 | +# run accuracy benchmark |
| 93 | +bash run_benchmark.sh --input_model=yolov5s_int8.pb --dataset_location=/path/to/dataset --mode=accuracy |
| 94 | +``` |
| 95 | + |
| 96 | +Finally, the program will generate the quantized Yolo-v5 model with relative 1% loss. |
0 commit comments