|
| 1 | +# Neural Network Compression Framework (NNCF) |
| 2 | + |
| 3 | +This module contains a PyTorch\*-based framework and samples for neural networks compression. The framework is organized as a Python\* package that can be built and used in a standalone mode. The framework architecture is unified to make it easy to add different compression methods. The samples demonstrate the usage of compression algorithms for three different use cases on public models and datasets: Image Classification, Object Detection and Semantic Segmentation. |
| 4 | + |
| 5 | +## Key Features |
| 6 | + |
| 7 | +- Support of various compression algorithms, applied during a model fine-tuning process to achieve best compression parameters and accuracy: |
| 8 | + - [Quantization](./docs/compression_algorithms/Quantization.md) |
| 9 | + - [Binarization](./docs/compression_algorithms/Binarization.md) |
| 10 | + - [Sparsity](./docs/compression_algorithms/Sparsity.md) |
| 11 | + - [Filter pruning](./docs/compression_algorithms/Pruning.md) |
| 12 | +- Automatic, configurable model graph transformation to obtain the compressed model. The source model is wrapped by the custom class and additional compression-specific layers are inserted in the graph. |
| 13 | +- Common interface for compression methods |
| 14 | +- GPU-accelerated layers for faster compressed model fine-tuning |
| 15 | +- Distributed training support |
| 16 | +- Configuration file examples for each supported compression algorithm. |
| 17 | +- Git patches for prominent third-party repositories ([mmdetection](https://github.com/open-mmlab/mmdetection), [huggingface-transformers](https://github.com/huggingface/transformers)) demonstrating the process of integrating NNCF into custom training pipelines |
| 18 | +- Exporting compressed models to ONNX\* checkpoints ready for usage with [OpenVINO™ toolkit](https://github.com/opencv/dldt). |
| 19 | + |
| 20 | +## Usage |
| 21 | +The NNCF is organized as a regular Python package that can be imported in your target training pipeline script. |
| 22 | +The basic workflow is loading a JSON configuration script containing NNCF-specific parameters determining the compression to be applied to your model, and then passing your model along with the configuration script to the `nncf.create_compressed_model` function. |
| 23 | +This function returns a wrapped model ready for compression fine-tuning, and handle to the object allowing you to control the compression during the training process: |
| 24 | + |
| 25 | +```python |
| 26 | +import nncf |
| 27 | +from nncf import create_compressed_model, Config as NNCFConfig |
| 28 | + |
| 29 | +# Instantiate your uncompressed model |
| 30 | +from torchvision.models.resnet import resnet50 |
| 31 | +model = resnet50() |
| 32 | + |
| 33 | +# Load a configuration file to specify compression |
| 34 | +nncf_config = NNCFConfig.from_json("resnet50_int8.json") |
| 35 | + |
| 36 | +# Provide data loaders for compression algorithm initialization, if necessary |
| 37 | +nncf_config = register_default_init_args(nncf_config, loss_criterion, train_loader) |
| 38 | + |
| 39 | +# Apply the specified compression algorithms to the model |
| 40 | +comp_ctrl, compressed_model = create_compressed_model(model, nncf_config) |
| 41 | + |
| 42 | +# Now use compressed_model as a usual torch.nn.Module to fine-tune compression parameters along with the model weights |
| 43 | + |
| 44 | +# ... the rest of the usual PyTorch-powered training pipeline |
| 45 | + |
| 46 | +# Export to ONNX or .pth when done fine-tuning |
| 47 | +comp_ctrl.export_model("compressed_model.onnx") |
| 48 | +torch.save(compressed_model.state_dict(), "compressed_model.pth") |
| 49 | +``` |
| 50 | + |
| 51 | +For a more detailed description of NNCF usage in your training code, see [Usage.md](./docs/Usage.md). For in-depth examples of NNCF integration, browse the [sample scripts](#Model Compression Samples) code, or the [example patches](#Third-party repository integration) to third-party repositories. |
| 52 | + |
| 53 | +For more details about the framework architecture, refer to the [NNCFArchitecture.md](./docs/NNCFArchitecture.md). |
| 54 | + |
| 55 | + |
| 56 | +### Model Compression Samples |
| 57 | + |
| 58 | +For a quicker start with NNCF-powered compression, you can also try the sample scripts, each of which provides a basic training pipeline for classification, semantic segmentation and object detection neural network training correspondingly. |
| 59 | + |
| 60 | +To run the samples please refer to the corresponding tutorials: |
| 61 | +- [Image Classification sample](examples/classification/README.md) |
| 62 | +- [Object Detection sample](examples/object_detection/README.md) |
| 63 | +- [Semantic Segmentation sample](examples/semantic_segmentation/README.md) |
| 64 | + |
| 65 | +### Third-party repository integration |
| 66 | +NNCF may be straightforwardly integrated into training/evaluation pipelines of third-party repositories. |
| 67 | +See [third_party_integration](./third_party_integration) for examples of code modifications (Git patches and base commit IDs are provided) that are necessary to integrate NNCF into select repositories. |
| 68 | + |
| 69 | + |
| 70 | +### System requirements |
| 71 | +- Ubuntu\* 16.04 or later (64-bit) |
| 72 | +- Python\* 3.6 or later |
| 73 | +- NVidia CUDA\* Toolkit 10.2 or later |
| 74 | +- PyTorch\* 1.5 or higher. |
| 75 | + |
| 76 | +### Installation |
| 77 | +We suggest to install or use the package in the [Python virtual environment](https://docs.python.org/3/tutorial/venv.html). |
| 78 | + |
| 79 | +#### As a package built from checked-out repository: |
| 80 | +1) Install the following system dependencies: |
| 81 | + |
| 82 | +`sudo apt-get install python3-dev` |
| 83 | + |
| 84 | +2) Install the package and its dependencies by running the following in the repository root directory: |
| 85 | + |
| 86 | +- For CPU & GPU-powered execution: |
| 87 | +`python setup.py install` |
| 88 | +- For CPU-only installation |
| 89 | +`python setup.py install --cpu-only` |
| 90 | + |
| 91 | +#### As a Docker image |
| 92 | +Use one of the Dockerfiles in the [docker](./docker) directory to build an image with an environment already set up and ready for running NNCF [sample scripts](#Model Compression Samples). |
| 93 | + |
| 94 | + |
| 95 | +## NNCF compression results |
| 96 | + |
| 97 | +Achieved using sample scripts and NNCF configuration files provided with this repository. See README.md files for [sample scripts](#Model Compression Samples) for links to exact configuration files and final PyTorch checkpoints. |
| 98 | + |
| 99 | + |
| 100 | +|Model|Compression algorithm|Dataset|PyTorch FP32 baseline|PyTorch compressed accuracy| |
| 101 | +| :---: | :---: | :---: | :---: | :---: | |
| 102 | +|ResNet-50|None|ImageNet|-|76.13| |
| 103 | +|ResNet-50|INT8|ImageNet|76.13|76.05| |
| 104 | +|ResNet-50|Mixed, 44.8% INT8 / 55.2% INT4|ImageNet|76.13|76.3| |
| 105 | +|ResNet-50|INT8 + Sparsity 61% (RB)|ImageNet|76.13|75.28| |
| 106 | +|ResNet-50|Filter pruning, 30%, magnitude criterion|ImageNet|76.13|75.7| |
| 107 | +|ResNet-50|Filter pruning, 30%, geometric median criterion|ImageNet|76.13|75.7| |
| 108 | +|Inception V3|None|ImageNet|-|77.32| |
| 109 | +|Inception V3|INT8|ImageNet|77.32|76.92| |
| 110 | +|Inception V3|INT8 + Sparsity 61% (RB)|ImageNet|77.32|76.98| |
| 111 | +|MobileNet V2|None|ImageNet|-|71.81| |
| 112 | +|MobileNet V2|INT8|ImageNet|71.81|71.34| |
| 113 | +|MobileNet V2|Mixed, 46.6% INT8 / 53.4% INT4|ImageNet|71.81|70.89| |
| 114 | +|MobileNet V2|INT8 + Sparsity 52% (RB)|ImageNet|71.81|70.99| |
| 115 | +|SqueezeNet V1.1|None|ImageNet|-|58.18| |
| 116 | +|SqueezeNet V1.1|INT8|ImageNet|58.18|58.02| |
| 117 | +|SqueezeNet V1.1|Mixed, 54.7% INT8 / 45.3% INT4|ImageNet|58.18|58.84| |
| 118 | +|ResNet-18|None|ImageNet|-|69.76| |
| 119 | +|ResNet-18|XNOR (weights), scale/threshold (activations)|ImageNet|69.76|61.61| |
| 120 | +|ResNet-18|DoReFa (weights), scale/threshold (activations)|ImageNet|69.76|61.59| |
| 121 | +|ResNet-18|Filter pruning, 30%, magnitude criterion|ImageNet|69.76|68.69| |
| 122 | +|ResNet-18|Filter pruning, 30%, geometric median criterion|ImageNet|69.76|68.97| |
| 123 | +|ResNet-34|None|ImageNet|-|73.31| |
| 124 | +|ResNet-34|Filter pruning, 30%, magnitude criterion|ImageNet|73.31|72.54| |
| 125 | +|ResNet-34|Filter pruning, 30%, geometric median criterion|ImageNet|73.31|72.60| |
| 126 | +|SSD300-BN|None|VOC12+07|-|78.28| |
| 127 | +|SSD300-BN|INT8|VOC12+07|78.28|78.07| |
| 128 | +|SSD300-BN|INT8 + Sparsity 70% (Magnitude)|VOC12+07|78.28|78.01| |
| 129 | +|SSD512-BN|None|VOC12+07|-|80.26| |
| 130 | +|SSD512-BN|INT8|VOC12+07|80.26|80.02| |
| 131 | +|SSD512-BN|INT8 + Sparsity 70% (Magnitude)|VOC12+07|80.26|79.98| |
| 132 | +|UNet|None|CamVid|-|71.95| |
| 133 | +|UNet|INT8|CamVid|71.95|71.66| |
| 134 | +|UNet|INT8 + Sparsity 60% (Magnitude)|CamVid|71.95|71.72| |
| 135 | +|ICNet|None|CamVid|-|67.89| |
| 136 | +|ICNet|INT8|CamVid|67.89|67.87| |
| 137 | +|ICNet|INT8 + Sparsity 60% (Magnitude)|CamVid|67.89|67.24| |
| 138 | +|UNet|None|Mapillary|-|56.23| |
| 139 | +|UNet|INT8|Mapillary|56.23|56.12| |
| 140 | +|UNet|INT8 + Sparsity 60% (Magnitude)|Mapillary|56.23|56.0| |
| 141 | + |
0 commit comments