Skip to main content

Batch-Normalization Folding

In this repository, we propose an implementation of the batch-normalization folding algorithm from IJCAI 2022. Batch-Normalization Folding implements the batch normalization layer by folding it into a appropriate layer. The original batch-normalization layers are removed without changing the predictive function defined by the neural network. The simpliest scenario is an application for a fully-connected layer followed by a batch-normalization layer, we get

x \mapsto \gamma \frac{Ax + b - \mu}{\sigma + \epsilon} + \beta = \gamma \frac{A}{\sigma +\epsilon} x + \frac{b - \mu}{\sigma + \epsilon} + \beta

Thus the two layers can be expressed as a single fully-connected layer at inference without any change in the predictive function.

Use

This repository is available as a pip package (use pip install tensorflow-batchnorm-folding). This implementation is compatible with tf.keras.Model instances. It was tested with the following models

  • ResNet 50
  • MobileNet V2
  • MobileNet V3
  • EfficentNet B0

To run a simple test:

from batch_normalization_folding.folder import fold_batchnormalization_layers
import tensorflow as tf
mod=tf.keras.applications.efficientnet.EfficientNetB0()
folded_model,output_str=fold_batchnormalization_layers(mod,verbose=True)

The output_str is either the ratio num_layers_folded/num_layers_not_folded or 'failed' to state a failure in the process.

Parameters

The function fold_batchnormalization_layers can be called with multiple parameters.

Parameter Option
model Model to fold.
folding_mechanism ban-off uses BaN-OFF als folding mechanism (recommended)
simple uses a folding mechanism that only folds neighboring layers.
Default: ban-off
verbose True prints additional information during the folding process.
False Disables the printing of additional information.
Default: False

Layer folding

This Python implementation supports folding into many types of layers, supported is the folding of batch-normalization parameters into:

  • Dense
  • Conv1D
  • Conv2D
  • DepthwiseConv2D

To Do

  • unit test on all keras applciations models
  • check package installement
  • deal with Concatenate layers

Cite

@inproceedings{yvinec2022fold,
  title={To Fold or Not to Fold: a Necessary and Sufficient Condition on Batch-Normalization Layers Folding},
  author={Yvinec, Edouard and Dapogny, Arnaud and Bailly, Kevin},
  journal={IJCAI},
  year={2022}
}

Performance on Base Models

+------------------------------------+
|             ResNet 50              |
+------------------------------------+
| BN layers folded         |    53   |
| BN layers not folded     |    0    |
+------------------------------------+
|          EfficientNet B0           |
+------------------------------------+
| BN layers folded         |    49   |
| BN layers not folded     |    0    |
+------------------------------------+
|            MobileNet V2            |
+------------------------------------+
| BN layers folded         |    52   |
| BN layers not folded     |    0    |
+------------------------------------+
|            MobileNet V3            |
+------------------------------------+
| BN layers folded         |    34   |
| BN layers not folded     |    0    |
+------------------------------------+
|        Inception ResNet V2         |
+------------------------------------+
| BN layers folded         |   204   |
| BN layers not folded     |    0    |
+------------------------------------+
|            Inception V3            |
+------------------------------------+
| BN layers folded         |    94   |
| BN layers not folded     |    0    |
+------------------------------------+
|               NASNet               |
+------------------------------------+
| BN layers folded         |    28   |
| BN layers not folded     |   164   |
+------------------------------------+
|            DenseNet 121            |
+------------------------------------+
| BN layers folded         |    59   |
| BN layers not folded     |    62   |
+------------------------------------+

Metadata

Release files for tensorflow-batchnorm-folding 1.0.9

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for tensorflow-batchnorm-folding 1.0.9
File Size Uploaded Provenance
tensorflow_batchnorm_folding-1.0.9.tar.gz 26.1 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for tensorflow-batchnorm-folding 1.0.9
File Interpreter ABI Platform
tensorflow_batchnorm_folding-1.0.9-py3-none-any.whl Python 3 none any Details

Total release size: 52.3 kB

Release files / tensorflow_batchnorm_folding-1.0.9.tar.gz

Download URL tensorflow_batchnorm_folding-1.0.9.tar.gz
Size 26.1 kB
Tags Source
SHA-256 checksum
How to use checksums
fd7e600635cb7e661ef70171e77d19b107c526ae1ac1992f8afe24f198ef0c0d
BLAKE2b-256 checksum
How to use checksums
ed3548ea2c5ed08e80ca52820e3c9a244c4743262b4488f9c5e266ea84d01e18
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.13

Release files / tensorflow_batchnorm_folding-1.0.9-py3-none-any.whl

Download URL tensorflow_batchnorm_folding-1.0.9-py3-none-any.whl
Size 26.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e8f86010acbf7b30cc7511fa458df4e717db32aad38fab07bcc7da89813bff74
BLAKE2b-256 checksum
How to use checksums
4ccf6c6892cd1d23372c41efbbadb9c0560601f3d5235194b3958ba7a7c204a4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.13

Security for tensorflow-batchnorm-folding 1.0.9

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page