Spaces:
Runtime error
Runtime error
Toy Leksut
commited on
Commit
·
7d6107b
1
Parent(s):
be07d9e
add main function
Browse files
app.py
CHANGED
@@ -64,49 +64,54 @@ def generate_adversial_image(img, epsilon):
|
|
64 |
return undo_preprocess(processed_img), undo_preprocess(adv_img), preds, adv_preds
|
65 |
|
66 |
|
|
|
67 |
|
68 |
-
with gr.Blocks() as demo:
|
69 |
-
|
70 |
-
gr.Markdown('''## Generate Adversarial Image with Fast Gradient Sign Method
|
71 |
-
|
72 |
-
Given an input image and a neural network, the adversarial image can be generated from
|
73 |
-
|
74 |
-
<code>adv_img = input_img + epsilon*input_img.grad</code>
|
75 |
-
|
76 |
-
''')
|
77 |
-
|
78 |
-
with gr.Box():
|
79 |
-
input_image = gr.Image(type="pil", label="Input Image")
|
80 |
-
example_images = gr.Dataset(components=[input_image],
|
81 |
-
samples=[['coral.jpg'], ['goldfish.jpg'], ['otter.jpg'], ['panda.jpg']])
|
82 |
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
|
|
98 |
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
110 |
|
111 |
|
112 |
-
|
|
|
|
64 |
return undo_preprocess(processed_img), undo_preprocess(adv_img), preds, adv_preds
|
65 |
|
66 |
|
67 |
+
def main():
|
68 |
|
69 |
+
with gr.Blocks() as demo:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
|
71 |
+
gr.Markdown('''## Generate Adversarial Image with Fast Gradient Sign Method
|
72 |
+
|
73 |
+
Given an input image and a neural network, the adversarial image can be generated from
|
74 |
+
|
75 |
+
<code>adv_img = input_img + epsilon*input_img.grad</code>
|
76 |
+
|
77 |
+
''')
|
78 |
+
|
79 |
+
with gr.Box():
|
80 |
+
input_image = gr.Image(type="pil", label="Input Image")
|
81 |
+
example_images = gr.Dataset(components=[input_image],
|
82 |
+
samples=[['coral.jpg'], ['goldfish.jpg'], ['otter.jpg'], ['panda.jpg']])
|
83 |
+
|
84 |
+
with gr.Row():
|
85 |
+
epsilon = gr.Slider(minimum=0, maximum=0.5, value=0.02, step=0.01, label="epsilon")
|
86 |
+
btn = gr.Button("Generate Adversarial Image")
|
87 |
|
88 |
+
gr.Markdown('''### Original Image''')
|
89 |
+
with gr.Box():
|
90 |
+
with gr.Row():
|
91 |
+
img_before = gr.Image(label="Original Image")
|
92 |
+
label_before = gr.Label(label="Original Prediction")
|
93 |
+
|
94 |
+
gr.Markdown('''### Adversarial Image''')
|
95 |
+
with gr.Box():
|
96 |
+
with gr.Row():
|
97 |
+
img_after = gr.Image(label="Adversarial Image")
|
98 |
+
label_after = gr.Label(label="New Prediction")
|
99 |
+
|
100 |
+
gr.Markdown('''The prediction is done by ResNet18. Example images are from [Unsplash](https://unsplash.com).''')
|
101 |
+
|
102 |
+
|
103 |
+
# events
|
104 |
+
btn.click(fn=generate_adversial_image,
|
105 |
+
inputs=[input_image, epsilon],
|
106 |
+
outputs=[img_before, img_after, label_before, label_after])
|
107 |
+
|
108 |
+
example_images.click(fn=set_example_image,
|
109 |
+
inputs=example_images,
|
110 |
+
outputs=example_images.components)
|
111 |
+
|
112 |
+
demo.launch()
|
113 |
+
|
114 |
|
115 |
|
116 |
+
if __name__ == '__main__':
|
117 |
+
main()
|