Skip to content

Enable yolov5 Example for TF 3x API #1943

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions examples/.config/model_params_tensorflow_3x.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,13 @@
"main_script": "main.py",
"batch_size": 1000
},
"yolo_v5": {
"model_src_dir": "object_detection/yolo_v5/quantization/ptq",
"dataset_location": "/tf_dataset2/datasets/coco_yolov5/coco",
"input_model": "/tf_dataset2/models/tensorflow/yolo_v5/yolov5s.pb",
"main_script": "main.py",
"batch_size": 1
},
"faster_rcnn_resnet50": {
"model_src_dir": "object_detection/faster_rcnn_resnet50/quantization/ptq",
"dataset_location": "/tf_dataset/tensorflow/coco_val.record",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
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.

# Prerequisite


## 1. Environment
Recommend python 3.10 or higher version.

### Install Intel® Neural Compressor
```shell
pip install neural-compressor
```

### Install Tensorflow
```shell
pip install tensorflow
```
> Note: Validated TensorFlow [Version](/docs/source/installation_guide.md#validated-software-environment).

### Installation Dependency packages
```shell
cd examples/3.x_api/tensorflow/object_detection/yolo_v5/quantization/ptq
pip install -r requirements.txt
```

### Install Intel Extension for Tensorflow

#### Quantizing the model on Intel GPU(Mandatory to install ITEX)
Intel Extension for Tensorflow is mandatory to be installed for quantizing the model on Intel GPUs.

```shell
pip install --upgrade intel-extension-for-tensorflow[xpu]
```
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)

#### Quantizing the model on Intel CPU(Optional to install ITEX)
Intel Extension for Tensorflow for Intel CPUs is experimental currently. It's not mandatory for quantizing the model on Intel CPUs.

```shell
pip install --upgrade intel-extension-for-tensorflow[cpu]
```

> **Note**:
> 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.

## 2. Prepare model

Users can choose to automatically or manually download the model.
### Automatic download

Run the `prepare_model.sh` script.
```shell
. prepare_model.sh
```

This script will load yolov5 model to `./yolov5/yolov5s.pb`.

### Manual download

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).
Generate the pretrained PyTorch model and then export to a Tensorflow supported format with the following commands:
```shell
python yolov5/models/tf.py --weights yolov5/yolov5s.pt
python yolov5/export.py --weights yolov5/yolov5s.pt --include pb
```

The yolov5 model will be loaded to `./yolov5/yolov5s.pb`.

## 3. Prepare Dataset

Users can choose to automatically or manually download the dataset.
### Automatic download

Run the `prepare_dataset.sh` script.
```shell
. prepare_dataset.sh
```
The validation set of coco2017 will be downloaded into a `./coco` folder.

# Run

## 1. Quantization
```python
bash run_quant.sh --input_model=./yolov5/yolov5s.pb --output_model=yolov5s_int8.pb --dataset_location=/path/to/dataset
```

## 2. Benchmark
```python
# run performance benchmark
bash run_benchmark.sh --input_model=yolov5s_int8.pb --dataset_location=/path/to/dataset --mode=performance

# run accuracy benchmark
bash run_benchmark.sh --input_model=yolov5s_int8.pb --dataset_location=/path/to/dataset --mode=accuracy
```

Finally, the program will generate the quantized Yolo-v5 model with relative 1% loss.
Loading
Loading