Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Init the refactoring #143

Merged
merged 101 commits into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
101 commits
Select commit Hold shift + click to select a range
439d7ed
Simplify annotations as python>=3.9
nhuet Mar 14, 2024
bec8cd8
Remove decomon.keras_utils.LinalgSolve as it exists now as keras.ops.…
nhuet Mar 14, 2024
02f00ee
Update github actions versions to use node 20
nhuet Mar 14, 2024
4f7bd2f
Use tensorflow >=2.16 to avoid having reinstall keras3
nhuet Mar 15, 2024
8fb5abf
Prepare new structure of decomon.layers
nhuet Jan 30, 2024
4bae8c2
Remove old tests
nhuet Feb 2, 2024
127be44
Deactivate imports in decomon.__init__()
nhuet Feb 1, 2024
d3102db
Remove some of previous code in conftest.py
nhuet Feb 6, 2024
f8620d5
Add batch_multid_dot() operation on tensors
nhuet Feb 1, 2024
34ecc3c
Move is_merge_layer in keras_utils
nhuet Feb 2, 2024
e23a6ba
Create new DecomonLayer base class to be used for bounds propagation
nhuet Feb 1, 2024
a171316
Test decomon layer base class
nhuet Feb 2, 2024
75c66a1
Update get_x_input_shape_wo_batchsize() to take model_input_shape as arg
nhuet Feb 6, 2024
639a439
Update get_lower_box() to adapt to tensor-multid model input
nhuet Feb 6, 2024
1d935bd
Implement a naive version of DecomonDense
nhuet Feb 6, 2024
f757f50
Implement DecomonDense
nhuet Feb 8, 2024
0617335
Update batch_multid_dot() for diagonal entries
nhuet Feb 9, 2024
13ca5a5
Adapt DecomonLayer and DecomonDense to manage diagonal bounds
nhuet Feb 9, 2024
6f30f95
Manage case with empty affine bounds to merge
nhuet Feb 9, 2024
6bf8bc6
Adapt batch_multid_dot to case where both tensors have no batch axis
nhuet Feb 9, 2024
568cf71
Update DecomonLayer.compute_output_shape() to different affine inputs…
nhuet Feb 9, 2024
3680453
Update DecomonDense to adapt to empty, diagonal, and w/o batch inputs
nhuet Feb 10, 2024
9efb607
Update perturbation domain to manage empty/diagonal/no batch affine b…
nhuet Feb 12, 2024
e563236
Adapt DecomonLayer to affine inputs w/o batchsize or empty (identity)
nhuet Feb 12, 2024
337abc7
Implement DecomonActivation
nhuet Feb 8, 2024
f540d33
Set model_output_shape_length in tests for backward decomon layers
nhuet Feb 12, 2024
c445a69
Generate decomon inputs for each case (id, diag, no-batch)
nhuet Feb 12, 2024
84546de
Add a fixture managing different keras/input inputs
nhuet Feb 12, 2024
a0334b9
Add a test for methods checking types of inputs (identity/diag/nobatch)
nhuet Feb 12, 2024
7ea50b4
Change (flatten) inputs/outputs format for DecomonLayer.call()
nhuet Feb 12, 2024
b407eaf
Add checks on compute_output_shape()
nhuet Feb 12, 2024
32ea8c6
Test DecomonDense on all kind of inputs
nhuet Feb 12, 2024
1b5947b
Keep only "naive" implementation of decomon dense layer
nhuet Feb 12, 2024
3f4382c
Test DecomonActivation on all kind of inputs (diag/nobatch/identity)
nhuet Feb 12, 2024
547a281
Manage case with empty (identity) affine bounds in outputs
nhuet Feb 12, 2024
083043f
Fix DecomonLinear.compute_output_shape()
nhuet Feb 12, 2024
4b61748
Fix typing issues
nhuet Feb 12, 2024
ff13190
Add dtype to decomon inputs
nhuet Feb 13, 2024
a6ba40d
Use use_bias in dense layer
nhuet Feb 13, 2024
63bc337
Integrate previous "standard" inputs in tests
nhuet Feb 13, 2024
7f4980a
Add a method needs_keras_model_inputs() for the decomon layer
nhuet Feb 15, 2024
69ced25
Fix typo
nhuet Feb 15, 2024
cf1aa0f
Add DecomonMergeLayer to manage merging layer
nhuet Feb 15, 2024
2a4dc97
Implement and test DecomonAdd
nhuet Feb 16, 2024
f19f8b3
Allow decomon layers import from decomon.layers
nhuet Feb 16, 2024
a26ac57
Merge tests for unary layers in a single test
nhuet Feb 16, 2024
d8031f4
Add model_input_shape and model_output_shape properties to DecomonLayer
nhuet Feb 19, 2024
78f5f70
Fix merge layers with pytorch backend
nhuet Feb 19, 2024
0893f22
Implement to_decomon() conversion method
nhuet Feb 19, 2024
b7472bc
Rename `model_inputs` into `perturbation_domain_inputs`
nhuet Feb 19, 2024
3f33f54
Derive a tensor of keras model input shape from x
nhuet Feb 19, 2024
a1e2177
Add fixture to generate toy models (from a given input_shape x dtype)
nhuet Feb 22, 2024
7670794
Update forward conversion and test it
nhuet Feb 19, 2024
87336dc
Test keras model preprocessing (split activation)
nhuet Feb 22, 2024
c5e024c
Add a call_oracle() method on decomon layers
nhuet Feb 21, 2024
789af43
Avoid passing constant oracle bounds to backward *linear* layers
nhuet Feb 21, 2024
6168062
Update backward conversion and test it
nhuet Feb 21, 2024
b3dab6d
Fix convert_backward with embedded submodels + full crown
nhuet Feb 21, 2024
5132777
Add plot_model() utility
nhuet Feb 22, 2024
b109cd2
Fix input_shape for cnn toy model
nhuet Feb 22, 2024
0a69431
Tighten (fully recursive) crown for model embedding submodels
nhuet Feb 26, 2024
fa95b9f
Remove old backward layers
nhuet Feb 19, 2024
494922f
Update clone/convert with new api
nhuet Feb 29, 2024
48db81e
Add simple inputs with lower < keras_input < upper
nhuet Feb 28, 2024
4f6f8ff
Choose random values between -1 and 1 (instead of 0 and 1)
nhuet Feb 29, 2024
67ca34d
Add LinkToPerturbationDomainInput layer to use in clone() for fully l…
nhuet Feb 29, 2024
51b9602
Create a ForwardInput layer for the first forward layer
nhuet Feb 29, 2024
5fd6eca
Introduce oracle layers to manage "decomon outputs -> oracle"
nhuet Mar 1, 2024
a206727
Add the name "perturbation_domain_input" to the input generated durin…
nhuet Mar 1, 2024
d431692
Add a layer to reduce crown bounds coming from parent nodes of a merg…
nhuet Mar 1, 2024
cf1b961
Update BallDomain
nhuet Mar 4, 2024
7ce1f56
Fix PerturbationDomain for affine bounds w/o batchsize
nhuet Mar 8, 2024
1885d25
Fix checks for identity, diag and nobatch in inputs_outputs_specs
nhuet Mar 8, 2024
3708beb
Add a Fuse layer to combine affine (and ibp) bounds on successive models
nhuet Mar 4, 2024
1a2bb46
Fuse forward outputs with specified backward_bounds if not using a crown
nhuet Mar 11, 2024
c90d7a4
Add ConvertOutput to change final ibp, affine setting for decomon output
nhuet Mar 11, 2024
07f8aaa
Improve backward_bounds management
nhuet Mar 8, 2024
4783f75
Testing multiple outputs
nhuet Mar 12, 2024
d94dbb5
Testing single tensor backward bound for adversarial robustness study
nhuet Mar 12, 2024
a064ea3
Manage special case of crown + identity model
nhuet Mar 12, 2024
ba23702
Xfail test_clone with crown + add model + standard-multid inputs
nhuet Mar 12, 2024
a9ef3e1
Change color of decomon specific layers in plot_model
nhuet Mar 12, 2024
12bafb8
Clean old code
nhuet Mar 14, 2024
c0856fc
Remove unused imports
nhuet Mar 14, 2024
7b72cf5
Import clone at decomon and decomon.models levels
nhuet Mar 15, 2024
dd1638b
Convert slope as Slope in clone() if passed as a string
nhuet Mar 15, 2024
94e6e43
Update example in "getting started"
nhuet Mar 15, 2024
72ac897
Add mapping between custom (keras->decomon) layers in clone
nhuet Mar 15, 2024
ac7957a
Add a tutorial for custom layers
nhuet Mar 15, 2024
d86e1d8
Add test for clone with custom layers
nhuet Mar 15, 2024
65b5dbc
Add method to ease final constant bounds computation for a decomon model
nhuet Mar 18, 2024
5bdfb9c
Update get_range_box(), get_range_noise(), and get_adv_box() for the …
nhuet Mar 18, 2024
143731e
Update explanation texts in tutorials and fix imports
nhuet Mar 18, 2024
f361a99
Remove unused method get_input_from_constant_bounds()
nhuet Mar 18, 2024
d228e7b
Remove last softmax layer during preprocessing
nhuet Mar 18, 2024
4bc82e3
Implement softsign activation decomon layer
nhuet Mar 18, 2024
ae63d93
Update and add get_config() methods to decomon objects
nhuet Mar 18, 2024
e06e4d9
Reduce size of toy network to pass on github runners
nhuet Mar 19, 2024
e59b7dd
Prepare test_clone for cnn model
nhuet Mar 19, 2024
537148d
Make mypy ignore model_visualization.py
nhuet Mar 19, 2024
1c85777
Fix mypy errors
nhuet Mar 19, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 3 additions & 8 deletions .github/workflows/build-doc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,9 @@ jobs:
echo "AUTODOC_NOTEBOOKS_BRANCH=${AUTODOC_NOTEBOOKS_BRANCH}" >> $GITHUB_ENV
# check computed variables
echo "Notebooks source: ${AUTODOC_NOTEBOOKS_REPO_URL}/tree/${AUTODOC_NOTEBOOKS_BRANCH}"
- uses: actions/checkout@v3
with:
submodules: true
- uses: actions/checkout@v4
- name: Setup python
uses: actions/setup-python@v4
uses: actions/setup-python@v5
with:
python-version: ${{ env.python-version }}
cache: "pip"
Expand All @@ -92,9 +90,6 @@ jobs:
python -m pip install -U pip setuptools
pip install .
pip install -r docs/requirements.txt
# ensure having keras 3 instead of keras 2.15 installed by tensorflow
pip uninstall -y keras
pip install "keras>=3"
- name: generate documentation
id: sphinx-build
run: |
Expand All @@ -119,7 +114,7 @@ jobs:
')
echo doc_version=${doc_version} >> $GITHUB_OUTPUT
- name: upload as artifact
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
name: ${{ inputs.doc-artifact-name }}
path: ${{ inputs.doc-path }}
10 changes: 5 additions & 5 deletions .github/workflows/deploy-doc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
runs-on: ubuntu-latest
if: inputs.binder-env-fullref != ''
steps:
- uses: actions/checkout@v3 # checkout triggering branch to get scripts/trigger_binder.sh
- uses: actions/checkout@v4 # checkout triggering branch to get scripts/trigger_binder.sh
- name: Trigger a build for default binder env ref on each BinderHub deployments in the mybinder.org federation
run: |
binder_env_full_ref=${{ inputs.binder-env-fullref }}
Expand All @@ -39,9 +39,9 @@ jobs:
deploy-doc:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Download artifacts
uses: actions/download-artifact@v3
uses: actions/download-artifact@v4
with:
name: ${{ inputs.doc-artifact-name }}
path: ${{ inputs.doc-path }}
Expand All @@ -59,10 +59,10 @@ jobs:
needs: [deploy-doc]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
with:
ref: gh-pages
- uses: actions/setup-python@v4
- uses: actions/setup-python@v5
with:
python-version: "3.10"
- name: Generate versions.json
Expand Down
8 changes: 4 additions & 4 deletions .github/workflows/python-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ jobs:
outputs:
package_version: ${{ steps.get_package_version.outputs.version }}
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: '3.9'
- name: Install dependencies
Expand All @@ -33,7 +33,7 @@ jobs:
echo "version=$version"
echo "version=$version" >> $GITHUB_OUTPUT
- name: Upload as build artifacts
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
name: dist
path: dist
Expand All @@ -59,7 +59,7 @@ jobs:
outputs:
tuto-tag-name: ${{ steps.push-tuto-release-tag.outputs.new_tag_name }}
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: replace decomon version to install in colab notebooks
run: |
version=${{ needs.deploy.outputs.package_version }}
Expand Down
22 changes: 11 additions & 11 deletions .github/workflows/python-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,18 @@ jobs:
linters:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: create requirements.txt so that pip cache with setup-python works
run: echo "pre-commit" > requirements_precommit.txt
- uses: actions/setup-python@v4
- uses: actions/setup-python@v5
with:
python-version: 3.8
python-version: 3.9
cache: pip
cache-dependency-path: requirements_precommit.txt
- name: install pre-commit
run: python -m pip install pre-commit
- name: get cached pre-commit hooks
uses: actions/cache@v3
uses: actions/cache@v4
with:
path: ~/.cache/pre-commit
key: pre-commit|${{ env.pythonLocation }}|${{ hashFiles('.pre-commit-config.yaml') }}
Expand All @@ -36,8 +36,8 @@ jobs:
type-checking:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: 3.9
cache: pip
Expand All @@ -48,7 +48,7 @@ jobs:
python -m pip install --upgrade tox setuptools
- name: Restore cached .tox
id: cache-tox
uses: actions/cache@v3
uses: actions/cache@v4
with:
path: .tox
key:
Expand All @@ -70,8 +70,8 @@ jobs:
run:
shell: bash
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
cache: pip
Expand All @@ -82,7 +82,7 @@ jobs:
python -m pip install --upgrade tox setuptools
- name: Restore cached .tox
id: cache-tox
uses: actions/cache@v3
uses: actions/cache@v4
with:
path: .tox
key:
Expand Down Expand Up @@ -123,7 +123,7 @@ jobs:
echo "coverage_exists=${coverage_exists}" >> $GITHUB_OUTPUT
- name: Export coverage report (if existing)
if: steps.check-coverage-report.outputs.coverage_exists == 'true'
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
name: coverage
path: |
Expand Down
6 changes: 3 additions & 3 deletions binder/environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ dependencies:
- kaggle
- pandas
- scipy
- tensorboard>=2.13
# - tensorflow>=2.15 # conflicting dependencies with decomon (keras 2)
- tensorboard>=2.16
- tensorflow>=2.16
- jupyter-server-proxy
- .. # decomon
- .. # decomon
6 changes: 0 additions & 6 deletions binder/postBuild
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
# install tensorflow backend separately from decomon (dependencies conflict as tf 2.15 requires keras 2)
pip install tensorflow>=2.15
# use keras 3 instead of keras 2 installed by tensorflow
pip uninstall -y keras
pip install "keras>=3"

