class_docstr: To run: "python -m saliency.core.grad_cam_test" from top-level saliency directory.
class_docstr_tok: ["To","run",":","``","python","-m","saliency.core.grad_cam_test","''","from","top-level","saliency","directory","."]
docstr: Tests the GradCAM method using a simple network.
  
  Simple test case where the network contains one convolutional layer that
  acts as a horizontal line detector and the input image is a 5x5 matrix with
  a centered 3x3 grid of 1s and 0s elsewhere.
  
  The computed GradCAM mask should detect the pixels of highest importance to
  be along the two horizontal lines in the image (exact expected values stored
  in ref_mask).
docstr_tok: ["Tests","the","GradCAM","method","using","a","simple","network",".","Simple","test","case","where","the","network","contains","one","convolutional","layer","that","acts","as","a","horizontal","line","detector","and","the","input","image","is","a","5x5","matrix","with","a","centered","3x3","grid","of","1s","and","0s","elsewhere",".","The","computed","GradCAM","mask","should","detect","the","pixels","of","highest","importance","to","be","along","the","two","horizontal","lines","in","the","image","(","exact","expected","values","stored","in","ref_mask",")","."]
code: def testGradCamGetMask(self):
      """Tests the GradCAM method using a simple network.
  
      Simple test case where the network contains one convolutional layer that
      acts as a horizontal line detector and the input image is a 5x5 matrix with
      a centered 3x3 grid of 1s and 0s elsewhere.
  
      The computed GradCAM mask should detect the pixels of highest importance to
      be along the two horizontal lines in the image (exact expected values stored
      in ref_mask).
      """
  
      def create_call_model_function():
          def call_model(
              x_value_batch, call_model_args=None, expected_keys=None
          ):
              # simulates conv layer output and grads where the kernel for the conv
              # layer is a horizontal line detector of kernel size 3 and the input is
              # a 3x3 square of ones in the center of the image.
              grad = np.zeros([5, 5])
              grad[(0, -1), (0, -1)] = 2
              grad[(1, -1), 1:-1] = 3
              output = np.zeros([5, 5])
              output[:] = [1, 2, 3, 2, 1]
              output[(0, -1), :] *= -1
              output[2, :] = 0
              grad = grad.reshape(x_value_batch.shape)
              output = output.reshape(x_value_batch.shape)
              return {
                  CONVOLUTION_LAYER_VALUES: output,
                  CONVOLUTION_OUTPUT_GRADIENTS: grad,
              }
  
          return call_model
  
      call_model_function = create_call_model_function()
  
      # Generate test input (centered matrix of 1s surrounded by 0s)
      # and generate corresponding GradCAM mask
      img = np.zeros([INPUT_HEIGHT_WIDTH, INPUT_HEIGHT_WIDTH])
      img[1:-1, 1:-1] = 1
      img = img.reshape([INPUT_HEIGHT_WIDTH, INPUT_HEIGHT_WIDTH, 1])
      mask = self.grad_cam_instance.GetMask(
          img,
          call_model_function=call_model_function,
          call_model_args=None,
          should_resize=True,
          three_dims=False,
      )
  
      # Compare generated mask to expected result
      ref_mask = np.array(
          [
              [0.0, 0.0, 0.0, 0.0, 0.0],
              [0.33, 0.67, 1.0, 0.67, 0.33],
              [0.0, 0.0, 0.0, 0.0, 0.0],
              [0.33, 0.67, 1.0, 0.67, 0.33],
              [0.0, 0.0, 0.0, 0.0, 0.0],
          ]
      )
      self.assertTrue(
          np.allclose(mask, ref_mask, atol=0.01),
          "Generated mask did not match reference mask.",
      )
  
file_path: saliency/saliency/core/grad_cam_test.py
imports: import unittest
  import unittest.mock
  from .base import SHAPE_ERROR_MESSAGE
  from .None import grad_cam
  import numpy
label_desc: Use image node_id 2 for calling the GradCamTest obj's underlying member method code with example usage: obj.testGradCamGetMask() and returns: call_model, dict