Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Update Examples for TF 3x API
Signed-off-by: zehao-intel <[email protected]>
  • Loading branch information
zehao-intel committed Jul 4, 2024
commit f23de0df53ac075d6b4ef430af7bc2bed89fb3c6
109 changes: 108 additions & 1 deletion examples/.config/model_params_tensorflow_3x.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,20 @@
"batch_size": 64,
"fp32_model_url": "https://storage.googleapis.com/intel-optimized-tensorflow/models/v2_7_0/fp32_bert_squad.pb"
},
"distilbert_base": {
"model_src_dir": "nlp/distilbert_base/quantization/ptq",
"dataset_location": "/tf_dataset2/datasets/sst2_validation_dataset",
"input_model": "/tf_dataset2/models/tensorflow/distilbert_base/fp32/distilbert_base_fp32.pb",
"main_script": "run_inference.py",
"batch_size": 128
},
"distilbert_base_sq": {
"model_src_dir": "nlp/distilbert_base/quantization/ptq",
"dataset_location": "/tf_dataset2/datasets/sst2_validation_dataset",
"input_model": "/tf_dataset2/models/tensorflow/distilbert_base/fp32/distilbert_base_fp32.pb",
"main_script": "run_inference.py",
"batch_size": 128
},
"opt_125m_sq": {
"model_src_dir": "nlp/large_language_models/quantization/ptq/smoothquant",
"dataset_location": "",
Expand All @@ -29,8 +43,44 @@
"main_script": "main.py",
"batch_size": 1
},
"transformer_lt": {
"model_src_dir": "nlp/transformer_lt/quantization/ptq",
"dataset_location": "/tf_dataset/tensorflow/transformer-lt-official-fp32-inference/transformer_lt_official_fp32_pretrained_model/data",
"input_model": "/tf_dataset/tensorflow/transformer-lt-official-fp32-inference/transformer_lt_official_fp32_pretrained_model/graph/fp32_graphdef.pb",
"main_script": "main.py",
"batch_size": 64
},
"densenet121": {
"model_src_dir": "cv/densenet121/quantization/ptq",
"dataset_location": "/tf_dataset/dataset/imagenet",
"input_model": "/tf_dataset/tensorflow/densenet/densenet-121.pb",
"main_script": "main.py",
"batch_size": 32
},
"inception_v3": {
"model_src_dir": "cv/inception_v3/quantization/ptq",
"dataset_location": "/tf_dataset/dataset/imagenet",
"input_model": "/tf_dataset/pre-trained-models/inceptionv3/fp32/freezed_inceptionv3.pb",
"main_script": "main.py",
"batch_size": 32,
"fp32_model_url": "https://storage.googleapis.com/intel-optimized-tensorflow/models/v1_8/inceptionv3_fp32_pretrained_model.pb"
},
"mobilenetv2": {
"model_src_dir": "cv/mobilenet_v2/quantization/ptq",
"dataset_location": "/tf_dataset/dataset/imagenet",
"input_model": "/tf_dataset/pre-train-model-slim/pbfile/frozen_pb/frozen_mobilenet_v2.pb",
"main_script": "main.py",
"batch_size": 32
},
"vgg16": {
"model_src_dir": "cv/vgg16/quantization/ptq",
"dataset_location": "/tf_dataset/dataset/imagenet",
"input_model": "/tf_dataset/pre-train-model-slim/pbfile/frozen_pb/frozen_vgg16.pb",
"main_script": "main.py",
"batch_size": 32
},
"ViT": {
"model_src_dir": "image_recognition/vision_transformer/quantization/ptq",
"model_src_dir": "cv/vision_transformer/quantization/ptq",
"dataset_location": "/tf_dataset/dataset/imagenet",
"input_model": "/tf_dataset/tensorflow/vit/HF-ViT-Base16-Img224-frozen.pb",
"main_script": "main.py",
Expand All @@ -42,6 +92,63 @@
"input_model": "/tf_dataset/tensorflow/graphsage/graphsage_frozen_model.pb",
"main_script": "main.py",
"batch_size": 1000
},
"faster_rcnn_resnet50": {
"model_src_dir": "object_detection/faster_rcnn_resnet50/quantization/ptq",
"dataset_location": "/tf_dataset/tensorflow/coco_val.record",
"input_model": "/tf_dataset/pre-train-model-oob/object_detection/faster_rcnn_resnet50/frozen_inference_graph.pb",
"main_script": "main.py",
"batch_size": 10
},
"mask_rcnn_inception_v2": {
"model_src_dir": "object_detection/mask_rcnn_inception_v2/quantization/ptq",
"dataset_location": "/tf_dataset/tensorflow/coco_val.record",
"input_model": "/tf_dataset/pre-train-model-oob/object_detection/mask_rcnn_inception_v2/frozen_inference_graph.pb",
"main_script": "main.py",
"batch_size": 10
},
"mask_rcnn_inception_v2_ckpt": {
"model_src_dir": "object_detection/mask_rcnn_inception_v2/quantization/ptq",
"dataset_location": "/tf_dataset/tensorflow/coco_val.record",
"input_model": "/tf_dataset/pre-train-model-oob/object_detection/mask_rcnn_inception_v2",
"main_script": "main.py",
"batch_size": 10
},
"ssd_mobilenet_v1": {
"model_src_dir": "object_detection/ssd_mobilenet_v1/quantization/ptq",
"dataset_location": "/tf_dataset/tensorflow/coco_val.record",
"input_model": "/tf_dataset/pre-train-model-oob/object_detection/ssd_mobilenet_v1/frozen_inference_graph.pb",
"main_script": "main.py",
"batch_size": 10
},
"ssd_mobilenet_v1_ckpt": {
"model_src_dir": "object_detection/ssd_mobilenet_v1/quantization/ptq",
"dataset_location": "/tf_dataset/tensorflow/coco_val.record",
"input_model": "/tf_dataset/pre-train-model-oob/object_detection/ssd_mobilenet_v1",
"main_script": "main.py",
"batch_size": 10
},
"wide_deep_large_ds": {
"model_src_dir": "recommendation/wide_deep_large_ds/quantization/ptq",
"dataset_location": "/tf_dataset/tensorflow/wide_deep_large_ds/dataset",
"input_model": "/tf_dataset/tensorflow/wide_deep_large_ds/fp32_optimized_graph.pb",
"main_script": "inference.py",
"batch_size": 256,
"fp32_model_url": "https://storage.googleapis.com/intel-optimized-tensorflow/models/v1_8/wide_deep_fp32_pretrained_model.pb"
},
"3dunet-mlperf": {
"model_src_dir": "semantic_image_segmentation/3dunet-mlperf/quantization/ptq",
"dataset_location": "/tf_dataset2/models/tensorflow/3dunet/build",
"input_model": "/tf_dataset2/models/tensorflow/3dunet/3dunet_dynamic_ndhwc.pb",
"main_script": "run_accuracy.py",
"batch_size": 100
},
"style_transfer": {
"model_src_dir": "style_transfer/arbitrary_style_transfer/quantization/ptq",
"dataset_location": "style_images,content_images",
"input_model": "/tf_dataset/tensorflow/style_transfer/arbitrary_style_transfer/model.ckpt",
"main_script": "style_tune.py",
"batch_size": 1
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
Step-by-Step
============

This document list steps of reproducing densenet121 model tuning and benchmark results via Neural Compressor.
This example can run on Intel CPUs and GPUs.

> **Note**:
> The models is supported in Validated TensorFlow [Version](/docs/source/installation_guide.md#validated-software-environment).
# Prerequisite

## 1. Environment

### Installation
Recommend python 3.9 or higher version.
```shell
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 pre-trained model
The densenet-series comes from [tensorflow-densenet](https://github.com/pudae/tensorflow-densenet), please also follow the step [Prepare pre-trained model](#3-prepare-pre-trained-model) to get the pb files or use openvino download tools.
```shell
git clone https://github.com/openvinotoolkit/open_model_zoo.git
cd open_model_zoo/tools/downloader
git checkout tags/2021.2
pip install -r requirements.in
python downloader.py --name densenet-{121|161|169}-tf -o /PATH/TO/MODEL
```

## 3. Prepare Dataset

TensorFlow [models](https://github.com/tensorflow/models) repo provides [scripts and instructions](https://github.com/tensorflow/models/tree/master/research/slim#an-automated-script-for-processing-imagenet-data) to download, process and convert the ImageNet dataset to the TF records format.
We also prepared related scripts in ` examples/3.x_api/tensorflow/cv` directory. To download the raw images, the user must create an account with image-net.org. If you have downloaded the raw data and preprocessed the validation data by moving the images into the appropriate sub-directory based on the label (synset) of the image. we can use below command ro convert it to tf records format.

```shell
cd examples/3.x_api/tensorflow/cv
# convert validation subset
bash prepare_dataset.sh --output_dir=./densenet121/quantization/ptq/data --raw_dir=/PATH/TO/img_raw/val/ --subset=validation
# convert train subset
bash prepare_dataset.sh --output_dir=./densenet121/quantization/ptq/data --raw_dir=/PATH/TO/img_raw/train/ --subset=train
```
> **Note**:
> The raw ImageNet dataset resides in JPEG files should be in the following directory structure. Taking validation set as an example:<br>
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/PATH/TO/img_raw/val/n01440764/ILSVRC2012_val_00000293.JPEG<br>
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/PATH/TO/img_raw/val/n01440764/ILSVRC2012_val_00000543.JPEG<br>
> where 'n01440764' is the unique synset label associated with these images.

# Run

## 1 Quantization

```shell
bash run_quant.sh --input_model=/PATH/TO/densenet-121.pb \
--output_model=./nc_densenet121.pb --dataset_location=/path/to/ImageNet/
```

## 2. Benchmark
```shell
bash run_benchmark.sh --input_model=./nc_densenet121.pb --mode=accuracy --dataset_location=/path/to/ImageNet/ --batch_size=32
bash run_benchmark.sh --input_model=./nc_densenet121.pb --mode=performance --dataset_location=/path/to/ImageNet/ --batch_size=1
```
Loading