Skip to main content

Formal verification of neural networks using abstract interpretation and affine arithmetic

Project description

AbstractNN

PyPI version Python 3.8+ License: MIT Documentation Status Code style: black

AbstractNN is a Python library for formal verification of neural networks using abstract interpretation and affine arithmetic. It provides mathematically sound guarantees about network behavior under input perturbations.

๐ŸŽฏ Key Features

  • Sound Verification: Mathematically proven bounds on network outputs
  • Affine Arithmetic: Symbolic tracking of input-output dependencies
  • Multiple Relaxations: Linear, interval, and hybrid relaxation strategies
  • ONNX Support: Works with models exported from PyTorch, TensorFlow, etc.
  • Partial Evaluation: Efficient verification of network sub-regions
  • Soundness Checking: Monte Carlo validation of formal bounds
  • Production Ready: Well-tested, documented, and type-hinted

๐Ÿ“ฆ Installation

From PyPI (recommended)

pip install abstractnn

From source

git clone https://github.com/guillaume117/abstractNN.git
cd abstractNN
pip install -e .

# Download pre-trained models
chmod +x scripts/download_models.sh
./scripts/download_models.sh

With optional dependencies

# Development tools
pip install abstractNN[dev]

# Documentation building
pip install abstractNN[docs]

# GPU acceleration
pip install abstractNN[gpu]

# Visualization tools
pip install abstractNN[viz]

# All extras
pip install abstractNN[dev,docs,gpu,viz]

๐Ÿš€ Quick Start

Basic Verification

from abstractnn import AffineEngine, BoundPropagator, ONNXParser

# Load your ONNX model
parser = ONNXParser('model.onnx')
layers = parser.parse()

# Create verification engine
engine = AffineEngine()
propagator = BoundPropagator(engine)

# Define perturbed input
import numpy as np
image = np.random.rand(3, 28, 28).astype(np.float32)
noise_level = 0.1  # Lโˆž perturbation radius

# Create symbolic expressions
input_exprs = engine.create_input_expressions(image, noise_level)

# Propagate through network
output_exprs = propagator.propagate(input_exprs, layers, image.shape)

# Get guaranteed output bounds
bounds = [expr.get_bounds() for expr in output_exprs]
print(f"Output bounds: [{bounds[0][0]:.4f}, {bounds[0][1]:.4f}]")

VGG16 Partial Verification

from abstractnn import verify_partial_soundness

# Verify first 5 layers of VGG16
result = verify_partial_soundness(
    model_path='vgg16.onnx',
    image=test_image,
    noise_level=0.01,
    num_layers=5,
    num_mc_samples=100
)

if result['success']:
    print(f"Sound: {result['soundness_report']['is_sound']}")
    print(f"Coverage: {result['soundness_report']['coverage_ratio']*100:.1f}%")

Command Line Interface

# Verify a model (soundness checking)
abstractnn-verify --model model.onnx --image test.npy --epsilon 0.01

# Formal evaluation with affine arithmetic (default FMNIST example)
abstractnn-eval

# Custom evaluation
abstractnn-eval --model custom.onnx --image test.png --noise 0.1 --output results.json

# With detailed report
abstractnn-eval --detailed-report --export-report-csv report.csv

# Get library info
abstractnn-info

โš ๏ธ Large Model Files

Note: Pre-trained model files (like VGG16) are NOT included in the repository due to their large size (500+ MB).

To download models:

# Option 1: Use download script
./scripts/download_models.sh

# Option 2: Models will be automatically downloaded when running tests
python -m pytest tests/test_vgg16_formal.py

The models will be saved to the models/ directory.

๐ŸŽฏ Use Cases

1. Soundness Verification (abstractnn-verify)

Verify that formal bounds are sound by comparing with Monte Carlo sampling:

abstractnn-verify --model vgg16.onnx --image test.npy --epsilon 0.01 --layers 5

2. Formal Evaluation (abstractnn-eval)

Compute guaranteed output bounds for all classes:

# Use default FMNIST example
abstractnn-eval

# Custom model and image
abstractnn-eval \
    --model mymodel.onnx \
    --image myimage.png \
    --noise 0.05 \
    --output results.json \
    --detailed-report

๐Ÿ“š Documentation

Full documentation is available at abstractnn.readthedocs.io

๐Ÿงช Running Tests

# Run all tests
pytest tests/ -v

# Run with coverage
pytest tests/ --cov=abstractnn --cov-report=html

# Run specific test
pytest tests/test_affine_engine.py -v

๐Ÿ—๏ธ Architecture

