Spaces:
Runtime error
Runtime error
Trying to implement actual model code
Browse files
app.py
CHANGED
|
@@ -1,11 +1,88 @@
|
|
| 1 |
import numpy as np
|
| 2 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
def image_mod(image):
|
| 5 |
return image.rotate(45)
|
| 6 |
|
| 7 |
-
inp = gr.inputs.Image(label="Input Image", type="pil")
|
| 8 |
-
out = gr.outputs.Image()
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import numpy as np
|
| 2 |
import gradio as gr
|
| 3 |
+
"""An example of generating a gif explanation for an image of my dog."""
|
| 4 |
+
import argparse
|
| 5 |
+
import os
|
| 6 |
+
from os.path import exists, dirname
|
| 7 |
+
import sys
|
| 8 |
+
|
| 9 |
+
parent_dir = dirname(os.path.abspath(os.getcwd()))
|
| 10 |
+
sys.path.append(parent_dir)
|
| 11 |
+
|
| 12 |
+
from bayes.explanations import BayesLocalExplanations, explain_many
|
| 13 |
+
from bayes.data_routines import get_dataset_by_name
|
| 14 |
+
from bayes.models import *
|
| 15 |
+
from image_posterior import create_gif
|
| 16 |
+
|
| 17 |
+
parser = argparse.ArgumentParser()
|
| 18 |
+
parser.add_argument("--cred_width", type=float, default=0.1)
|
| 19 |
+
parser.add_argument("--save_loc", type=str, required=True)
|
| 20 |
+
parser.add_argument("--n_top_segs", type=int, default=5)
|
| 21 |
+
parser.add_argument("--n_gif_images", type=int, default=20)
|
| 22 |
+
|
| 23 |
+
IMAGE_NAME = "imagenet_diego"
|
| 24 |
+
BLENHEIM_SPANIEL_CLASS = 156
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
def get_image_data():
|
| 28 |
+
"""Gets the image data and model."""
|
| 29 |
+
puppy_image = get_dataset_by_name(IMAGE_NAME, get_label=False)
|
| 30 |
+
model_and_data = process_imagenet_get_model(puppy_image)
|
| 31 |
+
return puppy_image, model_and_data
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
def segmentation_generation():
|
| 35 |
+
cred_width = 0.1
|
| 36 |
+
n_top_segs = 5
|
| 37 |
+
n_gif_images = 20
|
| 38 |
+
puppy_image, model_and_data = get_image_data()
|
| 39 |
+
|
| 40 |
+
# Unpack datax
|
| 41 |
+
xtest = model_and_data["xtest"]
|
| 42 |
+
ytest = model_and_data["ytest"]
|
| 43 |
+
segs = model_and_data["xtest_segs"]
|
| 44 |
+
get_model = model_and_data["model"]
|
| 45 |
+
label = model_and_data["label"]
|
| 46 |
+
|
| 47 |
+
# Unpack instance and segments
|
| 48 |
+
instance = xtest[0]
|
| 49 |
+
segments = segs[0]
|
| 50 |
+
|
| 51 |
+
# Get wrapped model
|
| 52 |
+
cur_model = get_model(instance, segments)
|
| 53 |
+
|
| 54 |
+
# Get background data
|
| 55 |
+
xtrain = get_xtrain(segments)
|
| 56 |
+
|
| 57 |
+
prediction = np.argmax(cur_model(xtrain[:1]), axis=1)
|
| 58 |
+
assert prediction == BLENHEIM_SPANIEL_CLASS, f"Prediction is {prediction} not {BLENHEIM_SPANIEL_CLASS}"
|
| 59 |
+
|
| 60 |
+
# Compute explanation
|
| 61 |
+
exp_init = BayesLocalExplanations(training_data=xtrain,
|
| 62 |
+
data="image",
|
| 63 |
+
kernel="lime",
|
| 64 |
+
categorical_features=np.arange(xtrain.shape[1]),
|
| 65 |
+
verbose=True)
|
| 66 |
+
rout = exp_init.explain(classifier_f=cur_model,
|
| 67 |
+
data=np.ones_like(xtrain[0]),
|
| 68 |
+
label=BLENHEIM_SPANIEL_CLASS,
|
| 69 |
+
cred_width=cred_width,
|
| 70 |
+
focus_sample=False,
|
| 71 |
+
l2=False)
|
| 72 |
+
|
| 73 |
+
# Create the gif of the explanation
|
| 74 |
+
return create_gif(rout['blr'], segments, instance, n_gif_images, n_top_segs)
|
| 75 |
|
| 76 |
def image_mod(image):
|
| 77 |
return image.rotate(45)
|
| 78 |
|
|
|
|
|
|
|
| 79 |
|
| 80 |
+
if __name__ == "__main__":
|
| 81 |
+
args = parser.parse_args()
|
| 82 |
+
# main(args)
|
| 83 |
+
|
| 84 |
+
inp = gr.inputs.Image(label="Input Image", type="pil")
|
| 85 |
+
out = gr.outputs.Image()
|
| 86 |
+
|
| 87 |
+
iface = gr.Interface(segmentation_generation, inputs=inp, outputs=out, examples=[["./imagenet_diego.png"]])
|
| 88 |
+
iface.launch()
|