def visualize(sess, images_ph, im, y, prediction_class, neuron_selector,
              base_name):
    import saliency
    graph = tf.get_default_graph()
    # Construct the saliency object. This doesn't yet compute the saliency mask, it just sets up the necessary ops.
    gradient_saliency = saliency.GradientSaliency(graph, sess, y, images_ph)
    guided_backprop = saliency.GuidedBackprop(graph, sess, y, images_ph)
    algo = guided_backprop
    # Compute the vanilla mask and the smoothed mask.

    for i, image in enumerate(im):
        prediction_class = 1  #np.argmax(prediction_class[i])
        vanilla_mask_3d = algo.GetMask(
            image, feed_dict={neuron_selector: prediction_class})
        #smoothgrad_mask_3d = algo.GetSmoothedMask(im, feed_dict = {neuron_selector: prediction_class})

        # Call the visualization methods to convert the 3D tensors to 2D grayscale.
        vanilla_mask_grayscale = saliency.VisualizeImageGrayscale(
            vanilla_mask_3d)
        #smoothgrad_mask_grayscale = saliency.VisualizeImageGrayscale(smoothgrad_mask_3d)
        vanilla_mask_grayscale = ((vanilla_mask_grayscale + 1) * 127.5).astype(
            np.uint8)
        #smoothgrad_mask_grayscale = ((smoothgrad_mask_grayscale + 1) * 127.5).astype(np.uint8)
        result = Image.fromarray(vanilla_mask_grayscale, mode='L')
        #smoothed_result = Image.fromarray(smoothgrad_mask_grayscale, mode='L')
        #print 'sal name', base_name[i]
        print 'sal_image_dest_dir', sal_image_dest_dir
        sal_name = sal_image_dest_dir + str(base_name[i]).replace(
            '.jpg', '_sal.jpg')
        print 'saving {} ...'.format(sal_name)
        result.save(sal_name)
Пример #2
0
def get_saliency_image(graph, sess, y, image, saliency_method):
  """generates saliency image.

  Args:
    graph: tensor flow graph.
    sess: the current session.
    y: the pre-softmax activation we want to assess attribution with respect to.
    image: float32 image tensor with size [1, None, None].
    saliency_method: string indicating saliency map type to generate.

  Returns:
    a saliency map and a smoothed saliency map.

  Raises:
    ValueError: if the saliency_method string does not match any included method
  """
  if saliency_method == 'integrated_gradients':
    integrated_placeholder = saliency.IntegratedGradients(graph, sess, y, image)
    return integrated_placeholder
  elif saliency_method == 'gradient':
    gradient_placeholder = saliency.GradientSaliency(graph, sess, y, image)
    return gradient_placeholder
  elif saliency_method == 'guided_backprop':
    gb_placeholder = saliency.GuidedBackprop(graph, sess, y, image)
    return gb_placeholder
  else:
    raise ValueError('No saliency method method matched. Verification of'
                     'input needed')
Пример #3
0
def get_saliency_constructors(model_graph,
                              model_session,
                              logit_tensor,
                              input_tensor,
                              gradcam=False,
                              conv_layer_gradcam=None):
    """Initialize mask functions in saliency package.

    Args:
        model_graph: tf graph of the model.
        model_session: tf session with trained model loaded.
        logit_tensor: tensor corresponding to the model logit output.
        input_tensor: tensor coresponding to the input data.
        gradcam: boolean to indicate whether to include gradcam.
        conv_layer_gradcam: tensor corresponding to activations
                            from a conv layer, from the trained model.
                            Authors recommend last layer.
    Returns:
        saliency_constructor: dictionary (name of method, and value is
                              function to each saliency method.
        neuron_selector: tensor of specific output to explain.
    """

    assert (type(tf.Graph()) == type(model_graph)),\
        ("Model graph should be of type {}".format(type(tf.Graph())))

    if gradcam and conv_layer_gradcam is None:
        raise ValueError("If gradcam is True, then conv_layer_gradcam"
                         "is be provided.")
    with model_graph.as_default():
        with tf.name_scope("saliency"):
            neuron_selector = tf.placeholder(tf.int32)
            y_salient = logit_tensor[neuron_selector]
    gradient_saliency = saliency.GradientSaliency(model_graph, model_session,
                                                  y_salient, input_tensor)
    guided_backprop = saliency.GuidedBackprop(model_graph, model_session,
                                              y_salient, input_tensor)
    integrated_gradients = saliency.IntegratedGradients(
        model_graph, model_session, y_salient, input_tensor)
    saliency_constructor = {
        'vng': gradient_saliency,
        'gbp': guided_backprop,
        'ig': integrated_gradients
    }
    if gradcam:
        gradcam = saliency.GradCam(model_graph, model_session, y_salient,
                                   input_tensor, conv_layer_gradcam)
        saliency_constructor['gc'] = gradcam
    return saliency_constructor, neuron_selector
