def guide_grad_cam_test(model, img, target_cls, target_layer, dst, desc):
    img = Image.open(img).convert('RGB')
    prep_img = preprocess_image(img)

    # Grad cam
    gcv2 = GradCam(model, target_layer=target_layer)
    # Generate cam mask
    cam = gcv2.generate_cam(prep_img, target_cls)
    print('Grad cam completed')

    # Guided backprop
    GBP = GuidedBackprop(model)
    # Get gradients
    guided_grads = GBP.generate_gradients(prep_img, target_cls)
    print('Guided backpropagation completed')

    # Guided Grad cam
    cam_gb = guided_grad_cam(cam, guided_grads)

    filename = os.path.join(dst, desc + '_ggrad_cam.jpg')
    save_gradient_images(cam_gb, filename)

    grayscale_cam_gb = convert_to_grayscale(cam_gb)
    filename = os.path.join(dst, desc + 'ggrad_cam_gray.jpg')
    save_gradient_images(grayscale_cam_gb, filename)
    print('Guided grad cam completed')
Beispiel #2
0
def smooth_grad_test(model, img, target_cls, dst, param_n, param_sigma_mult,
                     desc):
    VBP = VanillaBackprop(model)
    # GBP = GuidedBackprop(pretrained_model)  # if you want to use GBP dont forget to
    # change the parametre in generate_smooth_grad
    from PIL import Image
    from utils.misc import preprocess_image
    img = Image.open(img).convert("RGB")
    prep_img = preprocess_image(img)

    param_n = param_n
    param_sigma_multiplier = param_sigma_mult
    smooth_grad = generate_smooth_grad(
        VBP,  # ^This parameter
        prep_img,
        target_cls,
        param_n,
        param_sigma_multiplier)

    import os
    # Save colored gradients
    filename = os.path.join(dst, desc + '_smooth_grad_color.jpg')
    save_gradient_images(smooth_grad, filename)
    # Convert to grayscale
    grayscale_smooth_grad = convert_to_grayscale(smooth_grad)
    # Save grayscale gradients
    filename = os.path.join(dst, desc + '_smooth_grad_gray.jpg')
    save_gradient_images(grayscale_smooth_grad, filename)
    print('Smooth grad completed')
def grad_times_image(model, origin_image, target_cls, dst, desc):
    origin_image = Image.open(origin_image).convert('RGB')
    prep_img = preprocess_image(origin_image)

    vbp = VanillaBackprop(model)
    vanilla_grads = vbp.generate_gradients(prep_img, target_cls)
    grad_times_image = vanilla_grads[0] * prep_img.detach().numpy()[0]
    # Convert to grayscale
    grayscale_vanilla_grads = convert_to_grayscale(grad_times_image)
    # Save grayscale gradients
    file_utils.makesure_path(dst)
    filename = os.path.join(dst, desc + '_Vanilla_grad_times_image_gray.jpg')

    save_gradient_images(grayscale_vanilla_grads, filename)
Beispiel #4
0
def layer_activation_guided_backprop_test(model, img, target_layer, target_pos,
                                          target_class, dst, desc):
    img = Image.open(img).convert("RGB")
    img = preprocess_image(img)
    # File export name
    filename = os.path.join(
        dst, '%s_layer_%d_filter_%d.jpg' % (desc, target_layer, target_pos))
    # Guided backprop
    GBP = GuidedBackprop(model)
    # Get gradients
    guided_grads = GBP.generate_gradients(img, target_class, target_layer,
                                          target_pos)
    # Save colored gradients
    filename = os.path.join(dst, desc + '_guided_bp_color.jpg')
    save_gradient_images(guided_grads, filename)
    # Convert to grayscale
    grayscale_guided_grads = convert_to_grayscale(guided_grads)
    # Save grayscale gradients
    filename = os.path.join(dst, desc + '_guided_bp_gray.jpg')
    save_gradient_images(grayscale_guided_grads, filename)
    # Positive and negative saliency maps
    pos_sal, neg_sal = get_positive_negative_saliency(guided_grads)
    filename = os.path.join(dst, desc + '_pos_sal.jpg')
    save_gradient_images(pos_sal, filename)

    filename = os.path.join(dst, desc + '_neg_sal.jpg')
    save_gradient_images(neg_sal, filename)

    print('Layer Guided backprop completed')
def vannilla_back(model, origin_image, target_cls, dst, desc):
    origin_image = Image.open(origin_image).convert('RGB')
    prep_img = misc.preprocess_image(origin_image)
    vbp = VanillaBackprop(model)
    # Generate gradients
    vanilla_grads = vbp.generate_gradients(prep_img, target_cls)
    # Save colored gradients
    file_utils.makesure_path(dst)
    filename = os.path.join(dst, desc + '_Vanilla_BP_color.png')
    save_gradient_images(vanilla_grads, filename)
    # Convert to grayscale
    grayscale_vanilla_grads = convert_to_grayscale(vanilla_grads)
    # Save grayscale gradients
    filename = os.path.join(dst, desc + '_Vanilla_BP_GRAY.png')
    save_gradient_images(grayscale_vanilla_grads, filename)
