Exemplo n.º 1
0
def compute_heatmap(gradcam, image_path, target_class):

    # Obtain normalized image
    normalized_image = get_normalized_image(image_path)

    # Obtain the image mask by applying gradcam
    mask = gradcam(normalized_image, target_index=target_class)

    # Obtain the result of merging the mask with the torch image
    heatmap = get_heatmap(mask)

    return heatmap
Exemplo n.º 2
0
def main():
    # Retrieve the model
    print(f'Loading weights for model: {model_name}...')
    model, layer_name = load_fine_trained(model_name, model_weights)

    print(f'Weights loaded!')

    # Instantiate the gradCAM class
    gradcam = GradCAM(model=model, \
                      target_layer=layer_name
                      )

    # load pickle
    data = pd.read_csv(experiment_data_fpath)
    images = data['images'].tolist()
    labels = data['labels'].apply(lambda x: x[1:-1].split(',')).tolist()
    categories = data['categories'].apply(
        lambda x: x[1:-1].split(',')).tolist()
    main_category_index = data['main_category_index'].tolist()

    for i, im_path in enumerate(images):
        label_set = labels[i]
        category_set = categories[i]
        # n = random.randint(0,1)
        n = int(main_category_index[i])
        label = int(label_set[n])

        category = category_set[n]
        second_category = category_set[1 - n]
        img_name = f'im_{i}_{category}_other_category_{second_category}'

        # get guided grad cam map and guided backprop
        """ Guided Back Propagation """
        # Instantiate guided backpropagation to retrieve the mask
        guided_backpropagation = GuidedBackPropagation(model=model)

        # Obtain the initial image
        input_image = get_normalized_image(im_path)

        # Obtain the image mask by applying gradcam
        guided_backpropagation_mask = guided_backpropagation(
            input_image, label)

        # Normalize the guided back propagation mask
        guided_backpropagation_image = normalize(guided_backpropagation_mask)

        # Save the image
        save_image(
            guided_backpropagation_image,
            f'../results/experiment_5_1/{model_name}/{img_name}_guided_backpropagation.png'
        )
        """ Guided GradCAM"""
        # Reset the guided_backpropagation_mask of the input, so that the input_image tensor can be used again
        input_image.grad.zero_()

        # Obtain the gradcam mask
        mask = gradcam(input_image, target_index=label)

        # Merge the gradcam with the guided back propagation to get guided gradcam results
        guided_gradCAM = guided_backpropagation_mask * mask[..., np.newaxis]

        # Normalize the guided gradCAM results to the [1,255] range
        guided_gradCAM = normalize(guided_gradCAM)

        # save image
        save_image(
            guided_gradCAM,
            f'../results/experiment_5_1/{model_name}/{img_name}_guided_gradcam.png'
        )
Exemplo n.º 3
0
            print(f"....Skipping unusual layer...{module_name}...")
            continue

        # Print information!
        print(f"Investigating {model_name}'s {module_name} module")

        # Instantiate the gradCAM class
        gradcam = GradCAM(  model = model, \
                            target_layer = module
                            )

        # Obtain pre-normalized image
        image = get_image('../../datasets/test_images/cat_dog.png')

        # Obtain normalized image
        normalized_image = get_normalized_image(
            '../../datasets/test_images/cat_dog.png')

        # Obtain the image mask by applying gradcam
        mask = gradcam(normalized_image)

        # Obtain the result of merging the mask with the torch image
        heatmap = get_heatmap(mask)

        # Save the heatmap!
        save_image(
            heatmap,
            f'../../results/linear_probing/heatmap/cat_dog_heatmap_{model_name}_{module_name}.png'
        )

        # Merge the heatmap with
        cam = heatmap + np.float32(image)
    # Read the ground truth file to get list of images to process
    with open(ground_truth_bounding_boxes_path,'rb') as opened_file:
        ground_truth_bounding_boxes = pickle.load(opened_file)

    # Get the list of images to iterate through
    images = list(ground_truth_bounding_boxes.keys())

    # Iterate through every image and compute bounding boxes
    for image_identifier in tqdm(images):
        
        # Strip the .xml from the identifier
        image_identifier = image_identifier.strip('.xml')

        # Define the image path
        image_path = VOC2007_images_directory + image_identifier + '.jpg'

        # Obtain normalized image
        normalized_image = get_normalized_image(image_path)

        # Obtain the image mask by applying gradcam
        mask = gradcam(normalized_image)

        # Obtain the result of merging the mask with the torch image
        heatmap = get_heatmap(mask)

        # Save the heatmap!
        save_image(heatmap, f'../../../results/experiment_4_1/heatmaps/{image_identifier}.png')


                
def main():
    # Retrieve the model
    print(f'Loading weights for model: {model_name}...')
    model, layer_name = load_fine_trained(model_name, model_weights)

    print(f'Weights loaded!')

    # Instantiate the gradCAM class
    gradcam = GradCAM(model=model, \
                      target_layer=layer_name
                      )

    # load pickle
    data = pd.read_csv(experiment_data_fpath)
    images = data['images'].tolist()
    labels = data['labels'].tolist()
    categories = data['categories'].tolist()


    for i, im_path in enumerate(images):
        label = int(labels[i])
        category = categories[i]

        img_name = f'im_{i}_{category}'
        n = random.randint(0, 1) # if 0, guided back prop is model A and g-grad cam is model B, else guided grad cam is model A and guided back prop is model B
        # get guided grad cam map and guided backprop
        """ Guided Back Propagation """
        # Instantiate guided backpropagation to retrieve the mask
        guided_backpropagation = GuidedBackPropagation(model=model)

        # Obtain the initial image
        input_image = get_normalized_image(im_path)

        # Obtain the image mask by applying gradcam
        guided_backpropagation_mask = guided_backpropagation(input_image, label)

        # Normalize the guided back propagation mask
        guided_backpropagation_image = normalize(guided_backpropagation_mask)

        # Save the image
        img_dir = f'../results/experiment_5_2/{img_name}/guided_back_prop'
        Path(img_dir).mkdir(parents=True, exist_ok=True)

        save_image(guided_backpropagation_image, f'{img_dir}/{model_name}.png')

        """ Guided GradCAM"""
        # Reset the guided_backpropagation_mask of the input, so that the input_image tensor can be used again
        input_image.grad.zero_()

        # Obtain the gradcam mask
        mask = gradcam(input_image, target_index=label)

        # Merge the gradcam with the guided back propagation to get guided gradcam results
        guided_gradCAM = guided_backpropagation_mask * mask[..., np.newaxis]

        # Normalize the guided gradCAM results to the [1,255] range
        guided_gradCAM = normalize(guided_gradCAM)

        # save image
        img_dir = f'../results/experiment_5_2/{img_name}/guided_gradcam'
        Path(img_dir).mkdir(parents=True, exist_ok=True)

        save_image(guided_gradCAM, f'{img_dir}/{model_name}.png')