Can GradCAM be different when all conditons are same without batch_size?












0















I Use CNN-architecture.



And i used gradCAM using keras-vis.



I found something strange.



When i just changed input image's batch_size, it's result is different.
(same batch_size, same result)



I don't know why these things happen.



in function visualize_cam_with_losses, can 'grads' be different if model and input image is same?



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)









share|improve this question



























    0















    I Use CNN-architecture.



    And i used gradCAM using keras-vis.



    I found something strange.



    When i just changed input image's batch_size, it's result is different.
    (same batch_size, same result)



    I don't know why these things happen.



    in function visualize_cam_with_losses, can 'grads' be different if model and input image is same?



    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)









    share|improve this question

























      0












      0








      0








      I Use CNN-architecture.



      And i used gradCAM using keras-vis.



      I found something strange.



      When i just changed input image's batch_size, it's result is different.
      (same batch_size, same result)



      I don't know why these things happen.



      in function visualize_cam_with_losses, can 'grads' be different if model and input image is same?



      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)









      share|improve this question














      I Use CNN-architecture.



      And i used gradCAM using keras-vis.



      I found something strange.



      When i just changed input image's batch_size, it's result is different.
      (same batch_size, same result)



      I don't know why these things happen.



      in function visualize_cam_with_losses, can 'grads' be different if model and input image is same?



      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)






      keras deep-learning conv-neural-network visualize






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 18 '18 at 9:22









      김민우김민우

      11




      11
























          0






          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53359413%2fcan-gradcam-be-different-when-all-conditons-are-same-without-batch-size%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53359413%2fcan-gradcam-be-different-when-all-conditons-are-same-without-batch-size%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          這個網誌中的熱門文章

          Tangent Lines Diagram Along Smooth Curve

          Yusuf al-Mu'taman ibn Hud

          Zucchini