Beispiel #6
0
def integraded_gradients_test(model, img, target_cls, dst, desc):
    from PIL import Image
    img = Image.open(img).convert("RGB")
    prep_img = preprocess_image(img)
    # Vanilla backprop
    IG = IntegratedGradients(model)
    # Generate gradients
    integrated_grads = IG.generate_integrated_gradients(
        prep_img, target_cls, 100)
    # Convert to grayscale
    grayscale_integrated_grads = convert_to_grayscale(integrated_grads)
    # Save grayscale gradients
    import os
    filename = os.path.join(dst, desc + "intergraded_gray.jpg")
    save_gradient_images(grayscale_integrated_grads, filename)
    print('Integrated gradients completed.')
def guided_backprop(model, original_image, target_class, dst, desc):
    original_image = Image.open(original_image).convert('RGB')
    prep_img = preprocess_image(original_image)
    # Guided backprop
    GBP = GuidedBackprop(model)
    # Get gradients
    guided_grads = GBP.generate_gradients(prep_img, target_class)
    # Save colored gradients
    filename = os.path.join(dst, desc + '_guided_bp_color.jpg')
    save_gradient_images(guided_grads, filename)
    # Convert to grayscale
    grayscale_guided_grads = convert_to_grayscale(guided_grads)

    # Save grayscale gradients
    filename = os.path.join(dst, desc + '_guided_bp_gray.jpg')
    save_gradient_images(grayscale_guided_grads, filename)
    # Positive and negative saliency maps
    pos_sal, neg_sal = get_positive_negative_saliency(guided_grads)
    filename = os.path.join(dst, desc + '_pos_sal.jpg')
    save_gradient_images(pos_sal, filename)
    filename = os.path.join(dst, desc + '_neg_sal.jpg')
    save_gradient_images(neg_sal, filename)
Beispiel #8
0
    # Vanilla backprop
    IG = IntegratedGradients(model)
    # Generate gradients
    integrated_grads = IG.generate_integrated_gradients(
        prep_img, target_cls, 100)
    # Convert to grayscale
    grayscale_integrated_grads = convert_to_grayscale(integrated_grads)
    # Save grayscale gradients
    import os
    filename = os.path.join(dst, desc + "intergraded_gray.jpg")
    save_gradient_images(grayscale_integrated_grads, filename)
    print('Integrated gradients completed.')


if __name__ == '__main__':
    # Get params
    target_example = 0  # Snake
    (original_image, prep_img, target_class, file_name_to_export, pretrained_model) =\
        get_example_params(target_example)
    # Vanilla backprop
    IG = IntegratedGradients(pretrained_model)
    # Generate gradients
    integrated_grads = IG.generate_integrated_gradients(
        prep_img, target_class, 100)
    # Convert to grayscale
    grayscale_integrated_grads = convert_to_grayscale(integrated_grads)
    # Save grayscale gradients
    save_gradient_images(grayscale_integrated_grads,
                         file_name_to_export + '_Integrated_G_gray')
    print('Integrated gradients completed.')
Beispiel #9
0

if __name__ == '__main__':
    # Get params
    target_example = 0  # Snake
    (original_image, prep_img, target_class, file_name_to_export, pretrained_model) =\
        get_example_params(target_example)

    VBP = VanillaBackprop(pretrained_model)
    # GBP = GuidedBackprop(pretrained_model)  # if you want to use GBP dont forget to
    # change the parametre in generate_smooth_grad

    param_n = 50
    param_sigma_multiplier = 4
    smooth_grad = generate_smooth_grad(
        VBP,  # ^This parameter
        prep_img,
        target_class,
        param_n,
        param_sigma_multiplier)

    # Save colored gradients
    save_gradient_images(smooth_grad,
                         file_name_to_export + '_SmoothGrad_color')
    # Convert to grayscale
    grayscale_smooth_grad = convert_to_grayscale(smooth_grad)
    # Save grayscale gradients
    save_gradient_images(grayscale_smooth_grad,
                         file_name_to_export + '_SmoothGrad_gray')
    print('Smooth grad completed')
    filename = os.path.join(dst, desc + 'ggrad_cam_gray.jpg')
    save_gradient_images(grayscale_cam_gb, filename)
    print('Guided grad cam completed')


