Spaces:
Runtime error
Runtime error
cleaning up hardcoded aspects of code
Browse files- app.py +13 -32
- image_posterior.py +11 -16
app.py
CHANGED
|
@@ -16,43 +16,25 @@ from bayes.data_routines import get_dataset_by_name
|
|
| 16 |
from bayes.models import *
|
| 17 |
from image_posterior import create_gif
|
| 18 |
|
| 19 |
-
parser = argparse.ArgumentParser()
|
| 20 |
-
parser.add_argument("--cred_width", type=float, default=0.1)
|
| 21 |
-
parser.add_argument("--save_loc", type=str, required=True)
|
| 22 |
-
parser.add_argument("--n_top_segs", type=int, default=5)
|
| 23 |
-
parser.add_argument("--n_gif_images", type=int, default=20)
|
| 24 |
-
|
| 25 |
-
# app = flask.Flask(__name__, template_folder="./")
|
| 26 |
-
|
| 27 |
-
IMAGE_NAME = "imagenet_diego"
|
| 28 |
BLENHEIM_SPANIEL_CLASS = 156
|
| 29 |
|
| 30 |
|
| 31 |
-
def get_image_data():
|
| 32 |
"""Gets the image data and model."""
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
return
|
| 38 |
|
| 39 |
|
| 40 |
def segmentation_generation(image_name, c_width, n_top, n_gif_imgs):
|
| 41 |
print("GRADIO INPUTS:", image_name, c_width, n_top, n_gif_imgs)
|
| 42 |
|
| 43 |
-
# html = "<div style=\"background-image: url(./imagenet_diego.png); height: 400px; width: 400px;\"></div>"
|
| 44 |
-
|
| 45 |
-
html = (
|
| 46 |
-
"<div >"
|
| 47 |
-
"<img src='file/diego.gif' alt='picture of dog'/>"
|
| 48 |
-
+ "</div>"
|
| 49 |
-
)
|
| 50 |
-
return html
|
| 51 |
-
|
| 52 |
cred_width = c_width
|
| 53 |
n_top_segs = n_top
|
| 54 |
n_gif_images = n_gif_imgs
|
| 55 |
-
|
| 56 |
|
| 57 |
# Unpack datax
|
| 58 |
xtest = model_and_data["xtest"]
|
|
@@ -60,6 +42,7 @@ def segmentation_generation(image_name, c_width, n_top, n_gif_imgs):
|
|
| 60 |
segs = model_and_data["xtest_segs"]
|
| 61 |
get_model = model_and_data["model"]
|
| 62 |
label = model_and_data["label"]
|
|
|
|
| 63 |
|
| 64 |
# Unpack instance and segments
|
| 65 |
instance = xtest[0]
|
|
@@ -88,7 +71,7 @@ def segmentation_generation(image_name, c_width, n_top, n_gif_imgs):
|
|
| 88 |
l2=False)
|
| 89 |
|
| 90 |
# Create the gif of the explanation
|
| 91 |
-
return create_gif(rout['blr'], segments, instance, n_gif_images, n_top_segs)
|
| 92 |
|
| 93 |
def image_mod(image):
|
| 94 |
return image.rotate(45)
|
|
@@ -96,8 +79,8 @@ def image_mod(image):
|
|
| 96 |
if __name__ == "__main__":
|
| 97 |
# gradio's image inputs look like this: <PIL.Image.Image image mode=RGB size=305x266 at 0x7F3D01C91FA0>
|
| 98 |
# need to learn how to handle image inputs, or deal with file inputs or just file path strings
|
| 99 |
-
inp = gr.inputs.Textbox(lines=1, placeholder="
|
| 100 |
-
out = gr.outputs.HTML(label="Output
|
| 101 |
|
| 102 |
iface = gr.Interface(
|
| 103 |
segmentation_generation,
|
|
@@ -108,8 +91,6 @@ if __name__ == "__main__":
|
|
| 108 |
gr.inputs.Slider(minimum=10, maximum=50, step=1, default=20, label="n_gif_images", optional=False),
|
| 109 |
],
|
| 110 |
outputs=out,
|
| 111 |
-
examples=[["
|
| 112 |
)
|
| 113 |
-
iface.launch(enable_queue
|
| 114 |
-
|
| 115 |
-
# app.run(host='0.0.0.0', port=int(os.environ.get('PORT', 7860)))
|
|
|
|
| 16 |
from bayes.models import *
|
| 17 |
from image_posterior import create_gif
|
| 18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
BLENHEIM_SPANIEL_CLASS = 156
|
| 20 |
|
| 21 |
|
| 22 |
+
def get_image_data(image_name):
|
| 23 |
"""Gets the image data and model."""
|
| 24 |
+
if (image_name == "imagenet_diego.png"):
|
| 25 |
+
image = get_dataset_by_name("imagenet_diego", get_label=False)
|
| 26 |
+
model_and_data = process_imagenet_get_model(image)
|
| 27 |
+
|
| 28 |
+
return image, model_and_data
|
| 29 |
|
| 30 |
|
| 31 |
def segmentation_generation(image_name, c_width, n_top, n_gif_imgs):
|
| 32 |
print("GRADIO INPUTS:", image_name, c_width, n_top, n_gif_imgs)
|
| 33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
cred_width = c_width
|
| 35 |
n_top_segs = n_top
|
| 36 |
n_gif_images = n_gif_imgs
|
| 37 |
+
image, model_and_data = get_image_data(image_name)
|
| 38 |
|
| 39 |
# Unpack datax
|
| 40 |
xtest = model_and_data["xtest"]
|
|
|
|
| 42 |
segs = model_and_data["xtest_segs"]
|
| 43 |
get_model = model_and_data["model"]
|
| 44 |
label = model_and_data["label"]
|
| 45 |
+
print("LABEL:", label)
|
| 46 |
|
| 47 |
# Unpack instance and segments
|
| 48 |
instance = xtest[0]
|
|
|
|
| 71 |
l2=False)
|
| 72 |
|
| 73 |
# Create the gif of the explanation
|
| 74 |
+
return create_gif(rout['blr'], image_name, segments, instance, n_gif_images, n_top_segs)
|
| 75 |
|
| 76 |
def image_mod(image):
|
| 77 |
return image.rotate(45)
|
|
|
|
| 79 |
if __name__ == "__main__":
|
| 80 |
# gradio's image inputs look like this: <PIL.Image.Image image mode=RGB size=305x266 at 0x7F3D01C91FA0>
|
| 81 |
# need to learn how to handle image inputs, or deal with file inputs or just file path strings
|
| 82 |
+
inp = gr.inputs.Textbox(lines=1, placeholder="Select an example from below", default="", label="Input Image Path", optional=False)
|
| 83 |
+
out = gr.outputs.HTML(label="Output GIF")
|
| 84 |
|
| 85 |
iface = gr.Interface(
|
| 86 |
segmentation_generation,
|
|
|
|
| 91 |
gr.inputs.Slider(minimum=10, maximum=50, step=1, default=20, label="n_gif_images", optional=False),
|
| 92 |
],
|
| 93 |
outputs=out,
|
| 94 |
+
examples=[["imagenet_diego.png", 0.01, 7, 50]]
|
| 95 |
)
|
| 96 |
+
iface.launch(show_error=True, enable_queue=True)
|
|
|
|
|
|
image_posterior.py
CHANGED
|
@@ -41,7 +41,7 @@ def fill_segmentation(values, segmentation, image, n_max=5):
|
|
| 41 |
c_image[segmentation == i, c] = np.max(image)
|
| 42 |
return c_image.astype(int), out.astype(int)
|
| 43 |
|
| 44 |
-
def create_gif(explanation_blr, segments, image, n_images=20, n_max=5):
|
| 45 |
"""Create the gif corresponding to the image explanation.
|
| 46 |
|
| 47 |
Arguments:
|
|
@@ -64,25 +64,20 @@ def create_gif(explanation_blr, segments, image, n_images=20, n_max=5):
|
|
| 64 |
plt.imshow(c_image, alpha=0.3)
|
| 65 |
paths.append(os.path.join(tmpdirname, f"{i}.png"))
|
| 66 |
plt.savefig(paths[-1])
|
| 67 |
-
print("CREATING
|
| 68 |
# Save to gif
|
| 69 |
# https://stackoverflow.com/questions/61716066/creating-an-animation-out-of-matplotlib-pngs
|
| 70 |
# print(f"Saving gif to {save_loc}")
|
| 71 |
|
| 72 |
ims = [imageio.imread(f) for f in paths]
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
|
| 79 |
-
html = '''
|
| 80 |
-
<div style='max-width:100%; max-height:360px; overflow:auto'>
|
| 81 |
-
<video width="320" height="240" autoplay>
|
| 82 |
-
<source src="./test.mp4" type=video/mp4>
|
| 83 |
-
</video>
|
| 84 |
-
</div>
|
| 85 |
-
)'''
|
| 86 |
-
return html
|
| 87 |
-
# return imageio.mimwrite(imageio.RETURN_BYTES, ims)
|
| 88 |
|
|
|
|
| 41 |
c_image[segmentation == i, c] = np.max(image)
|
| 42 |
return c_image.astype(int), out.astype(int)
|
| 43 |
|
| 44 |
+
def create_gif(explanation_blr, img_name, segments, image, n_images=20, n_max=5):
|
| 45 |
"""Create the gif corresponding to the image explanation.
|
| 46 |
|
| 47 |
Arguments:
|
|
|
|
| 64 |
plt.imshow(c_image, alpha=0.3)
|
| 65 |
paths.append(os.path.join(tmpdirname, f"{i}.png"))
|
| 66 |
plt.savefig(paths[-1])
|
| 67 |
+
print("CREATING GIF NOW")
|
| 68 |
# Save to gif
|
| 69 |
# https://stackoverflow.com/questions/61716066/creating-an-animation-out-of-matplotlib-pngs
|
| 70 |
# print(f"Saving gif to {save_loc}")
|
| 71 |
|
| 72 |
ims = [imageio.imread(f) for f in paths]
|
| 73 |
+
imageio.mimwrite(f'{img_name}_explanation.gif', ims)
|
| 74 |
+
|
| 75 |
+
html = (
|
| 76 |
+
"<div >"
|
| 77 |
+
f"<img src='file/{img_name}_explanation.gif' alt='explanation gif'/>"
|
| 78 |
+
+ "</div>"
|
| 79 |
+
)
|
| 80 |
+
return html
|
| 81 |
+
|
| 82 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
|