CoolFace
Apppublic

ucinlp/Modeling-Uncertainty-in-Explainability

sourceHugging Faceupdated 4y agoView on Hugging Face
0likes
image_posterior.py103 linesDownload Raw Back to root
1"""Create a gif sampling from the posterior from an image.2 3The file includes routines to create gifs of posterior samples for image4explanations. To create the gif, we sample a number of draws from the posterior,5plot the explanation and the image, and repeat this to stitch together a gif.6 7The interpretation is that regions of the image that more frequency show up as8green are more likely to positively impact the prediction. Similarly, regions that 9more frequently show up as red are more likey to negatively impact the prediction.10"""11import os12from os.path import exists, dirname13import sys14 15import imageio16import matplotlib.pyplot as plt17import numpy as np18from skimage.segmentation import mark_boundaries19import tempfile20from tqdm import tqdm21import ffmpeg22import lime.lime_tabular as baseline_lime_tabular23import shap24import shutil25import json26 27# Make sure we can get bayes explanations28parent_dir = dirname(os.path.abspath(os.getcwd()))29sys.path.append(parent_dir)30 31from bayes.explanations import BayesLocalExplanations, explain_many32from bayes.data_routines import get_dataset_by_name33from bayes.models import *34 35labels_dict = {}36with open("labels.json") as file:37    labels_dict = json.load(file)38 39 40def fill_segmentation(values, segmentation, image, n_max=5):41    max_segs = np.argsort(abs(values))[-n_max:]42    out = np.zeros((224, 224))43    c_image = np.zeros(image.shape)44    for i in range(len(values)):45        if i in max_segs:46            out[segmentation == i] = 1 if values[i] > 0 else -147            c = 1 if values[i] > 0 else 048            c_image[segmentation == i, c] = np.max(image)49    return c_image.astype(int), out.astype(int)50 51def create_gif(explanation_blr, img_name, segments, image, prediction, n_images=20, n_max=5):52    """Create the gif corresponding to the image explanation.53 54    Arguments:55        explanation_coefficients: The explanation blr object.56        segments: The image segmentation.57        image: The image for which to compute the explantion.58        save_loc: The location to save the gif.59        n_images: Number of images to create the gif with.60        n_max: The number of superpixels to draw on the image.61    """62    draws = explanation_blr.draw_posterior_samples(n_images)63    # remove any existing files64    temp_path = tempfile.TemporaryDirectory().name65    for root, dirs, files in os.walk(temp_path):66        for f in files:67            os.unlink(os.path.join(root, f))68        for d in dirs:69            shutil.rmtree(os.path.join(root, d))70 71    # Setup temporary directory to store paths in 72    with tempfile.TemporaryDirectory() as tmpdirname:73        paths = []74        for i, d in tqdm(enumerate(draws)):75            c_image, filled_segs = fill_segmentation(d, segments, image, n_max=n_max)76            plt.cla()77            plt.axis('off')78            plt.imshow(mark_boundaries(image, filled_segs))79            plt.imshow(c_image, alpha=0.3)80            paths.append(os.path.join(tmpdirname, f"{i}.png"))81            plt.savefig(paths[-1])82    83        # Save to gif84        # https://stackoverflow.com/questions/61716066/creating-an-animation-out-of-matplotlib-pngs85        print(f"Saving gif to {str(prediction)}_explanation.gif")86 87        if(os.path.exists(f'{str(prediction)}_explanation.gif')):88            os.remove(f'{str(prediction)}_explanation.gif')89 90        ims = [imageio.imread(f) for f in paths]91        imageio.mimwrite(f'{str(prediction)}_explanation.gif', ims)92 93    html = (94        "<div>"95        + f"<img  src='file/{str(prediction)}_explanation.gif' alt='explanation gif'/>"96        + "</div>"97    )98 99    return html, f"Predction was {prediction}: {labels_dict[str(prediction)]}"100 101        102 103