Spaces:
Runtime error
Runtime error
AdrianAlan
commited on
Commit
•
c691438
1
Parent(s):
6ce4cca
Update inference
Browse files- app.py +96 -48
- requirements.txt +6 -5
app.py
CHANGED
@@ -1,23 +1,14 @@
|
|
1 |
import gradio as gr
|
2 |
import numpy as np
|
|
|
3 |
import matplotlib.pyplot as plt
|
4 |
import tensorflow as tf
|
5 |
|
6 |
from huggingface_hub import from_pretrained_keras
|
7 |
-
from tensorflow.keras.models import Model
|
8 |
-
from tensorflow.keras.layers import Input, Flatten
|
9 |
-
from qkeras import *
|
10 |
-
|
11 |
-
|
12 |
-
def load_keras_model(model_path: str):
|
13 |
-
org_model = from_pretrained_keras(model_path)
|
14 |
-
input_ = Input(shape=(18, 14), name="inputs_")
|
15 |
-
x = Flatten()(input_)
|
16 |
-
for layer in org_model.layers[1:]:
|
17 |
-
x = layer(x)
|
18 |
-
output = Activation("linear", name="outputs")(x)
|
19 |
-
return Model(input_, output, name="cicada")
|
20 |
|
|
|
|
|
|
|
21 |
|
22 |
def parse_input(et):
|
23 |
if not et:
|
@@ -32,41 +23,82 @@ def parse_input(et):
|
|
32 |
if np.any(et < 0) or np.any(et > 1023):
|
33 |
raise gr.Error("The input has to be in a range (0, 1023)!")
|
34 |
|
35 |
-
return et.reshape(1,
|
36 |
-
|
37 |
-
|
38 |
-
def inference(input_):
|
39 |
-
input_ = parse_input(input_)
|
40 |
-
return model.predict(input_)[0][0]
|
41 |
|
42 |
|
43 |
-
def saliency(input_):
|
44 |
-
input_ = parse_input(input_)
|
45 |
x = tf.constant(input_)
|
46 |
with tf.GradientTape() as tape:
|
47 |
tape.watch(x)
|
48 |
-
|
|
|
|
|
|
|
49 |
gradient = tape.gradient(predictions, x)
|
50 |
gradient = gradient.numpy()
|
51 |
min_val, max_val = np.min(gradient), np.max(gradient)
|
52 |
gradient = (gradient - min_val) / (max_val - min_val + tf.keras.backend.epsilon())
|
53 |
|
54 |
-
fig_i = plt.figure()
|
55 |
-
plt.imshow(input_.reshape(18, 14), cmap="Reds")
|
56 |
-
plt.colorbar()
|
57 |
-
plt.axis("off")
|
58 |
-
plt.tight_layout()
|
59 |
-
|
60 |
fig_s = plt.figure()
|
61 |
-
plt.imshow(gradient.reshape(18, 14), cmap="Greys")
|
62 |
-
plt.
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
68 |
|
69 |
-
model = load_keras_model("cicada-project/cicada-v1.1")
|
70 |
|
71 |
with gr.Blocks() as demo:
|
72 |
with gr.Row():
|
@@ -77,18 +109,34 @@ with gr.Blocks() as demo:
|
|
77 |
placeholder="\n".join([",".join(["0"] * 14)] * 18),
|
78 |
)
|
79 |
with gr.Row():
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
label = gr.Number(label="CICADA Anomaly Score")
|
84 |
with gr.Column():
|
85 |
-
with gr.
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
93 |
|
94 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
import numpy as np
|
3 |
+
import mplhep as hep
|
4 |
import matplotlib.pyplot as plt
|
5 |
import tensorflow as tf
|
6 |
|
7 |
from huggingface_hub import from_pretrained_keras
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
|
9 |
+
model_v1 = from_pretrained_keras("cicada-project/cicada-v1.1")
|
10 |
+
model_v2 = from_pretrained_keras("cicada-project/cicada-v2.1")
|
11 |
+
hep.style.use("CMS")
|
12 |
|
13 |
def parse_input(et):
|
14 |
if not et:
|
|
|
23 |
if np.any(et < 0) or np.any(et > 1023):
|
24 |
raise gr.Error("The input has to be in a range (0, 1023)!")
|
25 |
|
26 |
+
return et.reshape(1, 252)
|
|
|
|
|
|
|
|
|
|
|
27 |
|
28 |
|
29 |
+
def saliency(input_, version):
|
|
|
30 |
x = tf.constant(input_)
|
31 |
with tf.GradientTape() as tape:
|
32 |
tape.watch(x)
|
33 |
+
if version == "v1":
|
34 |
+
predictions = model_v1(x)
|
35 |
+
elif version == "v2":
|
36 |
+
predictions = model_v2(x)
|
37 |
gradient = tape.gradient(predictions, x)
|
38 |
gradient = gradient.numpy()
|
39 |
min_val, max_val = np.min(gradient), np.max(gradient)
|
40 |
gradient = (gradient - min_val) / (max_val - min_val + tf.keras.backend.epsilon())
|
41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
fig_s = plt.figure()
|
43 |
+
im = plt.imshow(gradient.reshape(18, 14), vmin=0., vmax=1., cmap="Greys")
|
44 |
+
ax = plt.gca()
|
45 |
+
cbar = ax.figure.colorbar(im, ax=ax)
|
46 |
+
cbar.ax.set_ylabel(r"Calorimeter Saliency (a.u.)")
|
47 |
+
plt.xticks(np.arange(14), labels=np.arange(4, 18))
|
48 |
+
plt.yticks(
|
49 |
+
np.arange(18),
|
50 |
+
labels=np.arange(18)[::-1],
|
51 |
+
rotation=90,
|
52 |
+
va="center",
|
53 |
+
)
|
54 |
+
plt.xlabel(r"i$\eta$")
|
55 |
+
plt.ylabel(r"i$\phi$")
|
56 |
+
|
57 |
+
return fig_s
|
58 |
+
|
59 |
+
|
60 |
+
def draw_input(input_):
|
61 |
+
fig_i = plt.figure()
|
62 |
+
im = plt.imshow(input_.reshape(18, 14), vmin=0, vmax=input_.max(), cmap="Purples")
|
63 |
+
ax = plt.gca()
|
64 |
+
cbar = ax.figure.colorbar(im, ax=ax)
|
65 |
+
cbar.ax.set_ylabel(r"Calorimeter E$_T$ deposit (GeV)")
|
66 |
+
plt.xticks(np.arange(14), labels=np.arange(4, 18))
|
67 |
+
plt.yticks(
|
68 |
+
np.arange(18),
|
69 |
+
labels=np.arange(18)[::-1],
|
70 |
+
rotation=90,
|
71 |
+
va="center",
|
72 |
+
)
|
73 |
+
plt.xlabel(r"i$\eta$")
|
74 |
+
plt.ylabel(r"i$\phi$")
|
75 |
+
return fig_i
|
76 |
+
|
77 |
+
|
78 |
+
def inference(input_, version):
|
79 |
+
if version == "v1":
|
80 |
+
return model_v1.predict(input_)[0][0]
|
81 |
+
elif version == "v2":
|
82 |
+
return model_v2.predict(input_)[0][0]
|
83 |
+
|
84 |
+
|
85 |
+
def generate():
|
86 |
+
matrix = np.clip(np.random.zipf(2, 252) - 1, 0, 1023)
|
87 |
+
matrix = matrix.reshape(18, 14).astype(str)
|
88 |
+
rows = [",".join(row) for row in matrix]
|
89 |
+
return "\n".join(rows)
|
90 |
+
|
91 |
+
|
92 |
+
def process_request(input_):
|
93 |
+
input_ = parse_input(input_)
|
94 |
+
return (
|
95 |
+
inference(input_, "v1"),
|
96 |
+
inference(input_, "v2"),
|
97 |
+
draw_input(input_),
|
98 |
+
saliency(input_, "v1"),
|
99 |
+
saliency(input_, "v2"),
|
100 |
+
)
|
101 |
|
|
|
102 |
|
103 |
with gr.Blocks() as demo:
|
104 |
with gr.Row():
|
|
|
109 |
placeholder="\n".join([",".join(["0"] * 14)] * 18),
|
110 |
)
|
111 |
with gr.Row():
|
112 |
+
generate_input = gr.Button("Generate random input")
|
113 |
+
magic = gr.Button("Do CICADA inference")
|
114 |
+
|
|
|
115 |
with gr.Column():
|
116 |
+
with gr.Row():
|
117 |
+
label_v1 = gr.Number(label="CICADA Anomaly Score for CICADA v1")
|
118 |
+
with gr.Row():
|
119 |
+
label_v2 = gr.Number(label="CICADA Anomaly Score for CICADA v2")
|
120 |
+
with gr.Row():
|
121 |
+
with gr.Tabs():
|
122 |
+
with gr.TabItem("Calorimeter Input"):
|
123 |
+
input_plot = gr.Plot()
|
124 |
+
with gr.TabItem("Saliency Map for CICADAv1"):
|
125 |
+
interpretation_plot_v1 = gr.Plot()
|
126 |
+
with gr.TabItem("Saliency Map for CICADAv2"):
|
127 |
+
interpretation_plot_v2 = gr.Plot()
|
128 |
+
|
129 |
+
generate_input.click(generate, None, input_)
|
130 |
+
magic.click(
|
131 |
+
process_request,
|
132 |
+
input_,
|
133 |
+
[
|
134 |
+
label_v1,
|
135 |
+
label_v2,
|
136 |
+
input_plot,
|
137 |
+
interpretation_plot_v1,
|
138 |
+
interpretation_plot_v2,
|
139 |
+
],
|
140 |
+
)
|
141 |
|
142 |
demo.launch()
|
requirements.txt
CHANGED
@@ -1,5 +1,6 @@
|
|
1 |
-
numpy
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
|
|
|
1 |
+
numpy==1.26.0
|
2 |
+
gradio==4.8.0
|
3 |
+
matplotlib==3.7.2
|
4 |
+
mplhep==0.3.31
|
5 |
+
tensorflow==2.10.0
|
6 |
+
huggingface_hub==0.16.4
|