abstractNN/
โ”œโ”€โ”€ abstractnn/           # Main package
โ”‚   โ”œโ”€โ”€ __init__.py
โ”‚   โ”œโ”€โ”€ affine_engine.py  # Affine expression management
โ”‚   โ”œโ”€โ”€ bound_propagator.py  # Layer-by-layer propagation
โ”‚   โ”œโ”€โ”€ relaxer.py        # Non-linear relaxations
โ”‚   โ”œโ”€โ”€ onnx_parser.py    # ONNX model parsing
โ”‚   โ”œโ”€โ”€ partial_evaluator.py  # Partial network evaluation
โ”‚   โ”œโ”€โ”€ soundness_checker.py  # Soundness validation
โ”‚   โ””โ”€โ”€ cli.py            # Command-line interface
โ”œโ”€โ”€ tests/                # Test suite
โ”œโ”€โ”€ docs/                 # Sphinx documentation
โ”œโ”€โ”€ examples/             # Usage examples
โ””โ”€โ”€ scripts/              # Utility scripts
```## ๐Ÿ”ฌ Research & CitationsIf you use abstractNN in your research, please cite:```cite@software{abstractnn2025,  title={abstractNN: Formal Verification of Neural Networks using Abstract Interpretation},  author={Berthelot, Guillaume},  year={2025},  url={https://github.com/guillaume117/abstractNN},  version={0.1.0}}```

## ๐Ÿค Contributing
Contributions are welcome! Please see CONTRIBUTING.md for guidelines.

```bash
# Setup development environment
git clone https://github.com/flyworthi/abstractNN.git
cd abstractNN

pip install -e .[dev]# Run testspytest tests/# Format codeblack abstractnn/ tests/isort abstractnn/ tests/# Type checkingmypy abstractnn/

๐Ÿ“Š Comparison with Other Tools

Method Soundness Tightness Scalability Speed
Monte Carlo โŒ No N/A โœ… High โœ… Fast
MILP โœ… Yes โœ… Exact โŒ Low โŒ Slow
Interval โœ… Yes โŒ Loose โœ… High โœ… Fast
AbstractNN โœ… Yes โš ๏ธ Good โš ๏ธ Medium โš ๏ธ Medium
abstractNN/
โ”œโ”€โ”€ abstractnn/ # Main package
โ”‚ โ”œโ”€โ”€ init.py
โ”‚ โ”œโ”€โ”€ affine_engine.py # Affine expression management
โ”‚ โ”œโ”€โ”€ bound_propagator.py # Layer-by-layer propagation
โ”‚ โ”œโ”€โ”€ relaxer.py # Non-linear relaxations
โ”‚ โ”œโ”€โ”€ onnx_parser.py # ONNX model parsing
โ”‚ โ”œโ”€โ”€ partial_evaluator.py # Partial network evaluation
โ”‚ โ”œโ”€โ”€ soundness_checker.py # Soundness validation
โ”‚ โ””โ”€โ”€ cli.py # Command-line interface
โ”œโ”€โ”€ tests/ # Test suite
โ”œโ”€โ”€ docs/ # Sphinx documentation
โ”œโ”€โ”€ examples/ # Usage examples
โ””โ”€โ”€ scripts/ # Utility scripts

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

abstractnn-0.1.1.tar.gz (76.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

abstractnn-0.1.1-py3-none-any.whl (48.6 kB view details)

Uploaded Python 3

File details

Details for the file abstractnn-0.1.1.tar.gz.

File metadata

  • Download URL: abstractnn-0.1.1.tar.gz
  • Upload date:
  • Size: 76.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.7

File hashes

Hashes for abstractnn-0.1.1.tar.gz
Algorithm Hash digest
SHA256 4efaea4c7c50cb3e9457bb4f32d765e39ae17276c20dafeb3efe149889c43c29
MD5 4ed4f0f189815fc6808aa1edffb78134
BLAKE2b-256 9f4e9127a3aa80690d46f17fa35f01734740e133ea1c83f3a3738673570bb30c

See more details on using hashes here.

File details

Details for the file abstractnn-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: abstractnn-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 48.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.7

File hashes

Hashes for abstractnn-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 2d18d35cb8fa894f0a2914d8a0791c478e46bea8662e6e6370a2ec577fbc74bc
MD5 616a29f6b4d3434adbddb36e34e083b4
BLAKE2b-256 e1d43b8911a9f111858a75a512602db77ff1c07a38cc20240a2cf5078773df9f

See more details on using hashes here.

Supported by

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