Может ли GradCAM отличаться, если все условия одинаковы без batch_size? - PullRequest
0 голосов
/ 18 ноября 2018

Я использую CNN-архитектуру.

И я использовал gradCAM, используя keras-vis.

Я нашел что-то странное.

Когда я только что изменил batch_size входного изображения, его результат отличается.(тот же batch_size, тот же результат)

Я не знаю, почему это происходит.

в функции visualize_cam_with_losses, могут ли 'grads' отличаться, если модель и входное изображение одинаковы?

penultimate_output = penultimate_layer.output
opt = Optimizer(input_tensor, losses, wrt_tensor=penultimate_output, 
norm_grads=False)
_, grads, penultimate_output_value = opt.minimize(seed_input, max_iter=1, 
grad_modifier=grad_modifier, verbose=False)

# For numerical stability. Very small grad values along with small penultimate_output_value can cause
# w * penultimate_output_value to zero out, even for reasonable fp precision of float32.
grads = grads / (np.max(grads) + K.epsilon())

# Average pooling across all feature maps.
# This captures the importance of feature map (channel) idx to the output.
channel_idx = 1 if K.image_data_format() == 'channels_first' else -1
other_axis = np.delete(np.arange(len(grads.shape)), channel_idx)
weights = np.mean(grads, axis=tuple(other_axis))

# Generate heatmap by computing weight * output over feature maps
output_dims = utils.get_img_shape(penultimate_output)[2:]
heatmap = np.zeros(shape=output_dims, dtype=K.floatx())
for i, w in enumerate(weights):
    if channel_idx == -1:
        heatmap += w * penultimate_output_value[0, ..., i]
    else:
        heatmap += w * penultimate_output_value[0, i, ...]

# ReLU thresholding to exclude pattern mismatch information (negative gradients).
heatmap = np.maximum(heatmap, 0)

# The penultimate feature map size is definitely smaller than input image.
input_dims = utils.get_img_shape(input_tensor)[2:]
heatmap = imresize(heatmap, input_dims, interp='bicubic', mode='F')

# Normalize and create heatmap.
heatmap = utils.normalize(heatmap)
return heatmap, np.uint8(cm.jet(heatmap)[..., :3] * 255)
...