if __name__ == '__main__':
    # Get params
    target_example = 0  # Snake
    (original_image, prep_img, target_class, file_name_to_export, pretrained_model) =\
        get_example_params(target_example)

    # Grad cam
    gcv2 = GradCam(pretrained_model, target_layer=11)
    # Generate cam mask
    cam = gcv2.generate_cam(prep_img, target_class)
    print('Grad cam completed')

    # Guided backprop
    GBP = GuidedBackprop(pretrained_model)
    # Get gradients
    guided_grads = GBP.generate_gradients(prep_img, target_class)
    print('Guided backpropagation completed')

    # Guided Grad cam
    cam_gb = guided_grad_cam(cam, guided_grads)
    save_gradient_images(cam_gb, file_name_to_export + '_GGrad_Cam')
    grayscale_cam_gb = convert_to_grayscale(cam_gb)
    save_gradient_images(grayscale_cam_gb,
                         file_name_to_export + '_GGrad_Cam_gray')
    print('Guided grad cam completed')
Beispiel #11
0

if __name__ == '__main__':
    cnn_layer = 10
    filter_pos = 5
    target_example = 2  # Spider
    (original_image, prep_img, target_class, file_name_to_export, pretrained_model) =\
        get_example_params(target_example)

    # File export name
    file_name_to_export = file_name_to_export + '_layer' + str(
        cnn_layer) + '_filter' + str(filter_pos)
    # Guided backprop
    GBP = GuidedBackprop(pretrained_model)
    # Get gradients
    guided_grads = GBP.generate_gradients(prep_img, target_class, cnn_layer,
                                          filter_pos)
    # Save colored gradients
    save_gradient_images(guided_grads,
                         file_name_to_export + '_Guided_BP_color')
    # Convert to grayscale
    grayscale_guided_grads = convert_to_grayscale(guided_grads)
    # Save grayscale gradients
    save_gradient_images(grayscale_guided_grads,
                         file_name_to_export + '_Guided_BP_gray')
    # Positive and negative saliency maps
    pos_sal, neg_sal = get_positive_negative_saliency(guided_grads)
    save_gradient_images(pos_sal, file_name_to_export + '_pos_sal')
    save_gradient_images(neg_sal, file_name_to_export + '_neg_sal')
    print('Layer Guided backprop completed')
    vbp = VanillaBackprop(model)
    vanilla_grads = vbp.generate_gradients(prep_img, target_cls)
    grad_times_image = vanilla_grads[0] * prep_img.detach().numpy()[0]
    # Convert to grayscale
    grayscale_vanilla_grads = convert_to_grayscale(grad_times_image)
    # Save grayscale gradients
    file_utils.makesure_path(dst)
    filename = os.path.join(dst, desc + '_Vanilla_grad_times_image_gray.jpg')

    save_gradient_images(grayscale_vanilla_grads, filename)


if __name__ == '__main__':
    # Get params
    target_example = 0  # Snake
    (original_image, prep_img, target_class, file_name_to_export, pretrained_model) =\
        get_example_params(target_example)
    # Vanilla backprop
    VBP = VanillaBackprop(pretrained_model)
    # Generate gradients
    vanilla_grads = VBP.generate_gradients(prep_img, target_class)

    grad_times_image = vanilla_grads[0] * prep_img.detach().numpy()[0]
    # Convert to grayscale
    grayscale_vanilla_grads = convert_to_grayscale(grad_times_image)
    # Save grayscale gradients
    save_gradient_images(
        grayscale_vanilla_grads,
        file_name_to_export + '_Vanilla_grad_times_image_gray')
    print('Grad times image completed.')
    prep_img = misc.preprocess_image(origin_image)
    vbp = VanillaBackprop(model)
    # Generate gradients
    vanilla_grads = vbp.generate_gradients(prep_img, target_cls)
    # Save colored gradients
    file_utils.makesure_path(dst)
    filename = os.path.join(dst, desc + '_Vanilla_BP_color.png')
    save_gradient_images(vanilla_grads, filename)
    # Convert to grayscale
    grayscale_vanilla_grads = convert_to_grayscale(vanilla_grads)
    # Save grayscale gradients
    filename = os.path.join(dst, desc + '_Vanilla_BP_GRAY.png')
    save_gradient_images(grayscale_vanilla_grads, filename)

if __name__ == '__main__':
    # Get params
    target_example = 1  # Snake
    (original_image, prep_img, target_class, file_name_to_export, pretrained_model) =\
        get_example_params(target_example)
    # Vanilla backprop
    VBP = VanillaBackprop(pretrained_model)
    # Generate gradients
    vanilla_grads = VBP.generate_gradients(prep_img, target_class)
    # Save colored gradients
    save_gradient_images(vanilla_grads, file_name_to_export + '_Vanilla_BP_color')
    # Convert to grayscale
    grayscale_vanilla_grads = convert_to_grayscale(vanilla_grads)
    # Save grayscale gradients
    save_gradient_images(grayscale_vanilla_grads, file_name_to_export + '_Vanilla_BP_gray')
    print('Vanilla backprop completed')