Spaces:
Sleeping
Sleeping
Normalize Range
Browse files
app.py
CHANGED
@@ -1,3 +1,6 @@
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
import numpy as np
|
3 |
import skimage.transform
|
@@ -7,8 +10,6 @@ import torchvision.transforms as transforms
|
|
7 |
from matplotlib import pyplot as plt
|
8 |
from numpy import matlib as mb
|
9 |
from PIL import Image
|
10 |
-
import csv
|
11 |
-
import sys
|
12 |
|
13 |
csv.field_size_limit(sys.maxsize)
|
14 |
|
@@ -85,24 +86,37 @@ def get_layer4(input_image):
|
|
85 |
return reference_layer4.data.to("cpu").numpy()
|
86 |
|
87 |
|
|
|
|
|
|
|
|
|
88 |
# Visualization
|
89 |
def visualize_similarities(image1, image2):
|
90 |
a = get_layer4(image1).squeeze()
|
91 |
b = get_layer4(image2).squeeze()
|
92 |
sim1, sim2 = compute_spatial_similarity(a, b)
|
93 |
|
|
|
|
|
|
|
94 |
fig, axes = plt.subplots(1, 2, figsize=(12, 5))
|
95 |
axes[0].imshow(display_transform(image1))
|
96 |
im1 = axes[0].imshow(
|
97 |
-
skimage.transform.resize(sim1, (224, 224)),
|
|
|
|
|
|
|
|
|
98 |
)
|
99 |
-
# axes[0].colorbar()
|
100 |
|
101 |
axes[1].imshow(display_transform(image2))
|
102 |
im2 = axes[1].imshow(
|
103 |
-
skimage.transform.resize(sim2, (224, 224)),
|
|
|
|
|
|
|
|
|
104 |
)
|
105 |
-
# axes[1].colorbar()
|
106 |
|
107 |
fig.colorbar(im1, ax=axes[0])
|
108 |
fig.colorbar(im2, ax=axes[1])
|
@@ -114,8 +128,8 @@ def visualize_similarities(image1, image2):
|
|
114 |
iface = gr.Interface(
|
115 |
fn=visualize_similarities,
|
116 |
inputs=[
|
117 |
-
gr.Image(type="pil"),
|
118 |
-
gr.Image(type="pil"),
|
119 |
],
|
120 |
allow_flagging="never",
|
121 |
outputs=[gr.Plot(type="matplotlib")],
|
|
|
1 |
+
import csv
|
2 |
+
import sys
|
3 |
+
|
4 |
import gradio as gr
|
5 |
import numpy as np
|
6 |
import skimage.transform
|
|
|
10 |
from matplotlib import pyplot as plt
|
11 |
from numpy import matlib as mb
|
12 |
from PIL import Image
|
|
|
|
|
13 |
|
14 |
csv.field_size_limit(sys.maxsize)
|
15 |
|
|
|
86 |
return reference_layer4.data.to("cpu").numpy()
|
87 |
|
88 |
|
89 |
+
def NormalizeData(data):
|
90 |
+
return (data - np.min(data)) / (np.max(data) - np.min(data))
|
91 |
+
|
92 |
+
|
93 |
# Visualization
|
94 |
def visualize_similarities(image1, image2):
|
95 |
a = get_layer4(image1).squeeze()
|
96 |
b = get_layer4(image2).squeeze()
|
97 |
sim1, sim2 = compute_spatial_similarity(a, b)
|
98 |
|
99 |
+
sim1 = NormalizeData(sim1)
|
100 |
+
sim2 = NormalizeData(sim2)
|
101 |
+
|
102 |
fig, axes = plt.subplots(1, 2, figsize=(12, 5))
|
103 |
axes[0].imshow(display_transform(image1))
|
104 |
im1 = axes[0].imshow(
|
105 |
+
skimage.transform.resize(sim1, (224, 224)),
|
106 |
+
alpha=0.5,
|
107 |
+
cmap="jet",
|
108 |
+
vmin=0,
|
109 |
+
vmax=1,
|
110 |
)
|
|
|
111 |
|
112 |
axes[1].imshow(display_transform(image2))
|
113 |
im2 = axes[1].imshow(
|
114 |
+
skimage.transform.resize(sim2, (224, 224)),
|
115 |
+
alpha=0.5,
|
116 |
+
cmap="jet",
|
117 |
+
vmin=0,
|
118 |
+
vmax=1,
|
119 |
)
|
|
|
120 |
|
121 |
fig.colorbar(im1, ax=axes[0])
|
122 |
fig.colorbar(im2, ax=axes[1])
|
|
|
128 |
iface = gr.Interface(
|
129 |
fn=visualize_similarities,
|
130 |
inputs=[
|
131 |
+
gr.inputs.Image(type="pil"),
|
132 |
+
gr.inputs.Image(type="pil"),
|
133 |
],
|
134 |
allow_flagging="never",
|
135 |
outputs=[gr.Plot(type="matplotlib")],
|