Spaces:
Running
Running
rakan
#8
by
TwistedFate
- opened
- app.py +22 -108
- requirements.txt +1 -1
app.py
CHANGED
@@ -1,4 +1,4 @@
|
|
1 |
-
import gradio
|
2 |
from huggingface_hub import Repository
|
3 |
import os
|
4 |
|
@@ -18,7 +18,8 @@ token = os.environ['model_fetch']
|
|
18 |
|
19 |
opt = SwapOptions().parse()
|
20 |
|
21 |
-
retina_repo = Repository(local_dir="retina_model", clone_from="felixrosberg/retinaface_resnet50",
|
|
|
22 |
|
23 |
from retina_model.models import *
|
24 |
|
@@ -29,21 +30,25 @@ RetinaFace = load_model("retina_model/retinaface_res50.h5",
|
|
29 |
"LandmarkHead": LandmarkHead,
|
30 |
"ClassHead": ClassHead})
|
31 |
|
32 |
-
arc_repo = Repository(local_dir="arcface_model", clone_from="felixrosberg/arcface_tf",
|
|
|
33 |
ArcFace = load_model("arcface_model/arc_res50.h5")
|
34 |
ArcFaceE = load_model("arcface_model/arc_res50e.h5")
|
35 |
|
36 |
-
g_repo = Repository(local_dir="g_model_c_hq", clone_from="felixrosberg/affa_config_c_hq",
|
|
|
37 |
G = load_model("g_model_c_hq/generator_t_28.h5", custom_objects={"AdaIN": AdaIN,
|
38 |
"AdaptiveAttention": AdaptiveAttention,
|
39 |
"InstanceNormalization": InstanceNormalization})
|
40 |
|
41 |
-
r_repo = Repository(local_dir="reconstruction_attack", clone_from="felixrosberg/reconstruction_attack",
|
|
|
42 |
R = load_model("reconstruction_attack/reconstructor_42.h5", custom_objects={"AdaIN": AdaIN,
|
43 |
"AdaptiveAttention": AdaptiveAttention,
|
44 |
"InstanceNormalization": InstanceNormalization})
|
45 |
|
46 |
-
permuter_repo = Repository(local_dir="identity_permuter", clone_from="felixrosberg/identitypermuter",
|
|
|
47 |
|
48 |
from identity_permuter.id_permuter import identity_permuter
|
49 |
|
@@ -55,65 +60,6 @@ blend_mask_base[80:244, 32:224] = 1
|
|
55 |
blend_mask_base = gaussian_filter(blend_mask_base, sigma=7)
|
56 |
|
57 |
|
58 |
-
theme = gr.themes.Monochrome(
|
59 |
-
secondary_hue="emerald",
|
60 |
-
neutral_hue="teal",
|
61 |
-
).set(
|
62 |
-
body_background_fill='*primary_950',
|
63 |
-
body_background_fill_dark='*secondary_950',
|
64 |
-
body_text_color='*primary_50',
|
65 |
-
body_text_color_dark='*secondary_100',
|
66 |
-
body_text_color_subdued='*primary_300',
|
67 |
-
body_text_color_subdued_dark='*primary_300',
|
68 |
-
background_fill_primary='*primary_600',
|
69 |
-
background_fill_primary_dark='*primary_400',
|
70 |
-
background_fill_secondary='*primary_950',
|
71 |
-
background_fill_secondary_dark='*primary_950',
|
72 |
-
border_color_accent='*secondary_600',
|
73 |
-
border_color_primary='*secondary_50',
|
74 |
-
border_color_primary_dark='*secondary_50',
|
75 |
-
color_accent='*secondary_50',
|
76 |
-
color_accent_soft='*primary_500',
|
77 |
-
color_accent_soft_dark='*primary_500',
|
78 |
-
link_text_color='*secondary_950',
|
79 |
-
link_text_color_dark='*primary_50',
|
80 |
-
link_text_color_active='*primary_50',
|
81 |
-
link_text_color_active_dark='*primary_50',
|
82 |
-
link_text_color_hover='*primary_50',
|
83 |
-
link_text_color_hover_dark='*primary_50',
|
84 |
-
link_text_color_visited='*primary_50',
|
85 |
-
block_background_fill='*primary_950',
|
86 |
-
block_background_fill_dark='*primary_950',
|
87 |
-
block_border_color='*secondary_500',
|
88 |
-
block_border_color_dark='*secondary_500',
|
89 |
-
block_info_text_color='*primary_50',
|
90 |
-
block_info_text_color_dark='*primary_50',
|
91 |
-
block_label_background_fill='*primary_950',
|
92 |
-
block_label_background_fill_dark='*secondary_950',
|
93 |
-
block_label_border_color='*secondary_500',
|
94 |
-
block_label_border_color_dark='*secondary_500',
|
95 |
-
block_label_text_color='*secondary_500',
|
96 |
-
block_label_text_color_dark='*secondary_500',
|
97 |
-
block_title_background_fill='*primary_950',
|
98 |
-
panel_background_fill='*primary_950',
|
99 |
-
panel_border_color='*primary_950',
|
100 |
-
checkbox_background_color='*primary_950',
|
101 |
-
checkbox_background_color_dark='*primary_950',
|
102 |
-
checkbox_background_color_focus='*primary_950',
|
103 |
-
checkbox_border_color='*secondary_500',
|
104 |
-
input_background_fill='*primary_800',
|
105 |
-
input_background_fill_focus='*primary_950',
|
106 |
-
input_background_fill_hover='*secondary_950',
|
107 |
-
input_placeholder_color='*secondary_950',
|
108 |
-
slider_color='*primary_950',
|
109 |
-
slider_color_dark='*primary_950',
|
110 |
-
table_even_background_fill='*primary_800',
|
111 |
-
table_odd_background_fill='*primary_600',
|
112 |
-
button_primary_background_fill='*primary_800',
|
113 |
-
button_primary_background_fill_dark='*primary_800'
|
114 |
-
)
|
115 |
-
|
116 |
-
|
117 |
def run_inference(target, source, slider, adv_slider, settings):
|
118 |
try:
|
119 |
source = np.array(source)
|
@@ -246,58 +192,26 @@ description = "Performs subject agnostic identity transfer from a source face to
|
|
246 |
"-Adversarial defense will add a permutation noise that disrupts the reconstruction attack.\n\n" \
|
247 |
"NOTE: There is no guarantees with the anonymization process currently.\n\n" \
|
248 |
"NOTE: source image with too high resolution may not work properly!"
|
249 |
-
examples = [["assets/rick.jpg", "assets/musk.jpg", 100, 10, []],
|
250 |
-
["assets/
|
251 |
article = """
|
252 |
Demo is based of recent research from my Ph.D work. Results expects to be published in the coming months.
|
253 |
"""
|
254 |
|
255 |
-
|
256 |
-
|
257 |
-
|
258 |
-
|
259 |
-
|
260 |
-
|
261 |
-
with gr.Group():
|
262 |
-
trg_in = gr.Image(type="pil", label='Target')
|
263 |
-
src_in = gr.Image(type="pil", label='Source')
|
264 |
-
with gr.Row():
|
265 |
-
b1 = gr.Button("Face Swap")
|
266 |
-
with gr.Row():
|
267 |
-
with gr.Accordion("Options", open=False):
|
268 |
-
chk_in = gr.CheckboxGroup(["Compare",
|
269 |
-
"Anonymize",
|
270 |
-
"Reconstruction Attack",
|
271 |
-
"Adversarial Defense"],
|
272 |
-
label="Mode",
|
273 |
-
info="Anonymize mode? "
|
274 |
-
"Apply reconstruction attack? "
|
275 |
-
"Apply defense against reconstruction attack?")
|
276 |
-
def_in = gr.Slider(0, 100, value=100,
|
277 |
-
label='Anonymization ratio (%)')
|
278 |
-
mrg_in = gr.Slider(0, 100, value=100,
|
279 |
-
label='Adversarial defense ratio (%)')
|
280 |
-
gr.Examples(examples=[["assets/musk.jpg"], ["assets/rick.jpg"]],
|
281 |
-
inputs=trg_in)
|
282 |
-
with gr.Column():
|
283 |
-
with gr.Group():
|
284 |
-
ano_out = gr.Image(type="pil", label='Output')
|
285 |
-
|
286 |
-
b1.click(run_inference, inputs=[trg_in, src_in, def_in, mrg_in, chk_in], outputs=ano_out)
|
287 |
-
"""iface = gradio.Interface(run_inference,
|
288 |
-
[gradio.Image(shape=None, type="pil", label='Target'),
|
289 |
-
gradio.Image(shape=None, type="pil", label='Source'),
|
290 |
-
gradio.Slider(0, 100, value=100, label="Anonymization ratio (%)"),
|
291 |
-
gradio.Slider(0, 100, value=100, label="Adversarial defense ratio (%)"),
|
292 |
-
gradio.CheckboxGroup(["compare",
|
293 |
"anonymize",
|
294 |
"reconstruction attack",
|
295 |
"adversarial defense"],
|
296 |
label='Options')],
|
297 |
-
|
298 |
title="Face Swap",
|
299 |
description=description,
|
300 |
examples=examples,
|
301 |
article=article,
|
302 |
-
layout="vertical")
|
303 |
-
|
1 |
+
import gradio
|
2 |
from huggingface_hub import Repository
|
3 |
import os
|
4 |
|
18 |
|
19 |
opt = SwapOptions().parse()
|
20 |
|
21 |
+
retina_repo = Repository(local_dir="retina_model", clone_from="felixrosberg/retinaface_resnet50",
|
22 |
+
private=True, use_auth_token=token, git_user="felixrosberg")
|
23 |
|
24 |
from retina_model.models import *
|
25 |
|
30 |
"LandmarkHead": LandmarkHead,
|
31 |
"ClassHead": ClassHead})
|
32 |
|
33 |
+
arc_repo = Repository(local_dir="arcface_model", clone_from="felixrosberg/arcface_tf",
|
34 |
+
private=True, use_auth_token=token)
|
35 |
ArcFace = load_model("arcface_model/arc_res50.h5")
|
36 |
ArcFaceE = load_model("arcface_model/arc_res50e.h5")
|
37 |
|
38 |
+
g_repo = Repository(local_dir="g_model_c_hq", clone_from="felixrosberg/affa_config_c_hq",
|
39 |
+
private=True, use_auth_token=token)
|
40 |
G = load_model("g_model_c_hq/generator_t_28.h5", custom_objects={"AdaIN": AdaIN,
|
41 |
"AdaptiveAttention": AdaptiveAttention,
|
42 |
"InstanceNormalization": InstanceNormalization})
|
43 |
|
44 |
+
r_repo = Repository(local_dir="reconstruction_attack", clone_from="felixrosberg/reconstruction_attack",
|
45 |
+
private=True, use_auth_token=token)
|
46 |
R = load_model("reconstruction_attack/reconstructor_42.h5", custom_objects={"AdaIN": AdaIN,
|
47 |
"AdaptiveAttention": AdaptiveAttention,
|
48 |
"InstanceNormalization": InstanceNormalization})
|
49 |
|
50 |
+
permuter_repo = Repository(local_dir="identity_permuter", clone_from="felixrosberg/identitypermuter",
|
51 |
+
private=True, use_auth_token=token, git_user="felixrosberg")
|
52 |
|
53 |
from identity_permuter.id_permuter import identity_permuter
|
54 |
|
60 |
blend_mask_base = gaussian_filter(blend_mask_base, sigma=7)
|
61 |
|
62 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
63 |
def run_inference(target, source, slider, adv_slider, settings):
|
64 |
try:
|
65 |
source = np.array(source)
|
192 |
"-Adversarial defense will add a permutation noise that disrupts the reconstruction attack.\n\n" \
|
193 |
"NOTE: There is no guarantees with the anonymization process currently.\n\n" \
|
194 |
"NOTE: source image with too high resolution may not work properly!"
|
195 |
+
examples = [["assets/rick.jpg", "assets/musk.jpg", 100, 10, ["compare"]],
|
196 |
+
["assets/musk.jpg", "assets/musk.jpg", 100, 10, ["anonymize"]]]
|
197 |
article = """
|
198 |
Demo is based of recent research from my Ph.D work. Results expects to be published in the coming months.
|
199 |
"""
|
200 |
|
201 |
+
iface = gradio.Interface(run_inference,
|
202 |
+
[gradio.inputs.Image(shape=None, label='Target'),
|
203 |
+
gradio.inputs.Image(shape=None, label='Source'),
|
204 |
+
gradio.inputs.Slider(0, 100, default=100, label="Anonymization ratio (%)"),
|
205 |
+
gradio.inputs.Slider(0, 100, default=100, label="Adversarial defense ratio (%)"),
|
206 |
+
gradio.inputs.CheckboxGroup(["compare",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
207 |
"anonymize",
|
208 |
"reconstruction attack",
|
209 |
"adversarial defense"],
|
210 |
label='Options')],
|
211 |
+
gradio.outputs.Image(),
|
212 |
title="Face Swap",
|
213 |
description=description,
|
214 |
examples=examples,
|
215 |
article=article,
|
216 |
+
layout="vertical")
|
217 |
+
iface.launch()
|
requirements.txt
CHANGED
@@ -1,5 +1,5 @@
|
|
1 |
tensorflow
|
2 |
-
tensorflow-addons
|
3 |
opencv-python-headless
|
4 |
scipy
|
5 |
pillow
|
1 |
tensorflow
|
2 |
+
tensorflow-addons
|
3 |
opencv-python-headless
|
4 |
scipy
|
5 |
pillow
|