marcoancona / DeepExplain

A unified framework of perturbation and gradient-based attribution methods for Deep Neural Networks interpretability. DeepExplain also includes support for Shapley Values sampling. (ICLR 2018)
https://arxiv.org/abs/1711.06104
MIT License
720 stars 133 forks source link

Input with unknown shape in Multiple-inputs models #45

Open ghost opened 4 years ago

ghost commented 4 years ago

Hi, I was trying to use DeepExplain on a tensorflow model which has two inputs:

So when I am trying to use explain method:

from deepexplain.tensorflow import DeepExplain

sess = Model.sess

X = Model.x

B = Model.batch_size

T = Model.z

index = 0

with DeepExplain(session = sess) as de:

    attributions = []

    output_size = T.shape[-1]

    mask = np.zeros((1, output_size))

    mask[0][index] = 1

    att = de.explain("deeplift", T * mask, [X, B], [data, 256])

I get the following error:

c:\users\matin\src\deepexplain\deepexplain\tensorflow\methods.py in _set_check_baseline(self) 178 if self.baseline is None: 179 if self.has_multiple_inputs: --> 180 self.baseline = [np.zeros([1,] + xi.get_shape().as_list()[1:]) for xi in self.X] 181 else: 182 self.baseline = np.zeros([1,] + self.X.get_shape().as_list()[1:])

"as_list() is not defined on an unknown TensorShape."

Is there a way to get around it?

marcoancona commented 4 years ago

The assumption is that all inputs have batch_size as the first dimension of the input tensor. This is what Keras does. As you have a pure TF model, it might be possible to fix DeepExplain to handle this case but it might be easier to define the size of your second input (which is always [1] as far as I understood)

marcoancona commented 4 years ago

Looking closer, the current DeepExplain requires each input to be at least two-dimensional. A workaround could be to define your second input as having shape [[1]]

mbuet2ner commented 4 years ago

On a similar note, I would like to use the occlusion techniques on a network where model.input shape is (?, 3, 224, 224), model.output shape is (?, 1000).

This of course works without any problems for the non-occlusion methods.

Since I can not add a keras reshape layer once the model is trained, I am not really sure how to proceed. Maybe there is an easy fix for that?

Thanks!