Skip to main content

PyTorch implementation for k-Lipschitz layers

Project description

deel-torchlip deel-torchlip

PyPI Python License

Documentation arXiv

Tests Linters

deel-torchlip is an open source Python API to build and train Lipschitz neural networks. It is built on top of PyTorch.

deel-torchlip provides:

  • Easy-to-use Lipschitz layers -- deel-torchlip layers are custom PyTorch layers and are very user-friendly. No need to be an expert in Lipschitz networks!
  • Custom losses for robustness -- The provided losses help improving adversarial robustness in classification tasks by increasing margins between outputs of the network (see our paper for more information).
  • Certified robustness -- One main advantage of Lipschitz networks is the costless computation of certificates ensuring that there is no adversarial attacks smaller than these certified radii of robustness.

For TensorFlow/Keras users, we released the deel-lip package offering a similar implementation based on Keras.

Install

The latest release can be installed using pip. The torch package will also be installed as a dependency. If torch is already present, be sure that the version is compatible with the deel-torchlip version.

$ pip install deel-torchlip

Usage

Creating a Lipschitz network is similar to building a PyTorch model: standard layers are replaced with their Lipschitz counterparts from deel-torchlip. PyTorch layers that are already Lipschitz can still be used in Lipschitz networks, such as torch.nn.ReLU() or torch.nn.Flatten().

import torch
from deel import torchlip

# Build a Lipschitz network with 4 layers, that can be used in a training loop,
# like any torch.nn.Sequential network
model = torchlip.Sequential(
    torchlip.SpectralConv2d(
        in_channels=3, out_channels=16, kernel_size=(3, 3), padding="same"
    ),
    torchlip.GroupSort2(),
    torch.nn.Flatten(),
    torchlip.SpectralLinear(15544, 64)
)

See the full documentation for a complete API description and for our tutorials to get started.

Citation

This library was built to support the work presented in our CVPR 2021 paper Achieving robustness in classification using optimal transport with Hinge regularization. If you use our library for your work, please cite our paper :wink:

@misc{2006.06520,
Author = {Mathieu Serrurier and Franck Mamalet and Alberto González-Sanz and Thibaut Boissin and Jean-Michel Loubes and Eustasio del Barrio},
Title = {Achieving robustness in classification using optimal transport with hinge regularization},
Year = {2020},
Eprint = {arXiv:2006.06520},
}

Contributions

Contributions are welcome! You can open an issue or fork this repository and propose a pull-request. The development environment with all required dependencies should be installed by running:

$ make prepare-dev

Code formatting and linting are performed with black and flake8. Tests are run with pytest. These three commands are gathered in:

$ make test

Finally, commits should respect pre-commit hooks. To be sure that your code changes are accepted, you can run the following target:

$ make check_all

License

Copyright IRT Antoine de Saint Exupéry et Université Paul Sabatier Toulouse III - All rights reserved. DEEL is a research program operated by IVADO, IRT Saint Exupéry, CRIAQ and ANITI - https://www.deel.ai/

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Acknowledgments

This project received funding from the French "Investing for the Future – PIA3" program within the Artificial and Natural Intelligence Toulouse Institute (ANITI). The authors gratefully acknowledge the support of the DEEL project.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

deel-torchlip-0.1.0.tar.gz (23.3 kB view details)

Uploaded Source

Built Distribution

deel_torchlip-0.1.0-py3-none-any.whl (42.9 kB view details)

Uploaded Python 3

File details

Details for the file deel-torchlip-0.1.0.tar.gz.

File metadata

  • Download URL: deel-torchlip-0.1.0.tar.gz
  • Upload date:
  • Size: 23.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.63.1 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.2

File hashes

Hashes for deel-torchlip-0.1.0.tar.gz
Algorithm Hash digest
SHA256 fd3a9eb0fdd9155774ba8bef3345d181341b189173536ac49e019b937eaa17b8
MD5 86ad2a5121a33b182d2b612e5d5abc6a
BLAKE2b-256 8ca367acad3647685d345bcf1e24c6d37b5d7a0a16a7c4c0e9f64f5f03c07609

See more details on using hashes here.

File details

Details for the file deel_torchlip-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: deel_torchlip-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 42.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.63.1 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.2

File hashes

Hashes for deel_torchlip-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a96d58907381e8079a4ec9dc9f4355a990ba361f59f40ef4fa590e8c1b39e371
MD5 93105173ecac2911024b711282e3c720
BLAKE2b-256 6d685190ed2f75f481fdc10dc33e49719372098372fdc73ff081418b39c1ad03

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page