This sample demonstrates a DL model compression in case of an image-classification problem. The sample consists of basic steps such as DL model initialization, dataset preparation, training loop over epochs, training and validation steps. The sample receives a configuration file where the training schedule, hyper-parameters, and compression settings are defined.
- Torchvision models (ResNets, VGG, Inception, etc.) and datasets (ImageNet, CIFAR 10, CIFAR 100) support
- Custom models support
- Configuration file examples for sparsity, quantization, and quantization with sparsity
- Export to ONNX that is supported by the OpenVINO™ toolkit
- DataParallel and DistributedDataParallel modes
- Tensorboard-compatible output
This scenario demonstrates quantization with fine-tuning of MobileNet v2 on the ImageNet dataset.
To prepare the ImageNet dataset, refer to the following tutorial.
- If you did not install the package, add the repository root folder to the
PYTHONPATH
environment variable. - Go to the
examples/classification
folder. - Run the following command to start compression with fine-tuning on GPUs:
It may take a few epochs to get the baseline accuracy results.
python main.py -m train --config configs/quantization/mobilenet_v2_imagenet_int8.json --data /data/imagenet/ --log-dir=../../results/quantization/mobilenet_v2_int8/
- Use the
--multiprocessing-distributed
flag to run in the distributed mode. - Use the
--resume
flag with the path to a previously saved model to resume training.
To estimate the test scores of your model checkpoint, use the following command:
python main.py -m test --config=configs/quantization/mobilenet_v2_imagenet_int8.json --resume <path_to_trained_model_checkpoint>
To validate an FP32 model checkpoint, make sure the compression algorithm settings are empty in the configuration file or pretrained=True
is set.
To export trained model to the ONNX format, use the following command:
python main.py -m test --config=configs/quantization/mobilenet_v2_imagenet_int8.json --resume=../../results/quantization/mobilenet_v2_int8/6/checkpoints/epoch_1.pth --to-onnx=../../results/mobilenet_v2_int8.onnx
To export a model to the OpenVINO IR and run it using the Intel® Deep Learning Deployment Toolkit, refer to this tutorial.
Model | Compression algorithm | Dataset | PyTorch compressed accuracy | Config path | PyTorch checkpoint |
---|---|---|---|---|---|
ResNet-50 | None | ImageNet | 76.13 | examples/classification/configs/quantization/resnet50_imagenet.json | - |
ResNet-50 | INT8 | ImageNet | 76.05 | examples/classification/configs/quantization/resnet50_imagenet_int8.json | Link |
ResNet-50 | Mixed, 44.8% INT8 / 55.2% INT4 | ImageNet | 76.3 | examples/classification/configs/quantization/resnet50_imagenet_mixed_int_hawq.json | Link |
ResNet-50 | INT8 + Sparsity 61% (RB) | ImageNet | 75.22 | examples/classification/configs/sparsity_quantization/resnet50_imagenet_rb_sparsity_int8.json | Link |
ResNet-50 | INT8 + Sparsity 50% (RB) | ImageNet | 75.60 | examples/classification/configs/sparsity_quantization/resnet50_imagenet_rb_sparsity50_int8.json | Link |
Inception V3 | None | ImageNet | 77.32 | examples/classification/configs/quantization/inception_v3_imagenet.json | - |
Inception V3 | INT8 | ImageNet | 76.96 | examples/classification/configs/quantization/inception_v3_imagenet_int8.json | Link |
Inception V3 | INT8 + Sparsity 61% (RB) | ImageNet | 77.02 | examples/classification/configs/sparsity_quantization/inception_v3_imagenet_rb_sparsity_int8.json | Link |
MobileNet V2 | None | ImageNet | 71.81 | examples/classification/configs/quantization/mobilenet_v2_imagenet.json | Link |
MobileNet V2 | INT8 | ImageNet | 71.34 | examples/classification/configs/quantization/mobilenet_v2_imagenet_int8.json | Link |
MobileNet V2 | Mixed, 46.6% INT8 / 53.4% INT4 | ImageNet | 70.89 | examples/classification/configs/quantization/mobilenet_v2_imagenet_mixed_int_hawq.json | Link |
MobileNet V2 | INT8 + Sparsity 52% (RB) | ImageNet | 70.99 | examples/classification/configs/sparsity_quantization/mobilenet_v2_imagenet_rb_sparsity_int8.json | Link |
SqueezeNet V1.1 | None | ImageNet | 58.18 | examples/classification/configs/quantization/squeezenet1_1_imagenet.json | - |
SqueezeNet V1.1 | INT8 | ImageNet | 58.02 | examples/classification/configs/quantization/squeezenet1_1_imagenet_int8.json | Link |
SqueezeNet V1.1 | Mixed, 54.7% INT8 / 45.3% INT4 | ImageNet | 58.85 | examples/classification/configs/quantization/squeezenet1_1_imagenet_mixed_int_hawq.json | Link |
As an example of NNCF convolution binarization capabilities, you may use the configs in examples/classification/configs/binarization
to binarize ResNet18. Use the same steps/command line parameters as for quantization (for best results, specify --pretrained
), except for the actual binarization config path.
Model | Compression algorithm | Dataset | PyTorch compressed accuracy | Config path | PyTorch Checkpoint |
---|---|---|---|---|---|
ResNet-18 | None | ImageNet | 69.76 | examples/classification/configs/binarization/resnet18_imagenet.json | - |
ResNet-18 | XNOR (weights), scale/threshold (activations) | ImageNet | 61.59 | examples/classification/configs/binarization/resnet18_imagenet_binarization_xnor.json | Link |
ResNet-18 | DoReFa (weights), scale/threshold (activations) | ImageNet | 61.56 | examples/classification/configs/binarization/resnet18_imagenet_binarization_dorefa.json | Link |
Model | Compression algorithm | Dataset | PyTorch compressed accuracy | Config path | PyTorch Checkpoint |
---|---|---|---|---|---|
ResNet-50 | None | ImageNet | 76.13 | examples/classification/configs/quantization/resnet50_imagenet.json | - |
ResNet-50 | Filter pruning, 30%, magnitude criterion | ImageNet | 75.7 | examples/classification/configs/pruning/resnet50_pruning_magnitude.json | Link |
ResNet-50 | Filter pruning, 30%, geometric median criterion | ImageNet | 75.7 | examples/classification/configs/pruning/resnet50_pruning_geometric_median.json | Link |
ResNet-18 | None | ImageNet | 69.76 | examples/classification/configs/binarization/resnet18_imagenet.json | - |
ResNet-18 | Filter pruning, 30%, magnitude criterion | ImageNet | 68.73 | examples/classification/configs/pruning/resnet18_pruning_magnitude.json | Link |
ResNet-18 | Filter pruning, 30%, geometric median criterion | ImageNet | 68.97 | examples/classification/configs/pruning/resnet18_pruning_geometric_median.json | Link |
ResNet-34 | None | ImageNet | 73.31 | examples/classification/configs/pruning/resnet34_imagenet.json | - |
ResNet-34 | Filter pruning, 30%, magnitude criterion | ImageNet | 72.54 | examples/classification/configs/pruning/resnet34_pruning_magnitude.json | Link |
ResNet-34 | Filter pruning, 30%, geometric median criterion | ImageNet | 72.62 | examples/classification/configs/pruning/resnet34_pruning_geometric_median.json | Link |