123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241 |
- # Get Python six functionality:
- from __future__ import\
- absolute_import, print_function, division, unicode_literals
- ###############################################################################
- ###############################################################################
- ###############################################################################
- import keras.layers
- import keras.models
- import numpy as np
- import pytest
- try:
- import deeplift
- except ImportError:
- deeplift = None
- from innvestigate.utils.tests import dryrun
- from innvestigate.analyzer import DeepLIFT
- from innvestigate.analyzer import DeepLIFTWrapper
- ###############################################################################
- ###############################################################################
- ###############################################################################
- @pytest.mark.fast
- @pytest.mark.precommit
- def test_fast__DeepLIFT():
- def method(model):
- return DeepLIFT(model)
- dryrun.test_analyzer(method, "trivia.*:mnist.log_reg")
- @pytest.mark.precommit
- def test_precommit__DeepLIFT():
- def method(model):
- return DeepLIFT(model)
- dryrun.test_analyzer(method, "mnist.*")
- @pytest.mark.precommit
- def test_precommit__DeepLIFT_Rescale():
- def method(model):
- if keras.backend.image_data_format() == "channels_first":
- input_shape = (1, 28, 28)
- else:
- input_shape = (28, 28, 1)
- model = keras.models.Sequential([
- keras.layers.Dense(10, input_shape=input_shape),
- keras.layers.ReLU(),
- ])
- return DeepLIFT(model)
- dryrun.test_analyzer(method, "mnist.log_reg")
- @pytest.mark.precommit
- def test_precommit__DeepLIFT_neuron_selection_index():
- class CustomAnalyzer(DeepLIFT):
- def analyze(self, X):
- index = 0
- return super(CustomAnalyzer, self).analyze(X, index)
- def method(model):
- return CustomAnalyzer(model, neuron_selection_mode="index")
- dryrun.test_analyzer(method, "mnist.*")
- @pytest.mark.precommit
- def test_precommit__DeepLIFT_larger_batch_size():
- class CustomAnalyzer(DeepLIFT):
- def analyze(self, X):
- X = np.concatenate((X, X), axis=0)
- return super(CustomAnalyzer, self).analyze(X)[0:1]
- def method(model):
- return CustomAnalyzer(model)
- dryrun.test_analyzer(method, "mnist.*")
- @pytest.mark.skip("There is a design issue to be fixed.")
- @pytest.mark.precommit
- def test_precommit__DeepLIFT_larger_batch_size_with_index():
- class CustomAnalyzer(DeepLIFT):
- def analyze(self, X):
- index = 0
- X = np.concatenate((X, X), axis=0)
- return super(CustomAnalyzer, self).analyze(X, index)[0:1]
- def method(model):
- return CustomAnalyzer(model, neuron_selection_mode="index")
- dryrun.test_analyzer(method, "mnist.*")
- @pytest.mark.slow
- @pytest.mark.application
- @pytest.mark.imagenet
- def test_imagenet__DeepLIFT():
- def method(model):
- return DeepLIFT(model)
- dryrun.test_analyzer(method, "imagenet.*")
- ###############################################################################
- ###############################################################################
- ###############################################################################
- require_deeplift = pytest.mark.skipif(deeplift is None,
- reason="Package deeplift is required.")
- @require_deeplift
- @pytest.mark.fast
- @pytest.mark.precommit
- @pytest.mark.skip(reason="DeepLIFT does not work with skip connection.")
- def test_fast__DeepLIFTWrapper():
- def method(model):
- return DeepLIFTWrapper(model)
- dryrun.test_analyzer(method, "trivia.*:mnist.log_reg")
- @require_deeplift
- @pytest.mark.precommit
- def test_precommit__DeepLIFTWrapper():
- def method(model):
- return DeepLIFTWrapper(model)
- dryrun.test_analyzer(method, "mnist.*")
- @require_deeplift
- @pytest.mark.precommit
- def test_precommit__DeepLIFTWrapper_neuron_selection_index():
- class CustomAnalyzer(DeepLIFTWrapper):
- def analyze(self, X):
- index = 0
- return super(CustomAnalyzer, self).analyze(X, index)
- def method(model):
- return CustomAnalyzer(model, neuron_selection_mode="index")
- dryrun.test_analyzer(method, "mnist.*")
- @require_deeplift
- @pytest.mark.precommit
- def test_precommit__DeepLIFTWrapper_larger_batch_size():
- class CustomAnalyzer(DeepLIFTWrapper):
- def analyze(self, X):
- X = np.concatenate((X, X), axis=0)
- return super(CustomAnalyzer, self).analyze(X)[0:1]
- def method(model):
- return CustomAnalyzer(model)
- dryrun.test_analyzer(method, "mnist.*")
- @require_deeplift
- @pytest.mark.precommit
- def test_precommit__DeepLIFTWrapper_larger_batch_size_with_index():
- class CustomAnalyzer(DeepLIFTWrapper):
- def analyze(self, X):
- index = 0
- X = np.concatenate((X, X), axis=0)
- return super(CustomAnalyzer, self).analyze(X, index)[0:1]
- def method(model):
- return CustomAnalyzer(model, neuron_selection_mode="index")
- dryrun.test_analyzer(method, "mnist.*")
- @require_deeplift
- @pytest.mark.slow
- @pytest.mark.application
- @pytest.mark.imagenet
- def test_imagenet__DeepLIFTWrapper():
- def method(model):
- return DeepLIFTWrapper(model)
- dryrun.test_analyzer(method, "imagenet.*")
- ###############################################################################
- ###############################################################################
- ###############################################################################
- @pytest.mark.fast
- @pytest.mark.precommit
- def test_fast__DeepLIFT_serialize():
- def method(model):
- return DeepLIFT(model)
- dryrun.test_serialize_analyzer(method, "trivia.*:mnist.log_reg")
- @pytest.mark.fast
- @pytest.mark.precommit
- def test_fast__DeepLIFTWrapper_serialize():
- def method(model):
- return DeepLIFTWrapper(model)
- with pytest.raises(AssertionError):
- # Issue in deeplift.
- dryrun.test_serialize_analyzer(method, "trivia.*:mnist.log_reg")
|