This sample demonstrates DL model compression capabilities for Object Detection task. The sample consists of basic steps such as DL model initialization, dataset preparation, training loop over epochs and validation steps. The sample receives a configuration file where the training schedule, hyper-parameters, and compression settings are defined.
- RetinaNet from the official TF repository with minor modifications (custom implementation of upsamling is replaced with equivalent tf.keras.layers.UpSampling2D). YOLOv4 from the keras-YOLOv3-model-set repository.
- Support TensorFlow Datasets (TFDS) and TFRecords for COCO2017 dataset.
- Configuration file examples for sparsity, quantization, filter pruning and quantization with sparsity.
- Export to Frozen Graph or TensorFlow SavedModel that is supported by the OpenVINO™ toolkit.
- Distributed training on multiple GPUs on one machine is supported using tf.distribute.MirroredStrategy.
To work with the sample you should install the corresponding Python package dependencies
pip install -r examples/tensorflow/requirements.txt
This scenario demonstrates quantization with fine-tuning of RetinaNet with ResNet-50 backbone on the COCO2017 dataset.
The object detection sample supports TensorFlow Datasets (TFDS) and TFRecords.
The dataset type is specified in the configuration file by setting the "dataset_type"
parameter to "tfds"
or "tfrecords"
accordingly.
The TFDS format is used by default in the configuration file.
Please read the following guide for more information on how to use TFDS to download and prepare a dataset. For the COCO2017 dataset, TFDS supports automatic download. All you need to do is to specify the dataset and its type in the configuration file as follows:
"dataset": "coco/2017",
"dataset_type": "tfds"
To download the COCO2017 dataset and convert it to TFRecord format please use download_and_preprocess_coco.sh script from the official TensorFlow TPU repository.
bash <path_to_tensorflow_tpu_repo>/tools/datasets/download_and_preprocess_coco.sh <path_to_coco_data_dir>
This script installs the required libraries and then runs the dataset preprocessing. The output of the script is *.tfrecord
files in your local data directory.
The COCO2017 dataset in TFRecords format should be specified in the configuration file as follows:
"dataset": "coco/2017",
"dataset_type": "tfrecords"
- If you did not install the package, add the repository root folder to the
PYTHONPATH
environment variable. - Go to the
examples/tensorflow/object_detection
folder. - Download the pre-trained weights in H5 format and provide the path to them using
--weights
flag. The link to the archive with pre-trained weights can be found in theTensorFlow checkpoint
column of the results table. Select the checkpoint corresponding to theNone
compression algorithm, which includes the pre-trained weights for the FP32 model, without applying any compression algorithms. - Run the following command to start compression with fine-tuning on all available GPUs on the machine:
python main.py \ --mode=train \ --config=configs/quantization/retinanet_coco_int8.json \ --weights=<path_to_H5_file_with_pretrained_weights> --data=<path_to_dataset> \ --log-dir=../../results/quantization/retinanet_coco_int8
- Use the
--resume
flag with the path to the checkpoint to resume training from the defined checkpoint or folder with checkpoints to resume training from the last checkpoint.
To estimate the test scores of your model checkpoint, use the following command:
python main.py \
--mode=test \
--config=configs/quantization/retinanet_coco_int8.json \
--data=<path_to_dataset> \
--resume=<path_to_trained_model_checkpoint>
To validate an model checkpoint, make sure the compression algorithm settings are empty in the configuration file and path to.h5
file with model weights is provided in command line argument --weights
.
To export trained model to the Frozen Graph, use the following command:
python main.py \
--mode=export \
--config=configs/quantization/retinanet_coco_int8.json \
--resume=<path_to_trained_model_checkpoint> \
--to-frozen-graph=../../results/retinanet_coco_int8.pb
To export trained model to the SavedModel, use the following command:
python main.py \
--mode=export \
--config=configs/quantization/retinanet_coco_int8.json \
--resume=<path_to_trained_model_checkpoint> \
--to-saved-model=../../results/saved_model
To export trained model to the Keras H5, use the following command:
python main.py \
--mode=export \
--config=configs/quantization/retinanet_coco_int8.json \
--resume=<path_to_trained_model_checkpoint> \
--to-h5=../../results/retinanet_coco_int8.h5
To reduce memory footprint (if no further training is scheduled) it is useful to save the checkpoint without optimizer. Use the following command:
python ../common/prepare_checkpoint.py \
--config=configs/quantization/retinanet_coco_int8.json \
--resume=<path_to_trained_model_checkpoint> \
--checkpoint-save-dir=<path_to_save_optimized_model_checkpoint>
To export a model to the OpenVINO IR and run it using the Intel® Deep Learning Deployment Toolkit, refer to this tutorial.
- Download pre-trained ResNet-50 checkpoint from here.
- If you did not install the package, add the repository root folder to the
PYTHONPATH
environment variable. - Go to the
examples/tensorflow/object_detection
folder. - Run the following command to start training RetinaNet from scratch on all available GPUs on the machine:
python main.py \ --mode=train \ --config=configs/retinanet_coco.json \ --data=<path_to_dataset> \ --log-dir=../../results/quantization/retinanet_coco_baseline \ --backbone-checkpoint=<path_to_resnet50-2018-02-07_folder>
- Export trained model to the Keras H5 format.
Model | Compression algorithm | Dataset | mAP (drop) % | NNCF config file | TensorFlow checkpoint |
---|---|---|---|---|---|
RetinaNet | None | COCO2017 | 33.44 | retinanet_coco.json | Link |
RetinaNet | INT8 (per-tensor, symmetric for weights; per-tensor, symmetric for activations) | COCO2017 | 33.22 (0.22) | retinanet_coco_int8.json | Link |
RetinaNet | Sparsity 50% (Magnitude) | COCO2017 | 33.13 (0.31) | retinanet_coco_magnitude_sparsity.json | Link |
YOLOv4 | None | COCO2017 | 47.04 | yolo_v4_coco.json | Link |
YOLOv4 | INT8 (per-channel, symmetric for weights; per-tensor, asymmetric for activations) | COCO2017 | 46.30 (0.74) | yolo_v4_coco_int8.json | Link |
YOLOv4 | Sparsity 50% (Magnitude) | COCO2017 | 46.54 (0.50) | yolo_v4_coco_magnitude_sparsity.json | Link |
Model | Compression algorithm | Dataset | mAP (drop) % | GFLOPS | MParams | NNCF config file | TensorFlow checkpoint |
---|---|---|---|---|---|---|---|
RetinaNet | None | COCO2017 | 33.44 | 194.1 (100%) | 60.8 (100%) | retinanet_coco.json | Link |
RetinaNet | Filter Pruning 40%, geometric_median criterion | COCO2017 | 32.7 (0.74) | 107.7 (55.49%) | 34.7 (57.07%) | retinanet_coco_pruning.json | Link |
RetinaNet | Filter Pruning 40%, geometric_median criterion + INT8 (per-tensor, symmetric for weights; per-tensor, symmetric for activations) | COCO2017 | 32.53 (0.91) | 107.6 (55.44%) | 34.7 (57.07%) | retinanet_coco_pruning_int8.json | Link |