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

@simon-at-fugu Try to enable deeptaylor analyzer. Please review :) #219

Open
wants to merge 7 commits into
base: updates_towards_tf2.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
20 changes: 10 additions & 10 deletions innvestigate/analyzer/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
from .base import NotAnalyzeableModelException
from .base import ReverseAnalyzerBase
# from .deeplift import DeepLIFTWrapper
#from .gradient_based import Gradient
#from .gradient_based import InputTimesGradient
#from .gradient_based import GuidedBackprop
#from .gradient_based import Deconvnet
#from .gradient_based import IntegratedGradients
#from .gradient_based import SmoothGrad
from .gradient_based import Gradient
from .gradient_based import InputTimesGradient
from .gradient_based import GuidedBackprop
from .gradient_based import Deconvnet
# from .gradient_based import IntegratedGradients
# from .gradient_based import SmoothGrad
# from .misc import Input
# from .misc import Random
# from .pattern_based import PatternNet
Expand Down Expand Up @@ -44,8 +44,8 @@
from .relevance_based.relevance_analyzer import LRPSequentialCompositeAFlat
from .relevance_based.relevance_analyzer import LRPSequentialCompositeBFlat
from .relevance_based.relevance_analyzer import LRPRuleUntilIndex
# from .deeptaylor import DeepTaylor
# from .deeptaylor import BoundedDeepTaylor
from .deeptaylor import DeepTaylor
from .deeptaylor import BoundedDeepTaylor
from .wrapper import WrapperBase
from .wrapper import AugmentReduceBase
from .wrapper import GaussianSmoother
Expand Down Expand Up @@ -108,8 +108,8 @@
"lrp.rule_until_index": LRPRuleUntilIndex,

# Deep Taylor
#"deep_taylor": DeepTaylor,
#"deep_taylor.bounded": BoundedDeepTaylor,
"deep_taylor": DeepTaylor,
"deep_taylor.bounded": BoundedDeepTaylor,

# # DeepLIFT
# "deep_lift.wrapper": DeepLIFTWrapper,
Expand Down
3 changes: 3 additions & 0 deletions innvestigate/analyzer/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,9 @@ def analyze(self, X, neuron_selection="max_activation", explained_layer_names=No
self._analyzed = True
ret = self._postprocess_analysis(ret)

if isinstance(ret, list) and len(ret) == 1:
ret = ret[0]

return ret

def _postprocess_analysis(self, hm):
Expand Down
33 changes: 19 additions & 14 deletions innvestigate/analyzer/deeptaylor.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from tensorflow.python.keras.engine.input_layer import InputLayer

from . import base
from .relevance_based import relevance_rule as lrp_rules
from .relevance_based import relevance_rule_base as lrp_rules
from ..utils.keras import checks as kchecks
from ..utils.keras import graph as kgraph

Expand Down Expand Up @@ -71,35 +71,40 @@ def do_nothing(Xs, Ys, As, reverse_state):
self._add_conditional_reverse_mapping(
lambda l: (not kchecks.contains_kernel(l) and
kchecks.contains_activation(l)),
self._gradient_reverse_mapping,
self._gradient_reverse_mapping(),
name="deep_taylor_relu",
)

# Assume conv layer beforehand -> unbounded
bn_mapping = kgraph.apply_mapping_to_fused_bn_layer(
lrp_rules.WSquareRule,
fuse_mode="one_linear",
)
# bn_mapping = kgraph.apply_mapping_to_fused_bn_layer(
# lrp_rules.WSquareRule,
# fuse_mode="one_linear",
# )
# self._add_conditional_reverse_mapping(
# kchecks.is_batch_normalization_layer,
# bn_mapping,
# name="deep_taylor_batch_norm",
# )
self._add_conditional_reverse_mapping(
kchecks.is_batch_normalization_layer,
bn_mapping,
self._gradient_reverse_mapping(),
name="deep_taylor_batch_norm",
)
# Special layers.
self._add_conditional_reverse_mapping(
kchecks.is_max_pooling,
self._gradient_reverse_mapping,
self._gradient_reverse_mapping(),
name="deep_taylor_max_pooling",
)
self._add_conditional_reverse_mapping(
kchecks.is_average_pooling,
self._gradient_reverse_mapping,
self._gradient_reverse_mapping(),
name="deep_taylor_average_pooling",
)
self._add_conditional_reverse_mapping(
lambda l: isinstance(l, keras_layers.Add),
# Ignore scaling with 0.5
self._gradient_reverse_mapping,
self._gradient_reverse_mapping(),
name="deep_taylor_add",
)
self._add_conditional_reverse_mapping(
Expand All @@ -112,7 +117,7 @@ def do_nothing(Xs, Ys, As, reverse_state):
keras_layers.SpatialDropout2D,
keras_layers.SpatialDropout3D,
)),
self._gradient_reverse_mapping,
self._gradient_reverse_mapping(),
name="deep_taylor_special_layers",
)

Expand All @@ -133,19 +138,19 @@ def do_nothing(Xs, Ys, As, reverse_state):
keras_layers.RepeatVector,
keras_layers.Reshape,
)),
self._gradient_reverse_mapping,
self._gradient_reverse_mapping(),
name="deep_taylor_no_transform",
)

return super(DeepTaylor, self)._create_analysis(
*args, **kwargs)

def _default_reverse_mapping(self, Xs, Ys, reversed_Ys, reverse_state):
def _default_reverse_mapping(self, layer):
"""
Block all default mappings.
"""
raise NotImplementedError(
"Layer %s not supported." % reverse_state["layer"])
"Layer %s not supported." % layer)

def _prepare_model(self, model):
"""
Expand Down
4 changes: 2 additions & 2 deletions innvestigate/utils/keras/functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -615,8 +615,8 @@ def boundedrule_explanation(ins, layer_func, layer_func_pos, layer_func_neg, out
#print("TRACING bound")
to_low = keras_layers.Lambda(lambda x: x * 0 + low_param)
to_high = keras_layers.Lambda(lambda x: x * 0 + high_param)
low = [to_low(x) for x in ins]
high = [to_high(x) for x in ins]
low = tf.map_fn(to_low, ins)
high = tf.map_fn(to_high, ins)

A = out_func(ins, layer_func)
B = out_func(low, layer_func_pos)
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"numpy",
"pillow",
"scipy",
"tensorflow==2.1",
"tensorflow>=2.3",
]

setup_requirements = [
Expand Down