albermax / innvestigate

A toolbox to iNNvestigate neural networks' predictions!
Other
1.27k stars 233 forks source link

Issues when using LRP #112

Closed liushuai10000 closed 6 years ago

liushuai10000 commented 6 years ago

Hi,

When I try to use LRP to generate the heatmap, an exception occurs: AttributeError: module 'keras.engine.topology' has no attribute 'Network'.

When I check the keras package, the topology.py has already been deprecated. Could you please fix this issue? Thanks.

One more problem is: it seems that the lrp does not support softmax. Because it raises: NotAnalyzeableModelException: This analysis method does not support softmax layers. Check triggerd by layers: [<keras.layers.core.Dense object at 0x7f7b8edb53c8>]

Any ideas to fix it?

Thanks!

AttributeError Traceback (most recent call last)

in () 1 import innvestigate ----> 2 analyzer = innvestigate.create_analyzer("lrp.z", model) 3 for i in range(7): 4 exp = explainer.explain(imgs[i].reshape((125,125,1)), classes[i]) 5 print("The image is classified as:" + str(labels[classes[i]])) ~/anaconda3/envs/tensorflow/lib/python3.6/site-packages/innvestigate/analyzer/__init__.py in create_analyzer(name, model, **kwargs) 91 :return: An instance of the chosen analyzer. 92 """ ---> 93 return analyzers[name](model, **kwargs) ~/anaconda3/envs/tensorflow/lib/python3.6/site-packages/innvestigate/analyzer/relevance_based/relevance_analyzer.py in __init__(self, model, *args, **kwargs) 519 520 def __init__(self, model, *args, **kwargs): --> 521 super(LRPZ, self).__init__(model, *args, rule="Z", **kwargs) 522 523 ~/anaconda3/envs/tensorflow/lib/python3.6/site-packages/innvestigate/analyzer/relevance_based/relevance_analyzer.py in __init__(self, model, *args, **kwargs) 401 402 # FINALIZED constructor. --> 403 super(LRP, self).__init__(model, *args, **kwargs) 404 405 def create_rule_mapping(self, layer, reverse_state): ~/anaconda3/envs/tensorflow/lib/python3.6/site-packages/innvestigate/analyzer/base.py in __init__(self, model, reverse_verbose, reverse_clip_values, reverse_project_bottleneck_layers, reverse_check_min_max_values, reverse_check_finite, reverse_keep_tensors, reverse_reapply_on_copied_layers, **kwargs) 583 self._reverse_reapply_on_copied_layers = ( 584 reverse_reapply_on_copied_layers) --> 585 super(ReverseAnalyzerBase, self).__init__(model, **kwargs) 586 587 def _gradient_reverse_mapping(self, Xs, Ys, reversed_Ys, reverse_state): ~/anaconda3/envs/tensorflow/lib/python3.6/site-packages/innvestigate/analyzer/base.py in __init__(self, model, neuron_selection_mode, allow_lambda_layers, **kwargs) 343 ) 344 --> 345 super(AnalyzerNetworkBase, self).__init__(model, **kwargs) 346 347 def _add_model_softmax_check(self): ~/anaconda3/envs/tensorflow/lib/python3.6/site-packages/innvestigate/analyzer/base.py in __init__(self, model, disable_model_checks) 77 self._disable_model_checks = disable_model_checks 78 ---> 79 self._do_model_checks() 80 81 def _add_model_check(self, check, message, check_type="exception"): ~/anaconda3/envs/tensorflow/lib/python3.6/site-packages/innvestigate/analyzer/base.py in _do_model_checks(self) 102 messages = [x["message"] for x in model_checks] 103 --> 104 checked = kgraph.model_contains(self._model, check) 105 tmp = zip(iutils.to_list(checked), messages, types) 106 ~/anaconda3/envs/tensorflow/lib/python3.6/site-packages/innvestigate/utils/keras/graph.py in model_contains(model, layer_condition, return_only_counts) 336 single_condition = False 337 --> 338 layers = get_model_layers(model) 339 collected_layers = [] 340 for condition in layer_condition: ~/anaconda3/envs/tensorflow/lib/python3.6/site-packages/innvestigate/utils/keras/graph.py in get_model_layers(model) 324 if kchecks.is_network(layer): 325 collect_layers(layer) --> 326 collect_layers(model) 327 328 return ret ~/anaconda3/envs/tensorflow/lib/python3.6/site-packages/innvestigate/utils/keras/graph.py in collect_layers(container) 322 assert layer not in ret 323 ret.append(layer) --> 324 if kchecks.is_network(layer): 325 collect_layers(layer) 326 collect_layers(model) ~/anaconda3/envs/tensorflow/lib/python3.6/site-packages/innvestigate/utils/keras/checks.py in is_network(layer) 252 Is network in network? 253 """ --> 254 return isinstance(layer, keras.engine.network.Network) 255 256 AttributeError: module 'keras.engine.topology' has no attribute 'Network'.
albermax commented 6 years ago

Hi,

which version of iNNvestigate are using? It seems like you are using an older one that relied on this class. I think updating will solve the issue.

LRP does not support softmax layers due to theoretical reason. I suggest to you to read the paper and have a look into our tutorials. There you can find how to circumvent this issue.

Cheers, Max