• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

nncf_pytorch: This repository contains a PyTorch*-based framework and samples fo ...

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称:

nncf_pytorch

开源软件地址:

https://gitee.com/openvinotoolkit-prc/nncf_pytorch

开源软件介绍:

Neural Network Compression Framework (NNCF)

NNCF provides a suite of advanced algorithms for Neural Networks inference optimization in OpenVINO™ with minimal accuracy drop.

NNCF is designed to work with models from PyTorch and TensorFlow.

NNCF provides samples that demonstrate the usage of compression algorithms for three different use cases on public PyTorch andTensorFlow models and datasets: Image Classification, Object Detection and Semantic Segmentation.Compression results achievable with the NNCF-powered samples can be found in a table atthe end of this document.

The framework is organized as a Python* package that can be built and used in a standalone mode. The frameworkarchitecture is unified to make it easy to add different compression algorithms for both PyTorch and TensorFlow deeplearning frameworks.

Key Features

  • Support of various compression algorithms, applied during a model fine-tuning process to achieve a better performance-accuracy trade-off:

    Compression algorithmPyTorchTensorFlow
    QuantizationSupportedSupported
    Mixed-Precision QuantizationSupportedNot supported
    BinarizationSupportedNot supported
    SparsitySupportedSupported
    Filter pruningSupportedSupported
  • Automatic, configurable model graph transformation to obtain the compressed model.

    NOTE: Limited support for TensorFlow models. The models created using Sequential or Keras Functional API are only supported.

  • Common interface for compression methods.

  • GPU-accelerated layers for faster compressed model fine-tuning.

  • Distributed training support.

  • Configuration file examples for each supported compression algorithm.

  • Git patches for prominent third-party repositories (huggingface-transformers) demonstrating the process of integrating NNCF into custom training pipelines

  • Exporting PyTorch compressed models to ONNX* checkpoints and TensorFlow compressed models to SavedModel or Frozen Graph format, ready to use with OpenVINO™ toolkit.

  • Support for Accuracy-Aware model training pipelines via the Adaptive Compression Level Training and Early Exit Training.

Usage

The NNCF is organized as a regular Python package that can be imported in your target training pipeline script.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 create_compressed_model function.This function returns a model with additional modifications necessary to enable algorithm-specific compression during fine-tuning and handle to the object allowing you to control the compression during the training process:

Usage example with PyTorch

import torchimport nncf  # Important - should be imported directly after torchfrom nncf import NNCFConfigfrom nncf.torch import create_compressed_model, register_default_init_args# Instantiate your uncompressed modelfrom torchvision.models.resnet import resnet50model = resnet50()# Load a configuration file to specify compressionnncf_config = NNCFConfig.from_json("resnet50_int8.json")# Provide data loaders for compression algorithm initialization, if necessaryimport torchvision.datasets as datasetsrepresentative_dataset = datasets.ImageFolder("/path")init_loader = torch.utils.data.DataLoader(representative_dataset)nncf_config = register_default_init_args(nncf_config, init_loader)# Apply the specified compression algorithms to the modelcompression_ctrl, compressed_model = create_compressed_model(model, nncf_config)# Now use compressed_model as a usual torch.nn.Module # to fine-tune compression parameters along with the model weights# ... the rest of the usual PyTorch-powered training pipeline# Export to ONNX or .pth when done fine-tuningcompression_ctrl.export_model("compressed_model.onnx")torch.save(compressed_model.state_dict(), "compressed_model.pth")

Usage example with TensorFlow

