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

iNNvestigate 2.0 #277

Merged
merged 118 commits into from
Jun 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
118 commits
Select commit Hold shift + click to select a range
70071c8
Parametrize tests
adrhill Nov 22, 2021
4dbd299
Add script to run tests through MonkeyType
adrhill Nov 22, 2021
22194c3
Update imports for switch to tf.keras
adrhill Nov 22, 2021
21faaa4
Add internal `cast_to_floatx`
adrhill Jul 16, 2021
646f31a
Add `apply_mask` helper function
adrhill Jul 16, 2021
4aa53a6
Update `gradients` and remove `Gradient` and `GradientWRT` layers
adrhill Jul 16, 2021
4fd9c94
More refactoring on `reverse_model`
adrhill Jul 16, 2021
07d29d9
Update `ScaleLayer`
adrhill Jul 16, 2021
6185a48
Fix import error
adrhill Nov 22, 2021
e371eff
Small readability improvements
adrhill Jul 16, 2021
cc7857e
Refactor `reverse_model`
adrhill Jul 16, 2021
d60586a
Fix LRP methods
adrhill Jul 16, 2021
92569c2
Remove circular import between checks.py and graph.py
adrhill Jul 19, 2021
a2a3ff6
Fix bug in `FiniteCheck`
adrhill Jul 19, 2021
0df89fd
Small linting changes
adrhill Jul 19, 2021
bb68ce7
Add more comments and linting changes
adrhill Jul 21, 2021
a29db4a
Remove `TestPhaseGaussianNoise`
adrhill Jul 21, 2021
ae8d726
Check `neuron_selection_mode` compatibility before creating subanalyzer
adrhill Jul 21, 2021
55816da
Move layer logic for `CountNonZero` and `SafeDivide` to backend
adrhill Jul 21, 2021
607f1a1
Fix `get_file` import for tf.keras
adrhill Jul 21, 2021
9710eac
Make `_compute_differences` more readable
adrhill Jul 21, 2021
bcf8b1d
Update backend to match layers based on id
adrhill Jul 23, 2021
349b69b
Revert `AugmentReduceBase` neuron selection mode
adrhill Jul 27, 2021
96fec6e
Fix `AugmentReduceBase` analyzers under tf.keras
adrhill Aug 4, 2021
433c2c0
Replace asserts with exceptions
adrhill Aug 4, 2021
6bb0dc4
Update layers
adrhill Aug 5, 2021
6b6dd81
Add more pytest markings
adrhill Aug 25, 2021
f79711d
Update tests to directly load Keras models
adrhill Aug 27, 2021
5671244
Load Imagenet models from `keras.applications`
adrhill Aug 27, 2021
295a4bc
Remove applications folder, only keeping pattern loader
adrhill Aug 27, 2021
2e6725a
Remove tests that are too similar
adrhill Aug 27, 2021
5e66823
Remove more unused layers
adrhill Aug 27, 2021
29fc6dd
Small fixes and linting
adrhill Aug 27, 2021
2cec8e5
Update .pylintrc
adrhill Aug 27, 2021
fa13dfd
Replace more custom layers with backend operations
adrhill Aug 27, 2021
29364a7
Fix Dropout layers in tests
adrhill Oct 6, 2021
7fe8939
Fix typos and linting
adrhill Oct 6, 2021
dfca707
Fix import in readme
adrhill Oct 6, 2021
16647a7
Update deps, remove Keras
adrhill Oct 6, 2021
6bb1544
Remove unused function
adrhill Oct 6, 2021
1db6151
Add output shape to neuron selection layers
adrhill Oct 6, 2021
73537e2
Use `MaxNeuronSelection` layer
adrhill Oct 6, 2021
bf1d1da
Remove output shape computation
adrhill Oct 6, 2021
b09f351
Remove `TestAnalysisHelper`
adrhill Oct 6, 2021
3a06d11
Fix loading of ResNets
adrhill Oct 6, 2021
59d15fd
Fix loading of MNIST MLP for pattern tests
adrhill Oct 6, 2021
35b282b
Fix test networks
adrhill Oct 7, 2021
2a29b1a
Fix instances where NumPy arrays were used instead of TF tensors
adrhill Oct 7, 2021
2e66d63
Update pattern methods to use Keras backend
adrhill Oct 21, 2021
0a70be8
Add error message to `get_reversed_tensor`
adrhill Oct 21, 2021
8352f39
Remove pattern methods
adrhill Nov 22, 2021
f6adc2f
Fixes for TF2.2
adrhill Oct 28, 2021
902cd85
Update dependencies to TF2.6
adrhill Nov 22, 2021
729774a
Add reference tests for layers
adrhill Dec 2, 2021
9cffa7c
Print error on reference tests
adrhill Dec 3, 2021
238fa85
Fix Module check
adrhill Dec 8, 2021
3918062
Update to TF2.7
adrhill Dec 8, 2021
42a1ed1
Fix Gradient reference test, typo
adrhill Dec 8, 2021
a4d6967
More linting fixes
adrhill Dec 10, 2021
0eeaa0b
Update readme
adrhill Dec 10, 2021
d00012a
Update `print_model_execution_graph`
adrhill Dec 16, 2021
7d3ddba
Clear Keras backend before running each analyzer
adrhill Dec 16, 2021
b9ab6d0
Fix reference tests, drop non-deterministic `SmoothGrad` test
adrhill Jan 24, 2022
efb96fe
Fix `MaxNeuronSelection` layer
adrhill Jan 24, 2022
76ff6af
Move groups of layers out of check functions
adrhill Jan 24, 2022
2d7f571
Update dependencies
adrhill Jan 24, 2022
abec3f8
Add more pytest marks
adrhill Jan 24, 2022
a6b961d
Simplify iNNvestigate backend file structure
adrhill Jan 24, 2022
c9af2b2
More linting
adrhill Jan 24, 2022
d5dbdf8
Delete duplicate file
adrhill Jan 24, 2022
b9d119f
Add pytest mark for perturbation methods
adrhill Jan 24, 2022
4b24794
Fix `_get_neuron_selection_array`
adrhill Jan 26, 2022
64bd81d
Add back `SmoothGrad` test with relaxed tolerances
adrhill Jan 26, 2022
4747c90
Simplify test file structure
adrhill Jan 31, 2022
c34eabd
Add new `Repeat` layer with tests
adrhill Jan 31, 2022
e9ff634
Add `ReduceMean`, `AddGaussianNoise` and reshape layers for augmentation
adrhill Jan 31, 2022
cf5f9e0
Add backend functions `shape` and `batch_size`, layer `MaxNeuronIndex`
adrhill Feb 1, 2022
c2f88bb
Fix neuron selection and add tests
adrhill Feb 1, 2022
699abe9
Fix `AugmentReduce` methods
adrhill Feb 1, 2022
5e7482b
More linting
adrhill Feb 1, 2022
d763fda
Update dependency lock file
adrhill Feb 1, 2022
3110509
Fix to pass `AugmentReduceBase` tests
adrhill Feb 1, 2022
c33568a
Update tox config
adrhill Feb 1, 2022
ab22374
Change default pytest mark to `-m fast`
adrhill Feb 1, 2022
cd57333
Reorder notebook folder
adrhill Feb 1, 2022
72f5ffb
Format notebooks with black
adrhill Feb 1, 2022
2386b08
Update notebook utils
adrhill Feb 1, 2022
3466aaf
Update method comparison notebook
adrhill Feb 1, 2022
7856003
Update network comparison notebook
adrhill Feb 7, 2022
b6345c1
Update dev introduction notebook
adrhill Feb 7, 2022
c034ea6
Add tests for `model_wo_softmax`
adrhill Feb 24, 2022
40bc2d5
Update MNIST method comparison notebook
adrhill Feb 24, 2022
1ce104d
Update neuron selection notebook
adrhill Feb 24, 2022
7946a12
Update major version number
adrhill Feb 24, 2022
13f729d
Update introduction notebook
adrhill Feb 24, 2022
c3cc73c
Update MNIST perturbation notebook
adrhill Feb 24, 2022
ce165d8
Add Pandas and ftfy to dev dependencies for sentiment analysis notebook
adrhill Feb 24, 2022
5d11b8a
Update sentiment analysis notebook
adrhill Feb 24, 2022
9e2dbc2
Ignore large formatting commits on git blame
adrhill Feb 24, 2022
6175baf
Add BIFOLD acknowledgement
adrhill Feb 24, 2022
fd64611
Fix method selection in perturbation notebook
adrhill Feb 25, 2022
cb19f00
Fix perturbation test by reverting reference
adrhill Feb 25, 2022
e015fab
Add VGG16 reference tests
adrhill Feb 28, 2022
4f0dc60
Linting fixes
adrhill Feb 28, 2022
741ab46
Add layerwise reference tests on VGG16
adrhill Apr 14, 2022
a594a77
Use `getattr` to check layer for bias
adrhill Apr 14, 2022
60e8f8a
Support TensorFlow v2.8
adrhill Apr 14, 2022
64bab68
Remove `LRPZIgnoreBias` and `LRPEpsilonIgnoreBias`
adrhill Apr 14, 2022
08569d4
Fix whitespace to pass formatting tests
adrhill Apr 14, 2022
a1ea049
Run tests even when formatting check fails
adrhill May 2, 2022
a96d0b4
Update pyproject.toml
adrhill May 2, 2022
a50aa32
Update CI to test on Python 3.10
adrhill May 2, 2022
e624c2a
Attempt to fix CI
adrhill May 2, 2022
615d80d
Add `"use_bias"` to `ScaleLayer` config
adrhill Jun 9, 2022
a8ffe1a
Add pre-commit hooks and pyupgrade
adrhill Jun 9, 2022
a23e012
Delete debugging script
adrhill Jun 9, 2022
6676fc7
Update type annotations using pyupgrade
adrhill Jun 9, 2022
a7a4b57
Fix to pass formatting test
adrhill Jun 9, 2022
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
3 changes: 3 additions & 0 deletions .git-blame-ignore-revs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Formatting commits
7bd44f465186adad90caafa765e0d5ab0e246ada
72f5ffbd9bc407a9e538598872cce3953de1b230
1 change: 1 addition & 0 deletions .gitconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
blame.ignorerevsfile=.git-blame-ignore-revs
37 changes: 22 additions & 15 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
name: Test iNNvestigate
name: CI

