captum icon indicating copy to clipboard operation
captum copied to clipboard

AssertionError: Cannot normalize by scale factor = 0

Open himsR opened this issue 3 years ago • 2 comments

Hello,

I followed the example in the tutorial to use integrated gradients for my model that takes in image as well as tabular features.

img_path = "/media/images/576.jpeg"
feats_valid_file = "/media/feats/valid_feats_std.npy"
datapoint = np.load(feats_valid_file, allow_pickle=True).astype(np.float32)[14]
datapoint = torch.tensor(datapoint).unsqueeze(0)  // shape -> 1, 724

img=read_image(img_path)
img=data_transforms['valid'](img)
input = img.unsqueeze(0) // shape -> (1, 3, 224, 224)
integrated_gradients = IntegratedGradients(model)
attributions_ig = integrated_gradients.attribute((input.to(device), datapoint.to(device)))
// shape of attributions_ig[0] --> (1,3,224,224) , attributions_ig[1] --> (1,724)
default_cmap = LinearSegmentedColormap.from_list('custom blue',
                                                 [(0, '#ffffff'),
                                                  (0.25, '#000000'),
                                                  (1, '#000000')], N=256)

_ = viz.visualize_image_attr(np.transpose(attributions_ig[0].squeeze().cpu().detach().numpy(), (1, 2, 0)),
                             np.transpose(img.squeeze().cpu().detach().numpy(), (1, 2, 0)),
                             method='heat_map',
                             cmap=default_cmap,
                             show_colorbar=True,
                             sign='positive',
                             outlier_perc=1)

But I get this error -->

Traceback (most recent call last): File "/media/himanshu/69c57ccf-aa7f-4a0a-bf49-539a27acad35/real_estate/data/onestage/singleImageModel.py", line 204, in explain _ = viz.visualize_image_attr(np.transpose(attributions_ig[0].squeeze().cpu().detach().numpy(), (1, 2, 0)), File "/home/himanshu/anaconda3/envs/newpytorch/lib/python3.9/site-packages/captum/attr/_utils/visualization.py", line 244, in visualize_image_attr norm_attr = _normalize_image_attr(attr, sign, outlier_perc) File "/home/himanshu/anaconda3/envs/newpytorch/lib/python3.9/site-packages/captum/attr/_utils/visualization.py", line 84, in _normalize_image_attr return _normalize_scale(attr_combined, threshold) File "/home/himanshu/anaconda3/envs/newpytorch/lib/python3.9/site-packages/captum/attr/_utils/visualization.py", line 42, in _normalize_scale assert scale_factor != 0, "Cannot normalize by scale factor = 0" AssertionError: Cannot normalize by scale factor = 0

Thanks

himsR avatar May 30 '22 18:05 himsR

Hi @himsR - thank you for the question. I wonder whether the attr argument consists of all zero's (or all negative values, since you are using sign='positive'). It is complaining because the top 1% of positive pixel attributions is equal to 0.

99warriors avatar Jun 01 '22 18:06 99warriors

Facing the same issue, any ideas for handling this properly? Thank you!

bsridatta avatar Jul 18 '23 08:07 bsridatta