# tensorboard launches at startup
mv binder/tensorboardserverextension.py ${NB_PYTHON_PREFIX}/lib/python*/site-packages/
# enable tensorboard extension
Expand Down
9 changes: 4 additions & 5 deletions docs/generate_nb_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import os
import re
import urllib.parse
from typing import List, Tuple

NOTEBOOKS_LIST_PLACEHOLDER = "[[notebooks-list]]"
NOTEBOOKS_PAGE_TEMPLATE_RELATIVE_PATH = "tutorials.template.md"
Expand All @@ -25,15 +24,15 @@

def extract_notebook_title_n_description(
notebook_filepath: str,
) -> Tuple[str, List[str]]:
) -> tuple[str, list[str]]:
# load notebook
with open(notebook_filepath, "rt", encoding="utf-8") as f:
notebook = json.load(f)

# find title + description: from first cell, h1 title + remaining text.
# or title from filename else
title = ""
description_lines: List[str] = []
description_lines: list[str] = []
cell = notebook["cells"][0]
if cell["cell_type"] == "markdown":
firstline = cell["source"][0].strip()
Expand All @@ -51,7 +50,7 @@ def extract_notebook_title_n_description(
return title, description_lines


def filter_tags_from_description(description_lines: List[str], html_tag_to_remove: str) -> List[str]:
def filter_tags_from_description(description_lines: list[str], html_tag_to_remove: str) -> list[str]:
description = "".join(description_lines)
# opening/closing tags
opening_tag = html_tag_to_remove
Expand Down Expand Up @@ -147,7 +146,7 @@ def get_binder_link(
return link


def get_repo_n_branches_for_binder_n_github_links() -> Tuple[bool, str, str, str, str, str, bool]:
def get_repo_n_branches_for_binder_n_github_links() -> tuple[bool, str, str, str, str, str, bool]:
# repos + branches to use for binder environment and notebooks content.
creating_links = True
use_nbgitpuller = False
Expand Down
2 changes: 1 addition & 1 deletion docs/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
sphinx
myst_parser
sphinx_rtd_theme
tensorflow>=2.15 # install a backend for keras 3 (and tensorflow is the one needed by default)
tensorflow>=2.16 # install a backend for keras 3 (and tensorflow is the one needed by default)
27 changes: 16 additions & 11 deletions docs/source/getting_started.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,34 @@ within a box domain.

````python
# Imports
import keras
import numpy as np
from keras.layers import Dense
from keras.layers import Dense, Input
from keras.models import Sequential

from decomon import get_lower_box, get_upper_box
from decomon.models import clone
# from decomon import get_lower_box, get_upper_box
from decomon import clone

# Toy example with a Keras model:
model = Sequential([Dense(10, activation="relu", input_dim=2)])
model = Sequential([Input((2,)), Dense(10, activation="relu")])

# Convert into a decomon neural network
decomon_model = clone(
model,
final_ibp=True, # keep constant bounds
final_affine=False # drop affine bounds
)

# Create a fake box with the right shape
x_min = np.zeros((1, 2))
x_max = np.ones((1, 2))

# Convert into a decomon neural network
decomon_model = clone(model)
x_box = np.concatenate([x_min[:, None], x_max[:, None]], axis=1)

# Get lower and upper bounds
lower_bound = get_lower_box(decomon_model, x_min, x_max)
upper_bound = get_upper_box(decomon_model, x_min, x_max)
lower_bound, upper_bound = decomon_model.predict_on_single_batch_np(x_box) # more efficient than predict on very small batch

print(lower_bound)
print(upper_bound)
print(f"lower bound: {lower_bound}")
print(f"upper bound: {upper_bound}")
````

Other types of domains are possible and illustrated
Expand Down
4 changes: 4 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,7 @@ module = [
"torch.*"
]
ignore_missing_imports = true

[[tool.mypy.overrides]]
module = "decomon.visualization.model_visualization"
ignore_errors = true
16 changes: 8 additions & 8 deletions src/decomon/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@

import keras

from . import layers, models
from .metrics.loss import get_adv_loss, get_lower_loss, get_model, get_upper_loss
from .models.models import get_AB as get_grid_params
from .models.models import get_AB_finetune as get_grid_slope
from .wrapper import (
check_adv_box,
from .models import clone

# from .metrics.loss import get_adv_loss, get_lower_loss, get_model, get_upper_loss
# from .models.models import get_AB as get_grid_params
# from .models.models import get_AB_finetune as get_grid_slope
from .wrapper import ( # check_adv_box,; refine_box,
get_adv_box,
get_adv_noise,
get_lower_box,
Expand All @@ -22,9 +22,9 @@
get_range_noise,
get_upper_box,
get_upper_noise,
refine_box,
)
from .wrapper_with_tuning import get_lower_box_tuning, get_upper_box_tuning

# from .wrapper_with_tuning import get_lower_box_tuning, get_upper_box_tuning

try:
__version__ = version("decomon")
Expand Down
Loading