Formal verification of neural networks using abstract interpretation and affine arithmetic
Project description
AbstractNN
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
๐ 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/guillaume117/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 | โ Loose | โ ๏ธ Medium | โ ๏ธ Medium |
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file abstractnn-0.1.3.tar.gz.
File metadata
- Download URL: abstractnn-0.1.3.tar.gz
- Upload date:
- Size: 63.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0f599429efdedbba9bfb14e0a45a6b5e633aa39e5c55c62ab3665d76c9863a79
|
|
| MD5 |
0cf501bcbada0a93c7b770e0bd5fec91
|
|
| BLAKE2b-256 |
d85a53e989507e34ebb1e0f561c3c32699a1e33a278db9f3592bf30e533c2eae
|
File details
Details for the file abstractnn-0.1.3-py3-none-any.whl.
File metadata
- Download URL: abstractnn-0.1.3-py3-none-any.whl
- Upload date:
- Size: 30.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d0cceca7033e6baa5c59124ad76ea9913f20144a34296939f660ebf3d50eeb0e
|
|
| MD5 |
ac02cbd0d5bbbfefea1c4dadf992a0f7
|
|
| BLAKE2b-256 |
c415fb0292663cb38f8e52d3f221bbdf3955e4d44b5a2a7523512b4f4cf882b1
|