This sample demonstrates a DL model compression in case of the Image Classification problem. 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.
- Models form the tf.keras.applications module (ResNets, MobileNets, Inception, etc.) and datasets (ImageNet, CIFAR 10, CIFAR 100) support.
- 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.
At this point it is assumed that you have already installed nncf. You can find information on downloading nncf here.
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 MobileNetV2 on the ImageNet dataset.
The classification 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. TFDS is used by default in all provided configuration files.
Please read the following guide for more information on how to use TFDS to download and prepare a dataset.
For the ImageNet dataset, TFDS requires a manual download. Please refer to the TFDS ImageNet Readme for download instructions. The TFDS ImageNet dataset should be specified in the configuration file as follows:
"dataset": "imagenet2012",
"dataset_type": "tfds"
To download the ImageNet dataset and convert it to TFRecord format, refer to the following tutorial. The ImageNet dataset in TFRecords format should be specified in the configuration file as follows:
"dataset": "imagenet2012",
"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/classification
folder.
Before compressing a model, it is highly recommended checking the accuracy of the pretrained model. All models which are supported in the sample has pretrained weights for ImageNet.
To load pretrained weights into a model and then evaluate the accuracy of that model, make sure that the pretrained=True option is set in the configuration file and use the following command:
python main.py \
--mode=test \
--config=configs/quantization/mobilenet_v2_imagenet_int8.json \
--data=<path_to_imagenet_dataset> \
--disable-compression
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/mobilenet_v2_imagenet_int8.json \
--data=<path_to_imagenet_dataset> \
--log-dir=../../results/quantization/mobilenet_v2_int8
It may take a few epochs to get the baseline accuracy results.
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 trained model checkpoint, use the following command:
python main.py \
--mode=test \
--config=configs/quantization/mobilenet_v2_imagenet_int8.json \
--data=<path_to_imagenet_dataset> \
--resume=<path_to_trained_model_checkpoint>
To export trained model to the Frozen Graph, use the following command:
python main.py \
--mode=export \
--config=configs/quantization/mobilenet_v2_imagenet_int8.json \
--resume=<path_to_trained_model_checkpoint> \
--to-frozen-graph=../../results/mobilenet_v2_int8.pb
To export trained model to the SavedModel, use the following command:
python main.py \
--mode=export \
--config=configs/quantization/mobilenet_v2_imagenet_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/mobilenet_v2_imagenet_int8.json \
--resume=<path_to_trained_model_checkpoint> \
--to-h5=../../results/mobilenet_v2_int8.h5
To export a model to the OpenVINO IR and run it using the Intel® Deep Learning Deployment Toolkit, refer to this tutorial.
Please see compression results for Tensorflow classification at our Model Zoo page.