import tensorflow as tffrom nncf import NNCFConfigfrom nncf.tensorflow import create_compressed_model, register_default_init_args# Instantiate your uncompressed modelfrom tensorflow.keras.applications import ResNet50model = ResNet50()# Load a configuration file to specify compressionnncf_config = NNCFConfig.from_json("resnet50_int8.json")# Provide dataset for compression algorithm initializationrepresentative_dataset = tf.data.Dataset.list_files("/path/*.jpeg")nncf_config = register_default_init_args(nncf_config, representative_dataset, batch_size=1)# Apply the specified compression algorithms to the modelcompression_ctrl, compressed_model = create_compressed_model(model, nncf_config)# Now use compressed_model as a usual Keras model# to fine-tune compression parameters along with the model weights# ... the rest of the usual TensorFlow-powered training pipeline# Export to Frozen Graph, TensorFlow SavedModel or .h5  when done fine-tuning compression_ctrl.export_model("compressed_model.pb", save_format='frozen_graph')

For a more detailed description of NNCF usage in your training code, see this tutorial.For in-depth examples of NNCF integration, browse the sample scripts code, or the example patches to third-party repositories.

Model Compression Samples

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.

To run the samples please refer to the corresponding tutorials:

Model Compression Notebooks

A collection of ready-to-run Jupyter* notebooks are also available to demonstrate how to use NNCF compression algorithmsto optimize models for inference with the OpenVINO Toolkit.

Third-party repository integration

NNCF may be straightforwardly integrated into training/evaluation pipelines of third-party repositories.

Used by

  • OpenVINO Training Extensions

    NNCF is integrated into OpenVINO Training Extensions as model optimization backend. So you can train, optimize and export new models based on the available model templates as well as run exported models with OpenVINO.

Git patches for third-party repository

See third_party_integration for examples of code modifications (Git patches and base commit IDs are provided) that are necessary to integrate NNCF into the following repositories:

System requirements

  • Ubuntu* 18.04 or later (64-bit)
  • Python* 3.6.2 or later
  • Supported frameworks:
    • PyTorch* >=1.5.0, <=1.9.1 (1.8.0 not supported)
    • TensorFlow* >=2.4.0, <=2.5.3

This repository is tested on Python* 3.6.2+, PyTorch* 1.9.1 (NVidia CUDA* Toolkit 10.2) and TensorFlow* 2.5.3 (NVidia CUDA* Toolkit 11.2).

Installation

We suggest to install or use the package in the Python virtual environment.

If you want to optimize a model from PyTorch, install PyTorch by following PyTorch installation guide.If you want to optimize a model from TensorFlow, install TensorFlow by following TensorFlow installation guide.

As a package built from a checked-out repository:

Install the package and its dependencies by running the following in the repository root directory:

python setup.py install

Alternatively, If you don't install any backend you can install NNCF and PyTorch in one line with:

python setup.py install --torch

Install NNCF and TensorFlow in one line:

python setup.py install --tf

NB: For launching example scripts in this repository, we recommend replacing the install option above with develop and setting the PYTHONPATH variable to the root of the checked-out repository.

As a PyPI package:

NNCF can be installed as a regular PyPI package via pip:

pip install nncf

Alternatively, If you don't install any backend you can install NNCF and PyTorch in one line with:

pip install nncf[torch]

Install NNCF and TensorFlow in one line:

pip install nncf[tf]

NNCF is also available via conda:

conda install -c conda-forge nncf

From a specific commit hash using pip:

pip install git+https://github.com/openvinotoolkit/nncf@bd189e2#egg=nncf

Note that in order for this to work for pip versions >= 21.3, your Git version must be at least 2.22.

As a Docker image

Use one of the Dockerfiles in the docker directory to build an image with an environment already set up and ready for running NNCF sample scripts.

Contributing

Refer to the CONTRIBUTING.md file for guidelines on contributions to the NNCF repository.

NNCF Compressed Model Zoo

Results achieved using sample scripts, example patches to third-party repositories and NNCF configuration files providedwith this repository. See README.md files for sample scripts and example patchesto find instruction and links to exact configuration files and final checkpoints.

PyTorch models

Classification