on:
- push
- pull_request

jobs:
linting:
# Run linting outside of matrix
formatting:
# Run formatting checks outside of matrix
name: Formatting
runs-on: ubuntu-latest
steps:
# Check-out repo and set-up python
Expand All @@ -25,12 +26,17 @@ jobs:
- name: Format check with isort
run: isort --check .
test:
needs: linting
name: Tests Python ${{ matrix.python-version }} - ${{ matrix.os }} - ${{ matrix.architecture }}
strategy:
fail-fast: true
matrix:
os: [ "ubuntu-latest", "windows-latest" ]
python-version: [ "3.7", ]
os:
- ubuntu-latest
architecture:
- x64
python-version:
- '3.7'
- '3.10'
defaults:
run:
shell: bash
Expand All @@ -43,27 +49,28 @@ jobs:
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}
architecture: ${{ matrix.architecture }}
# Install & configure poetry
- name: Install Poetry
uses: snok/install-poetry@v1.1.6
uses: snok/install-poetry@v1
with:
virtualenvs-create: true
virtualenvs-in-project: true
installer-parallel: true
# Load cached venv if cache exists
- name: Load cached venv
id: cached-pip-wheels
id: cached-poetry-dependencies
uses: actions/cache@v2
with:
path: ~/.cache
key: venv-${{ runner.os }}-${{ hashFiles('**/poetry.lock') }}
path: .venv
key: venv-${{ runner.os }}-${{ steps.setup-python.outputs.python-version }}-${{ hashFiles('**/poetry.lock') }}
# Install dependencies
- name: Install dependencies
run: poetry install --no-interaction --no-root
# Install your root project, if required
- name: Install library
if: steps.cached-poetry-dependencies.outputs.cache-hit != 'true'
run: poetry install --no-interaction
# Run fast tests on matrix, full tests for coverage
# Run fast tests on matrix.
# Coverage options can be found in pyproject.toml
- name: Run tests
run: |
source $VENV
source .venv/bin/activate
pytest ./tests -m fast
11 changes: 10 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -125,5 +125,14 @@ nosetests.cfg
*.h5
*.png

