예제 #1
0
def gen_gcam(imgs,
             model,
             target_layer='layer4',
             target_index=1,
             classes=get_imagenet_classes(),
             device='cuda',
             prep=True):
    """
    Visualize model responses given multiple images
    """

    # Get model and forward pass
    gcam, probs, ids, images = gen_model_forward(imgs,
                                                 model,
                                                 device=device,
                                                 prep=prep,
                                                 type='gcam')

    for i in range(target_index):
        # Grad-CAM
        gcam.backward(ids=ids[:, [i]])
        regions = gcam.generate(target_layer=target_layer)
        masks = []
        for j in range(len(images)):
            print("\t#{}: {} ({:.5f})".format(j, classes[ids[j, i]], probs[j,
                                                                           i]))

            # Grad-CAM
            mask = save_gradcam(gcam=regions[j, 0])
            masks += [mask]
    if len(masks) == 1:
        return masks[0]
    gcam.remove_hook()
    return masks
예제 #2
0
def gen_grounding_lime_batch(imgs,
                             model='resnet18',
                             label_name='explanation',
                             from_saved=True,
                             target_index=1,
                             layer='layer4',
                             device=0,
                             topk=True,
                             classes=get_imagenet_classes(),
                             save=True,
                             save_path='./results/gradcam_examples/',
                             show=True):
    #CUDA_VISIBLE_DEVICES=str(device)
    # Create result directory if it doesn't exist; all explanations should
    # be stored in a folder that is the predicted class

    if not os.path.exists(save_path):
        os.makedirs(save_path)

    if save:
        print('result path: {0}'.format(save_path))

    if isinstance(model, str):
        model_name = model
        model, classes, target_layer = get_model_info(model, device=device)
    else:
        model_name = 'custom'

    # Generate the explanations
    masks = generate_lime_explanation_batch(imgs,
                                            model,
                                            pred_rank=1,
                                            positive_only=True,
                                            show=show,
                                            device='cuda:' + str(device))

    cams = []
    for mask, img in zip(masks, imgs):
        cams += [get_cam(img, mask)]

    if save:
        for i in range(len(imgs)):
            res_path = save_path + str(target_index[i].cpu().numpy()) + '/'
            if not os.path.exists(res_path):
                os.makedirs(res_path)
            #print("saving explanation mask....\n")
            cv2.imwrite(res_path + 'original_img.png', get_displ_img(imgs[i]))
            np.save(res_path + "lime_mask.npy", masks[i])

    #just in case
    torch.cuda.empty_cache()

    return masks
예제 #3
0
def gen_grounding_bp_batch(imgs,
                           model='resnet18',
                           label_name='explanation',
                           from_saved=True,
                           target_index=1,
                           layer='layer4',
                           device=0,
                           topk=True,
                           classes=get_imagenet_classes(),
                           save=True,
                           save_path='./results/gradcam_examples/',
                           show=True):
    #CUDA_VISIBLE_DEVICES=str(device)
    # Create result directory if it doesn't exist; all explanations should
    # be stored in a folder that is the predicted class
    dateTimeObj = datetime.now()
    timestampStr = dateTimeObj.strftime("%d-%b-%Y_%H")
    if not os.path.exists(save_path):
        os.makedirs(save_path)

    if save:
        print('result path: {0}'.format(save_path))

    if isinstance(model, str):
        model_name = model
        model, classes, target_layer = get_model_info(model, device=device)
    else:
        model_name = 'custom'

    # Generate the explanations
    if topk:
        masks = gen_bp(imgs,
                       model,
                       target_index=target_index,
                       target_layer=layer,
                       device=device,
                       single=False,
                       prep=False,
                       classes=classes)
    else:
        masks = gen_bp_target(imgs,
                              model,
                              target_index=target_index,
                              device=device,
                              single=False,
                              prep=False,
                              classes=classes)

    cams = []
    for mask, img in zip(masks, imgs):
        cams += [get_cam(img, mask)]

    if show:
        #plot heatmaps
        fig = plt.figure(figsize=(10, 10))
        grid = ImageGrid(
            fig,
            111,  # similar to subplot(111)
            nrows_ncols=(2, 2),
            axes_pad=0.35,  # pad between axes in inch.
        )

        for ax, im in zip(grid, cams[:4]):
            ax.axis('off')
            # Iterating over the grid returns the Axes.
            ax.imshow(im)

    if save:
        for i in range(len(imgs)):
            res_path = save_path + str(target_index[i].cpu().numpy()) + '/'
            if not os.path.exists(res_path):
                os.makedirs(res_path)
            #print("saving explanation mask....\n")
            cv2.imwrite(res_path + 'original_img.png', get_displ_img(imgs[i]))
            cv2.imwrite(res_path + "bp_mask.png", np.uint8(cams[i] * 255))
            np.save(res_path + "bp_mask.npy", masks[i])

    #just in case
    torch.cuda.empty_cache()

    return masks
예제 #4
0
                '/work/lisabdunlap/explain-eval/data/imagenet_class_index.json',
                'r') as f:
            labels = json.load(f)
        for key in labels:
            index, label = labels[key]
            if index == target:
                return label, key

    preprocess = transforms.Compose([
        transforms.Resize((224, 224)),
        transforms.ToTensor(),
        # Normalization for ImageNet
        transforms.Normalize(mean=[0.485, 0.456, 0.406],
                             std=[0.229, 0.224, 0.225]),
    ])
    classes = get_imagenet_classes()

    dataset = datasets.ImageFolder(datadir, preprocess)
    model = models.vgg19(pretrained=True)
    indicies = [800, 1300, 2000, 2600, 3200, 3800, 4500, 5100, 5500, 6200]
    names = [121, 207, 249, 251, 275, 291, 310, 359, 454, 519]
    #indicies = [0, 800, 1300, 3200, 5100, 5500, 6200]
    #names = [111, 121, 207, 275, 359, 454, 519]
    #indicies = [5100, 5500, 6200]
    #names = [359, 454, 519]
    for start, label in zip(indicies, names):
        print('---------------- ' + str(label) + ' ----------------------')
        for i in range(50):
            img, _ = dataset[start + i]
            patch, idx, adv_idx = gen_adversarial_patch(
                img,