Пример #4
0
def Guided_Backprop_SmoothGrad(graph, sess, y, images):
    print('Guided_Backprop_SmoothGrad')
    guided_backprop = saliency.GuidedBackprop(graph, sess, y, images)

    # Compute the vanilla mask and the smoothed mask.
    vanilla_guided_backprop_mask_3d = guided_backprop.GetMask(
        im, feed_dict={neuron_selector: prediction_class})
    smoothgrad_guided_backprop_mask_3d = guided_backprop.GetSmoothedMask(
        im, feed_dict={neuron_selector: prediction_class})

    # Call the visualization methods to convert the 3D tensors to 2D grayscale.
    vanilla_mask_grayscale = saliency.VisualizeImageGrayscale(
        vanilla_guided_backprop_mask_3d)
    smoothgrad_mask_grayscale = saliency.VisualizeImageGrayscale(
        smoothgrad_guided_backprop_mask_3d)
    image_grad.append(vanilla_mask_grayscale)
    image_grad.append(smoothgrad_mask_grayscale)
Пример #5
0
def build_i3d_model(video_tensor):
    # model_name = "/home/ar/Experiment/ucf-101/rgb_backup01/models/rgb_scratch_10000_6_64_0.0001_decay/i3d_ucf_model-19999" # Note: I3D trained model
    model_name = "./models/rgb_imagenet_10000_6_64_0.0001_decay/i3d_ucf_model-9999"
    print("load model succeed")

    graph = tf.Graph()
    with graph.as_default():
        images_placeholder = tf.placeholder(tf.float32, [FLAGS.batch_size, FLAGS.n_frames, FLAGS.crop_size, FLAGS.crop_size, FLAGS.rgb_channels])
        #is_training = tf.placeholder(tf.bool)

        with tf.variable_scope('RGB'):
            logits, _ = InceptionI3d(
                           num_classes=FLAGS.classics,
                           spatial_squeeze=True,
                           final_endpoint='Logits', 
                           name='inception_i3d'
                           )(images_placeholder, is_training=False)

        # Create a saver for writing training checkpoints
        saver = tf.train.Saver()
        init = tf.global_variables_initializer()

        # Create a session for running Ops on the Graph
        sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True))
        sess.run(init)

        # Restore trained model
        saver.restore(sess, model_name)

        neuron_selector = tf.placeholder(tf.int32)
        y = logits[0][neuron_selector]

        prediction = tf.argmax(logits, 1)

    out_feature = sess.run(logits, 
                           feed_dict={images_placeholder: video_tensor})

    prediction_class = sess.run(prediction, 
                                feed_dict={images_placeholder: video_tensor})[0]
    #print(prediction_class)

    ###############################################################################################
    #gradient_saliency = saliency.GradientSaliency(graph, sess, y, images_placeholder)

    # Compute the vanilla mask and the smoothed mask.
    #vanilla_mask_3d = gradient_saliency.GetMask(video_tensor[0], feed_dict = {neuron_selector: prediction_class})
    #print(vanilla_mask_3d.shape)
    #smoothgrad_mask_3d = gradient_saliency.GetSmoothedMask(video_tensor[0], feed_dict = {neuron_selector: prediction_class})

    #vanilla_mask_grayscale = saliency.VisualizeImageGrayscale(vanilla_mask_3d)
    #print(vanilla_mask_grayscale.shape)
    #smoothgrad_mask_grayscale = saliency.VisualizeImageGrayscale(smoothgrad_mask_3d)
    ###############################################################################################
    guided_backprop = saliency.GuidedBackprop(graph, sess, y, images_placeholder)

    # Compute the vanilla mask and the smoothed mask.
    vanilla_guided_backprop_mask_3d = guided_backprop.GetMask(video_tensor[0], feed_dict = {neuron_selector: prediction_class})
    smoothgrad_guided_backprop_mask_3d = guided_backprop.GetSmoothedMask(video_tensor[0], feed_dict = {neuron_selector: prediction_class})

    vanilla_mask_grayscale = saliency.VisualizeImageGrayscale(vanilla_guided_backprop_mask_3d)
    smoothgrad_mask_grayscale = saliency.VisualizeImageGrayscale(smoothgrad_guided_backprop_mask_3d)
    ###############################################################################################

    return vanilla_mask_grayscale, smoothgrad_mask_grayscale
                                                  num_classes=1001)

        # Restore the checkpoint
        sess = tf.Session(graph=graph)
        saver = tf.train.Saver()
        saver.restore(sess, ckpt_file)

    # Construct the scalar neuron tensor.
    logits = graph.get_tensor_by_name('InceptionV3/Logits/SpatialSqueeze:0')
    neuron_selector = tf.placeholder(tf.int32)
    y = logits[0][neuron_selector]

    # Construct tensor for predictions.
    prediction = tf.argmax(logits, 1)