# ignore VGG16
tests/references/vgg16

# ignore nbconvert script output
examples/nbconvert_tmp
examples/nbconvert_tmp

# ignore data downloaded by notebooks
examples/images
examples/stanfordSentimentTreebank
examples/stanfordSentimentTreebank.zip
examples/__MACOSX
24 changes: 24 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v2.5.0
hooks:
- id: check-added-large-files
- repo: https://github.com/asottile/pyupgrade
rev: v2.34.0
hooks:
- id: pyupgrade
args: [--py37-plus]
- repo: local
hooks:
- id: black
name: black
entry: poetry run black src/
language: system
always_run: true
pass_filenames: false
- id: isort
name: isort
entry: poetry run isort src/
language: system
always_run: true
pass_filenames: false
22 changes: 12 additions & 10 deletions .pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,12 @@ disable=print-statement,
xreadlines-attribute,
deprecated-sys-function,
exception-escape,
comprehension-escape
comprehension-escape,
no-name-in-module, # TensorFlow 2 imports throw "No name [name] in module 'LazyLoader' (no-name-in-module)"
missing-module-docstring, # TODO: add back
missing-class-docstring, # TODO: add back
missing-function-docstring, # TODO: add back
too-few-public-methods,

# Enable the message, report, category or checker with the given id(s). You can
# either give multiple identifier separated by comma (,) or put this option
Expand Down Expand Up @@ -225,7 +230,6 @@ spelling-store-unknown-words=no
# List of note tags to take in consideration, separated by a comma.
notes=FIXME,
XXX,
TODO