PyTorch ModelCompression algorithmDatasetAccuracy (Drop) %
ResNet-50INT8ImageNet76.42 (-0.26)
ResNet-50INT8 (per-tensor for weights)ImageNet76.37 (-0.21)
ResNet-50Mixed, 44.8% INT8 / 55.2% INT4ImageNet76.2 (-0.04)
ResNet-50INT8 + Sparsity 61% (RB)ImageNet75.43 (0.73)
ResNet-50INT8 + Sparsity 50% (RB)ImageNet75.55 (0.61)
ResNet-50Filter pruning, 40%, geometric median criterionImageNet75.62 (0.54)
Inception V3INT8ImageNet78.25 (-0.91)
Inception V3INT8 + Sparsity 61% (RB)ImageNet77.58 (-0.24)
MobileNet V2INT8ImageNet71.35 (0.58)
MobileNet V2INT8 (per-tensor for weights)ImageNet71.3 (0.63)
MobileNet V2Mixed, 46.6% INT8 / 53.4% INT4ImageNet70.92 (1.01)
MobileNet V2INT8 + Sparsity 52% (RB)ImageNet71.11 (0.82)
MobileNet V3 smallINT8ImageNet66.94 (0.73)
SqueezeNet V1.1INT8ImageNet58.28 (-0.04)
SqueezeNet V1.1INT8 (per-tensor for weights)ImageNet58.26 (-0.02)
SqueezeNet V1.1Mixed, 54.7% INT8 / 45.3% INT4ImageNet58.9 (-0.66)
ResNet-18XNOR (weights), scale/threshold (activations)ImageNet61.63 (8.17)
ResNet-18DoReFa (weights), scale/threshold (activations)ImageNet61.61 (8.19)
ResNet-18Filter pruning, 40%, magnitude criterionImageNet69.26 (0.54)
ResNet-18Filter pruning, 40%, geometric median criterionImageNet69.32 (0.48)
ResNet-34Filter pruning, 40%, geometric median criterionImageNet72.73 (0.57)
GoogLeNetFilter pruning, 40%, geometric median criterionImageNet68.82 (0.93)

Object detection

PyTorch ModelCompression algorithmDatasetmAP (drop) %
SSD300-MobileNetINT8 + Sparsity 70% (Magnitude)VOC12+07 train, VOC07 eval62.94 (-0.71)
SSD300-VGG-BNINT8VOC12+07 train, VOC07 eval77.96 (0.32)
SSD300-VGG-BNINT8 + Sparsity 70% (Magnitude)VOC12+07 train, VOC07 eval77.59 (0.69)
SSD300-VGG-BNFilter pruning, 40%, geometric median criterionVOC12+07 train, VOC07 eval77.72 (0.56)
SSD512-VGG-BNINT8VOC12+07 train, VOC07 eval80.12 (0.14)
SSD512-VGG-BNINT8 + Sparsity 70% (Magnitude)VOC12+07 train, VOC07 eval79.67 (0.59)

Semantic segmentation

PyTorch ModelCompression algorithmDatasetAccuracy (Drop) %
UNetINT8CamVid71.8 (0.15)
UNetINT8 + Sparsity 60% (Magnitude)CamVid72.03 (-0.08)
ICNetINT8CamVid67.86 (0.03)
ICNetINT8 + Sparsity 60% (Magnitude)CamVid67.18 (0.71)
UNetINT8Mapillary55.87 (0.36)
UNetINT8 + Sparsity 60% (Magnitude)Mapillary55.65 (0.58)
UNetFilter pruning, 25%, geometric median criterionMapillary55.62 (0.61)

NLP (HuggingFace Transformers-powered models)

