utkuozbulak / pytorch-cnn-visualizations

Pytorch implementation of convolutional neural network visualization techniques
MIT License
7.86k stars 1.49k forks source link

question about guided backprop #65

Closed narrowsnap closed 4 years ago

narrowsnap commented 4 years ago

Hi, thank you for this great repo. I'm confused when I'm reading guided_backprop.py. On line 70-73, the gradient of output is [0, 0, ..., 1, 0].

one_hot_output = torch.FloatTensor(1, model_output.size()[-1]).zero_()
one_hot_output[0][target_class] = 1
# Backward pass
model_output.backward(gradient=one_hot_output)

I changed target class while draw cat_dog_Guided_BP_color map. But I find no matter what class is set, it always looks the same.

For example: cat_dog_Guided_BP_color_target_class_10.jpg cat_dog_Guided_BP_color_target_class_10

cat_dog_Guided_BP_color_target_class_100.jpg cat_dog_Guided_BP_color_target_class_100

cat_dog_Guided_BP_color_target_class_243.jpg(ground truth) cat_dog_Guided_BP_color_target_class_243

cat_dog_Guided_BP_color_target_class_500.jpg cat_dog_Guided_BP_color_target_class_500

cat_dog_Guided_BP_color_target_class_890.jpg cat_dog_Guided_BP_color_target_class_890

Bear-kai commented 4 years ago

They just look similar, not the same. This happen in not only guided backprop, but also other vis techniques. Actually, it is reasonable. Note: The cat is less important in the GT image.

narrowsnap commented 4 years ago

@Bear-Kai Dose this mean network recognizes objects via edge? And I don't understand why set gradient=one_hot_output rather than set gradient=ones_like(model_output). Any explain will be thanked.

Bear-kai commented 4 years ago

@narrowsnap

  1. In my opinion, the netwok recognizes things via the saliency parts, and also reject things by them.
  2. Since you only care about the logit (and the gradient from the logit) corresponding to the GT. one_hot_output is considered as a mask to filter out all non-GT logits.
narrowsnap commented 4 years ago

@Bear-kai Thank you! Get it.

utkuozbulak commented 4 years ago

Thank you for the answer @Bear-kai !