# Regular expression of note tags to take in consideration.
#notes-rgx=
Expand Down Expand Up @@ -436,20 +440,18 @@ good-names=i,
j,
k,
l,
n,
x,
y,
a,
b,
ex,
Run,
X,
Y,
Z,
R,
A,
B,
_

# Good variable names regexes, separated by a comma. If names match any regex,
# they will always be accepted
good-names-rgxs=[XYZRAB]+,
_[XYZRAB],
good-names-rgxs=(.*_)?[XYZRABC]s?,

# Include a hint for the correct naming format with invalid-name.
include-naming-hint=no
Expand Down
32 changes: 19 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
# [iNNvestigate neural networks!](https://github.com/albermax/innvestigate) [![Tweet](https://img.shields.io/twitter/url/http/shields.io.svg?style=social)](https://twitter.com/intent/tweet?text=iNNvestigate%20neural%20networks!&url=https://github.com/albermax/innvestigate&hashtags=iNNvestigate,artificialintelligence,machinelearning,deeplearning,datascience)

[![Build Status](https://github.com/albermax/innvestigate/actions/workflows/ci.yml/badge.svg)](https://github.com/albermax/innvestigate/actions/workflows/ci.yml)
[![Documentation](https://img.shields.io/badge/docs-stable-blue.svg)](https://innvestigate.readthedocs.io/en/latest/)

[![GitHub package version](https://img.shields.io/badge/Version-v1.0.9-green.svg)](https://github.com/albermax/innvestigate)
[![Keras package version](https://img.shields.io/badge/KerasVersion-v2.2.4-green.svg)](https://github.com/albermax/innvestigate)
[![License: BSD-2](https://img.shields.io/badge/License-BSD--2-blue.svg)](https://github.com/albermax/innvestigate/blob/master/LICENSE)
[![Build Status](https://travis-ci.org/albermax/innvestigate.svg?branch=master)](https://travis-ci.org/albermax/innvestigate)
[![PyPI package version](https://img.shields.io/pypi/v/innvestigate)](https://pypi.org/project/innvestigate/)
[![GitHub package version](https://img.shields.io/github/v/tag/albermax/innvestigate)](https://github.com/albermax/innvestigate/tags)
[![Keras package version](https://img.shields.io/badge/TensorFlow-^2.7-orange.svg)](https://github.com/albermax/innvestigate)
[![License: BSD-2](https://img.shields.io/badge/License-BSD--2-purple.svg)](https://github.com/albermax/innvestigate/blob/master/LICENSE)
[![Black](https://img.shields.io/badge/code_style-black-black.svg)](https://github.com/psf/black)

![Different explanation methods on ImageNet.](https://github.com/albermax/innvestigate/raw/master/examples/images/analysis_grid.png)

Expand Down Expand Up @@ -46,13 +49,10 @@ Our goal is to make analyzing neural networks' predictions easy!
## Installation

iNNvestigate can be installed with the following commands.
The library is based on Keras and therefore requires a supported [Keras-backend](https://keras.io/backend/)
o(Currently only the TensorFlow backend is supported. We test with Python 3.6, TensorFlow 1.12 and Cuda 9.x.):
The library is based on Keras and therefore requires TensorFlow 2:

```bash
pip install innvestigate
# Installing Keras backend
pip install [tensorflow | theano | cntk]
```

To use the example scripts and notebooks one additionally needs to install the package matplotlib:
Expand All @@ -61,11 +61,13 @@ To use the example scripts and notebooks one additionally needs to install the p
pip install matplotlib
```

The library's tests can be executed via:
The library's tests can be executed via `pytest`. The easiest way to do reproducible development on iNNvestigate is to install all dev dependencies via [Poetry](https://python-poetry.org):
```bash
git clone https://github.com/albermax/innvestigate.git
cd innvestigate
python setup.py test

poetry install
poetry run pytest
```

## Usage and Examples
Expand All @@ -78,11 +80,10 @@ The iNNvestigate library contains implementations for the following methods:
* *signal:*
* **deconvnet:** [DeConvNet](https://arxiv.org/abs/1311.2901) applies a ReLU in the gradient computation instead of the gradient of a ReLU.
* **guided:** [Guided BackProp](https://arxiv.org/abs/1412.6806) applies a ReLU in the gradient computation additionally to the gradient of a ReLU.
* **pattern.net:** [PatternNet](https://arxiv.org/abs/1705.05598) estimates the input signal of the output neuron.
* **pattern.net:** [PatternNet](https://arxiv.org/abs/1705.05598) estimates the input signal of the output neuron. (*Note: not available in iNNvestigate 2.0*)
* *attribution:*
* **input_t_gradient:** Input \* Gradient
* **deep_taylor[.bounded]:** [DeepTaylor](https://www.sciencedirect.com/science/article/pii/S0031320316303582?via%3Dihub) computes for each neuron a root point, that is close to the input, but which's output value is 0, and uses this difference to estimate the attribution of each neuron recursively.
* **pattern.attribution:** [PatternAttribution](https://arxiv.org/abs/1705.05598) applies Deep Taylor by searching root points along the signal direction of each neuron.
* **lrp.\*:** [LRP](http://journals.plos.org/plosone/article?id=10.1371/journal.pone.0130140) attributes recursively to each neuron's input relevance proportional to its contribution of the neuron output.
* **integrated_gradients:** [IntegratedGradients](https://arxiv.org/abs/1703.01365) integrates the gradient along a path from the input to a reference.
* *miscellaneous:*
Expand Down Expand Up @@ -126,7 +127,7 @@ Let's look at an example ([code](https://github.com/albermax/innvestigate/blob/m
```python
import innvestigate
import innvestigate.utils
import keras.applications.vgg16 as vgg16
import tensorflow.keras.applications.vgg16 as vgg16

# Get model
model, preprocess = vgg16.VGG16(), vgg16.preprocess_input
Expand Down Expand Up @@ -207,3 +208,8 @@ please open an issue or submit a pull request.
## Releases

[Can be found here.](https://github.com/albermax/innvestigate/blob/master/VERSION.md)


## Acknowledgements

> Adrian Hill acknowledges support by the Federal Ministry of Education and Research (BMBF) for the Berlin Institute for the Foundations of Learning and Data (BIFOLD) (01IS18037A).
2 changes: 0 additions & 2 deletions docs/source/conf.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# -*- coding: utf-8 -*-
#
# Configuration file for the Sphinx documentation builder.
#
# This file does only contain a selection of the most common options. For a
Expand Down
3 changes: 2 additions & 1 deletion examples/embedding_minimal_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@

import innvestigate

# Create Keras Sequential Model
model = Sequential()
model.add(Embedding(input_dim=219, output_dim=8))
model.add(Conv1D(filters=64, kernel_size=8, padding="valid", activation="relu"))
model.add(GlobalMaxPooling1D())
model.add(Dense(16, activation="relu"))
model.add(Dense(2, activation=None))

# test
# Analyze model
model.predict(np.random.randint(1, 219, (1, 100))) # [[0.04913538 0.04234646]]

analyzer = innvestigate.create_analyzer(
Expand Down
Loading