C3_W4_Lab_4 Empty heatmaps

Hi! The heatmaps generated in the lab are empty (black) or have activation regions that don’t match with the detected object. Is there some kind of bug I can fix ?

Hello there,

I just ran the Lab myself didn’t have any issues, you might have edited something. You shout try refreshing the workspace Look here

I ran into the same problem. For some input images, the heatmap is empty because the heatmap array returned by the get_CAM function is filled with nan’s. Again, it happens only for certain images, and I’m still trying to find out the source of this. If you run the Lab once on a random image, you will very likely not reproduce the issue.

EDIT:

The issue originates from the following part of get_CAM. In the commented lines, I try to explain how the problem arises:

conv_output_values = np.squeeze(conv_output_values.numpy())
# in certain cases, `conv_output_values` will contain only a very few channels 
# with positive values in them, and they can be very sparse, i.e., 
# only a few of the 14*14 values in those channels will be non-zero.
grads_values = grads_values.numpy()
# the mean per-channel gradients in `grads_values` can be very small,
# i.e. in the order of <~ 10^-10
for i in range(512): 
        conv_output_values[:,:,i] *= grads_values[i]
# as a result of this loop, the non-zero, sparse layers of `conv_output_values` will contain
# mostly zeros, and a few very small numbers; ...
heatmap = np.mean(conv_output_values, axis=-1)
# ... and when we take the mean of 512 values along the last axis, most of which are zeros and the rest are 
# very small numbers, np.mean() will return zero, due to rounding error, I guess.
heatmap = np.maximum(heatmap, 0)
# Finally, as a result of all the above, when we divide by `heatmap.max()`, we will divide by zero,
# thus filling up `heatmap` with nan's:
heatmap /= heatmap.max()

It might help to either increase floating point precision everywhere, or do the math in log space.

It might be possible that those images have a different number of channels, this has come up somewhere in the forum before that png images have four channels I think it was.