guided_backprop = saliency.GuidedBackprop(graph, sess, y, images)


def guided_vanilla(image):
    image = image / 127.5 - 1.0
    prediction_class = sess.run(prediction, feed_dict={images: [image]})[0]
    vanilla_guided_backprop_mask_3d = guided_backprop.GetMask(
        image, feed_dict={neuron_selector: prediction_class})
    vanilla_mask_grayscale = saliency.VisualizeImageGrayscale(
        vanilla_guided_backprop_mask_3d)
    return vanilla_mask_grayscale.tolist()


# Download human-readable labels for ImageNet.
inception_net = tf.keras.applications.InceptionV3()  # load the model
Пример #7
0
    ix = int(args.filename[-6])
else:
    ix = 10 + int(args.filename[-6])

cnt = 0

for j, imgid in enumerate(x_test[int(args.start):int(args.end)]):
    i = j + int(args.start)

    if y_test[i] == 1:
        img = LoadImage(root_dir + 'train/' + imgid + '.png')
        bb_coords = LoadImage2(root_dir + 'mask/' + imgid + '.png')
        l = sess.run(logits, feed_dict={inp: [img]})[0]
        prediction_class = sess.run(prediction, feed_dict={inp: [img]})[0]

        gbp = saliency.GuidedBackprop(sess.graph, sess, y, inp)
        gbp_mask = gbp.GetMask(img,
                               feed_dict={neuron_selector: prediction_class})
        gbp_mask = saliency.VisualizeImageGrayscale(gbp_mask)
        scores['gbp'].append(
            saliency_ttest(gbp_mask, bb_coords, prediction_class))

        gradient_saliency = saliency.GradientSaliency(sess.graph, sess, y, inp)
        vanilla_mask_3d = gradient_saliency.GetMask(
            np.reshape(img, (320, 320, 3)),
            feed_dict={neuron_selector: prediction_class})
        smoothgrad_mask_3d = gradient_saliency.GetSmoothedMask(
            np.reshape(img, (320, 320, 3)),
            feed_dict={neuron_selector: prediction_class})
        smoothgrad_mask_grayscale = saliency.VisualizeImageGrayscale(
            smoothgrad_mask_3d)
