PAIR-code / saliency

Framework-agnostic implementation for state-of-the-art saliency methods (XRAI, BlurIG, SmoothGrad, and more).
https://pair-code.github.io/saliency/
Apache License 2.0
950 stars 190 forks source link

assert x_baseline.shape == x_value.shape error #1

Closed neouyghur closed 7 years ago

neouyghur commented 7 years ago

Hi, I am getting assertion error on Integrated_gradient part, could you check it?

experiencor commented 7 years ago

FYI, I use the latest version without any problem.

neouyghur commented 7 years ago

@experiencor Have you implemented the integrated gradient (the last part)? X_value is an image, however the x_baseline is shape of the image, therefore this x_baseline.shape == x_value.shape can't be true.

experiencor commented 7 years ago

I did not provide x_baseline to GetMask but left it as default.

x_baseline = np.zeros_like(x_value), this creates a zero array with the same shape as x_value. (you can read the document here https://docs.scipy.org/doc/numpy-1.11.0/reference/generated/numpy.zeros_like.html)

You might have provided GetMask with incorrect x_baseline.

neouyghur commented 7 years ago

@experiencor you mean you have removed the x_baseline==baseline in the following code ?

vanilla_integrated_gradients_mask_3d = integrated_gradients.GetMask(
  im, feed_dict = {neuron_selector: prediction_class}, x_baseline=baseline)

smoothgrad_integrated_gradients_mask_3d = integrated_gradients.GetSmoothedMask(
  im, feed_dict = {neuron_selector: prediction_class}, x_baseline=baseline)

If so, I know it works it in that way. I have get the same result as author without providing x_baseline.

However, without doing these, the code does not work. The author should remove the x_baseline.

Besides that, in Example code the creation of the baseline is also not correct

baseline = np.array(im.shape)
baseline.fill(-1)

the author should use the np.zeros_like instead of the np.array.

nsthorat commented 7 years ago

Sorry about this!

The ipynb was incorrect, baseline is now initialized as follows: baseline = np.zeros(im.shape) baseline.fill(-1)

Previously, doing np.array(im.shape) makes a new np array with the values being the shape of image, when we really want to initialize an empty image the shape of the input.

A quick note on the -1 initialization, this is chosen because our Inception model uses -1 as black in the images. To keep true to the integrated gradients approach, we use black here, however you can use whatever baseline you want, and typically 0 and -1 don't look much different.