Esempio n. 1
0
    if arg.beta[0] <= 0 or arg.beta[1] <= 0:
        raise argparse.ArgumentTypeError("betas <= 0")


if __name__ == "__main__":
    arg = arg_parse()
    arg_check(arg)

    os.environ["CUDA_VISIBLE_DEVICES"] = arg.gpus
    torch_device = torch.device("cuda")

    train_path = "data/prostate/train/"
    val_path = "data/prostate/val/"
    test_path = "data/prostate/test/"

    preprocess = preprocess.get_preprocess(arg.augment)

    train_loader = Loader(train_path,
                          arg.batch_size,
                          transform=preprocess,
                          sampler='',
                          torch_type='float',
                          cpus=4,
                          shuffle=True,
                          drop_last=True)
    val_loader = Loader(val_path,
                        arg.batch_size,
                        transform=preprocess,
                        sampler=arg.sampler,
                        torch_type=arg.dtype,
                        cpus=arg.cpus,
Esempio n. 2
0
    #image_paths = image_paths
    print("Evaluating on {} images.".format(len(image_paths)))
    num_batches = int(len(image_paths) / batch_size)

    model = utils.load_model('vgg16').cuda()

    results = {}

    for batch_no in range(num_batches):
        print("Batch {} of {}".format(batch_no + 1, num_batches))
        image_batch = image_paths[batch_no * batch_size:(batch_no + 1) *
                                  batch_size]
        raw_imgs = [viz.pil_loader(image_path) for image_path in image_batch]
        # make sure preprocessing is correct
        inputs = [
            get_preprocess('vgg16', 'pattern_vanilla_grad')(raw_img)
            for raw_img in raw_imgs
        ]
        inputs = torch.stack(inputs).cuda()
        inputs = utils.cuda_var(inputs, requires_grad=True)

        diff_sum = 0

        with torch.cuda.device(0):
            torch.cuda.empty_cache()
            model = utils.load_model('vgg16').cuda()
            explainer = get_explainer(model, 'vanilla_grad')

            out = torch.softmax(model(inputs.clone()), axis=-1)
            classes = torch.max(out, dim=1)[1]
            out = out.detach().cpu().numpy()
Esempio n. 3
0
            # idx = torch.randperm(layer.nelement())
            # layer = layer.view(-1)[idx].view(layer.size())

            # reset randomization method
            model.load_state_dict(state_dict)


model_methods = [
    #['googlenet', 'vanilla_grad',       'camshow'],
    #['vgg16', 'grad_x_input',       'camshow'],
    #['vgg16', 'saliency',           'camshow'],
    #['vgg16', 'integrate_grad',     'camshow'],
    #['vgg16', 'deconv',             'camshow'],
    #['vgg16', 'guided_backprop',    'camshow'],
    ['vgg16',     'pattern_net',        'camshow'],
    ['vgg16',     'pattern_lrp',        'camshow'],
    ['vgg16',     'smooth_grad',        'camshow'],
    ['vgg16', 'deeplift_rescale', 'camshow']]

if __name__ == '__main__':
    for m in model_methods:
        method = m[1]
        image_path = '../images/bee.jpg'
        image_class = 309
        image_class_name = 'bee'
        raw_img = viz.pil_loader(image_path)
        img_input = get_preprocess('vgg16', method)(raw_img)
        img_input = utils.cuda_var(img_input.unsqueeze(0), requires_grad=True)
        target = torch.LongTensor([image_class])

        cascading_parameter_randomization(method, False, img_input, target)
    arg_check(arg)

    os.environ["CUDA_VISIBLE_DEVICES"] = arg.gpus
    torch_device = torch.device("cuda")

    data_path = "/data/00_Nuclues_segmentation/00_data/"
    if arg.in_channel == 1:
        data_path += "/2D/New(50_Cells)"
    elif arg.in_channel > 1 and arg.out_channel == 1:
        data_path += "/25D/%dchannel" % (arg.in_channel)

    train_path = data_path + "/%s/Train/" % (arg.data)
    valid_path = data_path + "/%s/Val/" % (arg.data)
    test_path = data_path + "/All/Test/"

    preprocess = preprocess.get_preprocess(arg)

    train_loader = NucleusLoader(train_path,
                                 arg.batch_size,
                                 transform=preprocess,
                                 sampler=arg.sampler,
                                 channel=arg.in_channel,
                                 torch_type=arg.dtype,
                                 cpus=arg.cpus,
                                 shuffle=True,
                                 drop_last=True)
    valid_loader = NucleusLoader(valid_path,
                                 arg.batch_size,
                                 transform=preprocess,
                                 sampler=arg.sampler,
                                 channel=arg.in_channel,
Esempio n. 5
0
# Take the sample image, and display it (original form)
image_path = "models/test_" + displayed_class + "_images/" + displayed_class + str(
    number_image) + ".jpg"

raw_img = viz.pil_loader(image_path)
plt.figure(figsize=(5, 5))
plt.imshow(raw_img)
plt.axis('off')
plt.title(displayed_class)

# Now, we want to display the saliency maps of this image, for every model_method element
all_saliency_maps = []

for model_name, method_name, _ in model_methods:
    # Get a specific picture transformation (see torchvision.transforms documentation)
    transf = get_preprocess(model_name, method_name)
    # Load the pretrained model
    model = utils.load_model(model_name)
    model.cuda()
    # Get the explainer
    explainer = get_explainer(model, method_name)

    # Transform the image
    inp = transf(raw_img)
    if method_name == 'googlenet':  # swap channel due to caffe weights
        inp_copy = inp.clone()
        inp[0] = inp_copy[2]
        inp[2] = inp_copy[0]
    inp = utils.cuda_var(inp.unsqueeze(0), requires_grad=True)

    target = torch.LongTensor([image_class]).cuda()
Esempio n. 6
0
def compute_saliency_map(model_name, displayed_class, number_image):
    model_methods = [
        [model_name, 'vanilla_grad', 'imshow'],
        [model_name, 'grad_x_input', 'imshow'],
        [model_name, 'saliency', 'imshow'],
        [model_name, 'integrate_grad', 'imshow'],
        [model_name, 'deconv', 'imshow'],
        [model_name, 'guided_backprop', 'imshow'],
        #[model_name, 'gradcam', 'camshow'],
        #[model_name, 'excitation_backprop', 'camshow'],
        #[model_name, 'contrastive_excitation_backprop', 'camshow']
    ]
    # Change 'image_class' to 0 if you want to display for a dog
    if (displayed_class == "dog"):
        image_class = 0
    elif (displayed_class == "cat"):
        image_class = 1
    else:
        print("ERROR: wrong displayed class")

    # Take the sample image, and display it (original form)
    image_path = "models/test_" + displayed_class + "_images/" + str(
        number_image)

    raw_img = viz.pil_loader(image_path)
    plt.figure(figsize=(5, 5))
    plt.imshow(raw_img)
    plt.axis('off')
    plt.title(displayed_class)

    # Now, we want to display the saliency maps of this image, for every model_method element
    all_saliency_maps = []

    for model_name, method_name, _ in model_methods:
        # Get a specific picture transformation (see torchvision.transforms documentation)
        transf = get_preprocess(model_name, method_name)
        # Load the pretrained model
        model = utils.load_model(model_name)
        model.cuda()
        # Get the explainer
        explainer = get_explainer(model, method_name)

        # Transform the image
        inp = transf(raw_img)
        if method_name == 'googlenet':  # swap channel due to caffe weights
            inp_copy = inp.clone()
            inp[0] = inp_copy[2]
            inp[2] = inp_copy[0]
        inp = utils.cuda_var(inp.unsqueeze(0), requires_grad=True)

        target = torch.LongTensor([image_class]).cuda()
        saliency = explainer.explain(inp, target)
        saliency = utils.upsample(saliency, (raw_img.height, raw_img.width))
        #all_saliency_maps.append(saliency.cpy().numpy())
        all_saliency_maps.append(saliency.cpu().numpy())

    # Display all the results
    plt.figure(figsize=(25, 15))
    plt.subplot(3, 5, 1)
    plt.imshow(raw_img)
    plt.axis('off')
    plt.title(displayed_class)
    for i, (saliency,
            (model_name, method_name,
             show_style)) in enumerate(zip(all_saliency_maps, model_methods)):
        plt.subplot(3, 5, i + 2 + i // 4)
        if show_style == 'camshow':
            viz.plot_cam(np.abs(saliency).max(axis=1).squeeze(),
                         raw_img,
                         'jet',
                         alpha=0.5)
        else:
            if model_name == 'googlenet' or method_name == 'pattern_net':
                saliency = saliency.squeeze()[::-1].transpose(1, 2, 0)
            else:
                saliency = saliency.squeeze().transpose(1, 2, 0)
            saliency -= saliency.min()
            saliency /= (saliency.max() + 1e-20)
            plt.imshow(saliency, cmap='gray')

        plt.axis('off')
        if method_name == 'excitation_backprop':
            plt.title('Exc_bp')
        elif method_name == 'contrastive_excitation_backprop':
            plt.title('CExc_bp')
        else:
            plt.title('%s' % (method_name))

    plt.tight_layout()

    if not os.path.exists('images/' + model_name + '/'):
        os.makedirs('images/' + model_name + '/')
    save_destination = 'images/' + model_name + '/' + str(
        number_image[:-4]) + '_saliency.png'

    plt.savefig(save_destination)
    plt.clf()
Esempio n. 7
0
                        help='Number of validation samples to run')
    parser.add_argument('--n_patches', type=int, default=100,
                        help='Number of patches to degrade')
    parser.add_argument('--imagenet_download_key', default=None,
                        help='Optional URL string for imagenet download')
    args = parser.parse_args()

    methods = args.methods
    batch_size = args.batch_size
    data_dir = args.data_dir
    val_size = args.val_size
    n_patches = args.n_patches
    imagenet_download_key = args.imagenet_download_key

    # Load data
    input_transform = get_preprocess('vgg16', 'pattern_vanilla_grad')
    imagenet_data = get_imagenet_dataloader(data_dir, input_transform, val_size, batch_size, imagenet_download_key)

    num_batches = len(imagenet_data)
    print("Evaluating on {} images.".format(val_size))

    model = utils.load_model('vgg16').cuda()

    results = {}

    for batch_idx, batch in enumerate(imagenet_data):
        t = time.time()
        print("Batch {} of {}".format(batch_idx+1, num_batches))

        for m in methods:
            print("Method: {}".format(m))