Пример #8
0
def compute_and_save_attr(model, data, indices, num_threads):
    """Given the name of a model and a set of data, select a set of images based on provided indices, and compute and save their saliency maps and object attributions."""

    base_dir = os.getcwd()
    data_dir = os.path.join(base_dir, 'data', data, 'val')
    model_dir = os.path.join(base_dir, 'models', model)
    sal_output_dir = os.path.join(base_dir, SAL_DIR, model + '-' + data)
    attr_output_dir = os.path.join(base_dir, ATTR_DIR, model + '-' + data)
    if not tf.gfile.Exists(sal_output_dir):
        tf.gfile.MakeDirs(sal_output_dir)
    if not tf.gfile.Exists(attr_output_dir):
        tf.gfile.MakeDirs(attr_output_dir)

    img_names = [sorted(tf.gfile.ListDirectory(data_dir))[i] for i in indices]
    img_paths = [os.path.join(data_dir, img_name) for img_name in img_names]
    imgs = load_imgs(img_paths, num_threads)

    input_name = 'input_tensor:0'
    logit_name = 'resnet_model/final_dense:0'
    conv_name = 'resnet_model/block_layer4:0'

    with tf.Session(graph=tf.Graph()) as sess:
        tf.saved_model.loader.load(sess, ['serve'], model_dir)
        graph = tf.get_default_graph()
        input_tensor = graph.get_tensor_by_name(input_name)
        logit_tensor = graph.get_tensor_by_name(logit_name)
        neuron_selector = tf.placeholder(tf.int32)
        y = logit_tensor[:, neuron_selector]
        pred_tensor = tf.argmax(logit_tensor, 1)

        vg = saliency.GradientSaliency(graph, sess, y, input_tensor)
        gb = saliency.GuidedBackprop(graph, sess, y, input_tensor)
        ig = saliency.IntegratedGradients(graph, sess, y, input_tensor)
        gc = saliency.GradCam(graph, sess, y, input_tensor,
                              graph.get_tensor_by_name(conv_name))

        def single_map(img, img_name):
            pred = sess.run(pred_tensor, feed_dict={input_tensor: [img]})[0]
            vg_mask = vg.GetMask(img, feed_dict={neuron_selector: pred})
            # *s is SmoothGrad
            vgs_mask = vg.GetSmoothedMask(img,
                                          feed_dict={neuron_selector: pred})
            gb_mask = gb.GetMask(img, feed_dict={neuron_selector: pred})
            gbs_mask = gb.GetSmoothedMask(img,
                                          feed_dict={neuron_selector: pred})
            baseline = np.zeros(img.shape) - np.expand_dims(
                np.expand_dims(_CHANNEL_MEANS, 0), 0)
            ig_mask = ig.GetMask(img,
                                 feed_dict={neuron_selector: pred},
                                 x_baseline=baseline)
            igs_mask = ig.GetSmoothedMask(img,
                                          feed_dict={neuron_selector: pred},
                                          x_baseline=baseline)
            gc_mask = gc.GetMask(img, feed_dict={neuron_selector: pred})
            gcs_mask = gc.GetSmoothedMask(img,
                                          feed_dict={neuron_selector: pred})
            # gbgc is guided GradCam
            gbgc_mask = gb_mask * gc_mask
            gbgcs_mask = gbs_mask * gcs_mask
            # Also include gradient x input
            masks = np.array([
                vg_mask, vgs_mask, gb_mask, gbs_mask, ig_mask, igs_mask,
                gc_mask, gcs_mask, gbgc_mask, gbgcs_mask, vg_mask * img,
                vgs_mask * img
            ])
            return masks, pred

        sal_maps = []
        preds = []
        for img, img_name in zip(imgs, img_names):
            sal_path = tf.gfile.Glob(
                os.path.join(sal_output_dir, img_name[:-4] + '*'))
            if len(sal_path) > 0:
                sal_maps.append(np.load(tf.gfile.GFile(sal_path[0], 'rb')))
                preds.append(sal_path[0].split('_')[-1])
                tf.logging.info(
                    'Loaded saliency maps for {}.'.format(img_name))
            else:
                masks, pred = single_map(img, img_name)
                sal_maps.append(masks)
                preds.append(pred)
                out_path = os.path.join(sal_output_dir,
                                        img_name[:-4] + '_' + str(pred))
                np.save(tf.gfile.GFile(out_path, 'w'), masks)
                tf.logging.info('Saved saliency maps for {}.'.format(img_name))

    # Locate the objects, convert 3D saliency maps to 2D, and compute
    # the attributions of the object segments by averaging over the
    # per-pixel attributions of the objects.
    loc_fpath = os.path.join(base_dir, 'data', data, 'val_loc.txt')
    lines = [tf.gfile.Open(loc_fpath).readlines()[i] for i in indices]
    locs = np.array([[
        int(int(l) * float(RESNET_SHAPE[0]) / IMG_SHAPE[0])
        for l in line.rstrip('\n').split(' ')[-1].split(',')
    ] for line in lines])
    pool = multiprocessing.Pool(num_threads)
    maps_3d = np.array(sal_maps).reshape(-1, RESNET_SHAPE[0], RESNET_SHAPE[1],
                                         3)
    maps_2d = np.array(pool.map(visualize_pos_attr, maps_3d))
    maps_2d = maps_2d.reshape(len(indices),
                              int(maps_2d.shape[0] // len(indices)),
                              RESNET_SHAPE[0], RESNET_SHAPE[1])
    mask_fpath = os.path.join(base_dir, 'data', data, 'val_mask')

    if data in ['obj', 'scene', 'scene_only']:
        # MCS and IDR are evaluated on 10000 images and masks are 10x100.
        # Find the right mask.
        obj_dict = {
            'backpack': 0,
            'bird': 1,
            'dog': 2,
            'elephant': 3,
            'kite': 4,
            'pizza': 5,
            'stop_sign': 6,
            'toilet': 7,
            'truck': 8,
            'zebra': 9,
        }

        # Loading val_mask from the data directory
        masks_mat = np.load(tf.gfile.GFile(mask_fpath, 'rb'),
                            allow_pickle=True)
        # Getting obj indices
        obj_inds = [obj_dict[i.split('.')[0].split('-')[0]] for i in img_names]
        # getting indices for a particular object class
        temp_inds = [int(i.split('.')[0][-2:]) for i in img_names]

        obj_masks = [
            masks_mat[obj_inds[i] * 100 + temp_inds[i]]
            for i, _ in enumerate(img_names)
        ]

    else:
        obj_masks = [
            np.load(tf.gfile.GFile(mask_fpath, 'rb'), allow_pickle=True)[i]
            for i in indices
        ]

    attrs = []
    for i in range(len(indices)):
        attr = single_attr(maps_2d[i], locs[i], obj_masks[i])
        attrs.append(attr)
        out_path = os.path.join(attr_output_dir,
                                img_names[i][:-4] + '_' + str(preds[i]))
        np.save(tf.gfile.GFile(out_path, 'w'), attr)
Пример #9
0
def simple_example():
    #--------------------
    mnist = input_data.read_data_sets('./MNIST_data', one_hot=True)

    #--------------------
    # Define a model.

    num_classes = 10
    input_shape = (None, 28, 28, 1)  # 784 = 28 * 28.
    output_shape = (None, num_classes)
    input_ph = tf.placeholder(tf.float32, shape=input_shape, name='input_ph')
    output_ph = tf.placeholder(tf.float32,
                               shape=output_shape,
                               name='output_ph')

    with tf.variable_scope('conv1', reuse=tf.AUTO_REUSE):
        conv1 = tf.layers.conv2d(input_ph,
                                 32,
                                 5,
                                 activation=tf.nn.relu,
                                 name='conv')
        conv1 = tf.layers.max_pooling2d(conv1, 2, 2, name='maxpool')

    with tf.variable_scope('conv2', reuse=tf.AUTO_REUSE):
        conv2 = tf.layers.conv2d(conv1,
                                 64,
                                 3,
                                 activation=tf.nn.relu,
                                 name='conv')
        conv2 = tf.layers.max_pooling2d(conv2, 2, 2, name='maxpool')

    with tf.variable_scope('fc1', reuse=tf.AUTO_REUSE):
        fc1 = tf.layers.flatten(conv2, name='flatten')
        fc1 = tf.layers.dense(fc1, 1024, activation=tf.nn.relu, name='dense')

    with tf.variable_scope('fc2', reuse=tf.AUTO_REUSE):
        model_output = tf.layers.dense(fc1,
                                       num_classes,
                                       activation=tf.nn.softmax,
                                       name='dense')

    #--------------------
    # Train.

    loss = tf.reduce_mean(-tf.reduce_sum(output_ph * tf.log(model_output),
                                         reduction_indices=[1]))
    train_step = tf.train.GradientDescentOptimizer(0.01).minimize(loss)

    sess = tf.Session()
    sess.run(tf.global_variables_initializer())

    print('Start training...')
    start_time = time.time()
    for _ in range(2000):
        batch_xs, batch_ys = mnist.train.next_batch(512)
        batch_xs = np.reshape(batch_xs, (-1, ) + input_shape[1:])
        sess.run(train_step,
                 feed_dict={
                     input_ph: batch_xs,
                     output_ph: batch_ys
                 })
        if 0 == idx % 100: print('.', end='', flush=True)
    print()
    print('End training: {} secs.'.format(time.time() - start_time))

    #--------------------
    # Evaluate.

    correct_prediction = tf.equal(tf.argmax(model_output, 1),
                                  tf.argmax(output_ph, 1))
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

    print('Start testing...')
    acc = sess.run(accuracy,
                   feed_dict={
                       input_ph:
                       np.reshape(mnist.test.images, (-1, ) + input_shape[1:]),
                       output_ph:
                       mnist.test.labels
                   })
    print('Test accuracy = {}.'.format(acc))
    print('End testing: {} secs.'.format(time.time() - start_time))

    if acc < 0.95:
        print('Failed to train...')
        return

    #--------------------
    # Visualize.

    images = np.reshape(mnist.test.images, (-1, ) + input_shape[1:])
    img = images[0]
    minval, maxval = np.min(img), np.max(img)
    img_scaled = np.squeeze((img - minval) / (maxval - minval), axis=-1)

    # Construct the scalar neuron tensor.
    logits = model_output
    neuron_selector = tf.placeholder(tf.int32)
    y = logits[0][neuron_selector]

    # Construct a tensor for predictions.
    prediction = tf.argmax(logits, 1)

    # Make a prediction.
    prediction_class = sess.run(prediction, feed_dict={input_ph: [img]})[0]

    #--------------------
    start_time = time.time()
    saliency_obj = saliency.Occlusion(sess.graph, sess, y, input_ph)
    print('Occlusion: {} secs.'.format(time.time() - start_time))

    # NOTE [info] >> An error exists in GetMask() of ${Saliency_HOME}/saliency/occlusion.py.
    #	<before>
    #		occlusion_window = np.array([size, size, x_value.shape[2]])
    #		occlusion_window.fill(value)
    #	<after>
    #		occlusion_window = np.full([size, size, x_value.shape[2]], value)
    mask_3d = saliency_obj.GetMask(
        img, feed_dict={neuron_selector: prediction_class})

    # Compute a 2D tensor for visualization.
    mask_gray = saliency.VisualizeImageGrayscale(mask_3d)
    mask_div = saliency.VisualizeImageDiverging(mask_3d)

    fig = plt.figure()
    ax = plt.subplot(1, 3, 1)
    ax.imshow(img_scaled, cmap=plt.cm.gray, vmin=0, vmax=1)
    ax.axis('off')
    ax.set_title('Input')
    ax = plt.subplot(1, 3, 2)
    ax.imshow(mask_gray, cmap=plt.cm.gray, vmin=0, vmax=1)
    ax.axis('off')
    ax.set_title('Grayscale')
    ax = plt.subplot(1, 3, 3)
    ax.imshow(mask_div, cmap=plt.cm.gray, vmin=0, vmax=1)
    ax.axis('off')
    ax.set_title('Diverging')
    fig.suptitle('Occlusion', fontsize=16)
    fig.tight_layout()
    #plt.savefig('./saliency_occlusion.png')
    plt.show()

    #--------------------
    start_time = time.time()
    conv_layer = sess.graph.get_tensor_by_name('conv2/conv/BiasAdd:0')
    saliency_obj = saliency.GradCam(sess.graph, sess, y, input_ph, conv_layer)
    print('GradCam: {} secs.'.format(time.time() - start_time))

    mask_3d = saliency_obj.GetMask(
        img, feed_dict={neuron_selector: prediction_class})

    # Compute a 2D tensor for visualization.
    mask_gray = saliency.VisualizeImageGrayscale(mask_3d)
    mask_div = saliency.VisualizeImageDiverging(mask_3d)

    fig = plt.figure()
    ax = plt.subplot(1, 3, 1)
    ax.imshow(img_scaled, cmap=plt.cm.gray, vmin=0, vmax=1)
    ax.axis('off')
    ax.set_title('Input')
    ax = plt.subplot(1, 3, 2)
    ax.imshow(mask_gray, cmap=plt.cm.gray, vmin=0, vmax=1)
    ax.axis('off')
    ax.set_title('Grayscale')
    ax = plt.subplot(1, 3, 3)
    ax.imshow(mask_div, cmap=plt.cm.gray, vmin=0, vmax=1)
    ax.axis('off')
    ax.set_title('Diverging')
    fig.suptitle('Grad-CAM', fontsize=16)
    fig.tight_layout()
    #plt.savefig('./saliency_gradcam.png')
    plt.show()

    #--------------------
    start_time = time.time()
    saliency_obj = saliency.GradientSaliency(sess.graph, sess, y, input_ph)
    print('GradientSaliency: {} secs.'.format(time.time() - start_time))

    vanilla_mask_3d = saliency_obj.GetMask(
        img, feed_dict={neuron_selector: prediction_class})
    smoothgrad_mask_3d = saliency_obj.GetSmoothedMask(
        img, feed_dict={neuron_selector: prediction_class})

    # Compute a 2D tensor for visualization.
    vanilla_mask_gray = saliency.VisualizeImageGrayscale(vanilla_mask_3d)
    smoothgrad_mask_gray = saliency.VisualizeImageGrayscale(smoothgrad_mask_3d)
    vanilla_mask_div = saliency.VisualizeImageDiverging(vanilla_mask_3d)
    smoothgrad_mask_div = saliency.VisualizeImageDiverging(smoothgrad_mask_3d)

    fig = plt.figure()
    ax = plt.subplot(2, 3, 1)
    ax.imshow(img_scaled, cmap=plt.cm.gray, vmin=0, vmax=1)
    ax.axis('off')
    ax.set_title('Input')
    ax = plt.subplot(2, 3, 2)
    ax.imshow(vanilla_mask_gray, cmap=plt.cm.gray, vmin=0, vmax=1)
    ax.axis('off')
    ax.set_title('Vanilla Grayscale')
    ax = plt.subplot(2, 3, 3)
    ax.imshow(smoothgrad_mask_gray, cmap=plt.cm.gray, vmin=0, vmax=1)
    ax.axis('off')
    ax.set_title('SmoothGrad Grayscale')
    ax = plt.subplot(2, 3, 5)
    ax.imshow(vanilla_mask_div, cmap=plt.cm.gray, vmin=0, vmax=1)
    ax.axis('off')
    ax.set_title('Vanilla Diverging')
    ax = plt.subplot(2, 3, 6)
    ax.imshow(smoothgrad_mask_div, cmap=plt.cm.gray, vmin=0, vmax=1)
    ax.axis('off')
    ax.set_title('SmoothGrad Diverging')
    fig.suptitle('Gradient Saliency', fontsize=16)
    fig.tight_layout()
    #plt.savefig('./saliency_gradientsaliency.png')
    plt.show()

    #--------------------
    start_time = time.time()
    saliency_obj = saliency.GuidedBackprop(sess.graph, sess, y, input_ph)
    print('GuidedBackprop: {} secs.'.format(time.time() - start_time))

    vanilla_mask_3d = saliency_obj.GetMask(
        img, feed_dict={neuron_selector: prediction_class})
    smoothgrad_mask_3d = saliency_obj.GetSmoothedMask(
        img, feed_dict={neuron_selector: prediction_class})

    # Compute a 2D tensor for visualization.
    vanilla_mask_gray = saliency.VisualizeImageGrayscale(vanilla_mask_3d)
    smoothgrad_mask_gray = saliency.VisualizeImageGrayscale(smoothgrad_mask_3d)
    vanilla_mask_div = saliency.VisualizeImageDiverging(vanilla_mask_3d)
    smoothgrad_mask_div = saliency.VisualizeImageDiverging(smoothgrad_mask_3d)

    fig = plt.figure()
    ax = plt.subplot(2, 3, 1)
    ax.imshow(img_scaled, cmap=plt.cm.gray, vmin=0, vmax=1)
    ax.axis('off')
    ax.set_title('Input')
    ax = plt.subplot(2, 3, 2)
    ax.imshow(vanilla_mask_gray, cmap=plt.cm.gray, vmin=0, vmax=1)
    ax.axis('off')
    ax.set_title('Vanilla Grayscale')
    ax = plt.subplot(2, 3, 3)
    ax.imshow(smoothgrad_mask_gray, cmap=plt.cm.gray, vmin=0, vmax=1)
    ax.axis('off')
    ax.set_title('SmoothGrad Grayscale')
    ax = plt.subplot(2, 3, 4)
    ax.imshow(vanilla_mask_div, cmap=plt.cm.gray, vmin=0, vmax=1)
    ax.axis('off')
    ax.set_title('Vanilla Diverging')
    ax = plt.subplot(2, 3, 5)
    ax.imshow(smoothgrad_mask_div, cmap=plt.cm.gray, vmin=0, vmax=1)
    ax.axis('off')
    ax.set_title('SmoothGrad Diverging')
    fig.suptitle('Guided Backprop', fontsize=16)
    fig.tight_layout()
    #plt.savefig('./saliency_guidedbackprop.png')
    plt.show()

    #--------------------
    start_time = time.time()
    saliency_obj = saliency.IntegratedGradients(sess.graph, sess, y, input_ph)
    print('IntegratedGradients: {} secs.'.format(time.time() - start_time))

    vanilla_mask_3d = saliency_obj.GetMask(
        img, feed_dict={neuron_selector: prediction_class})
    smoothgrad_mask_3d = saliency_obj.GetSmoothedMask(
        img, feed_dict={neuron_selector: prediction_class})

    # Compute a 2D tensor for visualization.
    vanilla_mask_gray = saliency.VisualizeImageGrayscale(vanilla_mask_3d)
    smoothgrad_mask_gray = saliency.VisualizeImageGrayscale(smoothgrad_mask_3d)
    vanilla_mask_div = saliency.VisualizeImageDiverging(vanilla_mask_3d)
    smoothgrad_mask_div = saliency.VisualizeImageDiverging(smoothgrad_mask_3d)

    fig = plt.figure()
    ax = plt.subplot(2, 3, 1)
    ax.imshow(img_scaled, cmap=plt.cm.gray, vmin=0, vmax=1)
    ax.axis('off')
    ax.set_title('Input')
    ax = plt.subplot(2, 3, 2)
    ax.imshow(vanilla_mask_gray, cmap=plt.cm.gray, vmin=0, vmax=1)
    ax.axis('off')
    ax.set_title('Vanilla Grayscale')
    ax = plt.subplot(2, 3, 3)
    ax.imshow(smoothgrad_mask_gray, cmap=plt.cm.gray, vmin=0, vmax=1)
    ax.axis('off')
    ax.set_title('SmoothGrad Grayscale')
    ax = plt.subplot(2, 3, 4)
    ax.imshow(vanilla_mask_div, cmap=plt.cm.gray, vmin=0, vmax=1)
    ax.axis('off')
    ax.set_title('Vanilla Diverging')
    ax = plt.subplot(2, 3, 5)
    ax.imshow(smoothgrad_mask_div, cmap=plt.cm.gray, vmin=0, vmax=1)
    ax.axis('off')
    ax.set_title('SmoothGrad Diverging')
    fig.suptitle('Integrated Gradients', fontsize=16)
    fig.tight_layout()
    #plt.savefig('./saliency_integratedgradients.png')
    plt.show()

    #--------------------
    start_time = time.time()
    xrai_obj = saliency.XRAI(sess.graph, sess, y, input_ph)
    print('XRAI: {} secs.'.format(time.time() - start_time))

    if True:
        xrai_attributions = xrai_obj.GetMask(
            img, feed_dict={neuron_selector: prediction_class})
    else:
        # Create XRAIParameters and set the algorithm to fast mode which will produce an approximate result.
        xrai_params = saliency.XRAIParameters()
        xrai_params.algorithm = 'fast'
        xrai_attributions_fast = xrai_obj.GetMask(
            img,
            feed_dict={neuron_selector: prediction_class},
            extra_parameters=xrai_params)

    # Show most salient 30% of the image.
    mask = xrai_attributions > np.percentile(xrai_attributions, 70)
    img_masked = img_scaled.copy()
    img_masked[~mask] = 0

    fig = plt.figure()
    ax = plt.subplot(1, 3, 1)
    ax.imshow(img_scaled, cmap=plt.cm.gray, vmin=0, vmax=1)
    ax.axis('off')
    ax.set_title('Input')
    ax = plt.subplot(1, 3, 2)
    ax.imshow(xrai_attributions, cmap=plt.cm.inferno)
    ax.axis('off')
    ax.set_title('XRAI Attributions')
    ax = plt.subplot(1, 3, 3)
    ax.imshow(img_masked, cmap=plt.cm.gray)
    ax.axis('off')
    ax.set_title('Masked Input')
    fig.suptitle('XRAI', fontsize=16)
    fig.tight_layout()
    #plt.savefig('./saliency_xrai.png')
    plt.show()