PyTorch ModelCompression algorithmDatasetAccuracy (Drop) %
BERT-base-chineseINT8XNLI77.22 (0.46)
BERT-base-casedINT8CoNLL200399.18 (-0.01)
BERT-base-casedINT8MRPC84.8 (-0.24)
BERT-large (Whole Word Masking)INT8SQuAD v1.1F1: 92.68 (0.53)
RoBERTa-largeINT8MNLImatched: 89.25 (1.35)
DistilBERT-baseINT8SST-290.3 (0.8)
MobileBERTINT8SQuAD v1.1F1: 89.4 (0.58)
GPT-2INT8WikiText-2 (raw)perplexity: 20.9 (-1.17)

TensorFlow models

Classification

Tensorflow ModelCompression algorithmDatasetAccuracy (Drop) %
Inception V3INT8 (per-tensor for weights)ImageNet78.36 (-0.44)
Inception V3Sparsity 54% (Magnitude)ImageNet77.87 (0.03)
Inception V3INT8 (per-tensor for weights) + Sparsity 61% (RB)ImageNet77.58 (0.32)
MobileNet V2INT8 (per-tensor for weights)ImageNet71.66 (0.19)
MobileNet V2Sparsity 50% (RB)ImageNet71.34 (0.51)
MobileNet V2INT8 (per-tensor for weights) + Sparsity 52% (RB)ImageNet71.0 (0.85)
MobileNet V3 smallINT8 (per-channel, symmetric for weights; per-tensor, asymmetric for activations)ImageNet67.75 (0.63)
MobileNet V3 smallINT8 (per-channel, symmetric for weights; per-tensor, asymmetric for activations) + Sparsity 42% (RB)ImageNet67.55 (0.83)
MobileNet V3 largeINT8 (per-channel, symmetric for weights; per-tensor, asymmetric for activations)ImageNet75.02 (0.79)
MobileNet V3 largeINT8 (per-channel, symmetric for weights; per-tensor, asymmetric for activations) + Sparsity 42% (RB)ImageNet75.28 (0.53)
ResNet50INT8 (per-tensor for weights)ImageNet75.0 (0.04)
ResNet50Sparsity 80% (RB)ImageNet74.36 (0.68)
ResNet50INT8 (per-tensor for weightsy) + Sparsity 65% (RB)ImageNet74.3 (0.74)
ResNet50Filter Pruning 40%, geometric_median criterionImageNet74.98 (0.06)
ResNet50Filter Pruning 40%, geometric_median criterion + INT8 (per-tensor for weights)ImageNet75.08 (-0.04)
TensorFlow Hub MobileNet V2Sparsity 35% (Magnitude)ImageNet71.90 (-0.06)

Object detection

TensorFlow ModelCompression algorithmDatasetmAP (drop) %
RetinaNetINT8 (per-tensor for weights)COCO201733.18 (0.26)
RetinaNetSparsity 50% (Magnitude)COCO201733.13 (0.31)
RetinaNetFilter Pruning 40%, geometric_median criterionCOCO201732.7 (0.74)
RetinaNetFilter Pruning 40%, geometric_median criterion + INT8 (per-tensor for weights)COCO201732.68 (0.76)
YOLOv4INT8 (per-channel, symmetric for weights; per-tensor, asymmetric for activations)COCO201746.30 (0.74)
YOLOv4Sparsity 50% (Magnitude)COCO201746.54 (0.50)

Instance segmentation

TensorFlow ModelCompression algorithmDatasetmAP (drop) %
MaskRCNNINT8 (per-tensor for weights)COCO2017bbox: 37.27 (0.06)
segm: 33.54 (0.02)
MaskRCNNSparsity 50% (Magnitude)COCO2017bbox: 36.93 (0.40)
segm: 33.23 (0.33)

Citing

@article{kozlov2020neural,    title =   {Neural network compression framework for fast model inference},    author =  {Kozlov, Alexander and Lazarevich, Ivan and Shamporov, Vasily and Lyalyushkin, Nikolay and Gorbachev, Yury},    journal = {arXiv preprint arXiv:2002.08679},    year =    {2020}}

Legal Information

[*] Other names and brands may be claimed as the property of others.


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap